src/3rdparty/libjpeg/jdmarker.c
changeset 30 5dc02b23752f
parent 0 1918ee327afb
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
     1 /*
     1 /*
     2  * jdmarker.c
     2  * jdmarker.c
     3  *
     3  *
     4  * Copyright (C) 1991-1998, Thomas G. Lane.
     4  * Copyright (C) 1991-1998, Thomas G. Lane.
       
     5  * Modified 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 decode JPEG datastream markers.
     9  * This file contains routines to decode JPEG datastream markers.
     9  * Most of the complexity arises from our desire to support input
    10  * Most of the complexity arises from our desire to support input
   232   return TRUE;
   233   return TRUE;
   233 }
   234 }
   234 
   235 
   235 
   236 
   236 LOCAL(boolean)
   237 LOCAL(boolean)
   237 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
   238 get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
       
   239 	 boolean is_arith)
   238 /* Process a SOFn marker */
   240 /* Process a SOFn marker */
   239 {
   241 {
   240   INT32 length;
   242   INT32 length;
   241   int c, ci;
   243   int c, ci;
   242   jpeg_component_info * compptr;
   244   jpeg_component_info * compptr;
   243   INPUT_VARS(cinfo);
   245   INPUT_VARS(cinfo);
   244 
   246 
       
   247   cinfo->is_baseline = is_baseline;
   245   cinfo->progressive_mode = is_prog;
   248   cinfo->progressive_mode = is_prog;
   246   cinfo->arith_code = is_arith;
   249   cinfo->arith_code = is_arith;
   247 
   250 
   248   INPUT_2BYTES(cinfo, length, return FALSE);
   251   INPUT_2BYTES(cinfo, length, return FALSE);
   249 
   252 
   313 
   316 
   314   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
   317   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
   315 
   318 
   316   TRACEMS1(cinfo, 1, JTRC_SOS, n);
   319   TRACEMS1(cinfo, 1, JTRC_SOS, n);
   317 
   320 
   318   if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
   321   if (length != (n * 2 + 6) || n > MAX_COMPS_IN_SCAN ||
       
   322       (n == 0 && !cinfo->progressive_mode))
       
   323       /* pseudo SOS marker only allowed in progressive mode */
   319     ERREXIT(cinfo, JERR_BAD_LENGTH);
   324     ERREXIT(cinfo, JERR_BAD_LENGTH);
   320 
   325 
   321   cinfo->comps_in_scan = n;
   326   cinfo->comps_in_scan = n;
   322 
   327 
   323   /* Collect the component-spec parameters */
   328   /* Collect the component-spec parameters */
   357 	   cinfo->Ah, cinfo->Al);
   362 	   cinfo->Ah, cinfo->Al);
   358 
   363 
   359   /* Prepare to scan data & restart markers */
   364   /* Prepare to scan data & restart markers */
   360   cinfo->marker->next_restart_num = 0;
   365   cinfo->marker->next_restart_num = 0;
   361 
   366 
   362   /* Count another SOS marker */
   367   /* Count another (non-pseudo) SOS marker */
   363   cinfo->input_scan_number++;
   368   if (n) cinfo->input_scan_number++;
   364 
   369 
   365   INPUT_SYNC(cinfo);
   370   INPUT_SYNC(cinfo);
   366   return TRUE;
   371   return TRUE;
   367 }
   372 }
   368 
   373 
   488 
   493 
   489 LOCAL(boolean)
   494 LOCAL(boolean)
   490 get_dqt (j_decompress_ptr cinfo)
   495 get_dqt (j_decompress_ptr cinfo)
   491 /* Process a DQT marker */
   496 /* Process a DQT marker */
   492 {
   497 {
   493   INT32 length;
   498   INT32 length, count, i;
   494   int n, i, prec;
   499   int n, prec;
   495   unsigned int tmp;
   500   unsigned int tmp;
   496   JQUANT_TBL *quant_ptr;
   501   JQUANT_TBL *quant_ptr;
       
   502   const int *natural_order;
   497   INPUT_VARS(cinfo);
   503   INPUT_VARS(cinfo);
   498 
   504 
   499   INPUT_2BYTES(cinfo, length, return FALSE);
   505   INPUT_2BYTES(cinfo, length, return FALSE);
   500   length -= 2;
   506   length -= 2;
   501 
   507 
   502   while (length > 0) {
   508   while (length > 0) {
       
   509     length--;
   503     INPUT_BYTE(cinfo, n, return FALSE);
   510     INPUT_BYTE(cinfo, n, return FALSE);
   504     prec = n >> 4;
   511     prec = n >> 4;
   505     n &= 0x0F;
   512     n &= 0x0F;
   506 
   513 
   507     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
   514     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
   511       
   518       
   512     if (cinfo->quant_tbl_ptrs[n] == NULL)
   519     if (cinfo->quant_tbl_ptrs[n] == NULL)
   513       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   520       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
   514     quant_ptr = cinfo->quant_tbl_ptrs[n];
   521     quant_ptr = cinfo->quant_tbl_ptrs[n];
   515 
   522 
   516     for (i = 0; i < DCTSIZE2; i++) {
   523     if (prec) {
       
   524       if (length < DCTSIZE2 * 2) {
       
   525 	/* Initialize full table for safety. */
       
   526 	for (i = 0; i < DCTSIZE2; i++) {
       
   527 	  quant_ptr->quantval[i] = 1;
       
   528 	}
       
   529 	count = length >> 1;
       
   530       } else
       
   531 	count = DCTSIZE2;
       
   532     } else {
       
   533       if (length < DCTSIZE2) {
       
   534 	/* Initialize full table for safety. */
       
   535 	for (i = 0; i < DCTSIZE2; i++) {
       
   536 	  quant_ptr->quantval[i] = 1;
       
   537 	}
       
   538 	count = length;
       
   539       } else
       
   540 	count = DCTSIZE2;
       
   541     }
       
   542 
       
   543     switch (count) {
       
   544     case (2*2): natural_order = jpeg_natural_order2; break;
       
   545     case (3*3): natural_order = jpeg_natural_order3; break;
       
   546     case (4*4): natural_order = jpeg_natural_order4; break;
       
   547     case (5*5): natural_order = jpeg_natural_order5; break;
       
   548     case (6*6): natural_order = jpeg_natural_order6; break;
       
   549     case (7*7): natural_order = jpeg_natural_order7; break;
       
   550     default:    natural_order = jpeg_natural_order;  break;
       
   551     }
       
   552 
       
   553     for (i = 0; i < count; i++) {
   517       if (prec)
   554       if (prec)
   518 	INPUT_2BYTES(cinfo, tmp, return FALSE);
   555 	INPUT_2BYTES(cinfo, tmp, return FALSE);
   519       else
   556       else
   520 	INPUT_BYTE(cinfo, tmp, return FALSE);
   557 	INPUT_BYTE(cinfo, tmp, return FALSE);
   521       /* We convert the zigzag-order table to natural array order. */
   558       /* We convert the zigzag-order table to natural array order. */
   522       quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
   559       quant_ptr->quantval[natural_order[i]] = (UINT16) tmp;
   523     }
   560     }
   524 
   561 
   525     if (cinfo->err->trace_level >= 2) {
   562     if (cinfo->err->trace_level >= 2) {
   526       for (i = 0; i < DCTSIZE2; i += 8) {
   563       for (i = 0; i < DCTSIZE2; i += 8) {
   527 	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
   564 	TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
   530 		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
   567 		 quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
   531 		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
   568 		 quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
   532       }
   569       }
   533     }
   570     }
   534 
   571 
   535     length -= DCTSIZE2+1;
   572     length -= count;
   536     if (prec) length -= DCTSIZE2;
   573     if (prec) length -= count;
   537   }
   574   }
   538 
   575 
   539   if (length != 0)
   576   if (length != 0)
   540     ERREXIT(cinfo, JERR_BAD_LENGTH);
   577     ERREXIT(cinfo, JERR_BAD_LENGTH);
   541 
   578 
   944 /*
   981 /*
   945  * Read markers until SOS or EOI.
   982  * Read markers until SOS or EOI.
   946  *
   983  *
   947  * Returns same codes as are defined for jpeg_consume_input:
   984  * Returns same codes as are defined for jpeg_consume_input:
   948  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
   985  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
       
   986  *
       
   987  * Note: This function may return a pseudo SOS marker (with zero
       
   988  * component number) for treat by input controller's consume_input.
       
   989  * consume_input itself should filter out (skip) the pseudo marker
       
   990  * after processing for the caller.
   949  */
   991  */
   950 
   992 
   951 METHODDEF(int)
   993 METHODDEF(int)
   952 read_markers (j_decompress_ptr cinfo)
   994 read_markers (j_decompress_ptr cinfo)
   953 {
   995 {
   973       if (! get_soi(cinfo))
  1015       if (! get_soi(cinfo))
   974 	return JPEG_SUSPENDED;
  1016 	return JPEG_SUSPENDED;
   975       break;
  1017       break;
   976 
  1018 
   977     case M_SOF0:		/* Baseline */
  1019     case M_SOF0:		/* Baseline */
       
  1020       if (! get_sof(cinfo, TRUE, FALSE, FALSE))
       
  1021 	return JPEG_SUSPENDED;
       
  1022       break;
       
  1023 
   978     case M_SOF1:		/* Extended sequential, Huffman */
  1024     case M_SOF1:		/* Extended sequential, Huffman */
   979       if (! get_sof(cinfo, FALSE, FALSE))
  1025       if (! get_sof(cinfo, FALSE, FALSE, FALSE))
   980 	return JPEG_SUSPENDED;
  1026 	return JPEG_SUSPENDED;
   981       break;
  1027       break;
   982 
  1028 
   983     case M_SOF2:		/* Progressive, Huffman */
  1029     case M_SOF2:		/* Progressive, Huffman */
   984       if (! get_sof(cinfo, TRUE, FALSE))
  1030       if (! get_sof(cinfo, FALSE, TRUE, FALSE))
   985 	return JPEG_SUSPENDED;
  1031 	return JPEG_SUSPENDED;
   986       break;
  1032       break;
   987 
  1033 
   988     case M_SOF9:		/* Extended sequential, arithmetic */
  1034     case M_SOF9:		/* Extended sequential, arithmetic */
   989       if (! get_sof(cinfo, FALSE, TRUE))
  1035       if (! get_sof(cinfo, FALSE, FALSE, TRUE))
   990 	return JPEG_SUSPENDED;
  1036 	return JPEG_SUSPENDED;
   991       break;
  1037       break;
   992 
  1038 
   993     case M_SOF10:		/* Progressive, arithmetic */
  1039     case M_SOF10:		/* Progressive, arithmetic */
   994       if (! get_sof(cinfo, TRUE, TRUE))
  1040       if (! get_sof(cinfo, FALSE, TRUE, TRUE))
   995 	return JPEG_SUSPENDED;
  1041 	return JPEG_SUSPENDED;
   996       break;
  1042       break;
   997 
  1043 
   998     /* Currently unsupported SOFn types */
  1044     /* Currently unsupported SOFn types */
   999     case M_SOF3:		/* Lossless, Huffman */
  1045     case M_SOF3:		/* Lossless, Huffman */