On 14.05.2010, at 20:53, <[email protected]> <[email protected]> wrote:

> That actually worked perfectly, thanks for the help, would you happen to know 
> how to print it out as a jpeg to a file, or a bmp, or something that can be 
> loaded later?

Sure, here you go. Just copy and paste this into FL_JPEG_Image.cxx or in your 
own files:

int fl_write_jpeg(Fl_RGB_Image *img, const char *name, int quality, float dpi)
{
  int ret = -1;
#ifdef HAVE_LIBJPEG
  if (img->w()<1) return ret;
  if (img->h()<1) return ret;
  if (img->d()!=3) return ret;
  if (!img->data()) return ret;

  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 */

  if ((outfile = fopen(name, "wb")) == NULL)      
    return ret;

  cinfo.err = jpeg_std_error(&jerr);
  jpeg_create_compress(&cinfo);
  jpeg_stdio_dest(&cinfo, outfile);

  cinfo.image_width = img->w();
  cinfo.image_height = img->h();
  cinfo.input_components = 3;
  cinfo.in_color_space = JCS_RGB;
  cinfo.density_unit = 1;
  cinfo.X_density = cinfo.Y_density = (UINT16)dpi;
  jpeg_set_defaults(&cinfo);
  jpeg_set_quality(&cinfo, quality, TRUE);
  // todo: set DPI here

  jpeg_start_compress(&cinfo, TRUE);
  row_stride = img->w() * 3;

  const uchar *data = img->array;
  while (cinfo.next_scanline < cinfo.image_height) {
    row_pointer[0] = (JSAMPLE*)data + (cinfo.next_scanline * row_stride);
    jpeg_write_scanlines(&cinfo, row_pointer, 1);
  }

  jpeg_finish_compress(&cinfo);
  fclose(outfile);
  jpeg_destroy_compress(&cinfo);
  ret = 0;

#endif // HAVE_LIB_JPEG
  return ret;
}

_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to