Beispiel #1
0
static int Decode_JPEG(unsigned char *inBuffer, unsigned char *outBuffer, unsigned int width, unsigned int height, int bufsize)
{
	int rowstride;
	unsigned int y;
	struct jpeg_decompress_struct dinfo;
	struct jpeg_error_mgr jerr;
	
	(void)width; /* unused */

	numbytes = 0;

	dinfo.err = jpeg_std_error(&jerr);
	jpeg_create_decompress(&dinfo);
	jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize);
	jpeg_read_header(&dinfo, TRUE);
	if (dinfo.dc_huff_tbl_ptrs[0] == NULL) {
		std_huff_tables(&dinfo);
	}
	dinfo.out_color_space = JCS_RGB;
	dinfo.dct_method = JDCT_IFAST;

	jpeg_start_decompress(&dinfo);

	rowstride = dinfo.output_width * dinfo.output_components;
	for (y = 0; y < dinfo.output_height; y++) {
		jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1);
		outBuffer += rowstride;
	}
	jpeg_finish_decompress(&dinfo);

	if (dinfo.output_height >= height) return 0;
	
	inBuffer += numbytes;
	jpegmemsrcmgr_build(&dinfo, inBuffer, bufsize - numbytes);

	numbytes = 0;
	jpeg_read_header(&dinfo, TRUE);
	if (dinfo.dc_huff_tbl_ptrs[0] == NULL) {
		std_huff_tables(&dinfo);
	}

	jpeg_start_decompress(&dinfo);
	rowstride = dinfo.output_width * dinfo.output_components;
	for (y = 0; y < dinfo.output_height; y++) {
		jpeg_read_scanlines(&dinfo, (JSAMPARRAY) &outBuffer, 1);
		outBuffer += rowstride;
	}
	jpeg_finish_decompress(&dinfo);
	jpeg_destroy_decompress(&dinfo);
	
	return 1;
}
Beispiel #2
0
static void guarantee_huff_tables(j_decompress_ptr dinfo)
{
    if ((dinfo->dc_huff_tbl_ptrs[0] == NULL) && (dinfo->dc_huff_tbl_ptrs[1] == NULL) &&
        (dinfo->ac_huff_tbl_ptrs[0] == NULL) && (dinfo->ac_huff_tbl_ptrs[1] == NULL))
    {
        std_huff_tables(dinfo);
    }
}
Beispiel #3
0
static void guarantee_huff_tables(j_decompress_ptr dinfo)
{
    if ( (dinfo->dc_huff_tbl_ptrs[0] == NULL) &&
            (dinfo->dc_huff_tbl_ptrs[1] == NULL) &&
            (dinfo->ac_huff_tbl_ptrs[0] == NULL) &&
            (dinfo->ac_huff_tbl_ptrs[1] == NULL) ) {
        //    mjpeg_debug( "Generating standard Huffman tables for this frame.");
        std_huff_tables(dinfo);
    }
}
Beispiel #4
0
int main()
{
  // make sure twofivesix becomes wild
  int **x = twofivesix;     // interpret first 4 bytes as pointer
  int *y = *x;              // read that pointer
  // (don't use it though)

  std_huff_tables();
  return 0;
}
Beispiel #5
0
jpeg_set_defaults (j_compress_ptr cinfo)
{
  int i;

  /* Safety check to ensure start_compress not called yet. */
  if (cinfo->global_state != CSTATE_START)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  /* Allocate comp_info array large enough for maximum component count.
   * Array is made permanent in case application wants to compress
   * multiple images at same param settings.
   */
  if (cinfo->comp_info == NULL)
    cinfo->comp_info = (jpeg_component_info *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));

  /* Initialize everything not dependent on the color space */

#if JPEG_LIB_VERSION >= 70
  cinfo->scale_num = 1;		/* 1:1 scaling */
  cinfo->scale_denom = 1;
#endif
  cinfo->data_precision = BITS_IN_JSAMPLE;
  /* Set up two quantization tables using default quality of 75 */
  jpeg_set_quality(cinfo, 75, TRUE);
  /* Set up two Huffman tables */
  std_huff_tables(cinfo);

  /* Initialize default arithmetic coding conditioning */
  for (i = 0; i < NUM_ARITH_TBLS; i++) {
    cinfo->arith_dc_L[i] = 0;
    cinfo->arith_dc_U[i] = 1;
    cinfo->arith_ac_K[i] = 5;
  }

  /* Default is no multiple-scan output */
  cinfo->scan_info = NULL;
  cinfo->num_scans = 0;

  /* Expect normal source image, not raw downsampled data */
  cinfo->raw_data_in = FALSE;

  /* Use Huffman coding, not arithmetic coding, by default */
  cinfo->arith_code = FALSE;

  /* By default, don't do extra passes to optimize entropy coding */
  cinfo->optimize_coding = FALSE;
  /* The standard Huffman tables are only valid for 8-bit data precision.
   * If the precision is higher, force optimization on so that usable
   * tables will be computed.  This test can be removed if default tables
   * are supplied that are valid for the desired precision.
   */
  if (cinfo->data_precision > 8)
    cinfo->optimize_coding = TRUE;

  /* By default, use the simpler non-cosited sampling alignment */
  cinfo->CCIR601_sampling = FALSE;

