xref: /aosp_15_r20/external/libjpeg-turbo/jdicc.c (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker  * jdicc.c
3*dfc6aa5cSAndroid Build Coastguard Worker  *
4*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright (C) 1997-1998, Thomas G. Lane, Todd Newman.
5*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright (C) 2017, D. R. Commander.
6*dfc6aa5cSAndroid Build Coastguard Worker  * For conditions of distribution and use, see the accompanying README.ijg
7*dfc6aa5cSAndroid Build Coastguard Worker  * file.
8*dfc6aa5cSAndroid Build Coastguard Worker  *
9*dfc6aa5cSAndroid Build Coastguard Worker  * This file provides code to read International Color Consortium (ICC) device
10*dfc6aa5cSAndroid Build Coastguard Worker  * profiles embedded in JFIF JPEG image files.  The ICC has defined a standard
11*dfc6aa5cSAndroid Build Coastguard Worker  * for including such data in JPEG "APP2" markers.  The code given here does
12*dfc6aa5cSAndroid Build Coastguard Worker  * not know anything about the internal structure of the ICC profile data; it
13*dfc6aa5cSAndroid Build Coastguard Worker  * just knows how to get the profile data from a JPEG file while reading it.
14*dfc6aa5cSAndroid Build Coastguard Worker  */
15*dfc6aa5cSAndroid Build Coastguard Worker 
16*dfc6aa5cSAndroid Build Coastguard Worker #define JPEG_INTERNALS
17*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
18*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
19*dfc6aa5cSAndroid Build Coastguard Worker #include "jerror.h"
20*dfc6aa5cSAndroid Build Coastguard Worker 
21*dfc6aa5cSAndroid Build Coastguard Worker 
22*dfc6aa5cSAndroid Build Coastguard Worker #define ICC_MARKER  (JPEG_APP0 + 2)     /* JPEG marker code for ICC */
23*dfc6aa5cSAndroid Build Coastguard Worker #define ICC_OVERHEAD_LEN  14            /* size of non-profile data in APP2 */
24*dfc6aa5cSAndroid Build Coastguard Worker 
25*dfc6aa5cSAndroid Build Coastguard Worker 
26*dfc6aa5cSAndroid Build Coastguard Worker /*
27*dfc6aa5cSAndroid Build Coastguard Worker  * Handy subroutine to test whether a saved marker is an ICC profile marker.
28*dfc6aa5cSAndroid Build Coastguard Worker  */
29*dfc6aa5cSAndroid Build Coastguard Worker 
30*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(boolean)
marker_is_icc(jpeg_saved_marker_ptr marker)31*dfc6aa5cSAndroid Build Coastguard Worker marker_is_icc(jpeg_saved_marker_ptr marker)
32*dfc6aa5cSAndroid Build Coastguard Worker {
33*dfc6aa5cSAndroid Build Coastguard Worker   return
34*dfc6aa5cSAndroid Build Coastguard Worker     marker->marker == ICC_MARKER &&
35*dfc6aa5cSAndroid Build Coastguard Worker     marker->data_length >= ICC_OVERHEAD_LEN &&
36*dfc6aa5cSAndroid Build Coastguard Worker     /* verify the identifying string */
37*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[0] == 0x49 &&
38*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[1] == 0x43 &&
39*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[2] == 0x43 &&
40*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[3] == 0x5F &&
41*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[4] == 0x50 &&
42*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[5] == 0x52 &&
43*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[6] == 0x4F &&
44*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[7] == 0x46 &&
45*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[8] == 0x49 &&
46*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[9] == 0x4C &&
47*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[10] == 0x45 &&
48*dfc6aa5cSAndroid Build Coastguard Worker     marker->data[11] == 0x0;
49*dfc6aa5cSAndroid Build Coastguard Worker }
50*dfc6aa5cSAndroid Build Coastguard Worker 
51*dfc6aa5cSAndroid Build Coastguard Worker 
52*dfc6aa5cSAndroid Build Coastguard Worker /*
53*dfc6aa5cSAndroid Build Coastguard Worker  * See if there was an ICC profile in the JPEG file being read; if so,
54*dfc6aa5cSAndroid Build Coastguard Worker  * reassemble and return the profile data.
55*dfc6aa5cSAndroid Build Coastguard Worker  *
56*dfc6aa5cSAndroid Build Coastguard Worker  * TRUE is returned if an ICC profile was found, FALSE if not.  If TRUE is
57*dfc6aa5cSAndroid Build Coastguard Worker  * returned, *icc_data_ptr is set to point to the returned data, and
58*dfc6aa5cSAndroid Build Coastguard Worker  * *icc_data_len is set to its length.
59*dfc6aa5cSAndroid Build Coastguard Worker  *
60*dfc6aa5cSAndroid Build Coastguard Worker  * IMPORTANT: the data at *icc_data_ptr is allocated with malloc() and must be
61*dfc6aa5cSAndroid Build Coastguard Worker  * freed by the caller with free() when the caller no longer needs it.
62*dfc6aa5cSAndroid Build Coastguard Worker  * (Alternatively, we could write this routine to use the IJG library's memory
63*dfc6aa5cSAndroid Build Coastguard Worker  * allocator, so that the data would be freed implicitly when
64*dfc6aa5cSAndroid Build Coastguard Worker  * jpeg_finish_decompress() is called.  But it seems likely that many
65*dfc6aa5cSAndroid Build Coastguard Worker  * applications will prefer to have the data stick around after decompression
66*dfc6aa5cSAndroid Build Coastguard Worker  * finishes.)
67*dfc6aa5cSAndroid Build Coastguard Worker  */
68*dfc6aa5cSAndroid Build Coastguard Worker 
69*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(boolean)
jpeg_read_icc_profile(j_decompress_ptr cinfo,JOCTET ** icc_data_ptr,unsigned int * icc_data_len)70*dfc6aa5cSAndroid Build Coastguard Worker jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr,
71*dfc6aa5cSAndroid Build Coastguard Worker                       unsigned int *icc_data_len)
72*dfc6aa5cSAndroid Build Coastguard Worker {
73*dfc6aa5cSAndroid Build Coastguard Worker   jpeg_saved_marker_ptr marker;
74*dfc6aa5cSAndroid Build Coastguard Worker   int num_markers = 0;
75*dfc6aa5cSAndroid Build Coastguard Worker   int seq_no;
76*dfc6aa5cSAndroid Build Coastguard Worker   JOCTET *icc_data;
77*dfc6aa5cSAndroid Build Coastguard Worker   unsigned int total_length;
78*dfc6aa5cSAndroid Build Coastguard Worker #define MAX_SEQ_NO  255         /* sufficient since marker numbers are bytes */
79*dfc6aa5cSAndroid Build Coastguard Worker   char marker_present[MAX_SEQ_NO + 1];      /* 1 if marker found */
80*dfc6aa5cSAndroid Build Coastguard Worker   unsigned int data_length[MAX_SEQ_NO + 1]; /* size of profile data in marker */
81*dfc6aa5cSAndroid Build Coastguard Worker   unsigned int data_offset[MAX_SEQ_NO + 1]; /* offset for data in marker */
82*dfc6aa5cSAndroid Build Coastguard Worker 
83*dfc6aa5cSAndroid Build Coastguard Worker   if (icc_data_ptr == NULL || icc_data_len == NULL)
84*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BUFFER_SIZE);
85*dfc6aa5cSAndroid Build Coastguard Worker   if (cinfo->global_state < DSTATE_READY)
86*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
87*dfc6aa5cSAndroid Build Coastguard Worker 
88*dfc6aa5cSAndroid Build Coastguard Worker   *icc_data_ptr = NULL;         /* avoid confusion if FALSE return */
89*dfc6aa5cSAndroid Build Coastguard Worker   *icc_data_len = 0;
90*dfc6aa5cSAndroid Build Coastguard Worker 
91*dfc6aa5cSAndroid Build Coastguard Worker   /* This first pass over the saved markers discovers whether there are
92*dfc6aa5cSAndroid Build Coastguard Worker    * any ICC markers and verifies the consistency of the marker numbering.
93*dfc6aa5cSAndroid Build Coastguard Worker    */
94*dfc6aa5cSAndroid Build Coastguard Worker 
95*dfc6aa5cSAndroid Build Coastguard Worker   for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
96*dfc6aa5cSAndroid Build Coastguard Worker     marker_present[seq_no] = 0;
97*dfc6aa5cSAndroid Build Coastguard Worker 
98*dfc6aa5cSAndroid Build Coastguard Worker   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
99*dfc6aa5cSAndroid Build Coastguard Worker     if (marker_is_icc(marker)) {
100*dfc6aa5cSAndroid Build Coastguard Worker       if (num_markers == 0)
101*dfc6aa5cSAndroid Build Coastguard Worker         num_markers = marker->data[13];
102*dfc6aa5cSAndroid Build Coastguard Worker       else if (num_markers != marker->data[13]) {
103*dfc6aa5cSAndroid Build Coastguard Worker         WARNMS(cinfo, JWRN_BOGUS_ICC);  /* inconsistent num_markers fields */
104*dfc6aa5cSAndroid Build Coastguard Worker         return FALSE;
105*dfc6aa5cSAndroid Build Coastguard Worker       }
106*dfc6aa5cSAndroid Build Coastguard Worker       seq_no = marker->data[12];
107*dfc6aa5cSAndroid Build Coastguard Worker       if (seq_no <= 0 || seq_no > num_markers) {
108*dfc6aa5cSAndroid Build Coastguard Worker         WARNMS(cinfo, JWRN_BOGUS_ICC);  /* bogus sequence number */
109*dfc6aa5cSAndroid Build Coastguard Worker         return FALSE;
110*dfc6aa5cSAndroid Build Coastguard Worker       }
111*dfc6aa5cSAndroid Build Coastguard Worker       if (marker_present[seq_no]) {
112*dfc6aa5cSAndroid Build Coastguard Worker         WARNMS(cinfo, JWRN_BOGUS_ICC);  /* duplicate sequence numbers */
113*dfc6aa5cSAndroid Build Coastguard Worker         return FALSE;
114*dfc6aa5cSAndroid Build Coastguard Worker       }
115*dfc6aa5cSAndroid Build Coastguard Worker       marker_present[seq_no] = 1;
116*dfc6aa5cSAndroid Build Coastguard Worker       data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
117*dfc6aa5cSAndroid Build Coastguard Worker     }
118*dfc6aa5cSAndroid Build Coastguard Worker   }
119*dfc6aa5cSAndroid Build Coastguard Worker 
120*dfc6aa5cSAndroid Build Coastguard Worker   if (num_markers == 0)
121*dfc6aa5cSAndroid Build Coastguard Worker     return FALSE;
122*dfc6aa5cSAndroid Build Coastguard Worker 
123*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for missing markers, count total space needed,
124*dfc6aa5cSAndroid Build Coastguard Worker    * compute offset of each marker's part of the data.
125*dfc6aa5cSAndroid Build Coastguard Worker    */
126*dfc6aa5cSAndroid Build Coastguard Worker 
127*dfc6aa5cSAndroid Build Coastguard Worker   total_length = 0;
128*dfc6aa5cSAndroid Build Coastguard Worker   for (seq_no = 1; seq_no <= num_markers; seq_no++) {
129*dfc6aa5cSAndroid Build Coastguard Worker     if (marker_present[seq_no] == 0) {
130*dfc6aa5cSAndroid Build Coastguard Worker       WARNMS(cinfo, JWRN_BOGUS_ICC);  /* missing sequence number */
131*dfc6aa5cSAndroid Build Coastguard Worker       return FALSE;
132*dfc6aa5cSAndroid Build Coastguard Worker     }
133*dfc6aa5cSAndroid Build Coastguard Worker     data_offset[seq_no] = total_length;
134*dfc6aa5cSAndroid Build Coastguard Worker     total_length += data_length[seq_no];
135*dfc6aa5cSAndroid Build Coastguard Worker   }
136*dfc6aa5cSAndroid Build Coastguard Worker 
137*dfc6aa5cSAndroid Build Coastguard Worker   if (total_length == 0) {
138*dfc6aa5cSAndroid Build Coastguard Worker     WARNMS(cinfo, JWRN_BOGUS_ICC);  /* found only empty markers? */
139*dfc6aa5cSAndroid Build Coastguard Worker     return FALSE;
140*dfc6aa5cSAndroid Build Coastguard Worker   }
141*dfc6aa5cSAndroid Build Coastguard Worker 
142*dfc6aa5cSAndroid Build Coastguard Worker   /* Allocate space for assembled data */
143*dfc6aa5cSAndroid Build Coastguard Worker   icc_data = (JOCTET *)malloc(total_length * sizeof(JOCTET));
144*dfc6aa5cSAndroid Build Coastguard Worker   if (icc_data == NULL)
145*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11);  /* oops, out of memory */
146*dfc6aa5cSAndroid Build Coastguard Worker 
147*dfc6aa5cSAndroid Build Coastguard Worker   /* and fill it in */
148*dfc6aa5cSAndroid Build Coastguard Worker   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
149*dfc6aa5cSAndroid Build Coastguard Worker     if (marker_is_icc(marker)) {
150*dfc6aa5cSAndroid Build Coastguard Worker       JOCTET FAR *src_ptr;
151*dfc6aa5cSAndroid Build Coastguard Worker       JOCTET *dst_ptr;
152*dfc6aa5cSAndroid Build Coastguard Worker       unsigned int length;
153*dfc6aa5cSAndroid Build Coastguard Worker       seq_no = marker->data[12];
154*dfc6aa5cSAndroid Build Coastguard Worker       dst_ptr = icc_data + data_offset[seq_no];
155*dfc6aa5cSAndroid Build Coastguard Worker       src_ptr = marker->data + ICC_OVERHEAD_LEN;
156*dfc6aa5cSAndroid Build Coastguard Worker       length = data_length[seq_no];
157*dfc6aa5cSAndroid Build Coastguard Worker       while (length--) {
158*dfc6aa5cSAndroid Build Coastguard Worker         *dst_ptr++ = *src_ptr++;
159*dfc6aa5cSAndroid Build Coastguard Worker       }
160*dfc6aa5cSAndroid Build Coastguard Worker     }
161*dfc6aa5cSAndroid Build Coastguard Worker   }
162*dfc6aa5cSAndroid Build Coastguard Worker 
163*dfc6aa5cSAndroid Build Coastguard Worker   *icc_data_ptr = icc_data;
164*dfc6aa5cSAndroid Build Coastguard Worker   *icc_data_len = total_length;
165*dfc6aa5cSAndroid Build Coastguard Worker 
166*dfc6aa5cSAndroid Build Coastguard Worker   return TRUE;
167*dfc6aa5cSAndroid Build Coastguard Worker }
168