save as jpeg

Started by
4 comments, last by dawei 21 years, 6 months ago
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
Advertisement
Forgot to mention: preferably using ''jpeglib.h''under linux. That''s how far I got!
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 */<br> int row_stride; /* physical row width in image buffer */<br><br> cinfo.err = jpeg_std_error(&jerr);<br> jpeg_create_compress(&cinfo);<br><br> if ((outfile = fopen(fileName, "wb")) == NULL) {<br> fprintf(stderr, "can''t open %s\n", fileName);<br> exit(1);<br> }<br> jpeg_stdio_dest(&cinfo, outfile);<br><br> cinfo.image_width = width; /* image width and height, in pixels */<br> cinfo.image_height = height;<br> cinfo.input_components = components; /* # of color components per pixel */<br> cinfo.in_color_space = JCS_RGB; /* colorspace of input image */<br> jpeg_set_defaults(&cinfo);<br> jpeg_start_compress(&cinfo, TRUE);<br><br> row_stride = width * components; /* JSAMPLEs per row in image_buffer */<br><br> while (cinfo.next_scanline < cinfo.image_height) {<br> row_pointer[0] = & imageBuffer[(cinfo.image_height - cinfo.next_scanline - 1) * row_stride];<br> (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);<br> }<br><br><br> jpeg_finish_compress(&cinfo);<br> fclose(outfile);<br> jpeg_destroy_compress(&cinfo);<br><br>}//end SaveJPEG<br><br><br>There are a couple more things in the example.c file that you will need, but it''s just cut and paste.<br>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.<br><br>Robbie
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!!
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
THAT''S IT! THANK YOU!
You''re the hero of the day. I''d never have found out by myself!
Have a nice day!

This topic is closed to new replies.

Advertisement