Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

heifio: avoid exit() call on failure #1316

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 23 additions & 13 deletions examples/heif_enc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,25 @@ void show_help(const char* argv0)


#if !HAVE_LIBJPEG
InputImage loadJPEG(const char* filename)
heif_error loadJPEG(const char *filename, InputImage *input_image)
{
std::cerr << "Cannot load JPEG because libjpeg support was not compiled.\n";
exit(1);

return {};
struct heif_error err = {
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Cannot load JPEG because libjpeg support was not compiled."};
return err;
}
#endif


#if !HAVE_LIBPNG
InputImage loadPNG(const char* filename, int output_bit_depth)
heif_error loadPNG(const char* filename, int output_bit_depth, InputImage *input_image)
{
std::cerr << "Cannot load PNG because libpng support was not compiled.\n";
exit(1);

return {};
struct heif_error err = {
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Cannot load PNG because libpng support was not compiled."};
return err;
}
#endif

Expand All @@ -223,7 +225,7 @@ heif_error loadTIFF(const char *filename, InputImage *input_image)
.code = heif_error_Unsupported_feature,
.subcode = heif_suberror_Unspecified,
.message = "Cannot load TIFF because libtiff support was not compiled."};
return err;
return err;
}
#endif

Expand Down Expand Up @@ -827,7 +829,11 @@ int main(int argc, char** argv)

InputImage input_image;
if (filetype == PNG) {
input_image = loadPNG(input_filename.c_str(), output_bit_depth);
heif_error err = loadPNG(input_filename.c_str(), output_bit_depth, &input_image);
if (err.code != heif_error_Ok) {
std::cerr << "Can not load TIFF input_image: " << err.message << std::endl;
exit(1);
}
}
else if (filetype == Y4M) {
heif_error err = loadY4M(input_filename.c_str(), &input_image);
Expand All @@ -844,7 +850,11 @@ int main(int argc, char** argv)
}
}
else {
input_image = loadJPEG(input_filename.c_str());
heif_error err = loadJPEG(input_filename.c_str(), &input_image);
if (err.code != heif_error_Ok) {
std::cerr << "Can not load JPEG input_image: " << err.message << std::endl;
exit(1);
}
}