#if JPEG_LIB_VERSION >= 70
  /* By default, apply fancy downsampling */
  cinfo->do_fancy_downsampling = TRUE;
#endif

  /* No input smoothing */
  cinfo->smoothing_factor = 0;

  /* DCT algorithm preference */
  cinfo->dct_method = JDCT_DEFAULT;

  /* No restart markers */
  cinfo->restart_interval = 0;
  cinfo->restart_in_rows = 0;

  /* Fill in default JFIF marker parameters.  Note that whether the marker
   * will actually be written is determined by jpeg_set_colorspace.
   *
   * By default, the library emits JFIF version code 1.01.
   * An application that wants to emit JFIF 1.02 extension markers should set
   * JFIF_minor_version to 2.  We could probably get away with just defaulting
   * to 1.02, but there may still be some decoders in use that will complain
   * about that; saying 1.01 should minimize compatibility problems.
   */
  cinfo->JFIF_major_version = 1; /* Default JFIF version = 1.01 */
  cinfo->JFIF_minor_version = 1;
  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  cinfo->Y_density = 1;

  /* Choose JPEG colorspace based on input space, set defaults accordingly */

  jpeg_default_colorspace(cinfo);
}
Beispiel #6
0
/* TODO: the whole purpose of fastforward_jpeg_stream_state(),
   (as well as add_huff_table()/add_huff_tables() above), is to wind
   the state of the jpeg stream forward, since the incoming
   data is headerless. On hide-sight, it might be cleaner
   and more future-proof against changes in libjpeg
   to construct a made-up header, read it then switch data stream. */
static void
fastforward_jpeg_stream_state(jpeg_decompress_data * jddp,
			      stream_DCT_state * ss, px_state_t * pxs)
{
    px_vendor_state_t *v_state = pxs->vendor_state;
    j_decompress_ptr cinfo = &jddp->dinfo;
    int i;
    int j, qcount = 0;
    jpeg_component_info *compptr;
    int factor = ((v_state->color_space == eGraySub) ? 8 : 1);
    extern const int jpeg_natural_order[];

    if (cinfo->comp_info == NULL)
	cinfo->comp_info = (jpeg_component_info *)
	    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo,
					JPOOL_PERMANENT,
					MAX_COMPONENTS *
					sizeof(jpeg_component_info));
    /* default_decompress_parms() starts */
    if (v_state->color_space == eGraySub) {
	cinfo->jpeg_color_space = JCS_GRAYSCALE;
	cinfo->out_color_space = JCS_GRAYSCALE;
    } else {
	cinfo->jpeg_color_space = JCS_YCbCr;
	cinfo->out_color_space = JCS_YCbCr;
    }
    cinfo->scale_num = 8;
    cinfo->scale_denom = 8;
    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;
    /* */
    cinfo->dither_mode = JDITHER_FS;
    /* */
    cinfo->two_pass_quantize = 1;
    /* */
    cinfo->desired_number_of_colors = 256;
    /* */
    cinfo->enable_1pass_quant = 0;
    cinfo->enable_external_quant = 0;
    cinfo->enable_2pass_quant = FALSE;
    /* default_decompress_parms() ends */
    std_huff_tables(cinfo);
    /* get_soi begins */
    for (i = 0; i < NUM_ARITH_TBLS; i++) {
	cinfo->arith_dc_L[i] = 0;
	cinfo->arith_dc_U[i] = 1;
	cinfo->arith_ac_K[i] = 5;
    }
    cinfo->restart_interval = 0;
    cinfo->CCIR601_sampling = FALSE;
    /* */
    cinfo->JFIF_major_version = 1;	/* Default JFIF version = 1.01 */
    cinfo->JFIF_minor_version = 1;
    cinfo->density_unit = 0;	/* Pixel size is unknown by default */
    cinfo->X_density = 1;	/* Pixel aspect ratio is square by default */
    cinfo->Y_density = 1;
    /* get_soi ends */
    /* get_sof */
    cinfo->is_baseline = 1;
    cinfo->progressive_mode = FALSE;
    cinfo->arith_code = FALSE;
    cinfo->data_precision = BITS_IN_JSAMPLE;
    if (v_state->color_space == eGraySub)
	cinfo->num_components = 1;
    else
	cinfo->num_components = 3;

    cinfo->image_width = v_state->SourceWidth;
    cinfo->image_height = v_state->BlockHeight;

    for (i = 0; i < cinfo->num_components; i++) {
	if (cinfo->quant_tbl_ptrs[i] == NULL)
	    cinfo->quant_tbl_ptrs[i] =
		jpeg_alloc_quant_table((j_common_ptr) cinfo);
	for (j = 0; j < 64; j++)
	    cinfo->quant_tbl_ptrs[i]->quantval[j] =
		v_state->qvalues[qcount++];
    }

