File indexing completed on 2024-05-12 04:19:35

0001 /*
0002  * Little cms
0003  * Copyright (C) 1998-2004 Marti Maria <x@unknown.com>
0004  *
0005  * Permission is hereby granted, free of charge, to any person obtaining
0006  * a copy of this software and associated documentation files (the
0007  * "Software"), to deal in the Software without restriction, including
0008  * without limitation the rights to use, copy, modify, merge, publish,
0009  * distribute, sublicense, and/or sell copies of the Software, and to
0010  * permit persons to whom the Software is furnished to do so, subject to
0011  * the following conditions:
0012  *
0013  * The above copyright notice and this permission notice shall be included
0014  * in all copies or substantial portions of the Software.
0015  *
0016  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
0017  * KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
0018  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
0019  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
0020  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
0021  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
0022  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
0023  *
0024  * iccprofile.c
0025  *
0026  * This file provides code to read and write International Color Consortium
0027  * (ICC) device profiles embedded in JFIF JPEG image files.  The ICC has
0028  * defined a standard format for including such data in JPEG "APP2" markers.
0029  * The code given here does not know anything about the internal structure
0030  * of the ICC profile data; it just knows how to put the profile data into
0031  * a JPEG file being written, or get it back out when reading.
0032  *
0033  * This code depends on new features added to the IJG JPEG library as of
0034  * IJG release 6b; it will not compile or work with older IJG versions.
0035  *
0036  * NOTE: this code would need surgery to work on 16-bit-int machines
0037  * with ICC profiles exceeding 64K bytes in size.  If you need to do that,
0038  * change all the "unsigned int" variables to "INT32".  You'll also need
0039  * to find a malloc() replacement that can allocate more than 64K.
0040  */
0041 
0042 #include "iccjpeg.h"
0043 #include <stdlib.h>         /* define malloc() */
0044 
0045 
0046 /*
0047  * Since an ICC profile can be larger than the maximum size of a JPEG marker
0048  * (64K), we need provisions to split it into multiple markers.  The format
0049  * defined by the ICC specifies one or more APP2 markers containing the
0050  * following data:
0051  *  Identifying string  ASCII "ICC_PROFILE\0"  (12 bytes)
0052  *  Marker sequence number  1 for first APP2, 2 for next, etc (1 byte)
0053  *  Number of markers   Total number of APP2's used (1 byte)
0054  *      Profile data        (remainder of APP2 data)
0055  * Decoders should use the marker sequence numbers to reassemble the profile,
0056  * rather than assuming that the APP2 markers appear in the correct sequence.
0057  */
0058 
0059 #define ICC_MARKER  (JPEG_APP0 + 2) /* JPEG marker code for ICC */
0060 #define ICC_OVERHEAD_LEN  14        /* size of non-profile data in APP2 */
0061 #define MAX_BYTES_IN_MARKER  65533  /* maximum data len of a JPEG marker */
0062 #define MAX_DATA_BYTES_IN_MARKER  (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
0063 
0064 
0065 /*
0066  * This routine writes the given ICC profile data into a JPEG file.
0067  * It *must* be called AFTER calling jpeg_start_compress() and BEFORE
0068  * the first call to jpeg_write_scanlines().
0069  * (This ordering ensures that the APP2 marker(s) will appear after the
0070  * SOI and JFIF or Adobe markers, but before all else.)
0071  */
0072 
0073 void
0074 write_icc_profile (j_compress_ptr cinfo,
0075            const JOCTET *icc_data_ptr,
0076            unsigned int icc_data_len)
0077 {
0078   unsigned int num_markers; /* total number of markers we'll write */
0079   int cur_marker = 1;       /* per spec, counting starts at 1 */
0080   unsigned int length;      /* number of bytes to write in this marker */
0081 
0082   /* Calculate the number of markers we'll need, rounding up of course */
0083   num_markers = icc_data_len / MAX_DATA_BYTES_IN_MARKER;
0084   if (num_markers * MAX_DATA_BYTES_IN_MARKER != icc_data_len)
0085     num_markers++;
0086 
0087   while (icc_data_len > 0) {
0088     /* length of profile to put in this marker */
0089     length = icc_data_len;
0090     if (length > MAX_DATA_BYTES_IN_MARKER)
0091       length = MAX_DATA_BYTES_IN_MARKER;
0092     icc_data_len -= length;
0093 
0094     /* Write the JPEG marker header (APP2 code and marker length) */
0095     jpeg_write_m_header(cinfo, ICC_MARKER,
0096             (unsigned int) (length + ICC_OVERHEAD_LEN));
0097 
0098     /* Write the marker identifying string "ICC_PROFILE" (null-terminated).
0099      * We code it in this less-than-transparent way so that the code works
0100      * even if the local character set is not ASCII.
0101      */
0102     jpeg_write_m_byte(cinfo, 0x49);
0103     jpeg_write_m_byte(cinfo, 0x43);
0104     jpeg_write_m_byte(cinfo, 0x43);
0105     jpeg_write_m_byte(cinfo, 0x5F);
0106     jpeg_write_m_byte(cinfo, 0x50);
0107     jpeg_write_m_byte(cinfo, 0x52);
0108     jpeg_write_m_byte(cinfo, 0x4F);
0109     jpeg_write_m_byte(cinfo, 0x46);
0110     jpeg_write_m_byte(cinfo, 0x49);
0111     jpeg_write_m_byte(cinfo, 0x4C);
0112     jpeg_write_m_byte(cinfo, 0x45);
0113     jpeg_write_m_byte(cinfo, 0x0);
0114 
0115     /* Add the sequencing info */
0116     jpeg_write_m_byte(cinfo, cur_marker);
0117     jpeg_write_m_byte(cinfo, (int) num_markers);
0118 
0119     /* Add the profile data */
0120     while (length--) {
0121       jpeg_write_m_byte(cinfo, *icc_data_ptr);
0122       icc_data_ptr++;
0123     }
0124     cur_marker++;
0125   }
0126 }
0127 
0128 
0129 /*
0130  * Prepare for reading an ICC profile
0131  */
0132 
0133 void
0134 setup_read_icc_profile (j_decompress_ptr cinfo)
0135 {
0136   /* Tell the library to keep any APP2 data it may find */
0137   jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
0138 }
0139 
0140 
0141 /*
0142  * Handy subroutine to test whether a saved marker is an ICC profile marker.
0143  */
0144 
0145 static boolean
0146 marker_is_icc (jpeg_saved_marker_ptr marker)
0147 {
0148   return
0149     marker->marker == ICC_MARKER &&
0150     marker->data_length >= ICC_OVERHEAD_LEN &&
0151     /* verify the identifying string */
0152     GETJOCTET(marker->data[0]) == 0x49 &&
0153     GETJOCTET(marker->data[1]) == 0x43 &&
0154     GETJOCTET(marker->data[2]) == 0x43 &&
0155     GETJOCTET(marker->data[3]) == 0x5F &&
0156     GETJOCTET(marker->data[4]) == 0x50 &&
0157     GETJOCTET(marker->data[5]) == 0x52 &&
0158     GETJOCTET(marker->data[6]) == 0x4F &&
0159     GETJOCTET(marker->data[7]) == 0x46 &&
0160     GETJOCTET(marker->data[8]) == 0x49 &&
0161     GETJOCTET(marker->data[9]) == 0x4C &&
0162     GETJOCTET(marker->data[10]) == 0x45 &&
0163     GETJOCTET(marker->data[11]) == 0x0;
0164 }
0165 
0166 
0167 /*
0168  * See if there was an ICC profile in the JPEG file being read;
0169  * if so, reassemble and return the profile data.
0170  *
0171  * TRUE is returned if an ICC profile was found, FALSE if not.
0172  * If TRUE is returned, *icc_data_ptr is set to point to the
0173  * returned data, and *icc_data_len is set to its length.
0174  *
0175  * IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
0176  * and must be freed by the caller with free() when the caller no longer
0177  * needs it.  (Alternatively, we could write this routine to use the
0178  * IJG library's memory allocator, so that the data would be freed implicitly
0179  * at jpeg_finish_decompress() time.  But it seems likely that many apps
0180  * will prefer to have the data stick around after decompression finishes.)
0181  *
0182  * NOTE: if the file contains invalid ICC APP2 markers, we just silently
0183  * return FALSE.  You might want to issue an error message instead.
0184  */
0185 
0186 boolean
0187 read_icc_profile (j_decompress_ptr cinfo,
0188           JOCTET **icc_data_ptr,
0189           unsigned int *icc_data_len)
0190 {
0191   jpeg_saved_marker_ptr marker;
0192   int num_markers = 0;
0193   int seq_no;
0194   JOCTET *icc_data;
0195   unsigned int total_length;
0196 #define MAX_SEQ_NO  255     /* sufficient since marker numbers are bytes */
0197   char marker_present[MAX_SEQ_NO+1];      /* 1 if marker found */
0198   unsigned int data_length[MAX_SEQ_NO+1]; /* size of profile data in marker */
0199   unsigned int data_offset[MAX_SEQ_NO+1]; /* offset for data in marker */
0200 
0201   *icc_data_ptr = NULL;     /* avoid confusion if FALSE return */
0202   *icc_data_len = 0;
0203 
0204   /* This first pass over the saved markers discovers whether there are
0205    * any ICC markers and verifies the consistency of the marker numbering.
0206    */
0207 
0208   for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
0209     marker_present[seq_no] = 0;
0210 
0211   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
0212     if (marker_is_icc(marker)) {
0213       if (num_markers == 0)
0214     num_markers = GETJOCTET(marker->data[13]);
0215       else if (num_markers != GETJOCTET(marker->data[13]))
0216     return FALSE;       /* inconsistent num_markers fields */
0217       seq_no = GETJOCTET(marker->data[12]);
0218       if (seq_no <= 0 || seq_no > num_markers)
0219     return FALSE;       /* bogus sequence number */
0220       if (marker_present[seq_no])
0221     return FALSE;       /* duplicate sequence numbers */
0222       marker_present[seq_no] = 1;
0223       data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
0224     }
0225   }
0226 
0227   if (num_markers == 0)
0228     return FALSE;
0229 
0230   /* Check for missing markers, count total space needed,
0231    * compute offset of each marker's part of the data.
0232    */
0233 
0234   total_length = 0;
0235   for (seq_no = 1; seq_no <= num_markers; seq_no++) {
0236     if (marker_present[seq_no] == 0)
0237       return FALSE;     /* missing sequence number */
0238     data_offset[seq_no] = total_length;
0239     total_length += data_length[seq_no];
0240   }
0241 
0242   if (total_length <= 0)
0243     return FALSE;       /* found only empty markers? */
0244 
0245   /* Allocate space for assembled data */
0246   icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
0247   if (icc_data == NULL)
0248     return FALSE;       /* oops, out of memory */
0249 
0250   /* and fill it in */
0251   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
0252     if (marker_is_icc(marker)) {
0253       JOCTET FAR *src_ptr;
0254       JOCTET *dst_ptr;
0255       unsigned int length;
0256       seq_no = GETJOCTET(marker->data[12]);
0257       dst_ptr = icc_data + data_offset[seq_no];
0258       src_ptr = marker->data + ICC_OVERHEAD_LEN;
0259       length = data_length[seq_no];
0260       while (length--) {
0261     *dst_ptr++ = *src_ptr++;
0262       }
0263     }
0264   }
0265 
0266   *icc_data_ptr = icc_data;
0267   *icc_data_len = total_length;
0268 
0269   return TRUE;
0270 }