shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * jdicc.c
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * Copyright (C) 1997-1998, Thomas G. Lane, Todd Newman.
shun-iwasawa 82a8f5
 * Copyright (C) 2017, D. R. Commander.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * file.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * This file provides code to read International Color Consortium (ICC) device
shun-iwasawa 82a8f5
 * profiles embedded in JFIF JPEG image files.  The ICC has defined a standard
shun-iwasawa 82a8f5
 * for including such data in JPEG "APP2" markers.  The code given here does
shun-iwasawa 82a8f5
 * not know anything about the internal structure of the ICC profile data; it
shun-iwasawa 82a8f5
 * just knows how to get the profile data from a JPEG file while reading it.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define JPEG_INTERNALS
shun-iwasawa 82a8f5
#include "jinclude.h"
shun-iwasawa 82a8f5
#include "jpeglib.h"
shun-iwasawa 82a8f5
#include "jerror.h"
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#ifndef HAVE_STDLIB_H           /* <stdlib.h> should declare malloc() */</stdlib.h>
shun-iwasawa 82a8f5
extern void *malloc(size_t size);
shun-iwasawa 82a8f5
#endif
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
#define ICC_MARKER  (JPEG_APP0 + 2)     /* JPEG marker code for ICC */
shun-iwasawa 82a8f5
#define ICC_OVERHEAD_LEN  14            /* size of non-profile data in APP2 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * Handy subroutine to test whether a saved marker is an ICC profile marker.
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
LOCAL(boolean)
shun-iwasawa 82a8f5
marker_is_icc(jpeg_saved_marker_ptr marker)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  return
shun-iwasawa 82a8f5
    marker->marker == ICC_MARKER &&
shun-iwasawa 82a8f5
    marker->data_length >= ICC_OVERHEAD_LEN &&
shun-iwasawa 82a8f5
    /* verify the identifying string */
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[0]) == 0x49 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[1]) == 0x43 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[2]) == 0x43 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[3]) == 0x5F &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[4]) == 0x50 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[5]) == 0x52 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[6]) == 0x4F &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[7]) == 0x46 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[8]) == 0x49 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[9]) == 0x4C &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[10]) == 0x45 &&
shun-iwasawa 82a8f5
    GETJOCTET(marker->data[11]) == 0x0;
shun-iwasawa 82a8f5
}
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
/*
shun-iwasawa 82a8f5
 * See if there was an ICC profile in the JPEG file being read; if so,
shun-iwasawa 82a8f5
 * reassemble and return the profile data.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * TRUE is returned if an ICC profile was found, FALSE if not.  If TRUE is
shun-iwasawa 82a8f5
 * returned, *icc_data_ptr is set to point to the returned data, and
shun-iwasawa 82a8f5
 * *icc_data_len is set to its length.
shun-iwasawa 82a8f5
 *
shun-iwasawa 82a8f5
 * IMPORTANT: the data at *icc_data_ptr is allocated with malloc() and must be
shun-iwasawa 82a8f5
 * freed by the caller with free() when the caller no longer needs it.
shun-iwasawa 82a8f5
 * (Alternatively, we could write this routine to use the IJG library's memory
shun-iwasawa 82a8f5
 * allocator, so that the data would be freed implicitly when
shun-iwasawa 82a8f5
 * jpeg_finish_decompress() is called.  But it seems likely that many
shun-iwasawa 82a8f5
 * applications will prefer to have the data stick around after decompression
shun-iwasawa 82a8f5
 * finishes.)
shun-iwasawa 82a8f5
 */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
GLOBAL(boolean)
shun-iwasawa 82a8f5
jpeg_read_icc_profile(j_decompress_ptr cinfo, JOCTET **icc_data_ptr,
shun-iwasawa 82a8f5
                      unsigned int *icc_data_len)
