Skip to content

Commit

Permalink
完善AI生图prompt提取
Browse files Browse the repository at this point in the history
  • Loading branch information
jark006 committed Aug 26, 2024
1 parent 5d0e912 commit 5c4ed37
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 19 deletions.
87 changes: 68 additions & 19 deletions jarkViewer/include/exifParse.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ class ExifParse{
auto secondSpaceIdx = tagValue.find_last_of(' ');
if (firstSpaceIdx != string::npos && secondSpaceIdx != string::npos && firstSpaceIdx < secondSpaceIdx) {
auto n1 = handleMathDiv(tagValue.substr(0, firstSpaceIdx));
auto n2 = handleMathDiv(tagValue.substr(firstSpaceIdx + 1, secondSpaceIdx-firstSpaceIdx-1));
auto n2 = handleMathDiv(tagValue.substr(firstSpaceIdx + 1, secondSpaceIdx - firstSpaceIdx - 1));
auto n3 = handleMathDiv(tagValue.substr(secondSpaceIdx + 1));
tagValue = std::format("{}°{}' {}'' ({})", n1, n2, n3, tagValue);
}
Expand All @@ -218,14 +218,27 @@ class ExifParse{
tagValue = std::format("{}:{}:{} ({})", n1, n2, n3, tagValue);
}
}
else if (tagName == "Exif.Photo.UserComment") { // 可能包含AI生图prompt信息
auto a = tag.value().clone();
vector<uint8_t> buf(a->size());
a->copy(buf.data(), Exiv2::ByteOrder::bigEndian);
if (!memcmp(buf.data(), "UNICODE\0", 8)) {
wstring str((wchar_t*)(buf.data() + 8), (buf.size() - 8) / 2);
tagValue = Utils::wstringToUtf8(str);
}
}
else if (2 < tagValue.length() && tagValue.length() < 100) {
auto res = handleMathDiv(tagValue);
if (!res.empty())
tagValue = std::format("{} ({})", res, tagValue);
}

auto tmp = "\n" + translatedTagName + ": " + (tagValue.length() < 100 ? tagValue :
tagValue.substr(0, 100) + std::format(" ...] length:{}", tagValue.length()));
string tmp;
if (tagName == "Exif.Photo.UserComment")
tmp = "\n" + translatedTagName + ": " + tagValue;
else
tmp = "\n" + translatedTagName + ": " + (tagValue.length() < 100 ? tagValue :
tagValue.substr(0, 100) + std::format(" ...] length:{}", tagValue.length()));

if (toEnd)ossEnd << tmp;
else oss << tmp;
Expand Down Expand Up @@ -261,26 +274,65 @@ class ExifParse{
}

static string AI_Prompt(const std::wstring& path, const uint8_t* buf) {
int length = (buf[0x21] << 24) + (buf[0x22] << 16) + (buf[0x23] << 8) + buf[0x24];
if (length > 16 * 1024) {
return "";
if (!strncmp((const char*)buf + 0x25, "tEXtparameters", 14)) {
int length = (buf[0x21] << 24) + (buf[0x22] << 16) + (buf[0x23] << 8) + buf[0x24];
if (length > 16 * 1024) {
return "";
}

string prompt((const char*)buf + 0x29, length); // format: Windows-1252 ISO/IEC 8859-1(Latin-1)

prompt = Utils::wstringToUtf8(Utils::latin1ToWstring(prompt));

auto idx = prompt.find("parameters");
if (idx != string::npos) {
prompt.replace(idx, 11, "正提示词: ");
}

idx = prompt.find("Negative prompt");
if (idx != string::npos) {
prompt.replace(idx, 15, "反提示词");
}

return "\nAI生图提示词 Prompt:\n" + prompt;
}
else if (!strncmp((const char*)buf + 0x25, "iTXtparameters", 14)) {
int length = (buf[0x21] << 24) + (buf[0x22] << 16) + (buf[0x23] << 8) + buf[0x24];
if (length > 16 * 1024) {
return "";
}

string prompt((const char*)buf + 0x38, length - 15); // format: Windows-1252 ISO/IEC 8859-1(Latin-1)

prompt = Utils::wstringToUtf8(Utils::latin1ToWstring(prompt));

string prompt((const char*)buf + 0x29, length); // format: Windows-1252 ISO/IEC 8859-1(Latin-1)
auto idx = prompt.find("parameters");
if (idx != string::npos) {
prompt.replace(idx, 11, "正提示词: ");
}

prompt = Utils::wstringToUtf8(Utils::latin1ToWstring(prompt));
idx = prompt.find("Negative prompt");
if (idx != string::npos) {
prompt.replace(idx, 15, "反提示词");
}

auto idx = prompt.find("parameters");
if (idx != string::npos) {
prompt.replace(idx, 11, "正提示词: ");
return "\nAI生图提示词 Prompt:\n" + prompt;
}
else if (!strncmp((const char*)buf + 0x25, "tEXtprompt", 10)) {

idx = prompt.find("Negative prompt");
if (idx != string::npos) {
prompt.replace(idx, 15, "反提示词");
int length = (buf[0x21] << 24) + (buf[0x22] << 16) + (buf[0x23] << 8) + buf[0x24];
if (length > 16 * 1024) {
return "";
}

string prompt((const char*)buf + 0x29 + 7, length - 7); // format: Windows-1252 ISO/IEC 8859-1(Latin-1)

prompt = Utils::wstringToUtf8(Utils::latin1ToWstring(prompt));

return "\nAI生图提示词 Prompt:\n" + prompt;
}

return "\nAI生图提示词 Prompt:\n" + prompt;
return "";
}

static std::string getExif(const std::wstring& path, const uint8_t* buf, int fileSize) {
Expand All @@ -292,10 +344,7 @@ class ExifParse{
auto& xmpData = image->xmpData();
auto& iptcData = image->iptcData();

string prompt;
if (!strncmp((const char*)buf + 0x25, "tEXtparameters", 14)) {
prompt = AI_Prompt(path, buf);
}
string prompt = AI_Prompt(path, buf);

return exifDataToString(path, exifData) + xmpDataToString(path, xmpData) + iptcDataToString(path, iptcData) + prompt;
}
Expand Down
Binary file modified jarkViewer/jarkViewer.aps
Binary file not shown.
Binary file modified jarkViewer/jarkViewer.rc
Binary file not shown.

0 comments on commit 5c4ed37

Please sign in to comment.