ragephoto-extract: use library save function to save jpeg

This commit is contained in:
Syping 2025-10-29 05:50:02 +01:00
parent a404a29188
commit 2a66793f70
2 changed files with 8 additions and 36 deletions

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2023 Syping * Copyright (C) 2021-2025 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -26,13 +26,10 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
// Initialise RagePhoto
ragephoto_t ragephoto_in = ragephoto_open(); ragephoto_t ragephoto_in = ragephoto_open();
// Load Photo const bool loaded = ragephoto_loadfile(ragephoto_in, argv[1]);
const int loaded = ragephoto_loadfile(ragephoto_in, argv[1]); if (!loaded) {
if (loaded != 1) {
const int32_t error = ragephoto_error(ragephoto_in); const int32_t error = ragephoto_error(ragephoto_in);
if (error == RAGEPHOTO_ERROR_UNINITIALISED) { if (error == RAGEPHOTO_ERROR_UNINITIALISED) {
printf("Failed to open file: %s\n", argv[1]); printf("Failed to open file: %s\n", argv[1]);
@ -46,23 +43,8 @@ int main(int argc, char *argv[])
} }
} }
// Write jpeg const bool saved = ragephoto_savefilef(ragephoto_in, argv[2], RAGEPHOTO_FORMAT_JPEG);
#ifdef _WIN32 if (!saved) {
FILE *file = NULL;
fopen_s(&file, argv[2], "wb");
#else
FILE *file = fopen(argv[2], "wb");
#endif
if (!file) {
printf("Failed to write file: %s\n", argv[2]);
ragephoto_close(ragephoto_in);
return 1;
}
const size_t jpegSize = ragephoto_getphotosize(ragephoto_in);
const size_t fileSize = fwrite(ragephoto_getphotojpeg(ragephoto_in), sizeof(char), jpegSize, file);
fclose(file);
if (fileSize != jpegSize) {
printf("Failed to write file: %s\n", argv[2]); printf("Failed to write file: %s\n", argv[2]);
ragephoto_close(ragephoto_in); ragephoto_close(ragephoto_in);
return 1; return 1;

View file

@ -1,6 +1,6 @@
/***************************************************************************** /*****************************************************************************
* libragephoto RAGE Photo Parser * libragephoto RAGE Photo Parser
* Copyright (C) 2021-2024 Syping * Copyright (C) 2021-2025 Syping
* *
* Redistribution and use in source and binary forms, with or without modification, * Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met: * are permitted provided that the following conditions are met:
@ -26,10 +26,8 @@ int main(int argc, char *argv[])
return 0; return 0;
} }
// Initialise RagePhoto
RagePhoto ragePhoto; RagePhoto ragePhoto;
// Load Photo
const bool loaded = ragePhoto.loadFile(argv[1]); const bool loaded = ragePhoto.loadFile(argv[1]);
if (!loaded) { if (!loaded) {
@ -44,17 +42,9 @@ int main(int argc, char *argv[])
} }
} }
// Write jpeg const bool saved = ragePhoto.saveFile(argv[2], RagePhoto::JPEG);
std::ofstream ofs(argv[2], std::ios::out | std::ios::binary | std::ios::trunc);
if (!ofs.is_open()) {
std::cout << "Failed to write file: " << argv[2] << std::endl;
return 1;
}
ofs << ragePhoto.jpeg();
const bool ok = ofs.good();
ofs.close();
if (!ok) { if (!saved) {
std::cout << "Failed to write file: " << argv[2] << std::endl; std::cout << "Failed to write file: " << argv[2] << std::endl;
return 1; return 1;
} }