std::shared_ptr<heif_image> image = input_image.image;
Expand Down
21 changes: 12 additions & 9 deletions heifio/decoder_jpeg.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ extern "C" {
#define JPEG_ICC_MARKER (JPEG_APP0+2) /* JPEG marker code for ICC */
#define JPEG_ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */

static struct heif_error heif_error_ok = {heif_error_Ok, heif_suberror_Unspecified, "Success"};

static bool JPEGMarkerIsIcc(jpeg_saved_marker_ptr marker)
{
Expand Down Expand Up @@ -223,9 +224,8 @@ bool ReadEXIFFromJPEG(j_decompress_ptr cinfo,
#endif


InputImage loadJPEG(const char* filename)
heif_error loadJPEG(const char *filename, InputImage *input_image)
{
InputImage img;
struct heif_image* image = nullptr;


Expand All @@ -245,8 +245,11 @@ InputImage loadJPEG(const char* filename)

FILE* infile;
if ((infile = fopen(filename, "rb")) == NULL) {
std::cerr << "Can't open " << filename << "\n";
exit(1);
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Cannot open JPEG file"};
return err;
}


Expand All @@ -267,13 +270,13 @@ InputImage loadJPEG(const char* filename)
bool embeddedIccFlag = ReadICCProfileFromJPEG(&cinfo, &iccBuffer, &iccLen);
bool embeddedXMPFlag = ReadXMPFromJPEG(&cinfo, xmpData);
if (embeddedXMPFlag) {
img.xmp = xmpData;
input_image->xmp = xmpData;
}

bool embeddedEXIFFlag = ReadEXIFFromJPEG(&cinfo, exifData);
if (embeddedEXIFFlag) {
img.exif = exifData;
img.orientation = (heif_orientation) read_exif_orientation_tag(exifData.data(), (int) exifData.size());
input_image->exif = exifData;
input_image->orientation = (heif_orientation) read_exif_orientation_tag(exifData.data(), (int) exifData.size());
}

if (cinfo.jpeg_color_space == JCS_GRAYSCALE) {
Expand Down Expand Up @@ -484,8 +487,8 @@ InputImage loadJPEG(const char* filename)

fclose(infile);

img.image = std::shared_ptr<heif_image>(image,
input_image->image = std::shared_ptr<heif_image>(image,
[](heif_image* img) { heif_image_release(img); });

return img;
return heif_error_ok;
}
3 changes: 2 additions & 1 deletion heifio/decoder_jpeg.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "decoder.h"

InputImage loadJPEG(const char* filename);
LIBHEIF_API
heif_error loadJPEG(const char *filename, InputImage *input_image);

#endif //LIBHEIF_DECODER_JPEG_H
28 changes: 15 additions & 13 deletions heifio/decoder_png.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ extern "C" {
#include <png.h>
}

static struct heif_error heif_error_ok = {heif_error_Ok, heif_suberror_Unspecified, "Success"};

static void
user_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
{
Expand All @@ -45,17 +47,17 @@ user_read_fn(png_structp png_ptr, png_bytep data, png_size_t length)
} // user_read_data


InputImage loadPNG(const char* filename, int output_bit_depth)
heif_error loadPNG(const char* filename, int output_bit_depth, InputImage *input_image)
{
FILE* fh = fopen(filename, "rb");
if (!fh) {
std::cerr << "Can't open " << filename << "\n";
exit(1);
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Cannot open PNG file"};
return err;
}


InputImage input_image;

// ### Code copied from LibVideoGfx and slightly modified to use HeifPixelImage

struct heif_image* image = nullptr;
Expand Down Expand Up @@ -203,11 +205,11 @@ InputImage loadPNG(const char* filename, int output_bit_depth)
png_bytep exifPtr = nullptr;
png_uint_32 exifSize = 0;
if (png_get_eXIf_1(png_ptr, info_ptr, &exifSize, &exifPtr) == PNG_INFO_eXIf) {
input_image.exif.resize(exifSize);
memcpy(input_image.exif.data(), exifPtr, exifSize);
input_image->exif.resize(exifSize);
memcpy(input_image->exif.data(), exifPtr, exifSize);

// remove the EXIF orientation since it is informal only in PNG and we do not want to confuse with an orientation not matching irot/imir
modify_exif_orientation_tag_if_it_exists(input_image.exif.data(), (int) input_image.exif.size(), 1);
modify_exif_orientation_tag_if_it_exists(input_image->exif.data(), (int) input_image->exif.size(), 1);
}
#endif

Expand All @@ -227,8 +229,8 @@ InputImage loadPNG(const char* filename, int output_bit_depth)
// TODO: error
}
else {
input_image.xmp.resize(textLength);
memcpy(input_image.xmp.data(), textPtr->text, textLength);
input_image->xmp.resize(textLength);
memcpy(input_image->xmp.data(), textPtr->text, textLength);
}
}
}
Expand Down Expand Up @@ -444,8 +446,8 @@ InputImage loadPNG(const char* filename, int output_bit_depth)
delete[] row_pointers;
fclose(fh);

input_image.image = std::shared_ptr<heif_image>(image,
input_image->image = std::shared_ptr<heif_image>(image,
[](heif_image* img) { heif_image_release(img); });

return input_image;
return heif_error_ok;
}
3 changes: 2 additions & 1 deletion heifio/decoder_png.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "decoder.h"

InputImage loadPNG(const char* filename, int output_bit_depth);
LIBHEIF_API
heif_error loadPNG(const char* filename, int output_bit_depth, InputImage *input_image);

#endif //LIBHEIF_DECODER_PNG_H
Loading