mesa3d buffer uncompatible with jpg & my Baseimage buffer..help!

Started by
11 comments, last by Ravyne 15 years, 5 months ago
i use opengl and mesa3d.org to render the 2d drawing offscreen and get the buffer. again i can successfully save buffer to iSaveJpegdirect by using jpglib but i need to convert the mesa3d offscreen rendered buffer to our own cImage object. and there is uncompatible with mesa3d buffer and our own cImage class(object) buffer. help! please cImage image; image.Create(buffer8,4000,4000,24); iSaveJpeg(image,"C:\\jpgimage.jpg"); << //this one give me blank page (help!) iSaveJpegdirect(buffer8,"C:\\jpgimagedirect.jpg"); << this one is working and get the image i want cImage class can be found in the following link. Download BaseImage.h from Uploading.com Download BaseImage.cpp from Uploading.com ///////////////////// iSaveJpeg & iSaveJpegdirect are 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 = 4000;
	image_width = 4000;
	//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 */</span>
  <span class="cpp-keyword">int</span> row_stride;		<span class="cpp-comment">/* physical row width in image buffer */</span>
  cinfo.err = jpeg_std_error(&amp;jerr);
  jpeg_create_compress(&amp;cinfo);

  <span class="cpp-keyword">if</span> ((outfile = fopen(filename, <span class="cpp-literal">"wb"</span>)) == NULL) {
    fprintf(stderr, <span class="cpp-literal">"can't open %s\n"</span>, filename);
    exit(<span class="cpp-number">1</span>);
  }
  jpeg_stdio_dest(&amp;cinfo, outfile);
  cinfo.image_width = image_width; 	<span class="cpp-comment">/* image width and height, in pixels */</span>
  cinfo.image_height = image_height;
  cinfo.input_components = <span class="cpp-number">3</span>;		<span class="cpp-comment">/* # of color components per pixel */</span>
  cinfo.in_color_space = JCS_RGB; 	<span class="cpp-comment">/* colorspace of input image */</span>
  jpeg_set_defaults(&amp;cinfo);
  <span class="cpp-keyword">int</span> quality=<span class="cpp-number">200</span>;
  jpeg_set_quality(&amp;cinfo, quality, <span class="cpp-keyword">TRUE</span> <span class="cpp-comment">/* limit to baseline-JPEG values */</span>);
  jpeg_start_compress(&amp;cinfo, <span class="cpp-keyword">TRUE</span>);
  row_stride = image_width * <span class="cpp-number">3</span>;	<span class="cpp-comment">/* JSAMPLEs per row in image_buffer */</span>
  <span class="cpp-keyword">while</span> (cinfo.next_scanline &lt; cinfo.image_height) {
    row_pointer[<span class="cpp-number">0</span>] = &amp; image_buffer[cinfo.next_scanline * row_stride];
    (<span class="cpp-keyword">void</span>) jpeg_write_scanlines(&amp;cinfo, row_pointer, <span class="cpp-number">1</span>);
  }
  jpeg_finish_compress(&amp;cinfo);
  fclose(outfile);
  jpeg_destroy_compress(&amp;cinfo);
  <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;
}
<span class="cpp-comment">// It returns true, so that it can be used in ASSERT(Save()) for debugging</span>
<span class="cpp-keyword">template</span> &lt;<span class="cpp-keyword">class</span> T&gt; 
<span class="cpp-keyword">bool</span> iSaveJpeg(cBaseImage&lt;T&gt;&amp; image, <span class="cpp-keyword">const</span> string&amp; imageFile)
{
<span class="cpp-comment">//	if (imageFile.find(".tif")!=string::npos) return iSaveTiff(image, imageFile);</span>
<span class="cpp-comment">//	ofstream s(imageFile.c_str(),ios::out|ios::binary);</span>
<span class="cpp-comment">//	s &lt;&lt; image;</span>
<span class="cpp-comment">//	s.close();</span>
<span class="cpp-comment">//	return true;</span>
  <span class="cpp-comment">/* This struct contains the JPEG compression parameters and pointers to
   * working space (which is allocated as needed by the JPEG library).
   * It is possible to have several such structures, representing multiple
   * compression/decompression processes, in existence at once.  We refer
   * to any one struct (and its associated working data) as a "JPEG object".
   */</span>
  <span class="cpp-keyword">struct</span> jpeg_compress_struct cinfo;
  <span class="cpp-comment">/* This struct represents a JPEG error handler.  It is declared separately
   * because applications often want to supply a specialized error handler
   * (see the second half of this file for an example).  But here we just
   * take the easy way out and use the standard error handler, which will
   * print a message on stderr and call exit() if compression fails.
   * Note that this struct must live as long as the main JPEG parameter
   * struct, to avoid dangling-pointer problems.
   */</span>
  <span class="cpp-keyword">struct</span> jpeg_error_mgr jerr;
  <span class="cpp-comment">/* More stuff */</span>
  FILE * outfile;		<span class="cpp-comment">/* target file */</span>
  JSAMPROW row_pointer[<span class="cpp-number">1</span>];	<span class="cpp-comment">/* pointer to JSAMPLE row */</span>
  <span class="cpp-keyword">int</span> row_stride;		<span class="cpp-comment">/* physical row width in image buffer */</span>

  <span class="cpp-comment">/* Step 1: allocate and initialize JPEG compression object */</span>

  <span class="cpp-comment">/* We have to set up the error handler first, in case the initialization
   * step fails.  (Unlikely, but it could happen if you are out of memory.)
   * This routine fills in the contents of struct jerr, and returns jerr's
   * address which we place into the link field in cinfo.
   */</span>
  cinfo.err = jpeg_std_error(&amp;jerr);
  <span class="cpp-comment">/* Now we can initialize the JPEG compression object. */</span>
  jpeg_create_compress(&amp;cinfo);

  <span class="cpp-comment">/* Step 2: specify data destination (eg, a file) */</span>
  <span class="cpp-comment">/* Note: steps 2 and 3 can be done in either order. */</span>

  <span class="cpp-comment">/* Here we use the library-supplied code to send compressed data to a
   * stdio stream.  You can also write your own code to do something else.
   * VERY IMPORTANT: use "b" option to fopen() if you are on a machine that
   * requires it in order to write binary files.
   */</span>
  <span class="cpp-keyword">if</span> ((outfile = fopen(imageFile.c_str(), <span class="cpp-literal">"wb"</span>)) == NULL) {
<span class="cpp-comment">//    fprintf(stderr, "can't open %s\n", filename);</span>
<span class="cpp-comment">//    exit(1);</span>
	  assert(<span class="cpp-number">0</span>);
  }
  jpeg_stdio_dest(&amp;cinfo, outfile);

  <span class="cpp-comment">/* Step 3: set parameters for compression */</span>

  <span class="cpp-comment">/* First we supply a description of the input image.
   * Four fields of the cinfo struct must be filled in:
   */</span>
  <span class="cpp-keyword">int</span> ncomp = (image.GetBitCount()==<span class="cpp-number">24</span>)?<span class="cpp-number">3</span>:<span class="cpp-number">1</span>;
  cinfo.image_width = image.Width(); 	<span class="cpp-comment">/* image width and height, in pixels */</span>
  cinfo.image_height = image.Height();
  cinfo.input_components = ncomp;		<span class="cpp-comment">/* # of color components per pixel */</span>
  cinfo.in_color_space = (ncomp==<span class="cpp-number">1</span>)?JCS_GRAYSCALE:JCS_RGB; 	<span class="cpp-comment">/* colorspace of input image */</span>
  <span class="cpp-comment">/* Now use the library's routine to set default compression parameters.
   * (You must set at least cinfo.in_color_space before calling this,
   * since the defaults depend on the source color space.)
   */</span>
  jpeg_set_defaults(&amp;cinfo);
  <span class="cpp-comment">/* Now you can set any non-default parameters you wish to.
   * Here we just illustrate the use of quality (quantization table) scaling:
   */</span>
  jpeg_set_quality(&amp;cinfo, <span class="cpp-number">100</span>, <span class="cpp-keyword">TRUE</span> <span class="cpp-comment">/* limit to baseline-JPEG values */</span>);

  <span class="cpp-comment">/* Step 4: Start compressor */</span>

  <span class="cpp-comment">/* TRUE ensures that we will write a complete interchange-JPEG file.
   * Pass TRUE unless you are very sure of what you're doing.
   */</span>
  jpeg_start_compress(&amp;cinfo, <span class="cpp-keyword">TRUE</span>);

  <span class="cpp-comment">/* Step 5: while (scan lines remain to be written) */</span>
  <span class="cpp-comment">/*           jpeg_write_scanlines(…); */</span>

  <span class="cpp-comment">/* Here we use the library's state variable cinfo.next_scanline as the
   * loop counter, so that we don't have to keep track ourselves.
   * To keep things simple, we pass one scanline per call; you can pass
   * more if you wish, though.
   */</span>
  row_stride = image.Width() * ncomp;	<span class="cpp-comment">/* JSAMPLEs per row in image_buffer */</span>
  ubyte* line_buffer = <span class="cpp-keyword">new</span> ubyte[row_stride];

  <span class="cpp-keyword">while</span> (cinfo.next_scanline &lt; cinfo.image_height) {
    <span class="cpp-comment">/* jpeg_write_scanlines expects an array of pointers to scanlines.
     * Here the array is only one element long, but you could pass
     * more than one scanline at a time if that's more convenient.
     */</span>
	ubyte* line = image[cinfo.image_height-<span class="cpp-number">1</span>-cinfo.next_scanline].GetBufferPointer();;
	ubyte* ptr = line_buffer;
	<span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> x=<span class="cpp-number">0</span>; x&lt;ncomp*image.Width(); x+=ncomp, ptr+=ncomp)
	{
		<span class="cpp-keyword">for</span> (<span class="cpp-keyword">int</span> c=<span class="cpp-number">0</span>; c&lt;ncomp; c++)
			*(ptr+ncomp-<span class="cpp-number">1</span>-c) = line[x+c];
<span class="cpp-comment">//			*ptr = 255-line[x];</span>
	<span class="cpp-comment">//	image.GetRGB(line[x], *ptr, *(ptr+1), *(ptr+2));</span>
	}
    row_pointer[<span class="cpp-number">0</span>] = line_buffer;
    (<span class="cpp-keyword">void</span>) jpeg_write_scanlines(&amp;cinfo, row_pointer, <span class="cpp-number">1</span>);
  }
  <span class="cpp-keyword">delete</span> [] line_buffer;
  <span class="cpp-comment">/* Step 6: Finish compression */</span>

  jpeg_finish_compress(&amp;cinfo);
  <span class="cpp-comment">/* After finish_compress, we can close the output file. */</span>
  fclose(outfile);

  <span class="cpp-comment">/* Step 7: release JPEG compression object */</span>

  <span class="cpp-comment">/* This is an important step since it will release a good deal of memory. */</span>
  jpeg_destroy_compress(&amp;cinfo);

  <span class="cpp-keyword">return</span> <span class="cpp-keyword">true</span>;
}

