Skip to main content
GameDev.net gamedev.net
🔒 Locked

iSaveJpeg(jpglib) error

Started by wwinthan Oct 17, 2008 at 10:11 AM 0 replies 1k views
Original Post
wwinthan
wwinthan
I'm trying to convert image to dwf format and then dwf to image format. i can handle the first case but when i'm converting dwf to image, i bump into the following error( 0xC0000005: Access violation reading location 0x0cbe6000.) at the following code. while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; (void) jpeg_write_scanlines(&cinfo, row_pointer, 1); // <
if (0!=_access("C:\\city8.jpg", 0))
	{}        
        struct _stat st;
		if (0!=_stat("C:\\city8.jpg", &st))
		{}
        LPVOID buf = malloc(st.st_size);
        if (buf==NULL)
		{}
     
		FILE *fp = fopen("C:\\city8.jpg", "rb");
        if (st.st_size != (long)fread(buf, 1, st.st_size, fp))
        {
            fclose(fp);
            free(buf);        
        }
        fclose(fp);
	WT_Image image(454,
		367,
		WT_Image::JPEG,
		2,
		NULL,
		st.st_size,
		(WT_Byte*)buf,
		WT_Logical_Point(x,y-504),
		WT_Logical_Point(x+367,y-50),
		WD_False);
	
	
		
	
	image.serialize(file);
	const WT_Byte* buff = image.data();
	WT_Logical_Box boxx= image.bounds();
	double widthh =image.rows();
		//double(boxx.m_max.m_x - boxx.m_min.m_x);
		double heightt = image.columns();
	//double(boxx.m_max.m_y - boxx.m_min.m_y);
	bimage im;
	
	//bool result = iSaveJpegdirect(im,const_cast<unsigned char *>(buff),"C:\\finalimage.jpg");
	bool result = iSaveJpegdirect(im,(unsigned char*)(buff),"C:\\finalimage.jpg");
	free(buf);
//////////////////////////////////
//iSavedirectjpg goes here
template <class T> 
bool iSaveJpegdirect(cBaseImage<T>& image,unsigned char* buffersimage,char * filename)
{
	JSAMPLE* image_buffer;	/* Points to large array of R,G,B-order data */
	int image_height;	/* Number of rows in image */
	int image_width;	
	image_height = 454;
	image_width = 367;
image_buffer = buffersimage;

  struct jpeg_compress_struct cinfo;
  struct jpeg_error_mgr jerr;
  FILE * outfile;		/* target file */
  JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
  int row_stride;		/* physical row width in image buffer */
  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);

  if ((outfile = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "can't open %s\n", filename);
    exit(1);
  }
  jpeg_stdio_dest(&cinfo, outfile);
  cinfo.image_width = image_width; 	/* image width and height, in pixels */
  cinfo.image_height = image_height;
  cinfo.input_components = 3;		/* # of color components per pixel */
  cinfo.in_color_space = JCS_RGB; 	/* colorspace of input image */
  jpeg_set_defaults(&cinfo);
  int quality=200;
  jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
  jpeg_start_compress(&cinfo, TRUE);
  row_stride = image_width * 3;	/* JSAMPLEs per row in image_buffer */
  while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
    (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);  // <<Error here
  }
  jpeg_finish_compress(&cinfo);
  fclose(outfile);
  jpeg_destroy_compress(&cinfo);
  return true;
}

Graz
Graz
The first issue I can see is that you're indexing into the image_buffer array using row_stride, which is the physical offset in bytes between scanlines. This would make your function work for 8-bit images, but not for 24-bit or 32-bit images because the image stride would be too large.
Try using image_width instead of row_stride in the calculation of row_pointer[0]. Change this
row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];

to this
row_pointer[0] = & image_buffer[cinfo.next_scanline * image_width];

Be aware, though, that the image buffer may have padding to make scanlines start on some particular alignment, and that this may not work if your image is not a multiple of that alignment in width.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.