Path: blob/master/thirdparty/libjpeg-turbo/src/jdicc.c
9904 views
/*1* jdicc.c2*3* Copyright (C) 1997-1998, Thomas G. Lane, Todd Newman.4* Copyright (C) 2017, D. R. Commander.5* For conditions of distribution and use, see the accompanying README.ijg6* file.7*8* This file provides code to read International Color Consortium (ICC) device9* profiles embedded in JFIF JPEG image files. The ICC has defined a standard10* for including such data in JPEG "APP2" markers. The code given here does11* not know anything about the internal structure of the ICC profile data; it12* just knows how to get the profile data from a JPEG file while reading it.13*/1415#define JPEG_INTERNALS16#include "jinclude.h"17#include "jpeglib.h"18#include "jerror.h"192021#define ICC_MARKER (JPEG_APP0 + 2) /* JPEG marker code for ICC */22#define ICC_OVERHEAD_LEN 14 /* size of non-profile data in APP2 */232425/*26* Handy subroutine to test whether a saved marker is an ICC profile marker.27*/2829LOCAL(boolean)30marker_is_icc(jpeg_saved_marker_ptr marker)31{32return33marker->marker == ICC_MARKER &&34marker->data_length >= ICC_OVERHEAD_LEN &&35/* verify the identifying string */36marker->data[0] == 0x49 &&37marker->data[1] == 0x43 &&38marker->data[2] == 0x43 &&39marker->data[3] == 0x5F &&40marker->data[4] == 0x50 &&41marker->data[5] == 0x52 &&42marker->data[6] == 0x4F &&43marker->data[7] == 0x46 &&44marker->data[8] == 0x49 &&45marker->data[9] == 0x4C &&46marker->data[10] == 0x45 &&47marker->data[11] == 0x0;48}495051/*52* See if there was an ICC profile in the JPEG file being read; if so,53* reassemble and return the profile data.54*55* TRUE is returned if an ICC profile was found, FALSE if not. If TRUE is56* returned, *icc_data_ptr is set to point to the returned data, and57* *icc_data_len is set to its length.58*59* IMPORTANT: the data at *icc_data_ptr is allocated with malloc() and must be60* freed by the caller with free() when the caller no longer needs it.61* (Alternatively, we could write this routine to use the IJG library's memory62* allocator, so that the data would be freed implicitly when63* jpeg_finish_decompress() is called. But it seems likely that many64* applications will prefer to have the data stick around after decompression65* finishes.)66*/6768GLOBAL(boolean)69jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr,70unsigned int *icc_data_len)71{72jpeg_saved_marker_ptr marker;73int num_markers = 0;74int seq_no;75JOCTET *icc_data;76unsigned int total_length;77#define MAX_SEQ_NO 255 /* sufficient since marker numbers are bytes */78char marker_present[MAX_SEQ_NO + 1]; /* 1 if marker found */79unsigned int data_length[MAX_SEQ_NO + 1]; /* size of profile data in marker */80unsigned int data_offset[MAX_SEQ_NO + 1]; /* offset for data in marker */8182if (icc_data_ptr == NULL || icc_data_len == NULL)83ERREXIT(cinfo, JERR_BUFFER_SIZE);84if (cinfo->global_state < DSTATE_READY)85ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);8687*icc_data_ptr = NULL; /* avoid confusion if FALSE return */88*icc_data_len = 0;8990/* This first pass over the saved markers discovers whether there are91* any ICC markers and verifies the consistency of the marker numbering.92*/9394for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)95marker_present[seq_no] = 0;9697for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {98if (marker_is_icc(marker)) {99if (num_markers == 0)100num_markers = marker->data[13];101else if (num_markers != marker->data[13]) {102WARNMS(cinfo, JWRN_BOGUS_ICC); /* inconsistent num_markers fields */103return FALSE;104}105seq_no = marker->data[12];106if (seq_no <= 0 || seq_no > num_markers) {107WARNMS(cinfo, JWRN_BOGUS_ICC); /* bogus sequence number */108return FALSE;109}110if (marker_present[seq_no]) {111WARNMS(cinfo, JWRN_BOGUS_ICC); /* duplicate sequence numbers */112return FALSE;113}114marker_present[seq_no] = 1;115data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;116}117}118119if (num_markers == 0)120return FALSE;121122/* Check for missing markers, count total space needed,123* compute offset of each marker's part of the data.124*/125126total_length = 0;127for (seq_no = 1; seq_no <= num_markers; seq_no++) {128if (marker_present[seq_no] == 0) {129WARNMS(cinfo, JWRN_BOGUS_ICC); /* missing sequence number */130return FALSE;131}132data_offset[seq_no] = total_length;133total_length += data_length[seq_no];134}135136if (total_length == 0) {137WARNMS(cinfo, JWRN_BOGUS_ICC); /* found only empty markers? */138return FALSE;139}140141/* Allocate space for assembled data */142icc_data = (JOCTET *)malloc(total_length * sizeof(JOCTET));143if (icc_data == NULL)144ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11); /* oops, out of memory */145146/* and fill it in */147for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {148if (marker_is_icc(marker)) {149JOCTET FAR *src_ptr;150JOCTET *dst_ptr;151unsigned int length;152seq_no = marker->data[12];153dst_ptr = icc_data + data_offset[seq_no];154src_ptr = marker->data + ICC_OVERHEAD_LEN;155length = data_length[seq_no];156while (length--) {157*dst_ptr++ = *src_ptr++;158}159}160}161162*icc_data_ptr = icc_data;163*icc_data_len = total_length;164165return TRUE;166}167168169