</pre></div><!–ENDSCRIPT–> 
Advertisement
hi Graz,
help Please!
What makes you think your custom buffer is compatible with the mesa buffer? Some things to check: pixel format, byte ordering and padding/alignment constraints.

Most likely, you will not be able to do a straight-across copy like you seem to be relying on.

throw table_exception("(? ???)? ? ???");

since i'm new to image processing, what you mean by padding and alignment stuff? could you please explain more for me. besides, what does it mean by 32 bit alignment? help!
Alignment refers to a constraint on the memory location at which the data begins, for example, it might require that the data begin on a WORD or DWORD boundary.

Sometimes this alignment constraint is per line of the image, which means that the data in line N must be padded (usually with 0 by convention, but the bytes are nearly always ignored in practice) such that line N+1 begins at a memory location which also satisfies the alignment constraint. This can be seen in the bitmap file format and Win32 DIBs, IIRC. Sometimes, images are padded out to the next higher power of two, which would be 4096x4096 in your case if true.

Specifically, 32bit alignment means that data must begin at an address that is aligned on a 32bit boundary, or, in layman's terms, the address should end in only 0, 4, 8 or C (hex), any other alignment would produce incorrect results.

Now, 24bit color presents some interesting possibilities -- It does not align naturally with the any single data type, ie it doesn't fill a 32bit int like 32bit ARGB color format would. But it does align on a multiple of of the fundamental data type, byte. The long and the short of all that is that the Mesa color buffers for 24 bit mode might be a triplet of bytes per color, or a 32bit word which leaves 1 byte's worth unused. You'll have to find out what mesa does and compare that to what your own image class expects.

