1 /*
2  *  jpegicc.c
3  *
4  *  This source file was originally part of the LCMS package. I asked
5  *  Marti for permission to change its license from MIT to GPL (Aug 4,
6  *  2006). Daniel German
7  *
8  *  Copyright (C) 1998-2006 Tom Lane and Marti Maria
9  *
10  *  This program is free software; you can redistribute it and/or
11  *  modify it under the terms of the GNU General Public
12  *  License as published by the Free Software Foundation; either
13  *  version 2 of the License, or (at your option) any later version.
14  *
15  *  This software is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public
21  *  License along with this software; if not, write to the Free Software
22  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
23  *
24  *
25  *  Author: Tom Lane and Marti Maria
26  *
27  *
28  * jpegicc.c
29  *
30  * This file provides code to read and write International Color Consortium
31  * (ICC) device profiles embedded in JFIF JPEG image files.  The ICC has
32  * defined a standard format for including such data in JPEG "APP2" markers.
33  * The code given here does not know anything about the internal structure
34  * of the ICC profile data; it just knows how to put the profile data into
35  * a JPEG file being written, or get it back out when reading.
36  *
37  * This code depends on new features added to the IJG JPEG library as of
38  * IJG release 6b; it will not compile or work with older IJG versions.
39  *
40  * NOTE: this code would need surgery to work on 16-bit-int machines
41  * with ICC profiles exceeding 64K bytes in size.  If you need to do that,
42  * change all the "unsigned int" variables to "INT32".  You'll also need
43  * to find a malloc() replacement that can allocate more than 64K.
44  */
45 
46 
47 
48 
49 
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include "jpegicc.h"
53 
54 
55 /*
56  * Since an ICC profile can be larger than the maximum size of a JPEG marker
57  * (64K), we need provisions to split it into multiple markers.  The format
58  * defined by the ICC specifies one or more APP2 markers containing the
59  * following data:
60  *      Identifying string      ASCII "ICC_PROFILE\0"  (12 bytes)
61  *      Marker sequence number  1 for first APP2, 2 for next, etc (1 byte)
62  *      Number of markers       Total number of APP2's used (1 byte)
63  *      Profile data            (remainder of APP2 data)
64  * Decoders should use the marker sequence numbers to reassemble the profile,
65  * rather than assuming that the APP2 markers appear in the correct sequence.
66  */
67 
68 #define ICC_MARKER  (JPEG_APP0 + 2)     /* JPEG marker code for ICC */
69 #define ICC_OVERHEAD_LEN  14            /* size of non-profile data in APP2 */
70 #define MAX_BYTES_IN_MARKER  65533      /* maximum data len of a JPEG marker */
71 #define MAX_DATA_BYTES_IN_MARKER  (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
72 
73 
74 /*
75  * This routine writes the given ICC profile data into a JPEG file.
76  * It *must* be called AFTER calling jpeg_start_compress() and BEFORE
77  * the first call to jpeg_write_scanlines().
78  * (This ordering ensures that the APP2 marker(s) will appear after the
79  * SOI and JFIF or Adobe markers, but before all else.)
80  */
81 
jpegICCWriteProfile(j_compress_ptr cinfo,const JOCTET * icc_data_ptr,unsigned int icc_data_len)82 void jpegICCWriteProfile(j_compress_ptr cinfo,
83                         const JOCTET *icc_data_ptr,
84                         unsigned int icc_data_len)
85 {
86     unsigned int num_markers;   /* total number of markers we'll write */
87     int cur_marker = 1;         /* per spec, counting starts at 1 */
88     unsigned int length;                /* number of bytes to write in this marker */
89 
90     /* Calculate the number of markers we'll need, rounding up of course */
91     num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER;
92     if (num_markers * MAX_DATA_BYTES_IN_MARKER != icc_data_len)
93         num_markers++;
94 
95     while (icc_data_len > 0) {
96         /* length of profile to put in this marker */
97         length = icc_data_len;
98         if (length > MAX_DATA_BYTES_IN_MARKER)
99             length = MAX_DATA_BYTES_IN_MARKER;
100         icc_data_len -= length;
101 
102         /* Write the JPEG marker header (APP2 code and marker length) */
103         jpeg_write_m_header(cinfo, ICC_MARKER,
104                             (unsigned int) (length + ICC_OVERHEAD_LEN));
105 
106         /* Write the marker identifying string "ICC_PROFILE" (null-terminated).
107          * We code it in this less-than-transparent way so that the code works
108          * even if the local character set is not ASCII.
109          */
110         jpeg_write_m_byte(cinfo, 0x49);
111         jpeg_write_m_byte(cinfo, 0x43);
112         jpeg_write_m_byte(cinfo, 0x43);
113         jpeg_write_m_byte(cinfo, 0x5F);
114         jpeg_write_m_byte(cinfo, 0x50);
115         jpeg_write_m_byte(cinfo, 0x52);
116         jpeg_write_m_byte(cinfo, 0x4F);
117         jpeg_write_m_byte(cinfo, 0x46);
118         jpeg_write_m_byte(cinfo, 0x49);
119         jpeg_write_m_byte(cinfo, 0x4C);
120         jpeg_write_m_byte(cinfo, 0x45);
121         jpeg_write_m_byte(cinfo, 0x0);
122 
123         /* Add the sequencing info */
124         jpeg_write_m_byte(cinfo, cur_marker);
125         jpeg_write_m_byte(cinfo, (int) num_markers);
126 
127         /* Add the profile data */
128         while (length--) {
129             jpeg_write_m_byte(cinfo, *icc_data_ptr);
130             icc_data_ptr++;
131         }
132         cur_marker++;
133     }
134 }
135 
136 
137 /*
138  * Reading a JPEG file that may contain an ICC profile requires two steps:
139  *
140  * 1. After jpeg_create_decompress() but before jpeg_read_header(),
141  *    call setup_read_icc_profile().  This routine tells the IJG library
142  *    to save in memory any APP2 markers it may find in the file.
143  *
144  * 2. After jpeg_read_header(), call read_icc_profile() to find out
145  *    whether there was a profile and obtain it if so.
146  */
147 
148 /*
149  * Prepare for reading an ICC profile
150  */
151 
jpegICCSetupReadICCProfile(j_decompress_ptr cinfo)152 void jpegICCSetupReadICCProfile(j_decompress_ptr cinfo)
153 {
154     /* Tell the library to keep any APP2 data it may find */
155     jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
156 }
157 
158 
159 /*
160  * Handy subroutine to test whether a saved marker is an ICC profile marker.
161  */
162 
marker_is_icc(jpeg_saved_marker_ptr marker)163 static boolean marker_is_icc (jpeg_saved_marker_ptr marker)
164 {
165     return
166         marker->marker == ICC_MARKER &&
167         marker->data_length >= ICC_OVERHEAD_LEN &&
168         /* verify the identifying string */
169         GETJOCTET(marker->data[0]) == 0x49 &&
170         GETJOCTET(marker->data[1]) == 0x43 &&
171         GETJOCTET(marker->data[2]) == 0x43 &&
172         GETJOCTET(marker->data[3]) == 0x5F &&
173         GETJOCTET(marker->data[4]) == 0x50 &&
174         GETJOCTET(marker->data[5]) == 0x52 &&
175         GETJOCTET(marker->data[6]) == 0x4F &&
176         GETJOCTET(marker->data[7]) == 0x46 &&
177         GETJOCTET(marker->data[8]) == 0x49 &&
178         GETJOCTET(marker->data[9]) == 0x4C &&
179         GETJOCTET(marker->data[10]) == 0x45 &&
180         GETJOCTET(marker->data[11]) == 0x0;
181 }
182 
183 
184 /*
185  * See if there was an ICC profile in the JPEG file being read;
186  * if so, reassemble and return the profile data.
187  *
188  * TRUE is returned if an ICC profile was found, FALSE if not.
189  * If TRUE is returned, *icc_data_ptr is set to point to the
190  * returned data, and *icc_data_len is set to its length.
191  *
192  * IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
193  * and must be freed by the caller with free() when the caller no longer
194  * needs it.  (Alternatively, we could write this routine to use the
195  * IJG library's memory allocator, so that the data would be freed implicitly
196  * at jpeg_finish_decompress() time.  But it seems likely that many apps
197  * will prefer to have the data stick around after decompression finishes.)
198  *
199  * NOTE: if the file contains invalid ICC APP2 markers, we just silently
200  * return FALSE.  You might want to issue an error message instead.
201  */
202 
jpegICCReadProfile(j_decompress_ptr cinfo,JOCTET ** icc_data_ptr,unsigned int * icc_data_len)203 boolean jpegICCReadProfile(j_decompress_ptr cinfo,
204                           JOCTET **icc_data_ptr,
205                           unsigned int *icc_data_len)
206 {
207     jpeg_saved_marker_ptr marker;
208     int num_markers = 0;
209     int seq_no;
210     JOCTET *icc_data;
211     unsigned int total_length;
212 #define MAX_SEQ_NO  255         /* sufficient since marker numbers are bytes */
213     char marker_present[MAX_SEQ_NO+1];    /* 1 if marker found */
214     unsigned int data_length[MAX_SEQ_NO+1]; /* size of profile data in marker */
215     unsigned int data_offset[MAX_SEQ_NO+1]; /* offset for data in marker */
216 
217     *icc_data_ptr = NULL;               /* avoid confusion if FALSE return */
218     *icc_data_len = 0;
219 
220     /* This first pass over the saved markers discovers whether there are
221      * any ICC markers and verifies the consistency of the marker numbering.
222      */
223 
224     for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
225         marker_present[seq_no] = 0;
226 
227     for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
228 
229         if (marker_is_icc(marker)) {
230             if (num_markers == 0)
231                 num_markers = GETJOCTET(marker->data[13]);
232             else if (num_markers != GETJOCTET(marker->data[13]))
233                 return FALSE;           /* inconsistent num_markers fields */
234             seq_no = GETJOCTET(marker->data[12]);
235             if (seq_no <= 0 || seq_no > num_markers)
236                 return FALSE;           /* bogus sequence number */
237             if (marker_present[seq_no])
238                 return FALSE;           /* duplicate sequence numbers */
239             marker_present[seq_no] = 1;
240             data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
241         }
242     }
243 
244     if (num_markers == 0)
245         return FALSE;
246 
247     /* Check for missing markers, count total space needed,
248      * compute offset of each marker's part of the data.
249      */
250 
251     total_length = 0;
252     for (seq_no = 1; seq_no <= num_markers; seq_no++) {
253         if (marker_present[seq_no] == 0)
254             return FALSE;               /* missing sequence number */
255         data_offset[seq_no] = total_length;
256         total_length += data_length[seq_no];
257 
258     }
259 
260     if (total_length <= 0)
261         return FALSE;           /* found only empty markers? */
262 
263     /* Allocate space for assembled data */
264     icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
265     if (icc_data == NULL)
266         return FALSE;           /* oops, out of memory */
267 
268     /* and fill it in */
269     for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
270         if (marker_is_icc(marker)) {
271             JOCTET FAR *src_ptr;
272             JOCTET *dst_ptr;
273             unsigned int length;
274             seq_no = GETJOCTET(marker->data[12]);
275             dst_ptr = icc_data + data_offset[seq_no];
276             src_ptr = marker->data + ICC_OVERHEAD_LEN;
277             length = data_length[seq_no];
278             while (length--) {
279                 *dst_ptr++ = *src_ptr++;
280             }
281         }
282     }
283 
284     *icc_data_ptr = icc_data;
285     *icc_data_len = total_length;
286 
287     return TRUE;
288 }
289