shun-iwasawa 82a8f5
{
shun-iwasawa 82a8f5
  jpeg_saved_marker_ptr marker;
shun-iwasawa 82a8f5
  int num_markers = 0;
shun-iwasawa 82a8f5
  int seq_no;
shun-iwasawa 82a8f5
  JOCTET *icc_data;
shun-iwasawa 82a8f5
  unsigned int total_length;
shun-iwasawa 82a8f5
#define MAX_SEQ_NO  255         /* sufficient since marker numbers are bytes */
shun-iwasawa 82a8f5
  char marker_present[MAX_SEQ_NO + 1];      /* 1 if marker found */
shun-iwasawa 82a8f5
  unsigned int data_length[MAX_SEQ_NO + 1]; /* size of profile data in marker */
shun-iwasawa 82a8f5
  unsigned int data_offset[MAX_SEQ_NO + 1]; /* offset for data in marker */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (icc_data_ptr == NULL || icc_data_len == NULL)
shun-iwasawa 82a8f5
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
shun-iwasawa 82a8f5
  if (cinfo->global_state < DSTATE_READY)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  *icc_data_ptr = NULL;         /* avoid confusion if FALSE return */
shun-iwasawa 82a8f5
  *icc_data_len = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* This first pass over the saved markers discovers whether there are
shun-iwasawa 82a8f5
   * any ICC markers and verifies the consistency of the marker numbering.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (seq_no = 1; seq_no <= MAX_SEQ_NO; seq_no++)
shun-iwasawa 82a8f5
    marker_present[seq_no] = 0;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
shun-iwasawa 82a8f5
    if (marker_is_icc(marker)) {
shun-iwasawa 82a8f5
      if (num_markers == 0)
shun-iwasawa 82a8f5
        num_markers = GETJOCTET(marker->data[13]);
shun-iwasawa 82a8f5
      else if (num_markers != GETJOCTET(marker->data[13])) {
shun-iwasawa 82a8f5
        WARNMS(cinfo, JWRN_BOGUS_ICC);  /* inconsistent num_markers fields */
shun-iwasawa 82a8f5
        return FALSE;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      seq_no = GETJOCTET(marker->data[12]);
shun-iwasawa 82a8f5
      if (seq_no <= 0 || seq_no > num_markers) {
shun-iwasawa 82a8f5
        WARNMS(cinfo, JWRN_BOGUS_ICC);  /* bogus sequence number */
shun-iwasawa 82a8f5
        return FALSE;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      if (marker_present[seq_no]) {
shun-iwasawa 82a8f5
        WARNMS(cinfo, JWRN_BOGUS_ICC);  /* duplicate sequence numbers */
shun-iwasawa 82a8f5
        return FALSE;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
      marker_present[seq_no] = 1;
shun-iwasawa 82a8f5
      data_length[seq_no] = marker->data_length - ICC_OVERHEAD_LEN;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (num_markers == 0)
shun-iwasawa 82a8f5
    return FALSE;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Check for missing markers, count total space needed,
shun-iwasawa 82a8f5
   * compute offset of each marker's part of the data.
shun-iwasawa 82a8f5
   */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  total_length = 0;
shun-iwasawa 82a8f5
  for (seq_no = 1; seq_no <= num_markers; seq_no++) {
shun-iwasawa 82a8f5
    if (marker_present[seq_no] == 0) {
shun-iwasawa 82a8f5
      WARNMS(cinfo, JWRN_BOGUS_ICC);  /* missing sequence number */
shun-iwasawa 82a8f5
      return FALSE;
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
    data_offset[seq_no] = total_length;
shun-iwasawa 82a8f5
    total_length += data_length[seq_no];
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  if (total_length == 0) {
shun-iwasawa 82a8f5
    WARNMS(cinfo, JWRN_BOGUS_ICC);  /* found only empty markers? */
shun-iwasawa 82a8f5
    return FALSE;
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* Allocate space for assembled data */
shun-iwasawa 82a8f5
  icc_data = (JOCTET *)malloc(total_length * sizeof(JOCTET));
shun-iwasawa 82a8f5
  if (icc_data == NULL)
shun-iwasawa 82a8f5
    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 11);  /* oops, out of memory */
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  /* and fill it in */
shun-iwasawa 82a8f5
  for (marker = cinfo->marker_list; marker != NULL; marker = marker->next) {
shun-iwasawa 82a8f5
    if (marker_is_icc(marker)) {
shun-iwasawa 82a8f5
      JOCTET FAR *src_ptr;
shun-iwasawa 82a8f5
      JOCTET *dst_ptr;
shun-iwasawa 82a8f5
      unsigned int length;
shun-iwasawa 82a8f5
      seq_no = GETJOCTET(marker->data[12]);
shun-iwasawa 82a8f5
      dst_ptr = icc_data + data_offset[seq_no];
shun-iwasawa 82a8f5
      src_ptr = marker->data + ICC_OVERHEAD_LEN;
shun-iwasawa 82a8f5
      length = data_length[seq_no];
shun-iwasawa 82a8f5
      while (length--) {
shun-iwasawa 82a8f5
        *dst_ptr++ = *src_ptr++;
shun-iwasawa 82a8f5
      }
shun-iwasawa 82a8f5
    }
shun-iwasawa 82a8f5
  }
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  *icc_data_ptr = icc_data;
shun-iwasawa 82a8f5
  *icc_data_len = total_length;
shun-iwasawa 82a8f5
shun-iwasawa 82a8f5
  return TRUE;
shun-iwasawa 82a8f5
}