home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jcmarker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  17.3 KB  |  643 lines

  1. /*
  2.  * jcmarker.c
  3.  *
  4.  * Copyright (C) 1991-1995, Thomas G. Lane.
  5.  * This file is part of the Independent JPEG Group's software.
  6.  * For conditions of distribution and use, see the accompanying README file.
  7.  *
  8.  * This file contains routines to write JPEG datastream markers.
  9.  */
  10.  
  11. #define JPEG_INTERNALS
  12. #include "jinclude.h"
  13.  
  14. typedef enum {            /* JPEG marker codes */
  15.   M_SOF0  = 0xc0,
  16.   M_SOF1  = 0xc1,
  17.   M_SOF2  = 0xc2,
  18.   M_SOF3  = 0xc3,
  19.   
  20.   M_SOF5  = 0xc5,
  21.   M_SOF6  = 0xc6,
  22.   M_SOF7  = 0xc7,
  23.   
  24.   M_JPG   = 0xc8,
  25.   M_SOF9  = 0xc9,
  26.   M_SOF10 = 0xca,
  27.   M_SOF11 = 0xcb,
  28.   
  29.   M_SOF13 = 0xcd,
  30.   M_SOF14 = 0xce,
  31.   M_SOF15 = 0xcf,
  32.   
  33.   M_DHT   = 0xc4,
  34.   
  35.   M_DAC   = 0xcc,
  36.   
  37.   M_RST0  = 0xd0,
  38.   M_RST1  = 0xd1,
  39.   M_RST2  = 0xd2,
  40.   M_RST3  = 0xd3,
  41.   M_RST4  = 0xd4,
  42.   M_RST5  = 0xd5,
  43.   M_RST6  = 0xd6,
  44.   M_RST7  = 0xd7,
  45.   
  46.   M_SOI   = 0xd8,
  47.   M_EOI   = 0xd9,
  48.   M_SOS   = 0xda,
  49.   M_DQT   = 0xdb,
  50.   M_DNL   = 0xdc,
  51.   M_DRI   = 0xdd,
  52.   M_DHP   = 0xde,
  53.   M_EXP   = 0xdf,
  54.   
  55.   M_APP0  = 0xe0,
  56.   M_APP1  = 0xe1,
  57.   M_APP2  = 0xe2,
  58.   M_APP3  = 0xe3,
  59.   M_APP4  = 0xe4,
  60.   M_APP5  = 0xe5,
  61.   M_APP6  = 0xe6,
  62.   M_APP7  = 0xe7,
  63.   M_APP8  = 0xe8,
  64.   M_APP9  = 0xe9,
  65.   M_APP10 = 0xea,
  66.   M_APP11 = 0xeb,
  67.   M_APP12 = 0xec,
  68.   M_APP13 = 0xed,
  69.   M_APP14 = 0xee,
  70.   M_APP15 = 0xef,
  71.   
  72.   M_JPG0  = 0xf0,
  73.   M_JPG13 = 0xfd,
  74.   M_COM   = 0xfe,
  75.   
  76.   M_TEM   = 0x01,
  77.   
  78.   M_ERROR = 0x100
  79. } JPEG_MARKER;
  80.  
  81. #include "xp_core.h" /* defines of int32 ect */
  82. #include "jpeglib.h" /* Move these after JPEG_MARKER to avoid name
  83.                       * clash with Sun header, /usr/include/sys/stream.h,
  84.                       * which defines M_ERROR.
  85.                       */
  86.  
  87. /*
  88.  * Basic output routines.
  89.  *
  90.  * Note that we do not support suspension while writing a marker.
  91.  * Therefore, an application using suspension must ensure that there is
  92.  * enough buffer space for the initial markers (typ. 600-700 bytes) before
  93.  * calling jpeg_start_compress, and enough space to write the trailing EOI
  94.  * (a few bytes) before calling jpeg_finish_compress.  Multipass compression
  95.  * modes are not supported at all with suspension, so those two are the only
  96.  * points where markers will be written.
  97.  */
  98.  
  99. LOCAL void
  100. emit_byte (j_compress_ptr cinfo, int16 val)
  101. /* Emit a byte */
  102. {
  103.   struct jpeg_destination_mgr * dest = cinfo->dest;
  104.  
  105.   *(dest->next_output_byte)++ = (JOCTET) val;
  106.   if (--dest->free_in_buffer == 0) {
  107.     if (! (*dest->empty_output_buffer) (cinfo))
  108.       ERREXIT(cinfo, JERR_CANT_SUSPEND);
  109.   }
  110. }
  111.  
  112.  
  113. LOCAL void
  114. emit_marker (j_compress_ptr cinfo, JPEG_MARKER mark)
  115. /* Emit a marker code */
  116. {
  117.   emit_byte(cinfo, 0xFF);
  118.   emit_byte(cinfo, (int16) mark);
  119. }
  120.  
  121.  
  122. LOCAL void
  123. emit_2bytes (j_compress_ptr cinfo, int16 value)
  124. /* Emit a 2-byte integer; these are always MSB first in JPEG files */
  125. {
  126.   emit_byte(cinfo, (int16)((value >> 8) & 0xFF));
  127.   emit_byte(cinfo, (int16)(value & 0xFF));
  128. }
  129.  
  130.  
  131. /*
  132.  * Routines to write specific marker types.
  133.  */
  134.  
  135. LOCAL int16
  136. emit_dqt (j_compress_ptr cinfo, int16 index)
  137. /* Emit a DQT marker */
  138. /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
  139. {
  140.   JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
  141.   int16 prec;
  142.   int16 i;
  143.  
  144.   if (qtbl == NULL)
  145.     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
  146.  
  147.   prec = 0;
  148.   for (i = 0; i < DCTSIZE2; i++) {
  149.     if (qtbl->quantval[i] > 255)
  150.       prec = 1;
  151.   }
  152.  
  153.   if (! qtbl->sent_table) {
  154.     emit_marker(cinfo, M_DQT);
  155.  
  156.     emit_2bytes(cinfo, (int16)(prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2));
  157.  
  158.     emit_byte(cinfo, (int16)(index + (prec<<4)));
  159.  
  160.     for (i = 0; i < DCTSIZE2; i++) {
  161.       if (prec)
  162.     emit_byte(cinfo, (int16)(qtbl->quantval[i] >> 8));
  163.       emit_byte(cinfo, (int16)(qtbl->quantval[i] & 0xFF));
  164.     }
  165.  
  166.     qtbl->sent_table = TRUE;
  167.   }
  168.  
  169.   return prec;
  170. }
  171.  
  172.  
  173. LOCAL void
  174. emit_dht (j_compress_ptr cinfo, int16 index, boolean is_ac)
  175. /* Emit a DHT marker */
  176. {
  177.   JHUFF_TBL * htbl;
  178.   int16 length, i;
  179.   
  180.   if (is_ac) {
  181.     htbl = cinfo->ac_huff_tbl_ptrs[index];
  182.     index += 0x10;        /* output index has AC bit set */
  183.   } else {
  184.     htbl = cinfo->dc_huff_tbl_ptrs[index];
  185.   }
  186.  
  187.   if (htbl == NULL)
  188.     ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, index);
  189.   
  190.   if (! htbl->sent_table) {
  191.     emit_marker(cinfo, M_DHT);
  192.     
  193.     length = 0;
  194.     for (i = 1; i <= 16; i++)
  195.       length += htbl->bits[i];
  196.     
  197.     emit_2bytes(cinfo, (int16)(length + 2 + 1 + 16));
  198.     emit_byte(cinfo, index);
  199.     
  200.     for (i = 1; i <= 16; i++)
  201.       emit_byte(cinfo, htbl->bits[i]);
  202.     
  203.     for (i = 0; i < length; i++)
  204.       emit_byte(cinfo, htbl->huffval[i]);
  205.     
  206.     htbl->sent_table = TRUE;
  207.   }
  208. }
  209.  
  210.  
  211. LOCAL void
  212. emit_dac (j_compress_ptr cinfo)
  213. /* Emit a DAC marker */
  214. /* Since the useful info is so small, we want to emit all the tables in */
  215. /* one DAC marker.  Therefore this routine does its own scan of the table. */
  216. {
  217. #ifdef C_ARITH_CODING_SUPPORTED
  218.   char dc_in_use[NUM_ARITH_TBLS];
  219.   char ac_in_use[NUM_ARITH_TBLS];
  220.   int16 length, i;
  221.   jpeg_component_info *compptr;
  222.   
  223.   for (i = 0; i < NUM_ARITH_TBLS; i++)
  224.     dc_in_use[i] = ac_in_use[i] = 0;
  225.   
  226.   for (i = 0; i < cinfo->comps_in_scan; i++) {
  227.     compptr = cinfo->cur_comp_info[i];
  228.     dc_in_use[compptr->dc_tbl_no] = 1;
  229.     ac_in_use[compptr->ac_tbl_no] = 1;
  230.   }
  231.   
  232.   length = 0;
  233.   for (i = 0; i < NUM_ARITH_TBLS; i++)
  234.     length += dc_in_use[i] + ac_in_use[i];
  235.   
  236.   emit_marker(cinfo, M_DAC);
  237.   
  238.   emit_2bytes(cinfo, length*2 + 2);
  239.   
  240.   for (i = 0; i < NUM_ARITH_TBLS; i++) {
  241.     if (dc_in_use[i]) {
  242.       emit_byte(cinfo, i);
  243.       emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
  244.     }
  245.     if (ac_in_use[i]) {
  246.       emit_byte(cinfo, i + 0x10);
  247.       emit_byte(cinfo, cinfo->arith_ac_K[i]);
  248.     }
  249.   }
  250. #endif /* C_ARITH_CODING_SUPPORTED */
  251. }
  252.  
  253.  
  254. LOCAL void
  255. emit_dri (j_compress_ptr cinfo)
  256. /* Emit a DRI marker */
  257. {
  258.   emit_marker(cinfo, M_DRI);
  259.   
  260.   emit_2bytes(cinfo, 4);    /* fixed length */
  261.  
  262.   emit_2bytes(cinfo, (int16) cinfo->restart_interval);
  263. }
  264.  
  265.  
  266. LOCAL void
  267. emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
  268. /* Emit a SOF marker */
  269. {
  270.   int16 ci;
  271.   jpeg_component_info *compptr;
  272.   
  273.   emit_marker(cinfo, code);
  274.   
  275.   emit_2bytes(cinfo, (int16)(3 * cinfo->num_components + 2 + 5 + 1)); /* length */
  276.  
  277.   /* Make sure image isn't bigger than SOF field can handle */
  278.   if ((int32) cinfo->image_height > 65535L ||
  279.       (int32) cinfo->image_width > 65535L)
  280.     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (uint16) 65535);
  281.  
  282.   emit_byte(cinfo, (int16)(cinfo->data_precision));
  283.   emit_2bytes(cinfo, (int16) cinfo->image_height);
  284.   emit_2bytes(cinfo, (int16) cinfo->image_width);
  285.  
  286.   emit_byte(cinfo, (int16)(cinfo->num_components));
  287.  
  288.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  289.        ci++, compptr++) {
  290.     emit_byte(cinfo, (int16)(compptr->component_id));
  291.     emit_byte(cinfo, (int16)((compptr->h_samp_factor << 4) + compptr->v_samp_factor));
  292.     emit_byte(cinfo, (int16)(compptr->quant_tbl_no));
  293.   }
  294. }
  295.  
  296.  
  297. LOCAL void
  298. emit_sos (j_compress_ptr cinfo)
  299. /* Emit a SOS marker */
  300. {
  301.   int16 i, td, ta;
  302.   jpeg_component_info *compptr;
  303.   
  304.   emit_marker(cinfo, M_SOS);
  305.   
  306.   emit_2bytes(cinfo, (int16)(2 * cinfo->comps_in_scan + 2 + 1 + 3)); /* length */
  307.   
  308.   emit_byte(cinfo, (int16)(cinfo->comps_in_scan));
  309.   
  310.   for (i = 0; i < cinfo->comps_in_scan; i++) {
  311.     compptr = cinfo->cur_comp_info[i];
  312.     emit_byte(cinfo, (int16)(compptr->component_id));
  313.     td = compptr->dc_tbl_no;
  314.     ta = compptr->ac_tbl_no;
  315.     if (cinfo->progressive_mode) {
  316.       /* Progressive mode: only DC or only AC tables are used in one scan;
  317.        * furthermore, Huffman coding of DC refinement uses no table at all.
  318.        * We emit 0 for unused field(s); this is recommended by the P&M text
  319.        * but does not seem to be specified in the standard.
  320.        */
  321.       if (cinfo->Ss == 0) {
  322.     ta = 0;            /* DC scan */
  323.     if (cinfo->Ah != 0 && !cinfo->arith_code)
  324.       td = 0;        /* no DC table either */
  325.       } else {
  326.     td = 0;            /* AC scan */
  327.       }
  328.     }
  329.     emit_byte(cinfo, (int16)((td << 4) + ta));
  330.   }
  331.  
  332.   emit_byte(cinfo, (int16)(cinfo->Ss));
  333.   emit_byte(cinfo, (int16)(cinfo->Se));
  334.   emit_byte(cinfo, (int16)((cinfo->Ah << 4) + cinfo->Al));
  335. }
  336.  
  337.  
  338. LOCAL void
  339. emit_jfif_app0 (j_compress_ptr cinfo)
  340. /* Emit a JFIF-compliant APP0 marker */
  341. {
  342.   /*
  343.    * Length of APP0 block    (2 bytes)
  344.    * Block ID            (4 bytes - ASCII "JFIF")
  345.    * Zero byte            (1 byte to terminate the ID string)
  346.    * Version Major, Minor    (2 bytes - 0x01, 0x01)
  347.    * Units            (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
  348.    * Xdpu            (2 bytes - dots per unit horizontal)
  349.    * Ydpu            (2 bytes - dots per unit vertical)
  350.    * Thumbnail X size        (1 byte)
  351.    * Thumbnail Y size        (1 byte)
  352.    */
  353.   
  354.   emit_marker(cinfo, M_APP0);
  355.   
  356.   emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
  357.  
  358.   emit_byte(cinfo, 0x4A);    /* Identifier: ASCII "JFIF" */
  359.   emit_byte(cinfo, 0x46);
  360.   emit_byte(cinfo, 0x49);
  361.   emit_byte(cinfo, 0x46);
  362.   emit_byte(cinfo, 0);
  363.   /* We currently emit version code 1.01 since we use no 1.02 features.
  364.    * This may avoid complaints from some older decoders.
  365.    */
  366.   emit_byte(cinfo, 1);        /* Major version */
  367.   emit_byte(cinfo, 1);        /* Minor version */
  368.   emit_byte(cinfo, cinfo->density_unit); /* Pixel size information */
  369.   emit_2bytes(cinfo, (int16) cinfo->X_density);
  370.   emit_2bytes(cinfo, (int16) cinfo->Y_density);
  371.   emit_byte(cinfo, 0);        /* No thumbnail image */
  372.   emit_byte(cinfo, 0);
  373. }
  374.  
  375.  
  376. LOCAL void
  377. emit_adobe_app14 (j_compress_ptr cinfo)
  378. /* Emit an Adobe APP14 marker */
  379. {
  380.   /*
  381.    * Length of APP14 block    (2 bytes)
  382.    * Block ID            (5 bytes - ASCII "Adobe")
  383.    * Version Number        (2 bytes - currently 100)
  384.    * Flags0            (2 bytes - currently 0)
  385.    * Flags1            (2 bytes - currently 0)
  386.    * Color transform        (1 byte)
  387.    *
  388.    * Although Adobe TN 5116 mentions Version = 101, all the Adobe files
  389.    * now in circulation seem to use Version = 100, so that's what we write.
  390.    *
  391.    * We write the color transform byte as 1 if the JPEG color space is
  392.    * YCbCr, 2 if it's YCCK, 0 otherwise.  Adobe's definition has to do with
  393.    * whether the encoder performed a transformation, which is pretty useless.
  394.    */
  395.   
  396.   emit_marker(cinfo, M_APP14);
  397.   
  398.   emit_2bytes(cinfo, 2 + 5 + 2 + 2 + 2 + 1); /* length */
  399.  
  400.   emit_byte(cinfo, 0x41);    /* Identifier: ASCII "Adobe" */
  401.   emit_byte(cinfo, 0x64);
  402.   emit_byte(cinfo, 0x6F);
  403.   emit_byte(cinfo, 0x62);
  404.   emit_byte(cinfo, 0x65);
  405.   emit_2bytes(cinfo, 100);    /* Version */
  406.   emit_2bytes(cinfo, 0);    /* Flags0 */
  407.   emit_2bytes(cinfo, 0);    /* Flags1 */
  408.   switch (cinfo->jpeg_color_space) {
  409.   case JCS_YCbCr:
  410.     emit_byte(cinfo, 1);    /* Color transform = 1 */
  411.     break;
  412.   case JCS_YCCK:
  413.     emit_byte(cinfo, 2);    /* Color transform = 2 */
  414.     break;
  415.   default:
  416.     emit_byte(cinfo, 0);    /* Color transform = 0 */
  417.     break;
  418.   }
  419. }
  420.  
  421.  
  422. /*
  423.  * This routine is exported for possible use by applications.
  424.  * The intended use is to emit COM or APPn markers after calling
  425.  * jpeg_start_compress() and before the first jpeg_write_scanlines() call
  426.  * (hence, after write_file_header but before write_frame_header).
  427.  * Other uses are not guaranteed to produce desirable results.
  428.  */
  429.  
  430. METHODDEF void
  431. write_any_marker (j_compress_ptr cinfo, int marker,
  432.           const JOCTET *dataptr, unsigned int datalen)
  433. /* Emit an arbitrary marker with parameters */
  434. {
  435.   if (datalen <= (uint16) 65533) { /* safety check */
  436.     emit_marker(cinfo, (JPEG_MARKER) marker);
  437.   
  438.     emit_2bytes(cinfo, (int16) (datalen + 2)); /* total length */
  439.  
  440.     while (datalen--) {
  441.       emit_byte(cinfo, *dataptr);
  442.       dataptr++;
  443.     }
  444.   }
  445. }
  446.  
  447.  
  448. /*
  449.  * Write datastream header.
  450.  * This consists of an SOI and optional APPn markers.
  451.  * We recommend use of the JFIF marker, but not the Adobe marker,
  452.  * when using YCbCr or grayscale data.  The JFIF marker should NOT
  453.  * be used for any other JPEG colorspace.  The Adobe marker is helpful
  454.  * to distinguish RGB, CMYK, and YCCK colorspaces.
  455.  * Note that an application can write additional header markers after
  456.  * jpeg_start_compress returns.
  457.  */
  458.  
  459. METHODDEF void
  460. write_file_header (j_compress_ptr cinfo)
  461. {
  462.   emit_marker(cinfo, M_SOI);    /* first the SOI */
  463.  
  464.   if (cinfo->write_JFIF_header)    /* next an optional JFIF APP0 */
  465.     emit_jfif_app0(cinfo);
  466.   if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
  467.     emit_adobe_app14(cinfo);
  468. }
  469.  
  470.  
  471. /*
  472.  * Write frame header.
  473.  * This consists of DQT and SOFn markers.
  474.  * Note that we do not emit the SOF until we have emitted the DQT(s).
  475.  * This avoids compatibility problems with incorrect implementations that
  476.  * try to error-check the quant table numbers as soon as they see the SOF.
  477.  */
  478.  
  479. METHODDEF void
  480. write_frame_header (j_compress_ptr cinfo)
  481. {
  482.   int16 ci, prec;
  483.   boolean is_baseline;
  484.   jpeg_component_info *compptr;
  485.   
  486.   /* Emit DQT for each quantization table.
  487.    * Note that emit_dqt() suppresses any duplicate tables.
  488.    */
  489.   prec = 0;
  490.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  491.        ci++, compptr++) {
  492.     prec += emit_dqt(cinfo, (int16)(compptr->quant_tbl_no));
  493.   }
  494.   /* now prec is nonzero iff there are any 16-bit quant tables. */
  495.  
  496.   /* Check for a non-baseline specification.
  497.    * Note we assume that Huffman table numbers won't be changed later.
  498.    */
  499.   if (cinfo->arith_code || cinfo->progressive_mode ||
  500.       cinfo->data_precision != 8) {
  501.     is_baseline = FALSE;
  502.   } else {
  503.     is_baseline = TRUE;
  504.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  505.      ci++, compptr++) {
  506.       if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
  507.     is_baseline = FALSE;
  508.     }
  509.     if (prec && is_baseline) {
  510.       is_baseline = FALSE;
  511.       /* If it's baseline except for quantizer size, warn the user */
  512.       TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
  513.     }
  514.   }
  515.  
  516.   /* Emit the proper SOF marker */
  517.   if (cinfo->arith_code) {
  518.     emit_sof(cinfo, M_SOF9);    /* SOF code for arithmetic coding */
  519.   } else {
  520.     if (cinfo->progressive_mode)
  521.       emit_sof(cinfo, M_SOF2);    /* SOF code for progressive Huffman */
  522.     else if (is_baseline)
  523.       emit_sof(cinfo, M_SOF0);    /* SOF code for baseline implementation */
  524.     else
  525.       emit_sof(cinfo, M_SOF1);    /* SOF code for non-baseline Huffman file */
  526.   }
  527. }
  528.  
  529.  
  530. /*
  531.  * Write scan header.
  532.  * This consists of DHT or DAC markers, optional DRI, and SOS.
  533.  * Compressed data will be written following the SOS.
  534.  */
  535.  
  536. METHODDEF void
  537. write_scan_header (j_compress_ptr cinfo)
  538. {
  539.   int16 i;
  540.   jpeg_component_info *compptr;
  541.  
  542.   if (cinfo->arith_code) {
  543.     /* Emit arith conditioning info.  We may have some duplication
  544.      * if the file has multiple scans, but it's so small it's hardly
  545.      * worth worrying about.
  546.      */
  547.     emit_dac(cinfo);
  548.   } else {
  549.     /* Emit Huffman tables.
  550.      * Note that emit_dht() suppresses any duplicate tables.
  551.      */
  552.     for (i = 0; i < cinfo->comps_in_scan; i++) {
  553.       compptr = cinfo->cur_comp_info[i];
  554.       if (cinfo->progressive_mode) {
  555.     /* Progressive mode: only DC or only AC tables are used in one scan */
  556.     if (cinfo->Ss == 0) {
  557.       if (cinfo->Ah == 0)    /* DC needs no table for refinement scan */
  558.         emit_dht(cinfo, (int16)(compptr->dc_tbl_no), FALSE);
  559.     } else {
  560.       emit_dht(cinfo, (int16)(compptr->ac_tbl_no), TRUE);
  561.     }
  562.       } else {
  563.     /* Sequential mode: need both DC and AC tables */
  564.     emit_dht(cinfo, (int16)(compptr->dc_tbl_no), FALSE);
  565.     emit_dht(cinfo, (int16)(compptr->ac_tbl_no), TRUE);
  566.       }
  567.     }
  568.   }
  569.  
  570.   /* Emit DRI if required --- note that DRI value could change for each scan.
  571.    * If it doesn't, a tiny amount of space is wasted in multiple-scan files.
  572.    * We assume DRI will never be nonzero for one scan and zero for a later one.
  573.    */
  574.   if (cinfo->restart_interval)
  575.     emit_dri(cinfo);
  576.  
  577.   emit_sos(cinfo);
  578. }
  579.  
  580.  
  581. /*
  582.  * Write datastream trailer.
  583.  */
  584.  
  585. METHODDEF void
  586. write_file_trailer (j_compress_ptr cinfo)
  587. {
  588.   emit_marker(cinfo, M_EOI);
  589. }
  590.  
  591.  
  592. /*
  593.  * Write an abbreviated table-specification datastream.
  594.  * This consists of SOI, DQT and DHT tables, and EOI.
  595.  * Any table that is defined and not marked sent_table = TRUE will be
  596.  * emitted.  Note that all tables will be marked sent_table = TRUE at exit.
  597.  */
  598.  
  599. METHODDEF void
  600. write_tables_only (j_compress_ptr cinfo)
  601. {
  602.   int16 i;
  603.  
  604.   emit_marker(cinfo, M_SOI);
  605.  
  606.   for (i = 0; i < NUM_QUANT_TBLS; i++) {
  607.     if (cinfo->quant_tbl_ptrs[i] != NULL)
  608.       (void) emit_dqt(cinfo, i);
  609.   }
  610.  
  611.   if (! cinfo->arith_code) {
  612.     for (i = 0; i < NUM_HUFF_TBLS; i++) {
  613.       if (cinfo->dc_huff_tbl_ptrs[i] != NULL)
  614.     emit_dht(cinfo, i, FALSE);
  615.       if (cinfo->ac_huff_tbl_ptrs[i] != NULL)
  616.     emit_dht(cinfo, i, TRUE);
  617.     }
  618.   }
  619.  
  620.   emit_marker(cinfo, M_EOI);
  621. }
  622.  
  623.  
  624. /*
  625.  * Initialize the marker writer module.
  626.  */
  627.  
  628. GLOBAL void
  629. jinit_marker_writer (j_compress_ptr cinfo)
  630. {
  631.   /* Create the subobject */
  632.   cinfo->marker = (struct jpeg_marker_writer *)
  633.     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  634.                 SIZEOF(struct jpeg_marker_writer));
  635.   /* Initialize method pointers */
  636.   cinfo->marker->write_any_marker = write_any_marker;
  637.   cinfo->marker->write_file_header = write_file_header;
  638.   cinfo->marker->write_frame_header = write_frame_header;
  639.   cinfo->marker->write_scan_header = write_scan_header;
  640.   cinfo->marker->write_file_trailer = write_file_trailer;
  641.   cinfo->marker->write_tables_only = write_tables_only;
  642. }
  643.