< prev index next >

modules/javafx.graphics/src/main/native-iio/libjpeg7/jcmarker.c

Print this page


   1 /*
   2  * jcmarker.c
   3  *
   4  * Copyright (C) 1991-1998, 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 #include "jpeglib.h"
  14 
  15 
  16 typedef enum {                  /* JPEG marker codes */
  17   M_SOF0  = 0xc0,
  18   M_SOF1  = 0xc1,
  19   M_SOF2  = 0xc2,
  20   M_SOF3  = 0xc3,
  21 
  22   M_SOF5  = 0xc5,
  23   M_SOF6  = 0xc6,
  24   M_SOF7  = 0xc7,


  55   M_EXP   = 0xdf,
  56 
  57   M_APP0  = 0xe0,
  58   M_APP1  = 0xe1,
  59   M_APP2  = 0xe2,
  60   M_APP3  = 0xe3,
  61   M_APP4  = 0xe4,
  62   M_APP5  = 0xe5,
  63   M_APP6  = 0xe6,
  64   M_APP7  = 0xe7,
  65   M_APP8  = 0xe8,
  66   M_APP9  = 0xe9,
  67   M_APP10 = 0xea,
  68   M_APP11 = 0xeb,
  69   M_APP12 = 0xec,
  70   M_APP13 = 0xed,
  71   M_APP14 = 0xee,
  72   M_APP15 = 0xef,
  73 
  74   M_JPG0  = 0xf0,

  75   M_JPG13 = 0xfd,
  76   M_COM   = 0xfe,
  77 
  78   M_TEM   = 0x01,
  79 
  80   M_ERROR = 0x100
  81 } JPEG_MARKER;
  82 
  83 
  84 /* Private state */
  85 
  86 typedef struct {
  87   struct jpeg_marker_writer pub; /* public fields */
  88 
  89   unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
  90 } my_marker_writer;
  91 
  92 typedef my_marker_writer * my_marker_ptr;
  93 
  94 


 136 }
 137 
 138 
 139 /*
 140  * Routines to write specific marker types.
 141  */
 142 
 143 LOCAL(int)
 144 emit_dqt (j_compress_ptr cinfo, int index)
 145 /* Emit a DQT marker */
 146 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
 147 {
 148   JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
 149   int prec;
 150   int i;
 151 
 152   if (qtbl == NULL)
 153     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
 154 
 155   prec = 0;
 156   for (i = 0; i < DCTSIZE2; i++) {
 157     if (qtbl->quantval[i] > 255)
 158       prec = 1;
 159   }
 160 
 161   if (! qtbl->sent_table) {
 162     emit_marker(cinfo, M_DQT);
 163 
 164     emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);

 165 
 166     emit_byte(cinfo, index + (prec<<4));
 167 
 168     for (i = 0; i < DCTSIZE2; i++) {
 169       /* The table entries must be emitted in zigzag order. */
 170       unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
 171       if (prec)
 172         emit_byte(cinfo, (int) (qval >> 8));
 173       emit_byte(cinfo, (int) (qval & 0xFF));
 174     }
 175 
 176     qtbl->sent_table = TRUE;
 177   }
 178 
 179   return prec;
 180 }
 181 
 182 
 183 LOCAL(void)
 184 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
 185 /* Emit a DHT marker */
 186 {
 187   JHUFF_TBL * htbl;
 188   int length, i;
 189 
 190   if (is_ac) {


 218 }
 219 
 220 
 221 LOCAL(void)
 222 emit_dac (j_compress_ptr cinfo)
 223 /* Emit a DAC marker */
 224 /* Since the useful info is so small, we want to emit all the tables in */
 225 /* one DAC marker.  Therefore this routine does its own scan of the table. */
 226 {
 227 #ifdef C_ARITH_CODING_SUPPORTED
 228   char dc_in_use[NUM_ARITH_TBLS];
 229   char ac_in_use[NUM_ARITH_TBLS];
 230   int length, i;
 231   jpeg_component_info *compptr;
 232 
 233   for (i = 0; i < NUM_ARITH_TBLS; i++)
 234     dc_in_use[i] = ac_in_use[i] = 0;
 235 
 236   for (i = 0; i < cinfo->comps_in_scan; i++) {
 237     compptr = cinfo->cur_comp_info[i];


 238     dc_in_use[compptr->dc_tbl_no] = 1;


 239     ac_in_use[compptr->ac_tbl_no] = 1;
 240   }
 241 
 242   length = 0;
 243   for (i = 0; i < NUM_ARITH_TBLS; i++)
 244     length += dc_in_use[i] + ac_in_use[i];
 245 

 246   emit_marker(cinfo, M_DAC);
 247 
 248   emit_2bytes(cinfo, length*2 + 2);
 249 
 250   for (i = 0; i < NUM_ARITH_TBLS; i++) {
 251     if (dc_in_use[i]) {
 252       emit_byte(cinfo, i);
 253       emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
 254     }
 255     if (ac_in_use[i]) {
 256       emit_byte(cinfo, i + 0x10);
 257       emit_byte(cinfo, cinfo->arith_ac_K[i]);
 258     }
 259   }

 260 #endif /* C_ARITH_CODING_SUPPORTED */
 261 }
 262 
 263 
 264 LOCAL(void)
 265 emit_dri (j_compress_ptr cinfo)
 266 /* Emit a DRI marker */
 267 {
 268   emit_marker(cinfo, M_DRI);
 269 
 270   emit_2bytes(cinfo, 4);        /* fixed length */
 271 
 272   emit_2bytes(cinfo, (int) cinfo->restart_interval);
 273 }
 274 
 275 
 276 LOCAL(void)































 277 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
 278 /* Emit a SOF marker */
 279 {
 280   int ci;
 281   jpeg_component_info *compptr;
 282 
 283   emit_marker(cinfo, code);
 284 
 285   emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
 286 
 287   /* Make sure image isn't bigger than SOF field can handle */
 288   if ((long) cinfo->jpeg_height > 65535L ||
 289       (long) cinfo->jpeg_width > 65535L)
 290     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
 291 
 292   emit_byte(cinfo, cinfo->data_precision);
 293   emit_2bytes(cinfo, (int) cinfo->jpeg_height);
 294   emit_2bytes(cinfo, (int) cinfo->jpeg_width);
 295 
 296   emit_byte(cinfo, cinfo->num_components);


 303   }
 304 }
 305 
 306 
 307 LOCAL(void)
 308 emit_sos (j_compress_ptr cinfo)
 309 /* Emit a SOS marker */
 310 {
 311   int i, td, ta;
 312   jpeg_component_info *compptr;
 313 
 314   emit_marker(cinfo, M_SOS);
 315 
 316   emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
 317 
 318   emit_byte(cinfo, cinfo->comps_in_scan);
 319 
 320   for (i = 0; i < cinfo->comps_in_scan; i++) {
 321     compptr = cinfo->cur_comp_info[i];
 322     emit_byte(cinfo, compptr->component_id);
 323     td = compptr->dc_tbl_no;
 324     ta = compptr->ac_tbl_no;
 325     if (cinfo->progressive_mode) {
 326       /* Progressive mode: only DC or only AC tables are used in one scan;
 327        * furthermore, Huffman coding of DC refinement uses no table at all.
 328        * We emit 0 for unused field(s); this is recommended by the P&M text
 329        * but does not seem to be specified in the standard.
 330        */
 331       if (cinfo->Ss == 0) {
 332         ta = 0;                 /* DC scan */
 333         if (cinfo->Ah != 0 && !cinfo->arith_code)
 334           td = 0;               /* no DC table either */
 335       } else {
 336         td = 0;                 /* AC scan */
 337       }
 338     }
 339     emit_byte(cinfo, (td << 4) + ta);
 340   }
 341 
 342   emit_byte(cinfo, cinfo->Ss);
 343   emit_byte(cinfo, cinfo->Se);
 344   emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
 345 }
 346 
 347 
 348 LOCAL(void)
















 349 emit_jfif_app0 (j_compress_ptr cinfo)
 350 /* Emit a JFIF-compliant APP0 marker */
 351 {
 352   /*
 353    * Length of APP0 block       (2 bytes)
 354    * Block ID                   (4 bytes - ASCII "JFIF")
 355    * Zero byte                  (1 byte to terminate the ID string)
 356    * Version Major, Minor       (2 bytes - major first)
 357    * Units                      (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
 358    * Xdpu                       (2 bytes - dots per unit horizontal)
 359    * Ydpu                       (2 bytes - dots per unit vertical)
 360    * Thumbnail X size           (1 byte)
 361    * Thumbnail Y size           (1 byte)
 362    */
 363 
 364   emit_marker(cinfo, M_APP0);
 365 
 366   emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
 367 
 368   emit_byte(cinfo, 0x4A);       /* Identifier: ASCII "JFIF" */


 441   if (datalen > (unsigned int) 65533)           /* safety check */
 442     ERREXIT(cinfo, JERR_BAD_LENGTH);
 443 
 444   emit_marker(cinfo, (JPEG_MARKER) marker);
 445 
 446   emit_2bytes(cinfo, (int) (datalen + 2));      /* total length */
 447 }
 448 
 449 METHODDEF(void)
 450 write_marker_byte (j_compress_ptr cinfo, int val)
 451 /* Emit one byte of marker parameters following write_marker_header */
 452 {
 453   emit_byte(cinfo, val);
 454 }
 455 
 456 
 457 /*
 458  * Write datastream header.
 459  * This consists of an SOI and optional APPn markers.
 460  * We recommend use of the JFIF marker, but not the Adobe marker,
 461  * when using YCbCr or grayscale data.  The JFIF marker should NOT
 462  * be used for any other JPEG colorspace.  The Adobe marker is helpful
 463  * to distinguish RGB, CMYK, and YCCK colorspaces.
 464  * Note that an application can write additional header markers after
 465  * jpeg_start_compress returns.
 466  */
 467 
 468 METHODDEF(void)
 469 write_file_header (j_compress_ptr cinfo)
 470 {
 471   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 472 
 473   emit_marker(cinfo, M_SOI);    /* first the SOI */
 474 
 475   /* SOI is defined to reset restart interval to 0 */
 476   marker->last_restart_interval = 0;
 477 
 478   if (cinfo->write_JFIF_header) /* next an optional JFIF APP0 */
 479     emit_jfif_app0(cinfo);
 480   if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
 481     emit_adobe_app14(cinfo);
 482 }
 483 
 484 
 485 /*
 486  * Write frame header.
 487  * This consists of DQT and SOFn markers.

 488  * Note that we do not emit the SOF until we have emitted the DQT(s).
 489  * This avoids compatibility problems with incorrect implementations that
 490  * try to error-check the quant table numbers as soon as they see the SOF.
 491  */
 492 
 493 METHODDEF(void)
 494 write_frame_header (j_compress_ptr cinfo)
 495 {
 496   int ci, prec;
 497   boolean is_baseline;
 498   jpeg_component_info *compptr;
 499 
 500   /* Emit DQT for each quantization table.
 501    * Note that emit_dqt() suppresses any duplicate tables.
 502    */
 503   prec = 0;
 504   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 505        ci++, compptr++) {
 506     prec += emit_dqt(cinfo, compptr->quant_tbl_no);
 507   }
 508   /* now prec is nonzero iff there are any 16-bit quant tables. */
 509 
 510   /* Check for a non-baseline specification.
 511    * Note we assume that Huffman table numbers won't be changed later.
 512    */
 513   if (cinfo->arith_code || cinfo->progressive_mode ||
 514       cinfo->data_precision != 8) {
 515     is_baseline = FALSE;
 516   } else {
 517     is_baseline = TRUE;
 518     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 519          ci++, compptr++) {
 520       if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
 521         is_baseline = FALSE;
 522     }
 523     if (prec && is_baseline) {
 524       is_baseline = FALSE;
 525       /* If it's baseline except for quantizer size, warn the user */
 526       TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
 527     }
 528   }
 529 
 530   /* Emit the proper SOF marker */
 531   if (cinfo->arith_code) {
 532     if (cinfo->progressive_mode)
 533       emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
 534     else
 535       emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
 536   } else {
 537     if (cinfo->progressive_mode)
 538       emit_sof(cinfo, M_SOF2);  /* SOF code for progressive Huffman */
 539     else if (is_baseline)
 540       emit_sof(cinfo, M_SOF0);  /* SOF code for baseline implementation */
 541     else
 542       emit_sof(cinfo, M_SOF1);  /* SOF code for non-baseline Huffman file */
 543   }








 544 }
 545 
 546 
 547 /*
 548  * Write scan header.
 549  * This consists of DHT or DAC markers, optional DRI, and SOS.
 550  * Compressed data will be written following the SOS.
 551  */
 552 
 553 METHODDEF(void)
 554 write_scan_header (j_compress_ptr cinfo)
 555 {
 556   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 557   int i;
 558   jpeg_component_info *compptr;
 559 
 560   if (cinfo->arith_code) {
 561     /* Emit arith conditioning info.  We may have some duplication
 562      * if the file has multiple scans, but it's so small it's hardly
 563      * worth worrying about.
 564      */
 565     emit_dac(cinfo);
 566   } else {
 567     /* Emit Huffman tables.
 568      * Note that emit_dht() suppresses any duplicate tables.
 569      */
 570     for (i = 0; i < cinfo->comps_in_scan; i++) {
 571       compptr = cinfo->cur_comp_info[i];
 572       if (cinfo->progressive_mode) {
 573         /* Progressive mode: only DC or only AC tables are used in one scan */
 574         if (cinfo->Ss == 0) {
 575           if (cinfo->Ah == 0)   /* DC needs no table for refinement scan */
 576             emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
 577         } else {

 578           emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
 579         }
 580       } else {
 581         /* Sequential mode: need both DC and AC tables */
 582         emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
 583         emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
 584       }
 585     }
 586   }
 587 
 588   /* Emit DRI if required --- note that DRI value could change for each scan.
 589    * We avoid wasting space with unnecessary DRIs, however.
 590    */
 591   if (cinfo->restart_interval != marker->last_restart_interval) {
 592     emit_dri(cinfo);
 593     marker->last_restart_interval = cinfo->restart_interval;
 594   }
 595 
 596   emit_sos(cinfo);
 597 }
 598 
 599 
 600 /*
 601  * Write datastream trailer.
 602  */
 603 
 604 METHODDEF(void)
 605 write_file_trailer (j_compress_ptr cinfo)


 636     }
 637   }
 638 
 639   emit_marker(cinfo, M_EOI);
 640 }
 641 
 642 
 643 /*
 644  * Initialize the marker writer module.
 645  */
 646 
 647 GLOBAL(void)
 648 jinit_marker_writer (j_compress_ptr cinfo)
 649 {
 650   my_marker_ptr marker;
 651 
 652   /* Create the subobject */
 653   marker = (my_marker_ptr)
 654     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 655                                 SIZEOF(my_marker_writer));
 656   cinfo->marker = (struct jpeg_marker_writer *) marker;
 657   /* Initialize method pointers */
 658   marker->pub.write_file_header = write_file_header;
 659   marker->pub.write_frame_header = write_frame_header;
 660   marker->pub.write_scan_header = write_scan_header;
 661   marker->pub.write_file_trailer = write_file_trailer;
 662   marker->pub.write_tables_only = write_tables_only;
 663   marker->pub.write_marker_header = write_marker_header;
 664   marker->pub.write_marker_byte = write_marker_byte;
 665   /* Initialize private state */
 666   marker->last_restart_interval = 0;
 667 }
   1 /*
   2  * jcmarker.c
   3  *
   4  * Copyright (C) 1991-1998, Thomas G. Lane.
   5  * Modified 2003-2013 by Guido Vollbeding.
   6  * This file is part of the Independent JPEG Group's software.
   7  * For conditions of distribution and use, see the accompanying README file.
   8  *
   9  * This file contains routines to write JPEG datastream markers.
  10  */
  11 
  12 #define JPEG_INTERNALS
  13 #include "jinclude.h"
  14 #include "jpeglib.h"
  15 
  16 
  17 typedef enum {                  /* JPEG marker codes */
  18   M_SOF0  = 0xc0,
  19   M_SOF1  = 0xc1,
  20   M_SOF2  = 0xc2,
  21   M_SOF3  = 0xc3,
  22 
  23   M_SOF5  = 0xc5,
  24   M_SOF6  = 0xc6,
  25   M_SOF7  = 0xc7,


  56   M_EXP   = 0xdf,
  57 
  58   M_APP0  = 0xe0,
  59   M_APP1  = 0xe1,
  60   M_APP2  = 0xe2,
  61   M_APP3  = 0xe3,
  62   M_APP4  = 0xe4,
  63   M_APP5  = 0xe5,
  64   M_APP6  = 0xe6,
  65   M_APP7  = 0xe7,
  66   M_APP8  = 0xe8,
  67   M_APP9  = 0xe9,
  68   M_APP10 = 0xea,
  69   M_APP11 = 0xeb,
  70   M_APP12 = 0xec,
  71   M_APP13 = 0xed,
  72   M_APP14 = 0xee,
  73   M_APP15 = 0xef,
  74 
  75   M_JPG0  = 0xf0,
  76   M_JPG8  = 0xf8,
  77   M_JPG13 = 0xfd,
  78   M_COM   = 0xfe,
  79 
  80   M_TEM   = 0x01,
  81 
  82   M_ERROR = 0x100
  83 } JPEG_MARKER;
  84 
  85 
  86 /* Private state */
  87 
  88 typedef struct {
  89   struct jpeg_marker_writer pub; /* public fields */
  90 
  91   unsigned int last_restart_interval; /* last DRI value emitted; 0 after SOI */
  92 } my_marker_writer;
  93 
  94 typedef my_marker_writer * my_marker_ptr;
  95 
  96 


 138 }
 139 
 140 
 141 /*
 142  * Routines to write specific marker types.
 143  */
 144 
 145 LOCAL(int)
 146 emit_dqt (j_compress_ptr cinfo, int index)
 147 /* Emit a DQT marker */
 148 /* Returns the precision used (0 = 8bits, 1 = 16bits) for baseline checking */
 149 {
 150   JQUANT_TBL * qtbl = cinfo->quant_tbl_ptrs[index];
 151   int prec;
 152   int i;
 153 
 154   if (qtbl == NULL)
 155     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
 156 
 157   prec = 0;
 158   for (i = 0; i <= cinfo->lim_Se; i++) {
 159     if (qtbl->quantval[cinfo->natural_order[i]] > 255)
 160       prec = 1;
 161   }
 162 
 163   if (! qtbl->sent_table) {
 164     emit_marker(cinfo, M_DQT);
 165 
 166     emit_2bytes(cinfo,
 167       prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
 168 
 169     emit_byte(cinfo, index + (prec<<4));
 170 
 171     for (i = 0; i <= cinfo->lim_Se; i++) {
 172       /* The table entries must be emitted in zigzag order. */
 173       unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
 174       if (prec)
 175         emit_byte(cinfo, (int) (qval >> 8));
 176       emit_byte(cinfo, (int) (qval & 0xFF));
 177     }
 178 
 179     qtbl->sent_table = TRUE;
 180   }
 181 
 182   return prec;
 183 }
 184 
 185 
 186 LOCAL(void)
 187 emit_dht (j_compress_ptr cinfo, int index, boolean is_ac)
 188 /* Emit a DHT marker */
 189 {
 190   JHUFF_TBL * htbl;
 191   int length, i;
 192   
 193   if (is_ac) {


 221 }
 222 
 223 
 224 LOCAL(void)
 225 emit_dac (j_compress_ptr cinfo)
 226 /* Emit a DAC marker */
 227 /* Since the useful info is so small, we want to emit all the tables in */
 228 /* one DAC marker.  Therefore this routine does its own scan of the table. */
 229 {
 230 #ifdef C_ARITH_CODING_SUPPORTED
 231   char dc_in_use[NUM_ARITH_TBLS];
 232   char ac_in_use[NUM_ARITH_TBLS];
 233   int length, i;
 234   jpeg_component_info *compptr;
 235 
 236   for (i = 0; i < NUM_ARITH_TBLS; i++)
 237     dc_in_use[i] = ac_in_use[i] = 0;
 238 
 239   for (i = 0; i < cinfo->comps_in_scan; i++) {
 240     compptr = cinfo->cur_comp_info[i];
 241     /* DC needs no table for refinement scan */
 242     if (cinfo->Ss == 0 && cinfo->Ah == 0)
 243       dc_in_use[compptr->dc_tbl_no] = 1;
 244     /* AC needs no table when not present */
 245     if (cinfo->Se)
 246       ac_in_use[compptr->ac_tbl_no] = 1;
 247   }
 248 
 249   length = 0;
 250   for (i = 0; i < NUM_ARITH_TBLS; i++)
 251     length += dc_in_use[i] + ac_in_use[i];
 252 
 253   if (length) {
 254     emit_marker(cinfo, M_DAC);
 255 
 256     emit_2bytes(cinfo, length*2 + 2);
 257 
 258     for (i = 0; i < NUM_ARITH_TBLS; i++) {
 259       if (dc_in_use[i]) {
 260         emit_byte(cinfo, i);
 261         emit_byte(cinfo, cinfo->arith_dc_L[i] + (cinfo->arith_dc_U[i]<<4));
 262       }
 263       if (ac_in_use[i]) {
 264         emit_byte(cinfo, i + 0x10);
 265         emit_byte(cinfo, cinfo->arith_ac_K[i]);
 266       }
 267     }
 268   }
 269 #endif /* C_ARITH_CODING_SUPPORTED */
 270 }
 271 
 272 
 273 LOCAL(void)
 274 emit_dri (j_compress_ptr cinfo)
 275 /* Emit a DRI marker */
 276 {
 277   emit_marker(cinfo, M_DRI);
 278   
 279   emit_2bytes(cinfo, 4);        /* fixed length */
 280 
 281   emit_2bytes(cinfo, (int) cinfo->restart_interval);
 282 }
 283 
 284 
 285 LOCAL(void)
 286 emit_lse_ict (j_compress_ptr cinfo)
 287 /* Emit an LSE inverse color transform specification marker */
 288 {
 289   /* Support only 1 transform */
 290   if (cinfo->color_transform != JCT_SUBTRACT_GREEN ||
 291       cinfo->num_components < 3)
 292     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
 293 
 294   emit_marker(cinfo, M_JPG8);
 295   
 296   emit_2bytes(cinfo, 24);       /* fixed length */
 297 
 298   emit_byte(cinfo, 0x0D);       /* ID inverse transform specification */
 299   emit_2bytes(cinfo, MAXJSAMPLE);       /* MAXTRANS */
 300   emit_byte(cinfo, 3);          /* Nt=3 */
 301   emit_byte(cinfo, cinfo->comp_info[1].component_id);
 302   emit_byte(cinfo, cinfo->comp_info[0].component_id);
 303   emit_byte(cinfo, cinfo->comp_info[2].component_id);
 304   emit_byte(cinfo, 0x80);       /* F1: CENTER1=1, NORM1=0 */
 305   emit_2bytes(cinfo, 0);        /* A(1,1)=0 */
 306   emit_2bytes(cinfo, 0);        /* A(1,2)=0 */
 307   emit_byte(cinfo, 0);          /* F2: CENTER2=0, NORM2=0 */
 308   emit_2bytes(cinfo, 1);        /* A(2,1)=1 */
 309   emit_2bytes(cinfo, 0);        /* A(2,2)=0 */
 310   emit_byte(cinfo, 0);          /* F3: CENTER3=0, NORM3=0 */
 311   emit_2bytes(cinfo, 1);        /* A(3,1)=1 */
 312   emit_2bytes(cinfo, 0);        /* A(3,2)=0 */
 313 }
 314 
 315 
 316 LOCAL(void)
 317 emit_sof (j_compress_ptr cinfo, JPEG_MARKER code)
 318 /* Emit a SOF marker */
 319 {
 320   int ci;
 321   jpeg_component_info *compptr;
 322   
 323   emit_marker(cinfo, code);
 324   
 325   emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
 326 
 327   /* Make sure image isn't bigger than SOF field can handle */
 328   if ((long) cinfo->jpeg_height > 65535L ||
 329       (long) cinfo->jpeg_width > 65535L)
 330     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
 331 
 332   emit_byte(cinfo, cinfo->data_precision);
 333   emit_2bytes(cinfo, (int) cinfo->jpeg_height);
 334   emit_2bytes(cinfo, (int) cinfo->jpeg_width);
 335 
 336   emit_byte(cinfo, cinfo->num_components);


 343   }
 344 }
 345 
 346 
 347 LOCAL(void)
 348 emit_sos (j_compress_ptr cinfo)
 349 /* Emit a SOS marker */
 350 {
 351   int i, td, ta;
 352   jpeg_component_info *compptr;
 353   
 354   emit_marker(cinfo, M_SOS);
 355   
 356   emit_2bytes(cinfo, 2 * cinfo->comps_in_scan + 2 + 1 + 3); /* length */
 357   
 358   emit_byte(cinfo, cinfo->comps_in_scan);
 359   
 360   for (i = 0; i < cinfo->comps_in_scan; i++) {
 361     compptr = cinfo->cur_comp_info[i];
 362     emit_byte(cinfo, compptr->component_id);
 363 
 364     /* We emit 0 for unused field(s); this is recommended by the P&M text




 365      * but does not seem to be specified in the standard.
 366      */
 367 
 368     /* DC needs no table for refinement scan */
 369     td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
 370     /* AC needs no table when not present */
 371     ta = cinfo->Se ? compptr->ac_tbl_no : 0;
 372 


 373     emit_byte(cinfo, (td << 4) + ta);
 374   }
 375 
 376   emit_byte(cinfo, cinfo->Ss);
 377   emit_byte(cinfo, cinfo->Se);
 378   emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
 379 }
 380 
 381 
 382 LOCAL(void)
 383 emit_pseudo_sos (j_compress_ptr cinfo)
 384 /* Emit a pseudo SOS marker */
 385 {
 386   emit_marker(cinfo, M_SOS);
 387   
 388   emit_2bytes(cinfo, 2 + 1 + 3); /* length */
 389   
 390   emit_byte(cinfo, 0); /* Ns */
 391 
 392   emit_byte(cinfo, 0); /* Ss */
 393   emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */
 394   emit_byte(cinfo, 0); /* Ah/Al */
 395 }
 396 
 397 
 398 LOCAL(void)
 399 emit_jfif_app0 (j_compress_ptr cinfo)
 400 /* Emit a JFIF-compliant APP0 marker */
 401 {
 402   /*
 403    * Length of APP0 block       (2 bytes)
 404    * Block ID                   (4 bytes - ASCII "JFIF")
 405    * Zero byte                  (1 byte to terminate the ID string)
 406    * Version Major, Minor       (2 bytes - major first)
 407    * Units                      (1 byte - 0x00 = none, 0x01 = inch, 0x02 = cm)
 408    * Xdpu                       (2 bytes - dots per unit horizontal)
 409    * Ydpu                       (2 bytes - dots per unit vertical)
 410    * Thumbnail X size           (1 byte)
 411    * Thumbnail Y size           (1 byte)
 412    */
 413   
 414   emit_marker(cinfo, M_APP0);
 415   
 416   emit_2bytes(cinfo, 2 + 4 + 1 + 2 + 1 + 2 + 2 + 1 + 1); /* length */
 417 
 418   emit_byte(cinfo, 0x4A);       /* Identifier: ASCII "JFIF" */


 491   if (datalen > (unsigned int) 65533)                /* safety check */
 492     ERREXIT(cinfo, JERR_BAD_LENGTH);
 493 
 494   emit_marker(cinfo, (JPEG_MARKER) marker);
 495 
 496   emit_2bytes(cinfo, (int) (datalen + 2));      /* total length */
 497 }
 498 
 499 METHODDEF(void)
 500 write_marker_byte (j_compress_ptr cinfo, int val)
 501 /* Emit one byte of marker parameters following write_marker_header */
 502 {
 503   emit_byte(cinfo, val);
 504 }
 505 
 506 
 507 /*
 508  * Write datastream header.
 509  * This consists of an SOI and optional APPn markers.
 510  * We recommend use of the JFIF marker, but not the Adobe marker,
 511  * when using YCbCr or grayscale data.  The JFIF marker is also used
 512  * for other standard JPEG colorspaces.  The Adobe marker is helpful
 513  * to distinguish RGB, CMYK, and YCCK colorspaces.
 514  * Note that an application can write additional header markers after
 515  * jpeg_start_compress returns.
 516  */
 517 
 518 METHODDEF(void)
 519 write_file_header (j_compress_ptr cinfo)
 520 {
 521   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 522 
 523   emit_marker(cinfo, M_SOI);    /* first the SOI */
 524 
 525   /* SOI is defined to reset restart interval to 0 */
 526   marker->last_restart_interval = 0;
 527 
 528   if (cinfo->write_JFIF_header)      /* next an optional JFIF APP0 */
 529     emit_jfif_app0(cinfo);
 530   if (cinfo->write_Adobe_marker) /* next an optional Adobe APP14 */
 531     emit_adobe_app14(cinfo);
 532 }
 533 
 534 
 535 /*
 536  * Write frame header.
 537  * This consists of DQT and SOFn markers,
 538  * a conditional LSE marker and a conditional pseudo SOS marker.
 539  * Note that we do not emit the SOF until we have emitted the DQT(s).
 540  * This avoids compatibility problems with incorrect implementations that
 541  * try to error-check the quant table numbers as soon as they see the SOF.
 542  */
 543 
 544 METHODDEF(void)
 545 write_frame_header (j_compress_ptr cinfo)
 546 {
 547   int ci, prec;
 548   boolean is_baseline;
 549   jpeg_component_info *compptr;
 550   
 551   /* Emit DQT for each quantization table.
 552    * Note that emit_dqt() suppresses any duplicate tables.
 553    */
 554   prec = 0;
 555   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 556        ci++, compptr++) {
 557     prec += emit_dqt(cinfo, compptr->quant_tbl_no);
 558   }
 559   /* now prec is nonzero iff there are any 16-bit quant tables. */
 560 
 561   /* Check for a non-baseline specification.
 562    * Note we assume that Huffman table numbers won't be changed later.
 563    */
 564   if (cinfo->arith_code || cinfo->progressive_mode ||
 565       cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
 566     is_baseline = FALSE;
 567   } else {
 568     is_baseline = TRUE;
 569     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
 570          ci++, compptr++) {
 571       if (compptr->dc_tbl_no > 1 || compptr->ac_tbl_no > 1)
 572         is_baseline = FALSE;
 573     }
 574     if (prec && is_baseline) {
 575       is_baseline = FALSE;
 576       /* If it's baseline except for quantizer size, warn the user */
 577       TRACEMS(cinfo, 0, JTRC_16BIT_TABLES);
 578     }
 579   }
 580 
 581   /* Emit the proper SOF marker */
 582   if (cinfo->arith_code) {
 583     if (cinfo->progressive_mode)
 584       emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
 585     else
 586       emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
 587   } else {
 588     if (cinfo->progressive_mode)
 589       emit_sof(cinfo, M_SOF2);  /* SOF code for progressive Huffman */
 590     else if (is_baseline)
 591       emit_sof(cinfo, M_SOF0);  /* SOF code for baseline implementation */
 592     else
 593       emit_sof(cinfo, M_SOF1);  /* SOF code for non-baseline Huffman file */
 594   }
 595 
 596   /* Check to emit LSE inverse color transform specification marker */
 597   if (cinfo->color_transform)
 598     emit_lse_ict(cinfo);
 599 
 600   /* Check to emit pseudo SOS marker */
 601   if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
 602     emit_pseudo_sos(cinfo);
 603 }
 604 
 605 
 606 /*
 607  * Write scan header.
 608  * This consists of DHT or DAC markers, optional DRI, and SOS.
 609  * Compressed data will be written following the SOS.
 610  */
 611 
 612 METHODDEF(void)
 613 write_scan_header (j_compress_ptr cinfo)
 614 {
 615   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
 616   int i;
 617   jpeg_component_info *compptr;
 618 
 619   if (cinfo->arith_code) {
 620     /* Emit arith conditioning info.  We may have some duplication
 621      * if the file has multiple scans, but it's so small it's hardly
 622      * worth worrying about.
 623      */
 624     emit_dac(cinfo);
 625   } else {
 626     /* Emit Huffman tables.
 627      * Note that emit_dht() suppresses any duplicate tables.
 628      */
 629     for (i = 0; i < cinfo->comps_in_scan; i++) {
 630       compptr = cinfo->cur_comp_info[i];
 631       /* DC needs no table for refinement scan */
 632       if (cinfo->Ss == 0 && cinfo->Ah == 0)


 633         emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
 634       /* AC needs no table when not present */
 635       if (cinfo->Se)
 636         emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
 637     }






 638   }
 639 
 640   /* Emit DRI if required --- note that DRI value could change for each scan.
 641    * We avoid wasting space with unnecessary DRIs, however.
 642    */
 643   if (cinfo->restart_interval != marker->last_restart_interval) {
 644     emit_dri(cinfo);
 645     marker->last_restart_interval = cinfo->restart_interval;
 646   }
 647 
 648   emit_sos(cinfo);
 649 }
 650 
 651 
 652 /*
 653  * Write datastream trailer.
 654  */
 655 
 656 METHODDEF(void)
 657 write_file_trailer (j_compress_ptr cinfo)


 688     }
 689   }
 690 
 691   emit_marker(cinfo, M_EOI);
 692 }
 693 
 694 
 695 /*
 696  * Initialize the marker writer module.
 697  */
 698 
 699 GLOBAL(void)
 700 jinit_marker_writer (j_compress_ptr cinfo)
 701 {
 702   my_marker_ptr marker;
 703 
 704   /* Create the subobject */
 705   marker = (my_marker_ptr)
 706     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
 707                                 SIZEOF(my_marker_writer));
 708   cinfo->marker = &marker->pub;
 709   /* Initialize method pointers */
 710   marker->pub.write_file_header = write_file_header;
 711   marker->pub.write_frame_header = write_frame_header;
 712   marker->pub.write_scan_header = write_scan_header;
 713   marker->pub.write_file_trailer = write_file_trailer;
 714   marker->pub.write_tables_only = write_tables_only;
 715   marker->pub.write_marker_header = write_marker_header;
 716   marker->pub.write_marker_byte = write_marker_byte;
 717   /* Initialize private state */
 718   marker->last_restart_interval = 0;
 719 }
< prev index next >