LCOV - code coverage report
Current view: directory - image/decoders - iccjpeg.c (source / functions) Found Hit Coverage
Test: app.info Lines: 62 13 21.0 %
Date: 2012-06-02 Functions: 3 2 66.7 %

       1                 : /*
       2                 :  * iccprofile.c
       3                 :  *
       4                 :  * This file provides code to read and write International Color Consortium
       5                 :  * (ICC) device profiles embedded in JFIF JPEG image files.  The ICC has
       6                 :  * defined a standard format for including such data in JPEG "APP2" markers.
       7                 :  * The code given here does not know anything about the internal structure
       8                 :  * of the ICC profile data; it just knows how to put the profile data into
       9                 :  * a JPEG file being written, or get it back out when reading.
      10                 :  *
      11                 :  * This code depends on new features added to the IJG JPEG library as of
      12                 :  * IJG release 6b; it will not compile or work with older IJG versions.
      13                 :  *
      14                 :  * NOTE: this code would need surgery to work on 16-bit-int machines
      15                 :  * with ICC profiles exceeding 64K bytes in size.  If you need to do that,
      16                 :  * change all the "unsigned int" variables to "INT32".  You'll also need
      17                 :  * to find a malloc() replacement that can allocate more than 64K.
      18                 :  */
      19                 : 
      20                 : #include "iccjpeg.h"
      21                 : #include <stdlib.h>                       /* define malloc() */
      22                 : 
      23                 : 
      24                 : /*
      25                 :  * Since an ICC profile can be larger than the maximum size of a JPEG marker
      26                 :  * (64K), we need provisions to split it into multiple markers.  The format
      27                 :  * defined by the ICC specifies one or more APP2 markers containing the
      28                 :  * following data:
      29                 :  *      Identifying string      ASCII "ICC_PROFILE\0"  (12 bytes)
      30                 :  *      Marker sequence number  1 for first APP2, 2 for next, etc (1 byte)
      31                 :  *      Number of markers       Total number of APP2's used (1 byte)
      32                 :  *      Profile data            (remainder of APP2 data)
      33                 :  * Decoders should use the marker sequence numbers to reassemble the profile,
      34                 :  * rather than assuming that the APP2 markers appear in the correct sequence.
      35                 :  */
      36                 : 
      37                 : #define ICC_MARKER  (JPEG_APP0 + 2)     /* JPEG marker code for ICC */
      38                 : #define ICC_OVERHEAD_LEN  14            /* size of non-profile data in APP2 */
      39                 : #define MAX_BYTES_IN_MARKER  65533      /* maximum data len of a JPEG marker */
      40                 : #define MAX_DATA_BYTES_IN_MARKER  (MAX_BYTES_IN_MARKER - ICC_OVERHEAD_LEN)
      41                 : 
      42                 : /*
      43                 :  * Prepare for reading an ICC profile
      44                 :  */
      45                 : 
      46                 : void
      47               0 : setup_read_icc_profile (j_decompress_ptr cinfo)
      48                 : {
      49                 :   /* Tell the library to keep any APP2 data it may find */
      50               0 :   jpeg_save_markers(cinfo, ICC_MARKER, 0xFFFF);
      51               0 : }
      52                 : 
      53                 : 
      54                 : /*
      55                 :  * Handy subroutine to test whether a saved marker is an ICC profile marker.
      56                 :  */
      57                 : 
      58                 : static boolean
      59              10 : marker_is_icc (jpeg_saved_marker_ptr marker)
      60                 : {
      61              10 :   return
      62              10 :     marker->marker == ICC_MARKER &&
      63               0 :     marker->data_length >= ICC_OVERHEAD_LEN &&
      64                 :     /* verify the identifying string */
      65               0 :     GETJOCTET(marker->data[0]) == 0x49 &&
      66               0 :     GETJOCTET(marker->data[1]) == 0x43 &&
      67               0 :     GETJOCTET(marker->data[2]) == 0x43 &&
      68               0 :     GETJOCTET(marker->data[3]) == 0x5F &&
      69               0 :     GETJOCTET(marker->data[4]) == 0x50 &&
      70               0 :     GETJOCTET(marker->data[5]) == 0x52 &&
      71               0 :     GETJOCTET(marker->data[6]) == 0x4F &&
      72               0 :     GETJOCTET(marker->data[7]) == 0x46 &&
      73               0 :     GETJOCTET(marker->data[8]) == 0x49 &&
      74               0 :     GETJOCTET(marker->data[9]) == 0x4C &&
      75               0 :     GETJOCTET(marker->data[10]) == 0x45 &&
      76               0 :     GETJOCTET(marker->data[11]) == 0x0;
      77                 : }
      78                 : 
      79                 : 
      80                 : /*
      81                 :  * See if there was an ICC profile in the JPEG file being read;
      82                 :  * if so, reassemble and return the profile data.
      83                 :  *
      84                 :  * TRUE is returned if an ICC profile was found, FALSE if not.
      85                 :  * If TRUE is returned, *icc_data_ptr is set to point to the
      86                 :  * returned data, and *icc_data_len is set to its length.
      87                 :  *
      88                 :  * IMPORTANT: the data at **icc_data_ptr has been allocated with malloc()
      89                 :  * and must be freed by the caller with free() when the caller no longer
      90                 :  * needs it.  (Alternatively, we could write this routine to use the
      91                 :  * IJG library's memory allocator, so that the data would be freed implicitly
      92                 :  * at jpeg_finish_decompress() time.  But it seems likely that many apps
      93                 :  * will prefer to have the data stick around after decompression finishes.)
      94                 :  *
      95                 :  * NOTE: if the file contains invalid ICC APP2 markers, we just silently
      96                 :  * return FALSE.  You might want to issue an error message instead.
      97                 :  */
      98                 : 
      99                 : boolean
     100               5 : read_icc_profile (j_decompress_ptr cinfo,
     101                 :                   JOCTET **icc_data_ptr,
     102                 :                   unsigned int *icc_data_len)
     103                 : {
     104                 :   jpeg_saved_marker_ptr marker;
     105               5 :   int num_markers = 0;
     106                 :   int seq_no;
     107                 :   JOCTET *icc_data;
     108                 :   unsigned int total_length;
     109                 : #define MAX_SEQ_NO  255         /* sufficient since marker numbers are bytes */
     110                 :   char marker_present[MAX_SEQ_NO+1];      /* 1 if marker found */
     111                 :   unsigned int data_length[MAX_SEQ_NO+1]; /* size of profile data in marker */
     112                 :   unsigned int data_offset[MAX_SEQ_NO+1]; /* offset for data in marker */
     113                 : 
     114               5 :   *icc_data_ptr = NULL;         /* avoid confusion if FALSE return */
     115               5 :   *icc_data_len = 0;
     116                 : 
     117                 :   /* This first pass over the saved markers discovers whether there are
     118                 :    * any ICC markers and verifies the consistency of the marker numbering.
     119                 :    */
     120                 : 
     121            1280 :   for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
     122            1275 :     marker_present[seq_no] = 0;
     123                 : 
     124              15 :   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
     125              10 :     if (marker_is_icc(marker)) {
     126               0 :       if (num_markers == 0)
     127               0 :         num_markers = GETJOCTET(marker->data[13]);
     128               0 :       else if (num_markers != GETJOCTET(marker->data[13]))
     129               0 :         return FALSE;           /* inconsistent num_markers fields */
     130               0 :       seq_no = GETJOCTET(marker->data[12]);
     131               0 :       if (seq_no <= 0 || seq_no > num_markers)
     132               0 :         return FALSE;           /* bogus sequence number */
     133               0 :       if (marker_present[seq_no])
     134               0 :         return FALSE;           /* duplicate sequence numbers */
     135               0 :       marker_present[seq_no] = 1;
     136               0 :       data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
     137                 :     }
     138                 :   }
     139                 : 
     140               5 :   if (num_markers == 0)
     141               5 :     return FALSE;
     142                 : 
     143                 :   /* Check for missing markers, count total space needed,
     144                 :    * compute offset of each marker's part of the data.
     145                 :    */
     146                 : 
     147               0 :   total_length = 0;
     148               0 :   for (seq_no = 1; seq_no <= num_markers; seq_no++) {
     149               0 :     if (marker_present[seq_no] == 0)
     150               0 :       return FALSE;             /* missing sequence number */
     151               0 :     data_offset[seq_no] = total_length;
     152               0 :     total_length += data_length[seq_no];
     153                 :   }
     154                 : 
     155               0 :   if (total_length <= 0)
     156               0 :     return FALSE;               /* found only empty markers? */
     157                 : 
     158                 :   /* Allocate space for assembled data */
     159               0 :   icc_data = (JOCTET *) malloc(total_length * sizeof(JOCTET));
     160               0 :   if (icc_data == NULL)
     161               0 :     return FALSE;               /* oops, out of memory */
     162                 : 
     163                 :   /* and fill it in */
     164               0 :   for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
     165               0 :     if (marker_is_icc(marker)) {
     166                 :       JOCTET FAR *src_ptr;
     167                 :       JOCTET *dst_ptr;
     168                 :       unsigned int length;
     169               0 :       seq_no = GETJOCTET(marker->data[12]);
     170               0 :       dst_ptr = icc_data + data_offset[seq_no];
     171               0 :       src_ptr = marker->data + ICC_OVERHEAD_LEN;
     172               0 :       length = data_length[seq_no];
     173               0 :       while (length--) {
     174               0 :         *dst_ptr++ = *src_ptr++;
     175                 :       }
     176                 :     }
     177                 :   }
     178                 : 
     179               0 :   *icc_data_ptr = icc_data;
     180               0 :   *icc_data_len = total_length;
     181                 : 
     182               0 :   return TRUE;
     183                 : }

Generated by: LCOV version 1.7