Archived

This topic is now archived and is closed to further replies.

dawei

save as jpeg

Recommended Posts

Do you have an idea (maybe a tut) how to write the contents of a buffer to compressed jpeg. I already have .tga and .ppm(linux) as output, but that''s not enough, because I need internet-compatible output! Thanks in advance! DaWei

Share this post


Link to post
Share on other sites
Assuming you have the jpeg-6b jpeg.lib working right, here''s a little code for the hard parts. You''ll have to fill in your code to make it work.

//Get height and width of buffer with :
float V[4];
glGetFloatv(GL_VIEWPORT, V);
width = (int)V[2];
height =(int)V[3];


//get the pixels with:
glReadPixels(0,0,width,height, GL_RGB, GL_UNSIGNED_BYTE, imageBuffer);

//This function is in the jpeg-6b example.c file
//I modified it a little bit
void SaveJPEG(char *fileName, unsigned char *imageBuffer, int components, int height, int width){
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 = width; /* image width and height, in pixels */
cinfo.image_height = height;
cinfo.input_components = components; /* # of color components per pixel */
cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
jpeg_set_defaults(&cinfo);
jpeg_start_compress(&cinfo, TRUE);

row_stride = width * components; /* JSAMPLEs per row in image_buffer */

while (cinfo.next_scanline < cinfo.image_height) {
row_pointer[0] = & imageBuffer[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
}


jpeg_finish_compress(&cinfo);
fclose(outfile);
jpeg_destroy_compress(&cinfo);

}//end SaveJPEG


There are a couple more things in the example.c file that you will need, but it''s just cut and paste.
I''m sure someone can point out a trouble spot in my code, or how I can do something better, but this works for me. Hope that helps somewhat.

Robbie

Share this post


Link to post
Share on other sites
Thanks alot for your help. But yet another problem occured:
I got the jpeglib.h and libjpeg.so version 6.2. each (did you mean this by ''6b?). The Compiler finds everything and the lib is found, too. Still he pops up these "undefined reference" msgs. for every single jpeg-command. That normally is a hint for not having included (all) libs.
Any more hints you can give? Thank you very much!!

Share this post


Link to post
Share on other sites
I''m not exactly sure what you''re compile problem is.

Are you using C++? If so then did you remember to tell it that the jpeglib.h is C? I had this trouble when I tried to compile the first time. Try to make your include statement something like this:

extern "C"
{
#include "jpeglib.h"
}

That fixed some things for me.

Robbie

Share this post


Link to post
Share on other sites