| 1 |
/* |
| 2 |
libheif example application. |
| 3 |
|
| 4 |
MIT License |
| 5 |
|
| 6 |
Copyright (c) 2017 struktur AG, Joachim Bauch <[email protected]> |
| 7 |
|
| 8 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 |
of this software and associated documentation files (the "Software"), to deal |
| 10 |
in the Software without restriction, including without limitation the rights |
| 11 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 |
copies of the Software, and to permit persons to whom the Software is |
| 13 |
furnished to do so, subject to the following conditions: |
| 14 |
|
| 15 |
The above copyright notice and this permission notice shall be included in all |
| 16 |
copies or substantial portions of the Software. |
| 17 |
|
| 18 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 |
SOFTWARE. |
| 25 |
*/ |
| 26 |
|
| 27 |
#include <cerrno> |
| 28 |
#include <cstring> |
| 29 |
|
| 30 |
#include <vector> |
| 31 |
#include <limits> |
| 32 |
|
| 33 |
#include "encoder_jpeg.h" |
| 34 |
#include "exif.h" |
| 35 |
|
| 36 |
#include <jpeglib.h> |
| 37 |
|
| 38 |
#include "common_utils.h" |
| 39 |
|
| 40 |
#define JPEG_XMP_MARKER (JPEG_APP0+1) /* JPEG marker code for XMP */ |
| 41 |
#define JPEG_XMP_MARKER_ID "http://ns.adobe.com/xap/1.0/" |
| 42 |
|
| 43 |
|
| 44 |
struct ErrorHandler |
| 45 |
{ |
| 46 |
struct jpeg_error_mgr pub; /* "public" fields */ |
| 47 |
jmp_buf setjmp_buffer; /* for return to caller */ |
| 48 |
}; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
JpegEncoder::JpegEncoder(int quality) : quality_(quality) |
| 53 |
{ |
| 54 |
if (quality_ < 0 || quality_ > 100) { |
| 55 |
quality_ = kDefaultQuality; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
void JpegEncoder::UpdateDecodingOptions(const struct heif_image_handle* handle, |
| 60 |
struct heif_decoding_options* options) const |
| 61 |
{ |
| 62 |
options->convert_hdr_to_8bit = 1; |
| 63 |
} |
| 64 |
|
| 65 |
// static |
| 66 |
static void OnJpegError(j_common_ptr cinfo) |
| 67 |
{ |
| 68 |
ErrorHandler* handler = reinterpret_cast<ErrorHandler*>(cinfo->err); |
| 69 |
longjmp(handler->setjmp_buffer, 1); |
| 70 |
} |
| 71 |
|
| 72 |
#define MAX_BYTES_IN_MARKER 65533 /* maximum data len of a JPEG marker */ |
| 73 |
|
| 74 |
#if !defined(HAVE_JPEG_WRITE_ICC_PROFILE) |
| 75 |
|
| 76 |
#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */ |
| 77 |
#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */ |
| 78 |
#define MAX_DATA_BYTES_IN_MARKER (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN) |
| 79 |
|
| 80 |
/* |
| 81 |
* This routine writes the given ICC profile data into a JPEG file. It *must* |
| 82 |
* be called AFTER calling jpeg_start_compress() and BEFORE the first call to |
| 83 |
* jpeg_write_scanlines(). (This ordering ensures that the APP2 marker(s) will |
| 84 |
* appear after the SOI and JFIF or Adobe markers, but before all else.) |
| 85 |
*/ |
| 86 |
|
| 87 |
/* This function is copied almost as is from libjpeg-turbo */ |
| 88 |
|
| 89 |
static |
| 90 |
void jpeg_write_icc_profile(j_compress_ptr cinfo, const JOCTET* icc_data_ptr, |
| 91 |
unsigned int icc_data_len) |
| 92 |
{ |
| 93 |
unsigned int num_markers; /* total number of markers we'll write */ |
| 94 |
int cur_marker = 1; /* per spec, counting starts at 1 */ |
| 95 |
unsigned int length; /* number of bytes to write in this marker */ |
| 96 |
|
| 97 |
/* Calculate the number of markers we'll need, rounding up of course */ |
| 98 |
num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER; |
| 99 |
if (num_markers * MAX_DATA_BYTES_IN_MARKER != icc_data_len) |
| 100 |
num_markers++; |
| 101 |
|
| 102 |
while (icc_data_len > 0) { |
| 103 |
/* length of profile to put in this marker */ |
| 104 |
length = icc_data_len; |
| 105 |
if (length > MAX_DATA_BYTES_IN_MARKER) |
| 106 |
length = MAX_DATA_BYTES_IN_MARKER; |
| 107 |
icc_data_len -= length; |
| 108 |
|
| 109 |
/* Write the JPEG marker header (APP2 code and marker length) */ |
| 110 |
jpeg_write_m_header(cinfo, ICC_MARKER, |
| 111 |
(unsigned int) (length + ICC_OVERHEAD_LEN)); |
| 112 |
|
| 113 |
/* Write the marker identifying string "ICC_PROFILE" (null-terminated). We |
| 114 |
* code it in this less-than-transparent way so that the code works even if |
| 115 |
* the local character set is not ASCII. |
| 116 |
*/ |
| 117 |
jpeg_write_m_byte(cinfo, 0x49); |
| 118 |
jpeg_write_m_byte(cinfo, 0x43); |
| 119 |
jpeg_write_m_byte(cinfo, 0x43); |
| 120 |
jpeg_write_m_byte(cinfo, 0x5F); |
| 121 |
jpeg_write_m_byte(cinfo, 0x50); |
| 122 |
jpeg_write_m_byte(cinfo, 0x52); |
| 123 |
jpeg_write_m_byte(cinfo, 0x4F); |
| 124 |
jpeg_write_m_byte(cinfo, 0x46); |
| 125 |
jpeg_write_m_byte(cinfo, 0x49); |
| 126 |
jpeg_write_m_byte(cinfo, 0x4C); |
| 127 |
jpeg_write_m_byte(cinfo, 0x45); |
| 128 |
jpeg_write_m_byte(cinfo, 0x0); |
| 129 |
|
| 130 |
/* Add the sequencing info */ |
| 131 |
jpeg_write_m_byte(cinfo, cur_marker); |
| 132 |
jpeg_write_m_byte(cinfo, (int) num_markers); |
| 133 |
|
| 134 |
/* Add the profile data */ |
| 135 |
while (length--) { |
| 136 |
jpeg_write_m_byte(cinfo, *icc_data_ptr); |
| 137 |
icc_data_ptr++; |
| 138 |
} |
| 139 |
cur_marker++; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
#endif // !defined(HAVE_JPEG_WRITE_ICC_PROFILE) |
| 144 |
|
| 145 |
bool JpegEncoder::Encode(const struct heif_image_handle* handle, |
| 146 |
const struct heif_image* image, const std::string& filename) |
| 147 |
{ |
| 148 |
FILE* fp = fopen(filename.c_str(), "wb"); |
| 149 |
if (!fp) { |
| 150 |
fprintf(stderr, "Can't open %s: %s\n", filename.c_str(), strerror(errno)); |
| 151 |
return false; |
| 152 |
} |
| 153 |
|
| 154 |
struct jpeg_compress_struct cinfo; |
| 155 |
struct ErrorHandler jerr; |
| 156 |
cinfo.err = jpeg_std_error(reinterpret_cast<struct jpeg_error_mgr*>(&jerr)); |
| 157 |
jerr.pub.error_exit = &OnJpegError; |
| 158 |
if (setjmp(jerr.setjmp_buffer)) { |
| 159 |
cinfo.err->output_message(reinterpret_cast<j_common_ptr>(&cinfo)); |
| 160 |
jpeg_destroy_compress(&cinfo); |
| 161 |
fclose(fp); |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
jpeg_create_compress(&cinfo); |
| 166 |
jpeg_stdio_dest(&cinfo, fp); |
| 167 |
|
| 168 |
cinfo.image_width = heif_image_get_width(image, heif_channel_Y); |
| 169 |
cinfo.image_height = heif_image_get_height(image, heif_channel_Y); |
| 170 |
cinfo.input_components = 3; |
| 171 |
cinfo.in_color_space = JCS_YCbCr; |
| 172 |
jpeg_set_defaults(&cinfo); |
| 173 |
static const boolean kForceBaseline = TRUE; |
| 174 |
jpeg_set_quality(&cinfo, quality_, kForceBaseline); |
| 175 |
static const boolean kWriteAllTables = TRUE; |
| 176 |
jpeg_start_compress(&cinfo, kWriteAllTables); |
| 177 |
|
| 178 |
// --- Write EXIF |
| 179 |
|
| 180 |
if (handle) { |
| 181 |
size_t exifsize = 0; |
| 182 |
uint8_t* exifdata = GetExifMetaData(handle, &exifsize); |
| 183 |
if (exifdata) { |
| 184 |
if (exifsize > 4) { |
| 185 |
static const uint8_t kExifMarker = JPEG_APP0 + 1; |
| 186 |
|
| 187 |
uint32_t skip = four_bytes_to_uint32(exifdata[0], exifdata[1], exifdata[2], exifdata[3]); |
| 188 |
if (skip > (exifsize - 4)) { |
| 189 |
fprintf(stderr, "Invalid EXIF data (offset too large)\n"); |
| 190 |
free(exifdata); |
| 191 |
jpeg_destroy_compress(&cinfo); |
| 192 |
fclose(fp); |
| 193 |
return false; |
| 194 |
} |
| 195 |
skip += 4; |
| 196 |
|
| 197 |
uint8_t* ptr = exifdata + skip; |
| 198 |
size_t size = exifsize - skip; |
| 199 |
|
| 200 |
if (size > std::numeric_limits<uint32_t>::max()) { |
| 201 |
fprintf(stderr, "EXIF larger than 4GB is not supported"); |
| 202 |
free(exifdata); |
| 203 |
jpeg_destroy_compress(&cinfo); |
| 204 |
fclose(fp); |
| 205 |
return false; |
| 206 |
} |
| 207 |
|
| 208 |
auto size32 = static_cast<uint32_t>(size); |
| 209 |
|
| 210 |
// libheif by default normalizes the image orientation, so that we have to set the EXIF Orientation to "Horizontal (normal)" |
| 211 |
modify_exif_orientation_tag_if_it_exists(ptr, size32, 1); |
| 212 |
overwrite_exif_image_size_if_it_exists(ptr, size32, cinfo.image_width, cinfo.image_height); |
| 213 |
|
| 214 |
// We have to limit the size for the memcpy, otherwise GCC warns that we exceed the maximum size. |
| 215 |
if (size > 0x1000000) { |
| 216 |
size = 0x1000000; |
| 217 |
} |
| 218 |
|
| 219 |
std::vector<uint8_t> jpegExifMarkerData(6 + size); |
| 220 |
memcpy(jpegExifMarkerData.data() + 6, ptr, size); |
| 221 |
jpegExifMarkerData[0] = 'E'; |
| 222 |
jpegExifMarkerData[1] = 'x'; |
| 223 |
jpegExifMarkerData[2] = 'i'; |
| 224 |
jpegExifMarkerData[3] = 'f'; |
| 225 |
jpegExifMarkerData[4] = 0; |
| 226 |
jpegExifMarkerData[5] = 0; |
| 227 |
|
| 228 |
ptr = jpegExifMarkerData.data(); |
| 229 |
size = jpegExifMarkerData.size(); |
| 230 |
|
| 231 |
while (size > MAX_BYTES_IN_MARKER) { |
| 232 |
jpeg_write_marker(&cinfo, kExifMarker, ptr, |
| 233 |
static_cast<unsigned int>(MAX_BYTES_IN_MARKER)); |
| 234 |
|
| 235 |
ptr += MAX_BYTES_IN_MARKER; |
| 236 |
size -= MAX_BYTES_IN_MARKER; |
| 237 |
} |
| 238 |
|
| 239 |
jpeg_write_marker(&cinfo, kExifMarker, ptr, |
| 240 |
static_cast<unsigned int>(size)); |
| 241 |
} |
| 242 |
|
| 243 |
free(exifdata); |
| 244 |
} |
| 245 |
} |
| 246 |
|
| 247 |
// --- Write XMP |
| 248 |
|
| 249 |
// spec: https://raw.githubusercontent.com/adobe/xmp-docs/master/XMPSpecifications/XMPSpecificationPart3.pdf |
| 250 |
|
| 251 |
if (handle) { |
| 252 |
auto xmp = get_xmp_metadata(handle); |
| 253 |
if (xmp.size() > 65502) { |
| 254 |
fprintf(stderr, "XMP data too large, ExtendedXMP is not supported yet.\n"); |
| 255 |
} |
| 256 |
else if (!xmp.empty()) { |
| 257 |
std::vector<uint8_t> xmpWithId; |
| 258 |
xmpWithId.resize(xmp.size() + strlen(JPEG_XMP_MARKER_ID) + 1); |
| 259 |
strcpy((char*) xmpWithId.data(), JPEG_XMP_MARKER_ID); |
| 260 |
memcpy(xmpWithId.data() + strlen(JPEG_XMP_MARKER_ID) + 1, xmp.data(), xmp.size()); |
| 261 |
|
| 262 |
jpeg_write_marker(&cinfo, JPEG_XMP_MARKER, xmpWithId.data(), static_cast<unsigned int>(xmpWithId.size())); |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
// --- Write ICC |
| 267 |
|
| 268 |
if (handle) { |
| 269 |
size_t profile_size = heif_image_handle_get_raw_color_profile_size(handle); |
| 270 |
if (profile_size > 0) { |
| 271 |
uint8_t* profile_data = static_cast<uint8_t*>(malloc(profile_size)); |
| 272 |
heif_image_handle_get_raw_color_profile(handle, profile_data); |
| 273 |
jpeg_write_icc_profile(&cinfo, profile_data, (unsigned int) profile_size); |
| 274 |
free(profile_data); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
if (heif_image_get_bits_per_pixel(image, heif_channel_Y) != 8) { |
| 279 |
fprintf(stderr, "JPEG writer cannot handle image with >8 bpp.\n"); |
| 280 |
jpeg_destroy_compress(&cinfo); |
| 281 |
fclose(fp); |
| 282 |
return false; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
size_t stride_y; |
| 287 |
const uint8_t* row_y = heif_image_get_plane_readonly2(image, heif_channel_Y, |
| 288 |
&stride_y); |
| 289 |
size_t stride_u; |
| 290 |
const uint8_t* row_u = heif_image_get_plane_readonly2(image, heif_channel_Cb, |
| 291 |
&stride_u); |
| 292 |
size_t stride_v; |
| 293 |
const uint8_t* row_v = heif_image_get_plane_readonly2(image, heif_channel_Cr, |
| 294 |
&stride_v); |
| 295 |
|
| 296 |
JSAMPARRAY buffer = cinfo.mem->alloc_sarray( |
| 297 |
reinterpret_cast<j_common_ptr>(&cinfo), JPOOL_IMAGE, |
| 298 |
cinfo.image_width * cinfo.input_components, 1); |
| 299 |
JSAMPROW row[1] = {buffer[0]}; |
| 300 |
|
| 301 |
while (cinfo.next_scanline < cinfo.image_height) { |
| 302 |
size_t offset_y = cinfo.next_scanline * stride_y; |
| 303 |
const uint8_t* start_y = &row_y[offset_y]; |
| 304 |
size_t offset_u = (cinfo.next_scanline / 2) * stride_u; |
| 305 |
const uint8_t* start_u = &row_u[offset_u]; |
| 306 |
size_t offset_v = (cinfo.next_scanline / 2) * stride_v; |
| 307 |
const uint8_t* start_v = &row_v[offset_v]; |
| 308 |
|
| 309 |
JOCTET* bufp = buffer[0]; |
| 310 |
for (JDIMENSION x = 0; x < cinfo.image_width; ++x) { |
| 311 |
*bufp++ = start_y[x]; |
| 312 |
*bufp++ = start_u[x / 2]; |
| 313 |
*bufp++ = start_v[x / 2]; |
| 314 |
} |
| 315 |
jpeg_write_scanlines(&cinfo, row, 1); |
| 316 |
} |
| 317 |
jpeg_finish_compress(&cinfo); |
| 318 |
fclose(fp); |
| 319 |
jpeg_destroy_compress(&cinfo); |
| 320 |
return true; |
| 321 |
} |