SDWebImageHEIFCoder
SDWebImageHEIFCoder copied to clipboard
Metadata is lost after conversion
After decoding the HEIC picture with this library, all the meta information is lost. I wonder if other information except the rotation field can be retained? After reviewing the libheif source code, I found the following method:
size_t exifsize = 0;
uint8_t* exifdata = GetExifMetaData(handle, &exifsize);
if (exifdata && exifsize > 4) {
static const uint8_t kExifMarker = JPEG_APP0 + 1;
jpeg_write_marker(&cinfo, kExifMarker, exifdata + 4,
static_cast<unsigned int>(exifsize - 4));
free(exifdata);
}
size_t profile_size = heif_image_handle_get_raw_color_profile_size(handle);
if (profile_size > 0) {
uint8_t* profile_data = static_cast<uint8_t*>(malloc(profile_size));
heif_image_handle_get_raw_color_profile(handle, profile_data);
jpeg_write_icc_profile(&cinfo, profile_data, (unsigned int) profile_size);
free(profile_data);
}
if (heif_image_get_bits_per_pixel(image, heif_channel_Y) != 8) {
fprintf(stderr, "JPEG writer cannot handle image with >8 bpp.\n");
return false;
}
.......
But my C++ is very poor, do not know how to convert to OC code,any body have some idea? i'll be very thankful!
Looks like easy to fix.
The issue is because of that the HEIF coder does not read the ICC profile for color. This may cause some visual issues.
Since the upstream libheif supports to read color profile, it's easy to fix by converting it into CGColorSpace
. I'll provide the fix soon.
@zhzhl1993 And, can you provide your test demo HEIC image ? So that I can check the color profile and write test cases to compare.
I check again, what Metadata
do you want ?
The decode result is a UIImage
, or actually, CGImage
.
CGImage itself does not store anything metadata. For example, EXIF will transform to the bitmap buffer, it does not store these information.
If you want metadata, maybe you should use some low levels, the HEIF coder is used mostly for Web image loading and SDWebImage framework, they don't care about the keeping metadata.
@zhzhl1993 I'll wait for your reply. WIP PR is here: #25
@dreampiggy Thank you very much, I want is to keep all the information of HEIC image after converting to JPG, such as the EXIF, GPS, TIFF information that can be viewed in the preview application,not only color space.
@zhzhl1993 Hi.
Seems what you want is not the feature and core function of our SDWebImage coders (including SDImageHEICoder/WebPCoder, etc).
Our codec is used to fucus on iOS Web Image loading and rending, does not caring about the metadata to query. All of our current open APIs does not provide the metadata or extra information to framework users.
See avaiable APIs: https://github.com/SDWebImage/SDWebImage/blob/master/SDWebImage/Core/SDImageCoder.h...
Because our return value is UIImage
, UIImage however, does not support storing something metadata :). If we need to provide metadata as well, we need to change API to return some other object or introduce new APIs.
I as the framework maintainer, have no clear plan to supports this feature. Because this will include far more code changes. And also, need SDWebImage itself to adopt. But the user feature request seems really rare. (ROI seems to low ?)
I suggest you to directlly use some professional Image Processsing lib, like ImageMagick codec library
But anyway, that #25 PR will be merged. Which fix the Color Profile issue for HEIF coder.
A long time PR. This may be added to SDWebImage's 6.0's design of coders API.