1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
|
/*
libheif example application "heif".
MIT License
Copyright (c) 2023 Dirk Farin <dirk.farin@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <memory>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <cassert>
#include <algorithm>
#include <array>
#include "decoder_jpeg.h"
#include "exif.h"
extern "C" {
// Prevent duplicate definition for libjpeg-turbo v2.0
// Note: these 'undef's are only a workaround for a libjpeg-turbo-v2.0 bug and
// should be removed again later. Bug has been fixed in libjpeg-turbo-v2.0.1.
#include <jconfig.h>
#if defined(LIBJPEG_TURBO_VERSION_NUMBER) && LIBJPEG_TURBO_VERSION_NUMBER == 2000000
#undef HAVE_STDDEF_H
#undef HAVE_STDLIB_H
#endif
#include <jpeglib.h>
}
#define JPEG_EXIF_MARKER (JPEG_APP0+1) /* JPEG marker code for EXIF */
#define JPEG_EXIF_MARKER_LEN 6 // "Exif/0/0"
#define JPEG_XMP_MARKER (JPEG_APP0+1) /* JPEG marker code for XMP */
#define JPEG_XMP_MARKER_ID "http://ns.adobe.com/xap/1.0/"
#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)
{
return
marker->marker == JPEG_ICC_MARKER &&
marker->data_length >= JPEG_ICC_OVERHEAD_LEN &&
/* verify the identifying string */
GETJOCTET(marker->data[0]) == 0x49 &&
GETJOCTET(marker->data[1]) == 0x43 &&
GETJOCTET(marker->data[2]) == 0x43 &&
GETJOCTET(marker->data[3]) == 0x5F &&
GETJOCTET(marker->data[4]) == 0x50 &&
GETJOCTET(marker->data[5]) == 0x52 &&
GETJOCTET(marker->data[6]) == 0x4F &&
GETJOCTET(marker->data[7]) == 0x46 &&
GETJOCTET(marker->data[8]) == 0x49 &&
GETJOCTET(marker->data[9]) == 0x4C &&
GETJOCTET(marker->data[10]) == 0x45 &&
GETJOCTET(marker->data[11]) == 0x0;
}
bool ReadICCProfileFromJPEG(j_decompress_ptr cinfo,
JOCTET** icc_data_ptr,
unsigned int* icc_data_len)
{
jpeg_saved_marker_ptr marker;
int num_markers = 0;
int seq_no;
JOCTET* icc_data;
unsigned int total_length;
#define MAX_SEQ_NO 255 /* sufficient since marker numbers are bytes */
char marker_present[MAX_SEQ_NO + 1]; /* 1 if marker found */
unsigned int data_length[MAX_SEQ_NO + 1]; /* size of profile data in marker */
unsigned int data_offset[MAX_SEQ_NO + 1]; /* offset for data in marker */
*icc_data_ptr = NULL; /* avoid confusion if FALSE return */
*icc_data_len = 0;
/* This first pass over the saved markers discovers whether there are
* any ICC markers and verifies the consistency of the marker numbering.
*/
for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
marker_present[seq_no] = 0;
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (JPEGMarkerIsIcc(marker)) {
if (num_markers == 0)
num_markers = GETJOCTET(marker->data[13]);
else if (num_markers != GETJOCTET(marker->data[13]))
return FALSE; /* inconsistent num_markers fields */
seq_no = GETJOCTET(marker->data[12]);
if (seq_no <= 0 || seq_no > num_markers)
return FALSE; /* bogus sequence number */
if (marker_present[seq_no])
return FALSE; /* duplicate sequence numbers */
marker_present[seq_no] = 1;
data_length[seq_no] = marker->data_length - JPEG_ICC_OVERHEAD_LEN;
}
}
if (num_markers == 0)
return FALSE;
/* Check for missing markers, count total space needed,
* compute offset of each marker's part of the data.
*/
total_length = 0;
for (seq_no = 1; seq_no <= num_markers; seq_no++) {
if (marker_present[seq_no] == 0)
return FALSE; /* missing sequence number */
data_offset[seq_no] = total_length;
total_length += data_length[seq_no];
}
if (total_length <= 0)
return FALSE; /* found only empty markers? */
/* Allocate space for assembled data */
icc_data = (JOCTET*) malloc(total_length * sizeof(JOCTET));
if (icc_data == NULL)
return FALSE; /* oops, out of memory */
/* and fill it in */
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (JPEGMarkerIsIcc(marker)) {
JOCTET FAR* src_ptr;
JOCTET* dst_ptr;
unsigned int length;
seq_no = GETJOCTET(marker->data[12]);
dst_ptr = icc_data + data_offset[seq_no];
src_ptr = marker->data + JPEG_ICC_OVERHEAD_LEN;
length = data_length[seq_no];
while (length--) {
*dst_ptr++ = *src_ptr++;
}
}
}
*icc_data_ptr = icc_data;
*icc_data_len = total_length;
return TRUE;
}
static bool JPEGMarkerIsXMP(jpeg_saved_marker_ptr marker)
{
return
marker->marker == JPEG_XMP_MARKER &&
marker->data_length >= strlen(JPEG_XMP_MARKER_ID) + 1 &&
strncmp((const char*) (marker->data), JPEG_XMP_MARKER_ID, strlen(JPEG_XMP_MARKER_ID)) == 0;
}
bool ReadXMPFromJPEG(j_decompress_ptr cinfo,
std::vector<uint8_t>& xmpData)
{
jpeg_saved_marker_ptr marker;
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (JPEGMarkerIsXMP(marker)) {
int length = (int) (marker->data_length - (strlen(JPEG_XMP_MARKER_ID) + 1));
xmpData.resize(length);
memcpy(xmpData.data(), marker->data + strlen(JPEG_XMP_MARKER_ID) + 1, length);
return true;
}
}
return false;
}
static bool JPEGMarkerIsEXIF(jpeg_saved_marker_ptr marker)
{
return marker->marker == JPEG_EXIF_MARKER &&
marker->data_length >= JPEG_EXIF_MARKER_LEN &&
GETJOCTET(marker->data[0]) == 'E' &&
GETJOCTET(marker->data[1]) == 'x' &&
GETJOCTET(marker->data[2]) == 'i' &&
GETJOCTET(marker->data[3]) == 'f' &&
GETJOCTET(marker->data[4]) == 0 &&
GETJOCTET(marker->data[5]) == 0;
}
bool ReadEXIFFromJPEG(j_decompress_ptr cinfo,
std::vector<uint8_t>& exifData)
{
jpeg_saved_marker_ptr marker;
for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
if (JPEGMarkerIsEXIF(marker)) {
int length = (int) (marker->data_length - JPEG_EXIF_MARKER_LEN);
exifData.resize(length);
memcpy(exifData.data(), marker->data + JPEG_EXIF_MARKER_LEN, length);
return true;
}
}
return false;
}
#if JPEG_LIB_VERSION < 70
#define DCT_h_scaled_size DCT_scaled_size
#define DCT_v_scaled_size DCT_scaled_size
#endif
heif_error loadJPEG(const char *filename, InputImage *input_image)
{
struct heif_image* image = nullptr;
struct heif_error err = heif_error_ok;
// ### Code copied from LibVideoGfx and slightly modified to use HeifPixelImage
struct jpeg_decompress_struct cinfo;
struct jpeg_error_mgr jerr;
// to store embedded icc profile
uint32_t iccLen;
uint8_t* iccBuffer = NULL;
std::vector<uint8_t> xmpData;
std::vector<uint8_t> exifData;
// open input file
FILE* infile;
if ((infile = fopen(filename, "rb")) == NULL) {
struct heif_error err = {
.code = heif_error_Invalid_input,
.subcode = heif_suberror_Unspecified,
.message = "Cannot open JPEG file"};
return err;
}
// initialize decompressor
jpeg_create_decompress(&cinfo);
cinfo.err = jpeg_std_error(&jerr);
jpeg_stdio_src(&cinfo, infile);
/* Adding this part to prepare for icc profile reading. */
jpeg_save_markers(&cinfo, JPEG_ICC_MARKER, 0xFFFF);
jpeg_save_markers(&cinfo, JPEG_XMP_MARKER, 0xFFFF);
jpeg_save_markers(&cinfo, JPEG_EXIF_MARKER, 0xFFFF);
jpeg_read_header(&cinfo, TRUE);
bool embeddedIccFlag = ReadICCProfileFromJPEG(&cinfo, &iccBuffer, &iccLen);
bool embeddedXMPFlag = ReadXMPFromJPEG(&cinfo, xmpData);
if (embeddedXMPFlag) {
input_image->xmp = xmpData;
}
bool embeddedEXIFFlag = ReadEXIFFromJPEG(&cinfo, exifData);
if (embeddedEXIFFlag) {
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) {
cinfo.out_color_space = JCS_GRAYSCALE;
jpeg_start_decompress(&cinfo);
JSAMPARRAY buffer;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width * cinfo.output_components, 1);
// create destination image
err = heif_image_create(cinfo.output_width, cinfo.output_height,
heif_colorspace_monochrome,
heif_chroma_monochrome,
&image);
if (err.code) {
goto cleanup;
}
err = heif_image_add_plane(image, heif_channel_Y, cinfo.output_width, cinfo.output_height, 8);
if (err.code) { goto cleanup; }
size_t y_stride;
uint8_t* py = heif_image_get_plane2(image, heif_channel_Y, &y_stride);
// read the image
while (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
memcpy(py + (cinfo.output_scanline - 1) * y_stride, *buffer, cinfo.output_width);
}
}
else if (cinfo.jpeg_color_space == JCS_YCbCr) {
cinfo.out_color_space = JCS_YCbCr;
bool read_raw = false;
heif_chroma output_chroma = heif_chroma_420;
if (cinfo.comp_info[1].h_samp_factor == 1 &&
cinfo.comp_info[1].v_samp_factor == 1 &&
cinfo.comp_info[2].h_samp_factor == 1 &&
cinfo.comp_info[2].v_samp_factor == 1) {
if (cinfo.comp_info[0].h_samp_factor == 1 &&
cinfo.comp_info[0].v_samp_factor == 1) {
output_chroma = heif_chroma_444;
read_raw = true;
}
else if (cinfo.comp_info[0].h_samp_factor == 2 &&
cinfo.comp_info[0].v_samp_factor == 1) {
output_chroma = heif_chroma_422;
read_raw = true;
}
else if (cinfo.comp_info[0].h_samp_factor == 2 &&
cinfo.comp_info[0].v_samp_factor == 2) {
output_chroma = heif_chroma_420;
read_raw = true;
}
}
int cw=0,ch=0;
switch (output_chroma) {
case heif_chroma_420:
cw = (cinfo.image_width + 1) / 2;
ch = (cinfo.image_height + 1) / 2;
break;
case heif_chroma_422:
cw = (cinfo.image_width + 1) / 2;
ch = cinfo.image_height;
break;
case heif_chroma_444:
cw = cinfo.image_width;
ch = cinfo.image_height;
break;
default:
assert(false);
}
//read_raw = false;
cinfo.raw_data_out = boolean(read_raw);
jpeg_start_decompress(&cinfo);
// create destination image
struct heif_error err = heif_image_create(cinfo.output_width, cinfo.output_height,
heif_colorspace_YCbCr,
output_chroma,
&image);
if (err.code) { goto cleanup; }
err = heif_image_add_plane(image, heif_channel_Y, cinfo.output_width, cinfo.output_height, 8);
if (err.code) { goto cleanup; }
err = heif_image_add_plane(image, heif_channel_Cb, cw, ch, 8);
if (err.code) { goto cleanup; }
err = heif_image_add_plane(image, heif_channel_Cr, cw, ch, 8);
if (err.code) { goto cleanup; }
size_t stride[3];
uint8_t* p[3];
p[0] = heif_image_get_plane2(image, heif_channel_Y, &stride[0]);
p[1] = heif_image_get_plane2(image, heif_channel_Cb, &stride[1]);
p[2] = heif_image_get_plane2(image, heif_channel_Cr, &stride[2]);
// read the image
if (read_raw) {
// adapted from https://github.com/AOMediaCodec/libavif/blob/430ea2df584dcb95ff1632c17643ebbbb2f3bc81/apps/shared/avifjpeg.c
JSAMPIMAGE buffer;
buffer = (JSAMPIMAGE)(cinfo.mem->alloc_small)((j_common_ptr)&cinfo, JPOOL_IMAGE, sizeof(JSAMPARRAY) * cinfo.num_components);
// lines of output image to be read per jpeg_read_raw_data call
int readLines = 0;
// lines of samples to be read per call (for each channel)
int linesPerCall[3] = { 0, 0, 0 };
// expected count of sample lines (for each channel)
int targetRead[3] = { 0, 0, 0 };
for (int i = 0; i < cinfo.num_components; ++i) {
jpeg_component_info * comp = &cinfo.comp_info[i];
linesPerCall[i] = comp->v_samp_factor * comp->DCT_v_scaled_size;
targetRead[i] = comp->downsampled_height;
buffer[i] = (cinfo.mem->alloc_sarray)((j_common_ptr)&cinfo,
JPOOL_IMAGE,
comp->width_in_blocks * comp->DCT_h_scaled_size,
linesPerCall[i]);
readLines = std::max(readLines, linesPerCall[i]);
}
// count of already-read lines (for each channel)
int alreadyRead[3] = { 0, 0, 0 };
int width[3] = { (int)cinfo.output_width, cw, cw};
std::array<int,3> targetChannel{0,1,2};
if (cinfo.jpeg_color_space == JCS_RGB) {
targetChannel = {2, 0, 1};
}
else if (cinfo.jpeg_color_space == JCS_YCbCr ||
cinfo.jpeg_color_space == JCS_GRAYSCALE) {
targetChannel = {0, 1, 2};
}
else {
return heif_error{heif_error_Unsupported_filetype,
heif_suberror_Unsupported_image_type,
"JPEG with unsupported colorspace"};
}
while (cinfo.output_scanline < cinfo.output_height) {
jpeg_read_raw_data(&cinfo, buffer, readLines);
int workComponents = 3;
for (int i = 0; i < workComponents; ++i) {
int linesRead = std::min(targetRead[i] - alreadyRead[i], linesPerCall[i]);
for (int j = 0; j < linesRead; ++j) {
memcpy(p[targetChannel[i]] + ((size_t)stride[targetChannel[i]]) * (alreadyRead[i] + j),
buffer[i][j],
width[targetChannel[i]]);
}
alreadyRead[i] += linesPerCall[i];
}
}
}
else {
JSAMPARRAY buffer;
buffer = (*cinfo.mem->alloc_sarray)
((j_common_ptr) &cinfo, JPOOL_IMAGE, cinfo.output_width * cinfo.output_components, 1);
while (cinfo.output_scanline < cinfo.output_height) {
JOCTET* bufp;
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
bufp = buffer[0];
size_t y = cinfo.output_scanline - 1;
for (unsigned int x = 0; x < cinfo.output_width; x += 2) {
p[0][y * stride[0] + x] = *bufp++;
p[1][y / 2 * stride[1] + x / 2] = *bufp++;
p[2][y / 2 * stride[2] + x / 2] = *bufp++;
if (x + 1 < cinfo.output_width) {
p[0][y * stride[0] + x + 1] = *bufp++;
}
bufp += 2;
}
if (cinfo.output_scanline < cinfo.output_height) {
(void) jpeg_read_scanlines(&cinfo, buffer, 1);
bufp = buffer[0];
y = cinfo.output_scanline - 1;
for (unsigned int x = 0; x < cinfo.output_width; x++) {
p[0][y * stride[0] + x] = *bufp++;
bufp += 2;
}
}
}
}
}
else {
// TODO: error, unsupported JPEG colorspace
}
if (embeddedIccFlag && iccLen > 0) {
heif_image_set_raw_color_profile(image, "prof", iccBuffer, (size_t) iccLen);
}
input_image->image = std::shared_ptr<heif_image>(image,
[](heif_image* img) { heif_image_release(img); });
// cleanup
cleanup:
free(iccBuffer);
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
fclose(infile);
return err;
}
|