From: Dominic L. <ma...@us...> - 2004-06-11 20:51:30
|
Update of /cvsroot/robotflow/RobotFlow/Vision/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv14992 Modified Files: JPEGSave.cc Log Message: working jpeg compres with lib jpeg Index: JPEGSave.cc =================================================================== RCS file: /cvsroot/robotflow/RobotFlow/Vision/src/JPEGSave.cc,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** JPEGSave.cc 10 Jun 2004 19:21:55 -0000 1.3 --- JPEGSave.cc 11 Jun 2004 20:51:21 -0000 1.4 *************** *** 24,28 **** --- 24,32 ---- #include "BufferedNode.h" #include "gdk-pixbuf/gdk-pixbuf.h" + #include <stdio.h> + extern "C" { + #include "jpeglib.h" + } //forward declaration *************** *** 60,71 **** - gboolean pixbuf_save_callback(const gchar *buf, gsize count, GError **error, gpointer data_string) { - - string *my_string = reinterpret_cast<string*>(data_string); ! if (my_string) { ! *my_string += string(buf,count); ! } } --- 64,99 ---- + /* + * Initialize destination --- called by jpeg_start_compress + * before any data is actually written. + */ + METHODDEF(void) + init_destination (j_compress_ptr cinfo) + { + //cerr<<"init_destination"<<endl; + } ! /* ! * Empty the output buffer --- called whenever buffer fills up. ! */ ! ! METHODDEF(boolean) ! empty_output_buffer (j_compress_ptr cinfo) ! { ! //cerr<<"empty output buffer"<<endl; ! return TRUE; ! } ! ! ! /* ! * Terminate destination --- called by jpeg_finish_compress ! * after all data has been written. ! */ ! ! METHODDEF(void) ! term_destination (j_compress_ptr cinfo) ! { ! //cerr<<"term_destination"<<endl; } *************** *** 173,190 **** } //saving pixbuf to memory ! if (write_mem) { ! ! gboolean retval = gdk_pixbuf_save_to_callback (m_pixbuf, ! pixbuf_save_callback, ! &data_string, ! "jpeg", ! NULL, ! "quality", ! m_quality.c_str(),NULL); ! ! if (!retval) { ! throw new GeneralException("JPEGSave : Unable to save JPEG data to memory",__FILE__,__LINE__); ! } ! } --- 201,206 ---- } //saving pixbuf to memory ! if (write_mem) { ! libjpeg_compress(my_image, atoi(m_quality.c_str()), data_string); } *************** *** 227,232 **** --- 243,322 ---- }//calculate + void libjpeg_compress(Image &image, int quality, string &data_string) { + + struct jpeg_compress_struct cinfo; + struct jpeg_error_mgr jerr; + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + + /* + FILE * outfile; + if ((outfile = fopen("test.jpg", "wb")) == NULL) { + fprintf(stderr, "can't open %s\n", "test.jpg"); + exit(1); + } + + jpeg_stdio_dest(&cinfo, outfile); + */ + + //set destination to memory + struct jpeg_destination_mgr dest_mgr; + //set function pointers + dest_mgr.init_destination = init_destination; + dest_mgr.empty_output_buffer = empty_output_buffer; + dest_mgr.term_destination = term_destination; + + static unsigned char buffer[320 * 240 * 2]; + + dest_mgr.next_output_byte = &buffer[0]; + + dest_mgr.free_in_buffer = 320 * 240 *2; /* # of byte spaces remaining in buffer */ //320x240x2 + cinfo.dest = &dest_mgr; + + //set other image params + cinfo.image_width = image.get_width(); /* image width and height, in pixels */ + cinfo.image_height = image.get_height(); + cinfo.input_components = 3; /* # of color components per pixel */ + cinfo.in_color_space = JCS_RGB; /* colorspace of input image */ + cinfo.data_precision = 15; //15bPP + jpeg_set_quality(&cinfo,20,TRUE); /* set compression quality */ + jpeg_set_defaults(&cinfo); + /* Make optional parameter settings here */ + + jpeg_start_compress(&cinfo, TRUE); + + JSAMPROW row_pointer[1]; /* pointer to a single row */ + int row_stride; /* physical row width in buffer */ + + row_stride = image.get_width() * image.get_pixelsize(); /* JSAMPLEs per row in image_buffer */ + + unsigned char *image_buffer = image.get_data(); + + + while (cinfo.next_scanline < cinfo.image_height) { + row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride]; + jpeg_write_scanlines(&cinfo, row_pointer, 1); + } + + jpeg_finish_compress(&cinfo); + + + //output number of free bytes + //cerr<<"total size "<<320 * 240 * 2 - dest_mgr.free_in_buffer; + + //create buffer for jpeg data + data_string.resize(320 * 240 * 2 - dest_mgr.free_in_buffer); + + //copy jpeg data + memcpy(const_cast<char*>(data_string.c_str()),&buffer[0],data_string.size()); + + + jpeg_destroy_compress(&cinfo); + + //fclose(outfile); + } }; |