Sorry but looking at it again I noticed that the rgb colors aren't swaped,  
you only have a unnecessary loop and memory allocation.

This is my actual implementation that produce wrong colors.


int Fl_JPEG_Image::encode(Fl_Image *img, int quality,
                         unsigned char **outbuffer, int &outlen){
#ifdef HAVE_LIBJPEG
     int imgw = img->w(), imgh = img->h(), imgd = img->d();
     outlen = imgw * imgh * imgd;

     struct jpeg_compress_struct cinfo = {0};
        struct jpeg_error_mgr       jerr;

     if (imgd == 3) cinfo.in_color_space = JCS_RGB;
     else if (imgd == 1) cinfo.in_color_space = JCS_GRAYSCALE;
     else return -1;


     *outbuffer = NULL;
     unsigned long sz_outlen = 0;

        /* create our in-memory output buffer to hold the jpeg */
        *outbuffer   = (unsigned char *) malloc(outlen+8*1024); //safe margin

        cinfo.err = jpeg_std_error(&jerr);
        jpeg_create_compress(&cinfo);
        jpeg_mem_dest(&cinfo, outbuffer, &sz_outlen);

        cinfo.image_width      = imgw;
        cinfo.image_height     = imgh;
        cinfo.input_components = imgd;

        jpeg_set_defaults(&cinfo);
        jpeg_set_quality (&cinfo, quality, true);
        jpeg_start_compress(&cinfo, true);

        uchar * row_pointer;
        uchar *idata    = (uchar *)img->data()[0];

        /* main code to write jpeg data */
        int iwd = imgw * imgd;
     while (cinfo.next_scanline < cinfo.image_height) {
         row_pointer = idata + cinfo.next_scanline * iwd;
         (void) jpeg_write_scanlines(&cinfo, &row_pointer, 1);
     }
        jpeg_finish_compress(&cinfo);
        jpeg_destroy_compress(&cinfo);
        outlen = sz_outlen;

//      FILE* outfile = fopen("test.jpeg", "wb");
//      if (outfile){
//          fwrite(*outbuffer, outlen,1 ,outfile);
//          fclose(outfile);
//      }
        return 0;
#else
     return -1;
#endif // HAVE_LIBJPEG
}
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to