#define SET_COMP(index,id,hsamp,vsamp,quant,dctbl,actbl)  \
        (compptr = &cinfo->comp_info[index], \
         compptr->component_id = (id), \
         compptr->h_samp_factor = (hsamp), \
         compptr->v_samp_factor = (vsamp), \
         compptr->quant_tbl_no = (quant), \
         compptr->dc_tbl_no = (dctbl), \
         compptr->ac_tbl_no = (actbl), \
         compptr->DCT_h_scaled_size = 8, \
         compptr->DCT_v_scaled_size = 8, \
         compptr->width_in_blocks = cinfo->image_width/factor, \
         compptr->height_in_blocks = cinfo->image_height/factor, \
         compptr->quant_table = cinfo->quant_tbl_ptrs[index], \
         compptr->component_index = index, \
         compptr->component_needed = TRUE, \
         compptr->downsampled_width = cinfo->image_width, \
         compptr->downsampled_height = cinfo->image_height)

    SET_COMP(0, 1, 1, 1, 0, 0, 0);	/* not default! */
    if (v_state->color_space != eGraySub) {
	SET_COMP(1, 2, 1, 1, 1, 1, 1);
	SET_COMP(2, 3, 1, 1, 1, 1, 1);
    }
    /* end_sof */
    cinfo->rec_outbuf_height = 1;

    if (v_state->color_space == eGraySub) {
	cinfo->out_color_components = 1;
	cinfo->output_components = 1;
    } else {
	cinfo->out_color_components = 3;
	cinfo->output_components = 3;
    }
    cinfo->global_state = DSTATE_READY;
    /* no SOS markers, so we just do this by hand - mostly from get_sos() */
    cinfo->cur_comp_info[0] = &cinfo->comp_info[0];
    cinfo->comp_info[0].dc_tbl_no = 0;
    cinfo->comp_info[0].ac_tbl_no = 0;
    if (v_state->color_space != eGraySub) {
	cinfo->cur_comp_info[1] = &cinfo->comp_info[1];
	cinfo->comp_info[1].dc_tbl_no = 1;
	cinfo->comp_info[1].ac_tbl_no = 1;
	cinfo->cur_comp_info[2] = &cinfo->comp_info[2];
	cinfo->comp_info[2].dc_tbl_no = 1;
	cinfo->comp_info[2].ac_tbl_no = 1;
    }
    cinfo->Ss = 0;
    cinfo->Se = 63;
    cinfo->Ah = 0;
    cinfo->Al = 0;
    /* initial_setup() */
    cinfo->input_scan_number = 1;
    cinfo->block_size = DCTSIZE;

    cinfo->natural_order = jpeg_natural_order;
    cinfo->lim_Se = DCTSIZE2 - 1;
    cinfo->min_DCT_h_scaled_size = 8;
    cinfo->min_DCT_v_scaled_size = 8;
    cinfo->max_h_samp_factor = 1;
    cinfo->max_v_samp_factor = 1;
    if (v_state->color_space == eGraySub)
	cinfo->comps_in_scan = 1;
    else
	cinfo->comps_in_scan = 3;

    /* */
    jpeg_core_output_dimensions(cinfo);
    cinfo->total_iMCU_rows = 16;

    ss->phase = 2;		/* fast forward to phase 2, on to start_decompress */
}
Beispiel #7
0
jpeg_set_defaults (j_compress_ptr cinfo)
{
  int i;
__boundcheck_metadata_store((void *)(&i),(void *)((size_t)(&i)+sizeof(i)*8-1));


  /* Safety check to ensure start_compress not called yet. */
  if (cinfo->global_state != CSTATE_START)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  /* Allocate comp_info array large enough for maximum component count.
   * Array is made permanent in case application wants to compress
   * multiple images at same param settings.
   */
  if (cinfo->comp_info == NULL)
    cinfo->comp_info = (jpeg_component_info *)
      (*(void *(*)(j_common_ptr, int, size_t))(__boundcheck_ptr_reference(263,21,"jpeg_set_defaults",(void *)(cinfo->mem->alloc_small),(void *)cinfo->mem->alloc_small))) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));

  /* Initialize everything not dependent on the color space */

  cinfo->data_precision = BITS_IN_JSAMPLE;
  /* Set up two quantization tables using default quality of 75 */
  jpeg_set_quality(cinfo, 75, TRUE);
  /* Set up two Huffman tables */
  std_huff_tables(cinfo);

  /* Initialize default arithmetic coding conditioning */
  for (i = 0; i < NUM_ARITH_TBLS; i++) {
    cinfo->arith_dc_L[_RV_insert_check(0,16,276,5,"jpeg_set_defaults",i)] = 0;
    cinfo->arith_dc_U[_RV_insert_check(0,16,277,5,"jpeg_set_defaults",i)] = 1;
    cinfo->arith_ac_K[_RV_insert_check(0,16,278,5,"jpeg_set_defaults",i)] = 5;
  }

  /* Default is no multiple-scan output */
  cinfo->scan_info = NULL;
  cinfo->num_scans = 0;

  /* Expect normal source image, not raw downsampled data */
  cinfo->raw_data_in = FALSE;

  /* Use Huffman coding, not arithmetic coding, by default */
  cinfo->arith_code = FALSE;

  /* By default, don't do extra passes to optimize entropy coding */
  cinfo->optimize_coding = FALSE;
  /* The standard Huffman tables are only valid for 8-bit data precision.
   * If the precision is higher, force optimization on so that usable
   * tables will be computed.  This test can be removed if default tables
   * are supplied that are valid for the desired precision.
   */
  if (cinfo->data_precision > 8)
    cinfo->optimize_coding = TRUE;

  /* By default, use the simpler non-cosited sampling alignment */
  cinfo->CCIR601_sampling = FALSE;

  /* No input smoothing */
  cinfo->smoothing_factor = 0;

  /* DCT algorithm preference */
  cinfo->dct_method = JDCT_DEFAULT;

  /* No restart markers */
  cinfo->restart_interval = 0;
  cinfo->restart_in_rows = 0;

  /* Fill in default JFIF marker parameters.  Note that whether the marker
   * will actually be written is determined by jpeg_set_colorspace.
   */
  cinfo->density_unit = 0;	/* Pixel size is unknown by default */
  cinfo->X_density = 1;		/* Pixel aspect ratio is square by default */
  cinfo->Y_density = 1;

  /* Choose JPEG colorspace based on input space, set defaults accordingly */

  jpeg_default_colorspace(cinfo);
}
Beispiel #8
0
jpeg_set_defaults (j_compress_ptr cinfo)
{
  int i;

  
  if (cinfo->global_state != CSTATE_START)
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);

  if (cinfo->comp_info == NULL)
    cinfo->comp_info = (jpeg_component_info *)
      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
				  MAX_COMPONENTS * SIZEOF(jpeg_component_info));

  

  cinfo->data_precision = BITS_IN_JSAMPLE;
  
  jpeg_set_quality(cinfo, 75, TRUE);
  
  std_huff_tables(cinfo);

  
  for (i = 0; i < NUM_ARITH_TBLS; i++) {
    cinfo->arith_dc_L[i] = 0;
    cinfo->arith_dc_U[i] = 1;
    cinfo->arith_ac_K[i] = 5;
  }

  
  cinfo->scan_info = NULL;
  cinfo->num_scans = 0;

  
  cinfo->raw_data_in = FALSE;

  
  cinfo->arith_code = FALSE;

  
  cinfo->optimize_coding = FALSE;
  if (cinfo->data_precision > 8)
    cinfo->optimize_coding = TRUE;

  
  cinfo->CCIR601_sampling = FALSE;

  
  cinfo->smoothing_factor = 0;

  
  cinfo->dct_method = JDCT_DEFAULT;

  
  cinfo->restart_interval = 0;
  cinfo->restart_in_rows = 0;

  /* Fill in default JFIF marker parameters.  Note that whether the marker
   * will actually be written is determined by jpeg_set_colorspace.
   *
   * By default, the library emits JFIF version code 1.01.
   * An application that wants to emit JFIF 1.02 extension markers should set
   * JFIF_minor_version to 2.  We could probably get away with just defaulting
   * to 1.02, but there may still be some decoders in use that will complain
   * about that; saying 1.01 should minimize compatibility problems.
   */
  cinfo->JFIF_major_version = 1; 
  cinfo->JFIF_minor_version = 1;
  cinfo->density_unit = 0;	
  cinfo->X_density = 1;		
  cinfo->Y_density = 1;

  

  jpeg_default_colorspace(cinfo);
}