]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/jpeg6/jdapimin.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jdapimin.cpp
index 9086b490163624f29ac6ae061223071517318806..36324ac24178877f59559cea02115ea35303c6ec 100644 (file)
-/*\r
-\r
- * jdapimin.c\r
-\r
- *\r
-\r
- * Copyright (C) 1994-1995, Thomas G. Lane.\r
-\r
- * This file is part of the Independent JPEG Group's software.\r
-\r
- * For conditions of distribution and use, see the accompanying README file.\r
-\r
- *\r
-\r
- * This file contains application interface code for the decompression half\r
-\r
- * of the JPEG library.  These are the "minimum" API routines that may be\r
-\r
- * needed in either the normal full-decompression case or the\r
-\r
- * transcoding-only case.\r
-\r
- *\r
-\r
- * Most of the routines intended to be called directly by an application\r
-\r
- * are in this file or in jdapistd.c.  But also see jcomapi.c for routines\r
-\r
- * shared by compression and decompression, and jdtrans.c for the transcoding\r
-\r
- * case.\r
-\r
- */\r
-\r
-\r
-\r
-#define JPEG_INTERNALS\r
-\r
-#include "jinclude.h"\r
-\r
-#include "radiant_jpeglib.h"\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Initialization of a JPEG decompression object.\r
-\r
- * The error manager must already be set up (in case memory manager fails).\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL void\r
-\r
-jpeg_create_decompress (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  int i;\r
-\r
-\r
-\r
-  /* For debugging purposes, zero the whole master structure.\r
-\r
-   * But error manager pointer is already there, so save and restore it.\r
-\r
-   */\r
-\r
-  {\r
-\r
-    struct jpeg_error_mgr * err = cinfo->err;\r
-\r
-    i = sizeof(struct jpeg_decompress_struct);\r
-\r
-    i = SIZEOF(struct jpeg_decompress_struct);\r
-\r
-    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));\r
-\r
-    cinfo->err = err;\r
-\r
-  }\r
-\r
-  cinfo->is_decompressor = TRUE;\r
-\r
-\r
-\r
-  /* Initialize a memory manager instance for this object */\r
-\r
-  jinit_memory_mgr((j_common_ptr) cinfo);\r
-\r
-\r
-\r
-  /* Zero out pointers to permanent structures. */\r
-\r
-  cinfo->progress = NULL;\r
-\r
-  cinfo->src = NULL;\r
-\r
-\r
-\r
-  for (i = 0; i < NUM_QUANT_TBLS; i++)\r
-\r
-    cinfo->quant_tbl_ptrs[i] = NULL;\r
-\r
-\r
-\r
-  for (i = 0; i < NUM_HUFF_TBLS; i++) {\r
-\r
-    cinfo->dc_huff_tbl_ptrs[i] = NULL;\r
-\r
-    cinfo->ac_huff_tbl_ptrs[i] = NULL;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Initialize marker processor so application can override methods\r
-\r
-   * for COM, APPn markers before calling jpeg_read_header.\r
-\r
-   */\r
-\r
-  jinit_marker_reader(cinfo);\r
-\r
-\r
-\r
-  /* And initialize the overall input controller. */\r
-\r
-  jinit_input_controller(cinfo);\r
-\r
-\r
-\r
-  /* OK, I'm ready */\r
-\r
-  cinfo->global_state = DSTATE_START;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Destruction of a JPEG decompression object\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL void\r
-\r
-jpeg_destroy_decompress (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Abort processing of a JPEG decompression operation,\r
-\r
- * but don't destroy the object itself.\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL void\r
-\r
-jpeg_abort_decompress (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  jpeg_abort((j_common_ptr) cinfo); /* use common routine */\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Install a special processing method for COM or APPn markers.\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL void\r
-\r
-jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,\r
-\r
-                          jpeg_marker_parser_method routine)\r
-\r
-{\r
-\r
-  if (marker_code == JPEG_COM)\r
-\r
-    cinfo->marker->process_COM = routine;\r
-\r
-  else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)\r
-\r
-    cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;\r
-\r
-  else\r
-\r
-    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Set default decompression parameters.\r
-\r
- */\r
-\r
-\r
-\r
-LOCAL void\r
-\r
-default_decompress_parms (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  /* Guess the input colorspace, and set output colorspace accordingly. */\r
-\r
-  /* (Wish JPEG committee had provided a real way to specify this...) */\r
-\r
-  /* Note application may override our guesses. */\r
-\r
-  switch (cinfo->num_components) {\r
-\r
-  case 1:\r
-\r
-    cinfo->jpeg_color_space = JCS_GRAYSCALE;\r
-\r
-    cinfo->out_color_space = JCS_GRAYSCALE;\r
-\r
-    break;\r
-\r
-    \r
-\r
-  case 3:\r
-\r
-    if (cinfo->saw_JFIF_marker) {\r
-\r
-      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */\r
-\r
-    } else if (cinfo->saw_Adobe_marker) {\r
-\r
-      switch (cinfo->Adobe_transform) {\r
-\r
-      case 0:\r
-\r
-       cinfo->jpeg_color_space = JCS_RGB;\r
-\r
-       break;\r
-\r
-      case 1:\r
-\r
-       cinfo->jpeg_color_space = JCS_YCbCr;\r
-\r
-       break;\r
-\r
-      default:\r
-\r
-       WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);\r
-\r
-       cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */\r
-\r
-       break;\r
-\r
-      }\r
-\r
-    } else {\r
-\r
-      /* Saw no special markers, try to guess from the component IDs */\r
-\r
-      int cid0 = cinfo->comp_info[0].component_id;\r
-\r
-      int cid1 = cinfo->comp_info[1].component_id;\r
-\r
-      int cid2 = cinfo->comp_info[2].component_id;\r
-\r
-\r
-\r
-      if (cid0 == 1 && cid1 == 2 && cid2 == 3)\r
-\r
-       cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */\r
-\r
-      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)\r
-\r
-       cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */\r
-\r
-      else {\r
-\r
-       TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);\r
-\r
-       cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */\r
-\r
-      }\r
-\r
-    }\r
-\r
-    /* Always guess RGB is proper output colorspace. */\r
-\r
-    cinfo->out_color_space = JCS_RGB;\r
-\r
-    break;\r
-\r
-    \r
-\r
-  case 4:\r
-\r
-    if (cinfo->saw_Adobe_marker) {\r
-\r
-      switch (cinfo->Adobe_transform) {\r
-\r
-      case 0:\r
-\r
-       cinfo->jpeg_color_space = JCS_CMYK;\r
-\r
-       break;\r
-\r
-      case 2:\r
-\r
-       cinfo->jpeg_color_space = JCS_YCCK;\r
-\r
-       break;\r
-\r
-      default:\r
-\r
-       WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);\r
-\r
-       cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */\r
-\r
-       break;\r
-\r
-      }\r
-\r
-    } else {\r
-\r
-      /* No special markers, assume straight CMYK. */\r
-\r
-      cinfo->jpeg_color_space = JCS_CMYK;\r
-\r
-    }\r
-\r
-    cinfo->out_color_space = JCS_CMYK;\r
-\r
-    break;\r
-\r
-    \r
-\r
-  default:\r
-\r
-    cinfo->jpeg_color_space = JCS_UNKNOWN;\r
-\r
-    cinfo->out_color_space = JCS_UNKNOWN;\r
-\r
-    break;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Set defaults for other decompression parameters. */\r
-\r
-  cinfo->scale_num = 1;                /* 1:1 scaling */\r
-\r
-  cinfo->scale_denom = 1;\r
-\r
-  cinfo->output_gamma = 1.0;\r
-\r
-  cinfo->buffered_image = FALSE;\r
-\r
-  cinfo->raw_data_out = FALSE;\r
-\r
-  cinfo->dct_method = JDCT_DEFAULT;\r
-\r
-  cinfo->do_fancy_upsampling = TRUE;\r
-\r
-  cinfo->do_block_smoothing = TRUE;\r
-\r
-  cinfo->quantize_colors = FALSE;\r
-\r
-  /* We set these in case application only sets quantize_colors. */\r
-\r
-  cinfo->dither_mode = JDITHER_FS;\r
-\r
-#ifdef QUANT_2PASS_SUPPORTED\r
-\r
-  cinfo->two_pass_quantize = TRUE;\r
-\r
-#else\r
-\r
-  cinfo->two_pass_quantize = FALSE;\r
-\r
-#endif\r
-\r
-  cinfo->desired_number_of_colors = 256;\r
-\r
-  cinfo->colormap = NULL;\r
-\r
-  /* Initialize for no mode change in buffered-image mode. */\r
-\r
-  cinfo->enable_1pass_quant = FALSE;\r
-\r
-  cinfo->enable_external_quant = FALSE;\r
-\r
-  cinfo->enable_2pass_quant = FALSE;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Decompression startup: read start of JPEG datastream to see what's there.\r
-\r
- * Need only initialize JPEG object and supply a data source before calling.\r
-\r
- *\r
-\r
- * This routine will read as far as the first SOS marker (ie, actual start of\r
-\r
- * compressed data), and will save all tables and parameters in the JPEG\r
-\r
- * object.  It will also initialize the decompression parameters to default\r
-\r
- * values, and finally return JPEG_HEADER_OK.  On return, the application may\r
-\r
- * adjust the decompression parameters and then call jpeg_start_decompress.\r
-\r
- * (Or, if the application only wanted to determine the image parameters,\r
-\r
- * the data need not be decompressed.  In that case, call jpeg_abort or\r
-\r
- * jpeg_destroy to release any temporary space.)\r
-\r
- * If an abbreviated (tables only) datastream is presented, the routine will\r
-\r
- * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then\r
-\r
- * re-use the JPEG object to read the abbreviated image datastream(s).\r
-\r
- * It is unnecessary (but OK) to call jpeg_abort in this case.\r
-\r
- * The JPEG_SUSPENDED return code only occurs if the data source module\r
-\r
- * requests suspension of the decompressor.  In this case the application\r
-\r
- * should load more source data and then re-call jpeg_read_header to resume\r
-\r
- * processing.\r
-\r
- * If a non-suspending data source is used and require_image is TRUE, then the\r
-\r
- * return code need not be inspected since only JPEG_HEADER_OK is possible.\r
-\r
- *\r
-\r
- * This routine is now just a front end to jpeg_consume_input, with some\r
-\r
- * extra error checking.\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL int\r
-\r
-jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)\r
-\r
-{\r
-\r
-  int retcode;\r
-\r
-\r
-\r
-  if (cinfo->global_state != DSTATE_START &&\r
-\r
-      cinfo->global_state != DSTATE_INHEADER)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
-\r
-\r
-\r
-  retcode = jpeg_consume_input(cinfo);\r
-\r
-\r
-\r
-  switch (retcode) {\r
-\r
-  case JPEG_REACHED_SOS:\r
-\r
-    retcode = JPEG_HEADER_OK;\r
-\r
-    break;\r
-\r
-  case JPEG_REACHED_EOI:\r
-\r
-    if (require_image)         /* Complain if application wanted an image */\r
-\r
-      ERREXIT(cinfo, JERR_NO_IMAGE);\r
-\r
-    /* Reset to start state; it would be safer to require the application to\r
-\r
-     * call jpeg_abort, but we can't change it now for compatibility reasons.\r
-\r
-     * A side effect is to free any temporary memory (there shouldn't be any).\r
-\r
-     */\r
-\r
-    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */\r
-\r
-    retcode = JPEG_HEADER_TABLES_ONLY;\r
-\r
-    break;\r
-\r
-  case JPEG_SUSPENDED:\r
-\r
-    /* no work */\r
-\r
-    break;\r
-\r
-  }\r
-\r
-\r
-\r
-  return retcode;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Consume data in advance of what the decompressor requires.\r
-\r
- * This can be called at any time once the decompressor object has\r
-\r
- * been created and a data source has been set up.\r
-\r
- *\r
-\r
- * This routine is essentially a state machine that handles a couple\r
-\r
- * of critical state-transition actions, namely initial setup and\r
-\r
- * transition from header scanning to ready-for-start_decompress.\r
-\r
- * All the actual input is done via the input controller's consume_input\r
-\r
- * method.\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL int\r
-\r
-jpeg_consume_input (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  int retcode = JPEG_SUSPENDED;\r
-\r
-\r
-\r
-  /* NB: every possible DSTATE value should be listed in this switch */\r
-\r
-  switch (cinfo->global_state) {\r
-\r
-  case DSTATE_START:\r
-\r
-    /* Start-of-datastream actions: reset appropriate modules */\r
-\r
-    (*cinfo->inputctl->reset_input_controller) (cinfo);\r
-\r
-    /* Initialize application's data source module */\r
-\r
-    (*cinfo->src->init_source) (cinfo);\r
-\r
-    cinfo->global_state = DSTATE_INHEADER;\r
-\r
-    /*FALLTHROUGH*/\r
-\r
-  case DSTATE_INHEADER:\r
-\r
-    retcode = (*cinfo->inputctl->consume_input) (cinfo);\r
-\r
-    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */\r
-\r
-      /* Set up default parameters based on header data */\r
-\r
-      default_decompress_parms(cinfo);\r
-\r
-      /* Set global state: ready for start_decompress */\r
-\r
-      cinfo->global_state = DSTATE_READY;\r
-\r
-    }\r
-\r
-    break;\r
-\r
-  case DSTATE_READY:\r
-\r
-    /* Can't advance past first SOS until start_decompress is called */\r
-\r
-    retcode = JPEG_REACHED_SOS;\r
-\r
-    break;\r
-\r
-  case DSTATE_PRELOAD:\r
-\r
-  case DSTATE_PRESCAN:\r
-\r
-  case DSTATE_SCANNING:\r
-\r
-  case DSTATE_RAW_OK:\r
-\r
-  case DSTATE_BUFIMAGE:\r
-\r
-  case DSTATE_BUFPOST:\r
-\r
-  case DSTATE_STOPPING:\r
-\r
-    retcode = (*cinfo->inputctl->consume_input) (cinfo);\r
-\r
-    break;\r
-\r
-  default:\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
-\r
-  }\r
-\r
-  return retcode;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Have we finished reading the input file?\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL boolean\r
-\r
-jpeg_input_complete (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  /* Check for valid jpeg object */\r
-\r
-  if (cinfo->global_state < DSTATE_START ||\r
-\r
-      cinfo->global_state > DSTATE_STOPPING)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
-\r
-  return cinfo->inputctl->eoi_reached;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Is there more than one scan?\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL boolean\r
-\r
-jpeg_has_multiple_scans (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  /* Only valid after jpeg_read_header completes */\r
-\r
-  if (cinfo->global_state < DSTATE_READY ||\r
-\r
-      cinfo->global_state > DSTATE_STOPPING)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
-\r
-  return cinfo->inputctl->has_multiple_scans;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Finish JPEG decompression.\r
-\r
- *\r
-\r
- * This will normally just verify the file trailer and release temp storage.\r
-\r
- *\r
-\r
- * Returns FALSE if suspended.  The return value need be inspected only if\r
-\r
- * a suspending data source is used.\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL boolean\r
-\r
-jpeg_finish_decompress (j_decompress_ptr cinfo)\r
-\r
-{\r
-\r
-  if ((cinfo->global_state == DSTATE_SCANNING ||\r
-\r
-       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {\r
-\r
-    /* Terminate final pass of non-buffered mode */\r
-\r
-    if (cinfo->output_scanline < cinfo->output_height)\r
-\r
-      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);\r
-\r
-    (*cinfo->master->finish_output_pass) (cinfo);\r
-\r
-    cinfo->global_state = DSTATE_STOPPING;\r
-\r
-  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {\r
-\r
-    /* Finishing after a buffered-image operation */\r
-\r
-    cinfo->global_state = DSTATE_STOPPING;\r
-\r
-  } else if (cinfo->global_state != DSTATE_STOPPING) {\r
-\r
-    /* STOPPING = repeat call after a suspension, anything else is error */\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);\r
-\r
-  }\r
-\r
-  /* Read until EOI */\r
-\r
-  while (! cinfo->inputctl->eoi_reached) {\r
-\r
-    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)\r
-\r
-      return FALSE;            /* Suspend, come back later */\r
-\r
-  }\r
-\r
-  /* Do final cleanup */\r
-\r
-  (*cinfo->src->term_source) (cinfo);\r
-\r
-  /* We can use jpeg_abort to release memory and reset global_state */\r
-\r
-  jpeg_abort((j_common_ptr) cinfo);\r
-\r
-  return TRUE;\r
-\r
-}\r
-\r
+/*
+
+ * jdapimin.c
+
+ *
+
+ * Copyright (C) 1994-1995, Thomas G. Lane.
+
+ * This file is part of the Independent JPEG Group's software.
+
+ * For conditions of distribution and use, see the accompanying README file.
+
+ *
+
+ * This file contains application interface code for the decompression half
+
+ * of the JPEG library.  These are the "minimum" API routines that may be
+
+ * needed in either the normal full-decompression case or the
+
+ * transcoding-only case.
+
+ *
+
+ * Most of the routines intended to be called directly by an application
+
+ * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
+
+ * shared by compression and decompression, and jdtrans.c for the transcoding
+
+ * case.
+
+ */
+
+
+
+#define JPEG_INTERNALS
+
+#include "jinclude.h"
+
+#include "radiant_jpeglib.h"
+
+
+
+
+
+/*
+
+ * Initialization of a JPEG decompression object.
+
+ * The error manager must already be set up (in case memory manager fails).
+
+ */
+
+
+
+GLOBAL void
+
+jpeg_create_decompress (j_decompress_ptr cinfo)
+
+{
+
+  int i;
+
+
+
+  /* For debugging purposes, zero the whole master structure.
+
+   * But error manager pointer is already there, so save and restore it.
+
+   */
+
+  {
+
+    struct jpeg_error_mgr * err = cinfo->err;
+
+    i = sizeof(struct jpeg_decompress_struct);
+
+    i = SIZEOF(struct jpeg_decompress_struct);
+
+    MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
+
+    cinfo->err = err;
+
+  }
+
+  cinfo->is_decompressor = TRUE;
+
+
+
+  /* Initialize a memory manager instance for this object */
+
+  jinit_memory_mgr((j_common_ptr) cinfo);
+
+
+
+  /* Zero out pointers to permanent structures. */
+
+  cinfo->progress = NULL;
+
+  cinfo->src = NULL;
+
+
+
+  for (i = 0; i < NUM_QUANT_TBLS; i++)
+
+    cinfo->quant_tbl_ptrs[i] = NULL;
+
+
+
+  for (i = 0; i < NUM_HUFF_TBLS; i++) {
+
+    cinfo->dc_huff_tbl_ptrs[i] = NULL;
+
+    cinfo->ac_huff_tbl_ptrs[i] = NULL;
+
+  }
+
+
+
+  /* Initialize marker processor so application can override methods
+
+   * for COM, APPn markers before calling jpeg_read_header.
+
+   */
+
+  jinit_marker_reader(cinfo);
+
+
+
+  /* And initialize the overall input controller. */
+
+  jinit_input_controller(cinfo);
+
+
+
+  /* OK, I'm ready */
+
+  cinfo->global_state = DSTATE_START;
+
+}
+
+
+
+
+
+/*
+
+ * Destruction of a JPEG decompression object
+
+ */
+
+
+
+GLOBAL void
+
+jpeg_destroy_decompress (j_decompress_ptr cinfo)
+
+{
+
+  jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
+
+}
+
+
+
+
+
+/*
+
+ * Abort processing of a JPEG decompression operation,
+
+ * but don't destroy the object itself.
+
+ */
+
+
+
+GLOBAL void
+
+jpeg_abort_decompress (j_decompress_ptr cinfo)
+
+{
+
+  jpeg_abort((j_common_ptr) cinfo); /* use common routine */
+
+}
+
+
+
+
+
+/*
+
+ * Install a special processing method for COM or APPn markers.
+
+ */
+
+
+
+GLOBAL void
+
+jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
+
+                          jpeg_marker_parser_method routine)
+
+{
+
+  if (marker_code == JPEG_COM)
+
+    cinfo->marker->process_COM = routine;
+
+  else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
+
+    cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
+
+  else
+
+    ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
+
+}
+
+
+
+
+
+/*
+
+ * Set default decompression parameters.
+
+ */
+
+
+
+LOCAL void
+
+default_decompress_parms (j_decompress_ptr cinfo)
+
+{
+
+  /* Guess the input colorspace, and set output colorspace accordingly. */
+
+  /* (Wish JPEG committee had provided a real way to specify this...) */
+
+  /* Note application may override our guesses. */
+
+  switch (cinfo->num_components) {
+
+  case 1:
+
+    cinfo->jpeg_color_space = JCS_GRAYSCALE;
+
+    cinfo->out_color_space = JCS_GRAYSCALE;
+
+    break;
+
+    
+
+  case 3:
+
+    if (cinfo->saw_JFIF_marker) {
+
+      cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
+
+    } else if (cinfo->saw_Adobe_marker) {
+
+      switch (cinfo->Adobe_transform) {
+
+      case 0:
+
+       cinfo->jpeg_color_space = JCS_RGB;
+
+       break;
+
+      case 1:
+
+       cinfo->jpeg_color_space = JCS_YCbCr;
+
+       break;
+
+      default:
+
+       WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+
+       cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+
+       break;
+
+      }
+
+    } else {
+
+      /* Saw no special markers, try to guess from the component IDs */
+
+      int cid0 = cinfo->comp_info[0].component_id;
+
+      int cid1 = cinfo->comp_info[1].component_id;
+
+      int cid2 = cinfo->comp_info[2].component_id;
+
+
+
+      if (cid0 == 1 && cid1 == 2 && cid2 == 3)
+
+       cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
+
+      else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
+
+       cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
+
+      else {
+
+       TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
+
+       cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
+
+      }
+
+    }
+
+    /* Always guess RGB is proper output colorspace. */
+
+    cinfo->out_color_space = JCS_RGB;
+
+    break;
+
+    
+
+  case 4:
+
+    if (cinfo->saw_Adobe_marker) {
+
+      switch (cinfo->Adobe_transform) {
+
+      case 0:
+
+       cinfo->jpeg_color_space = JCS_CMYK;
+
+       break;
+
+      case 2:
+
+       cinfo->jpeg_color_space = JCS_YCCK;
+
+       break;
+
+      default:
+
+       WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
+
+       cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
+
+       break;
+
+      }
+
+    } else {
+
+      /* No special markers, assume straight CMYK. */
+
+      cinfo->jpeg_color_space = JCS_CMYK;
+
+    }
+
+    cinfo->out_color_space = JCS_CMYK;
+
+    break;
+
+    
+
+  default:
+
+    cinfo->jpeg_color_space = JCS_UNKNOWN;
+
+    cinfo->out_color_space = JCS_UNKNOWN;
+
+    break;
+
+  }
+
+
+
+  /* Set defaults for other decompression parameters. */
+
+  cinfo->scale_num = 1;                /* 1:1 scaling */
+
+  cinfo->scale_denom = 1;
+
+  cinfo->output_gamma = 1.0;
+
+  cinfo->buffered_image = FALSE;
+
+  cinfo->raw_data_out = FALSE;
+
+  cinfo->dct_method = JDCT_DEFAULT;
+
+  cinfo->do_fancy_upsampling = TRUE;
+
+  cinfo->do_block_smoothing = TRUE;
+
+  cinfo->quantize_colors = FALSE;
+
+  /* We set these in case application only sets quantize_colors. */
+
+  cinfo->dither_mode = JDITHER_FS;
+
+#ifdef QUANT_2PASS_SUPPORTED
+
+  cinfo->two_pass_quantize = TRUE;
+
+#else
+
+  cinfo->two_pass_quantize = FALSE;
+
+#endif
+
+  cinfo->desired_number_of_colors = 256;
+
+  cinfo->colormap = NULL;
+
+  /* Initialize for no mode change in buffered-image mode. */
+
+  cinfo->enable_1pass_quant = FALSE;
+
+  cinfo->enable_external_quant = FALSE;
+
+  cinfo->enable_2pass_quant = FALSE;
+
+}
+
+
+
+
+
+/*
+
+ * Decompression startup: read start of JPEG datastream to see what's there.
+
+ * Need only initialize JPEG object and supply a data source before calling.
+
+ *
+
+ * This routine will read as far as the first SOS marker (ie, actual start of
+
+ * compressed data), and will save all tables and parameters in the JPEG
+
+ * object.  It will also initialize the decompression parameters to default
+
+ * values, and finally return JPEG_HEADER_OK.  On return, the application may
+
+ * adjust the decompression parameters and then call jpeg_start_decompress.
+
+ * (Or, if the application only wanted to determine the image parameters,
+
+ * the data need not be decompressed.  In that case, call jpeg_abort or
+
+ * jpeg_destroy to release any temporary space.)
+
+ * If an abbreviated (tables only) datastream is presented, the routine will
+
+ * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
+
+ * re-use the JPEG object to read the abbreviated image datastream(s).
+
+ * It is unnecessary (but OK) to call jpeg_abort in this case.
+
+ * The JPEG_SUSPENDED return code only occurs if the data source module
+
+ * requests suspension of the decompressor.  In this case the application
+
+ * should load more source data and then re-call jpeg_read_header to resume
+
+ * processing.
+
+ * If a non-suspending data source is used and require_image is TRUE, then the
+
+ * return code need not be inspected since only JPEG_HEADER_OK is possible.
+
+ *
+
+ * This routine is now just a front end to jpeg_consume_input, with some
+
+ * extra error checking.
+
+ */
+
+
+
+GLOBAL int
+
+jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
+
+{
+
+  int retcode;
+
+
+
+  if (cinfo->global_state != DSTATE_START &&
+
+      cinfo->global_state != DSTATE_INHEADER)
+
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+
+
+  retcode = jpeg_consume_input(cinfo);
+
+
+
+  switch (retcode) {
+
+  case JPEG_REACHED_SOS:
+
+    retcode = JPEG_HEADER_OK;
+
+    break;
+
+  case JPEG_REACHED_EOI:
+
+    if (require_image)         /* Complain if application wanted an image */
+
+      ERREXIT(cinfo, JERR_NO_IMAGE);
+
+    /* Reset to start state; it would be safer to require the application to
+
+     * call jpeg_abort, but we can't change it now for compatibility reasons.
+
+     * A side effect is to free any temporary memory (there shouldn't be any).
+
+     */
+
+    jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
+
+    retcode = JPEG_HEADER_TABLES_ONLY;
+
+    break;
+
+  case JPEG_SUSPENDED:
+
+    /* no work */
+
+    break;
+
+  }
+
+
+
+  return retcode;
+
+}
+
+
+
+
+
+/*
+
+ * Consume data in advance of what the decompressor requires.
+
+ * This can be called at any time once the decompressor object has
+
+ * been created and a data source has been set up.
+
+ *
+
+ * This routine is essentially a state machine that handles a couple
+
+ * of critical state-transition actions, namely initial setup and
+
+ * transition from header scanning to ready-for-start_decompress.
+
+ * All the actual input is done via the input controller's consume_input
+
+ * method.
+
+ */
+
+
+
+GLOBAL int
+
+jpeg_consume_input (j_decompress_ptr cinfo)
+
+{
+
+  int retcode = JPEG_SUSPENDED;
+
+
+
+  /* NB: every possible DSTATE value should be listed in this switch */
+
+  switch (cinfo->global_state) {
+
+  case DSTATE_START:
+
+    /* Start-of-datastream actions: reset appropriate modules */
+
+    (*cinfo->inputctl->reset_input_controller) (cinfo);
+
+    /* Initialize application's data source module */
+
+    (*cinfo->src->init_source) (cinfo);
+
+    cinfo->global_state = DSTATE_INHEADER;
+
+    /*FALLTHROUGH*/
+
+  case DSTATE_INHEADER:
+
+    retcode = (*cinfo->inputctl->consume_input) (cinfo);
+
+    if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
+
+      /* Set up default parameters based on header data */
+
+      default_decompress_parms(cinfo);
+
+      /* Set global state: ready for start_decompress */
+
+      cinfo->global_state = DSTATE_READY;
+
+    }
+
+    break;
+
+  case DSTATE_READY:
+
+    /* Can't advance past first SOS until start_decompress is called */
+
+    retcode = JPEG_REACHED_SOS;
+
+    break;
+
+  case DSTATE_PRELOAD:
+
+  case DSTATE_PRESCAN:
+
+  case DSTATE_SCANNING:
+
+  case DSTATE_RAW_OK:
+
+  case DSTATE_BUFIMAGE:
+
+  case DSTATE_BUFPOST:
+
+  case DSTATE_STOPPING:
+
+    retcode = (*cinfo->inputctl->consume_input) (cinfo);
+
+    break;
+
+  default:
+
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  }
+
+  return retcode;
+
+}
+
+
+
+
+
+/*
+
+ * Have we finished reading the input file?
+
+ */
+
+
+
+GLOBAL boolean
+
+jpeg_input_complete (j_decompress_ptr cinfo)
+
+{
+
+  /* Check for valid jpeg object */
+
+  if (cinfo->global_state < DSTATE_START ||
+
+      cinfo->global_state > DSTATE_STOPPING)
+
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  return cinfo->inputctl->eoi_reached;
+
+}
+
+
+
+
+
+/*
+
+ * Is there more than one scan?
+
+ */
+
+
+
+GLOBAL boolean
+
+jpeg_has_multiple_scans (j_decompress_ptr cinfo)
+
+{
+
+  /* Only valid after jpeg_read_header completes */
+
+  if (cinfo->global_state < DSTATE_READY ||
+
+      cinfo->global_state > DSTATE_STOPPING)
+
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  return cinfo->inputctl->has_multiple_scans;
+
+}
+
+
+
+
+
+/*
+
+ * Finish JPEG decompression.
+
+ *
+
+ * This will normally just verify the file trailer and release temp storage.
+
+ *
+
+ * Returns FALSE if suspended.  The return value need be inspected only if
+
+ * a suspending data source is used.
+
+ */
+
+
+
+GLOBAL boolean
+
+jpeg_finish_decompress (j_decompress_ptr cinfo)
+
+{
+
+  if ((cinfo->global_state == DSTATE_SCANNING ||
+
+       cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
+
+    /* Terminate final pass of non-buffered mode */
+
+    if (cinfo->output_scanline < cinfo->output_height)
+
+      ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
+
+    (*cinfo->master->finish_output_pass) (cinfo);
+
+    cinfo->global_state = DSTATE_STOPPING;
+
+  } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
+
+    /* Finishing after a buffered-image operation */
+
+    cinfo->global_state = DSTATE_STOPPING;
+
+  } else if (cinfo->global_state != DSTATE_STOPPING) {
+
+    /* STOPPING = repeat call after a suspension, anything else is error */
+
+    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
+
+  }
+
+  /* Read until EOI */
+
+  while (! cinfo->inputctl->eoi_reached) {
+
+    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
+
+      return FALSE;            /* Suspend, come back later */
+
+  }
+
+  /* Do final cleanup */
+
+  (*cinfo->src->term_source) (cinfo);
+
+  /* We can use jpeg_abort to release memory and reset global_state */
+
+  jpeg_abort((j_common_ptr) cinfo);
+
+  return TRUE;
+
+}
+