throw table_exception("(? ???)? ? ???");

cBaseImage.h & cpp can be found below
http://pastebin.com/m53eaed68
mesa allocated and code here
http://pastebin.com/m7160eadb
Line 28 in the mesa code you posted seems to be allocating 4 channels, which means it is including an alpha channel in addition to the R, G and B channels. the 'bits' variable there also seems to be not the bits per pixel, but the pits per channel -- in other words, as best as I can tell, setting it to 32 like you have at the top means its allocating 1 float per channel per pixel, or 16 bytes per pixel -- you commented out setting it to 8, but 8 seems to have been correct, although you still have the additional alpha channel to deal with. You don't seem to be doing any alignment or padding, so that appears unnecessary if this code works on its own.

cBaseImage seems to allocate only 3 bytes per pixel, assuming that WidthBytes() simply divides by 8. It doesn't seem to allocate an alpha channel.

The bottom line is that you can't do a simple bitwise copy of one buffer into the other and expect it to work. As it stands, they are extremely different formats and have a different number of channels. You'll have to loop over each pixel and do the conversion or make cBaseImage's internal format such that it matches Mesa's internal format.

throw table_exception("(? ???)? ? ???");

i changed the format of mesa3d buffer to be compatible with buffer that cBaseImage accepts. but still it doesnt seem to be working out.
My last question is what is the use of SetPalette() and GetPalette for? they also accepts ubyte*(unsigned char)..thx
GLint bits=8;
const char *filename ="C:\\image81.ppm";
// const GLint z = 32;
const GLint z = 8;
const GLint stencil = 0;
const GLint accum = 0;
OSMesaContext ctx;
void *buffer;
GLint cBits;
assert(bits == 8 ||
bits == 16 ||
bits == 32);
assert(type == GL_UNSIGNED_BYTE ||
type == GL_UNSIGNED_SHORT ||
type == GL_FLOAT);
ctx = OSMesaCreateContextExt(OSMESA_RGB, z, stencil, accum, NULL );
buffer = malloc(WIDTH*HEIGHT*3*bits/8);
if (!OSMesaMakeCurrent( ctx, buffer, type, WIDTH, HEIGHT )) {

}
rendering comes here

GLubyte *buffer8 = (GLubyte *) buffer;

image1.Create(4000,4000,8);
image1.CreatePalette(256);
//image1.InitGrayPalette(256);
image1.SetPalette(buffer8,4000*4000*8);
//image1.InitGrayPalette();
//image1.Init8BitColorPalette();
iSaveJpeg(image1,"C:\\testing.jpg");
i tried above code and can't successfully save to jpg.
the internal format of mesa3d and cBaseImage is now compatible i suppose.
please help

This topic is closed to new replies.

Advertisement