src/3rdparty/libjpeg/jcmarker.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 /*
     1 /*
     2  * jcmarker.c
     2  * jcmarker.c
     3  *
     3  *
     4  * Copyright (C) 1991-1998, Thomas G. Lane.
     4  * Copyright (C) 1991-1998, Thomas G. Lane.
       
     5  * Modified 2003-2009 by Guido Vollbeding.
     5  * This file is part of the Independent JPEG Group's software.
     6  * This file is part of the Independent JPEG Group's software.
     6  * For conditions of distribution and use, see the accompanying README file.
     7  * For conditions of distribution and use, see the accompanying README file.
     7  *
     8  *
     8  * This file contains routines to write JPEG datastream markers.
     9  * This file contains routines to write JPEG datastream markers.
     9  */
    10  */
   151 
   152 
   152   if (qtbl == NULL)
   153   if (qtbl == NULL)
   153     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
   154     ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, index);
   154 
   155 
   155   prec = 0;
   156   prec = 0;
   156   for (i = 0; i < DCTSIZE2; i++) {
   157   for (i = 0; i <= cinfo->lim_Se; i++) {
   157     if (qtbl->quantval[i] > 255)
   158     if (qtbl->quantval[cinfo->natural_order[i]] > 255)
   158       prec = 1;
   159       prec = 1;
   159   }
   160   }
   160 
   161 
   161   if (! qtbl->sent_table) {
   162   if (! qtbl->sent_table) {
   162     emit_marker(cinfo, M_DQT);
   163     emit_marker(cinfo, M_DQT);
   163 
   164 
   164     emit_2bytes(cinfo, prec ? DCTSIZE2*2 + 1 + 2 : DCTSIZE2 + 1 + 2);
   165     emit_2bytes(cinfo,
       
   166       prec ? cinfo->lim_Se * 2 + 2 + 1 + 2 : cinfo->lim_Se + 1 + 1 + 2);
   165 
   167 
   166     emit_byte(cinfo, index + (prec<<4));
   168     emit_byte(cinfo, index + (prec<<4));
   167 
   169 
   168     for (i = 0; i < DCTSIZE2; i++) {
   170     for (i = 0; i <= cinfo->lim_Se; i++) {
   169       /* The table entries must be emitted in zigzag order. */
   171       /* The table entries must be emitted in zigzag order. */
   170       unsigned int qval = qtbl->quantval[jpeg_natural_order[i]];
   172       unsigned int qval = qtbl->quantval[cinfo->natural_order[i]];
   171       if (prec)
   173       if (prec)
   172 	emit_byte(cinfo, (int) (qval >> 8));
   174 	emit_byte(cinfo, (int) (qval >> 8));
   173       emit_byte(cinfo, (int) (qval & 0xFF));
   175       emit_byte(cinfo, (int) (qval & 0xFF));
   174     }
   176     }
   175 
   177 
   233   for (i = 0; i < NUM_ARITH_TBLS; i++)
   235   for (i = 0; i < NUM_ARITH_TBLS; i++)
   234     dc_in_use[i] = ac_in_use[i] = 0;
   236     dc_in_use[i] = ac_in_use[i] = 0;
   235   
   237   
   236   for (i = 0; i < cinfo->comps_in_scan; i++) {
   238   for (i = 0; i < cinfo->comps_in_scan; i++) {
   237     compptr = cinfo->cur_comp_info[i];
   239     compptr = cinfo->cur_comp_info[i];
   238     dc_in_use[compptr->dc_tbl_no] = 1;
   240     /* DC needs no table for refinement scan */
   239     ac_in_use[compptr->ac_tbl_no] = 1;
   241     if (cinfo->Ss == 0 && cinfo->Ah == 0)
       
   242       dc_in_use[compptr->dc_tbl_no] = 1;
       
   243     /* AC needs no table when not present */
       
   244     if (cinfo->Se)
       
   245       ac_in_use[compptr->ac_tbl_no] = 1;
   240   }
   246   }
   241   
   247   
   242   length = 0;
   248   length = 0;
   243   for (i = 0; i < NUM_ARITH_TBLS; i++)
   249   for (i = 0; i < NUM_ARITH_TBLS; i++)
   244     length += dc_in_use[i] + ac_in_use[i];
   250     length += dc_in_use[i] + ac_in_use[i];
   283   emit_marker(cinfo, code);
   289   emit_marker(cinfo, code);
   284   
   290   
   285   emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
   291   emit_2bytes(cinfo, 3 * cinfo->num_components + 2 + 5 + 1); /* length */
   286 
   292 
   287   /* Make sure image isn't bigger than SOF field can handle */
   293   /* Make sure image isn't bigger than SOF field can handle */
   288   if ((long) cinfo->image_height > 65535L ||
   294   if ((long) cinfo->jpeg_height > 65535L ||
   289       (long) cinfo->image_width > 65535L)
   295       (long) cinfo->jpeg_width > 65535L)
   290     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
   296     ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) 65535);
   291 
   297 
   292   emit_byte(cinfo, cinfo->data_precision);
   298   emit_byte(cinfo, cinfo->data_precision);
   293   emit_2bytes(cinfo, (int) cinfo->image_height);
   299   emit_2bytes(cinfo, (int) cinfo->jpeg_height);
   294   emit_2bytes(cinfo, (int) cinfo->image_width);
   300   emit_2bytes(cinfo, (int) cinfo->jpeg_width);
   295 
   301 
   296   emit_byte(cinfo, cinfo->num_components);
   302   emit_byte(cinfo, cinfo->num_components);
   297 
   303 
   298   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   304   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   299        ci++, compptr++) {
   305        ci++, compptr++) {
   318   emit_byte(cinfo, cinfo->comps_in_scan);
   324   emit_byte(cinfo, cinfo->comps_in_scan);
   319   
   325   
   320   for (i = 0; i < cinfo->comps_in_scan; i++) {
   326   for (i = 0; i < cinfo->comps_in_scan; i++) {
   321     compptr = cinfo->cur_comp_info[i];
   327     compptr = cinfo->cur_comp_info[i];
   322     emit_byte(cinfo, compptr->component_id);
   328     emit_byte(cinfo, compptr->component_id);
   323     td = compptr->dc_tbl_no;
   329 
   324     ta = compptr->ac_tbl_no;
   330     /* We emit 0 for unused field(s); this is recommended by the P&M text
   325     if (cinfo->progressive_mode) {
   331      * but does not seem to be specified in the standard.
   326       /* Progressive mode: only DC or only AC tables are used in one scan;
   332      */
   327        * furthermore, Huffman coding of DC refinement uses no table at all.
   333 
   328        * We emit 0 for unused field(s); this is recommended by the P&M text
   334     /* DC needs no table for refinement scan */
   329        * but does not seem to be specified in the standard.
   335     td = cinfo->Ss == 0 && cinfo->Ah == 0 ? compptr->dc_tbl_no : 0;
   330        */
   336     /* AC needs no table when not present */
   331       if (cinfo->Ss == 0) {
   337     ta = cinfo->Se ? compptr->ac_tbl_no : 0;
   332 	ta = 0;			/* DC scan */
   338 
   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);
   339     emit_byte(cinfo, (td << 4) + ta);
   340   }
   340   }
   341 
   341 
   342   emit_byte(cinfo, cinfo->Ss);
   342   emit_byte(cinfo, cinfo->Ss);
   343   emit_byte(cinfo, cinfo->Se);
   343   emit_byte(cinfo, cinfo->Se);
   344   emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
   344   emit_byte(cinfo, (cinfo->Ah << 4) + cinfo->Al);
       
   345 }
       
   346 
       
   347 
       
   348 LOCAL(void)
       
   349 emit_pseudo_sos (j_compress_ptr cinfo)
       
   350 /* Emit a pseudo SOS marker */
       
   351 {
       
   352   emit_marker(cinfo, M_SOS);
       
   353   
       
   354   emit_2bytes(cinfo, 2 + 1 + 3); /* length */
       
   355   
       
   356   emit_byte(cinfo, 0); /* Ns */
       
   357 
       
   358   emit_byte(cinfo, 0); /* Ss */
       
   359   emit_byte(cinfo, cinfo->block_size * cinfo->block_size - 1); /* Se */
       
   360   emit_byte(cinfo, 0); /* Ah/Al */
   345 }
   361 }
   346 
   362 
   347 
   363 
   348 LOCAL(void)
   364 LOCAL(void)
   349 emit_jfif_app0 (j_compress_ptr cinfo)
   365 emit_jfif_app0 (j_compress_ptr cinfo)
   482 }
   498 }
   483 
   499 
   484 
   500 
   485 /*
   501 /*
   486  * Write frame header.
   502  * Write frame header.
   487  * This consists of DQT and SOFn markers.
   503  * This consists of DQT and SOFn markers, and a conditional pseudo SOS marker.
   488  * Note that we do not emit the SOF until we have emitted the DQT(s).
   504  * Note that we do not emit the SOF until we have emitted the DQT(s).
   489  * This avoids compatibility problems with incorrect implementations that
   505  * This avoids compatibility problems with incorrect implementations that
   490  * try to error-check the quant table numbers as soon as they see the SOF.
   506  * try to error-check the quant table numbers as soon as they see the SOF.
   491  */
   507  */
   492 
   508 
   509 
   525 
   510   /* Check for a non-baseline specification.
   526   /* Check for a non-baseline specification.
   511    * Note we assume that Huffman table numbers won't be changed later.
   527    * Note we assume that Huffman table numbers won't be changed later.
   512    */
   528    */
   513   if (cinfo->arith_code || cinfo->progressive_mode ||
   529   if (cinfo->arith_code || cinfo->progressive_mode ||
   514       cinfo->data_precision != 8) {
   530       cinfo->data_precision != 8 || cinfo->block_size != DCTSIZE) {
   515     is_baseline = FALSE;
   531     is_baseline = FALSE;
   516   } else {
   532   } else {
   517     is_baseline = TRUE;
   533     is_baseline = TRUE;
   518     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   534     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   519 	 ci++, compptr++) {
   535 	 ci++, compptr++) {
   527     }
   543     }
   528   }
   544   }
   529 
   545 
   530   /* Emit the proper SOF marker */
   546   /* Emit the proper SOF marker */
   531   if (cinfo->arith_code) {
   547   if (cinfo->arith_code) {
   532     emit_sof(cinfo, M_SOF9);	/* SOF code for arithmetic coding */
   548     if (cinfo->progressive_mode)
       
   549       emit_sof(cinfo, M_SOF10); /* SOF code for progressive arithmetic */
       
   550     else
       
   551       emit_sof(cinfo, M_SOF9);  /* SOF code for sequential arithmetic */
   533   } else {
   552   } else {
   534     if (cinfo->progressive_mode)
   553     if (cinfo->progressive_mode)
   535       emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
   554       emit_sof(cinfo, M_SOF2);	/* SOF code for progressive Huffman */
   536     else if (is_baseline)
   555     else if (is_baseline)
   537       emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
   556       emit_sof(cinfo, M_SOF0);	/* SOF code for baseline implementation */
   538     else
   557     else
   539       emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
   558       emit_sof(cinfo, M_SOF1);	/* SOF code for non-baseline Huffman file */
   540   }
   559   }
       
   560 
       
   561   /* Check to emit pseudo SOS marker */
       
   562   if (cinfo->progressive_mode && cinfo->block_size != DCTSIZE)
       
   563     emit_pseudo_sos(cinfo);
   541 }
   564 }
   542 
   565 
   543 
   566 
   544 /*
   567 /*
   545  * Write scan header.
   568  * Write scan header.
   564     /* Emit Huffman tables.
   587     /* Emit Huffman tables.
   565      * Note that emit_dht() suppresses any duplicate tables.
   588      * Note that emit_dht() suppresses any duplicate tables.
   566      */
   589      */
   567     for (i = 0; i < cinfo->comps_in_scan; i++) {
   590     for (i = 0; i < cinfo->comps_in_scan; i++) {
   568       compptr = cinfo->cur_comp_info[i];
   591       compptr = cinfo->cur_comp_info[i];
   569       if (cinfo->progressive_mode) {
   592       /* DC needs no table for refinement scan */
   570 	/* Progressive mode: only DC or only AC tables are used in one scan */
   593       if (cinfo->Ss == 0 && cinfo->Ah == 0)
   571 	if (cinfo->Ss == 0) {
       
   572 	  if (cinfo->Ah == 0)	/* DC needs no table for refinement scan */
       
   573 	    emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
       
   574 	} else {
       
   575 	  emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
       
   576 	}
       
   577       } else {
       
   578 	/* Sequential mode: need both DC and AC tables */
       
   579 	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
   594 	emit_dht(cinfo, compptr->dc_tbl_no, FALSE);
       
   595       /* AC needs no table when not present */
       
   596       if (cinfo->Se)
   580 	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
   597 	emit_dht(cinfo, compptr->ac_tbl_no, TRUE);
   581       }
       
   582     }
   598     }
   583   }
   599   }
   584 
   600 
   585   /* Emit DRI if required --- note that DRI value could change for each scan.
   601   /* Emit DRI if required --- note that DRI value could change for each scan.
   586    * We avoid wasting space with unnecessary DRIs, however.
   602    * We avoid wasting space with unnecessary DRIs, however.