MacArthur, Ian (SELEX GALILEO, UK) wrote:
>> Here is the working patch that can be applied to fltk.
> 
> Domingo,
> 
> Please, try and remember that the mailing list, many mail servers, many
> usenet interfaces, will strip away atatchments.
> 
> I *never* see the attachments you post, from any of my systems.
> 
> If you have Good Stuff, please attach it to an STR, so that we do not
> lose it.

        Right; here's how it looks on the website:
        http://fltk.org/newsgroups.php?gfltk.general+v:32768

        Although I have been able to see the attachment over NNTP
        through my newsreader, I'd prefer to see patches for bugs/rfe's
        in an STR so we can later add them.

        However, a good reason to post patches here (as opposed to an STR)
        would be if the patch is not for FLTK to incorporate so much as it is
        for others reading the group to apply if they want a certain
        behavior that the FLTK devs might not want to actually add.

        It might also be getting posted here to meet LGPL requirement
        to make all FLTK toolkit modifications "public".

        With the above in mind, and for the benefit of others here
        on the group who can't read attachments, I've pasted Domingo's
        attachment as clear text:

Index: src/Fl_JPEG_Image.cxx
===================================================================
--- src/Fl_JPEG_Image.cxx       (revision 8670)
+++ src/Fl_JPEG_Image.cxx       (working copy)
@@ -376,6 +376,61 @@
 #endif // HAVE_LIBJPEG
 }

+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();
+
+    struct jpeg_compress_struct cinfo;
+       struct jpeg_error_mgr       jerr;
+
+    if (!((imgd == 3) || (imgd == 1)))  return -1;
+
+    *outbuffer = NULL;
+    outlen = 0;
+    unsigned long sz_outlen = 0;
+
+       cinfo.err = jpeg_std_error(&jerr);
+       jpeg_create_compress(&cinfo);
+       //FILE* outfile = fopen("test.jpeg", "wb");
+       jpeg_mem_dest(&cinfo, outbuffer, &sz_outlen);
+       //jpeg_stdio_dest(&cinfo, outfile);
+
+       cinfo.image_width      = imgw;
+       cinfo.image_height     = imgh;
+       cinfo.input_components = imgd;
+       cinfo.in_color_space   = imgd == 1 ? JCS_GRAYSCALE : JCS_RGB;
+
+       jpeg_set_defaults(&cinfo);
+       jpeg_set_quality (&cinfo, quality, 1);
+
+       jpeg_simple_progression(&cinfo);
+       jpeg_start_compress(&cinfo, 1);
+
+       uchar *idata    = (uchar *)*img->data();
+
+       /* main code to write jpeg data */
+       int iwd = imgw * imgd;
+
+    while (cinfo.next_scanline < cinfo.image_height) {
+        (void) jpeg_write_scanlines(&cinfo, &idata, 1);
+        idata += iwd;
+    }
+       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
+}
+
 //
 // End of "$Id$".
 //
Index: src/Fl_PNG_Image.cxx
===================================================================
--- src/Fl_PNG_Image.cxx        (revision 8670)
+++ src/Fl_PNG_Image.cxx        (working copy)
@@ -227,7 +227,116 @@
 #endif // HAVE_LIBPNG && HAVE_LIBZ
 }

+#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
+/* structure to store PNG image bytes */
+typedef struct {
+  uchar *buffer;
+  size_t size;
+  size_t size_allocated;
+} mem_encode;

+
+static void my_png_write_data(png_structp png_ptr, png_bytep data, png_size_t 
length){
+  mem_encode* p=(mem_encode*)png_get_io_ptr(png_ptr);
+  size_t nsize = p->size + length;
+
+  /* allocate or grow buffer */
+  if(nsize > p->size_allocated){
+      int alloc_size = nsize * 2;
+      if(p->buffer)
+        p->buffer = (uchar*)realloc(p->buffer, alloc_size);
+      else
+        p->buffer = (uchar*)malloc(alloc_size);
+
+      if(!p->buffer) {
+        png_error(png_ptr, "Write Error");
+        return;
+      }
+      p->size_allocated = alloc_size;
+  }
+
+  /* copy new bytes to end of buffer */
+  memcpy(p->buffer + p->size, data, length);
+  p->size += length;
+}
+
+/* This is optional but included to show how png_set_write_fn() is called */
+static void my_png_flush(png_structp png_ptr){}
+#endif // HAVE_LIBPNG && HAVE_LIBZ
+
+int Fl_PNG_Image::encode(Fl_Image *img, unsigned char **outbuffer, int 
&outlen){
+#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
+    int imgw = img->w(), imgh = img->h(), imgd = img->d();
+    outlen = imgw * imgh * imgd;
+
+    png_structp                        pp;             // PNG data
+    png_infop                  info;           // PNG image info
+    mem_encode state;
+    /* initialise - put this before png_write_png() call */
+    state.buffer = NULL;
+    state.size = 0;
+    state.size_allocated = 0;
+
+
+    if( !((imgd == 1) || (imgd ==3) || (imgd ==4))) return -1;
+    pp = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+    if(!pp) return -1;
+
+    info = png_create_info_struct(pp);
+    if (!info){
+        png_destroy_write_struct(&pp, 0);
+        return -1;
+    }
+
+    if (setjmp(png_jmpbuf(pp))){
+        png_destroy_write_struct(&pp, &info);
+        return -1;
+    }
+
+    //png_init_io(pp, fp);
+    /* if my_png_flush() is not needed, change the arg to NULL */
+    png_set_write_fn(pp, &state, my_png_write_data, my_png_flush);
+
+
+    png_set_compression_level(pp, Z_BEST_COMPRESSION);
+    png_set_IHDR(pp, info, imgw, imgh, 8,
+               imgd == 1 ? PNG_COLOR_TYPE_GRAY :
+               imgd == 3 ? PNG_COLOR_TYPE_RGB : PNG_COLOR_TYPE_RGB_ALPHA,
+           PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
+           PNG_FILTER_TYPE_DEFAULT);
+    png_set_sRGB(pp, info, PNG_sRGB_INTENT_PERCEPTUAL);
+    png_set_sRGB_gAMA_and_cHRM(pp, info, PNG_INFO_sRGB);
+
+    png_write_info(pp, info);
+
+    uchar *idata    = (uchar *)*img->data();
+    int iwd = imgw * imgd;
+    for (int y = 0; y < imgh; y++)
+    {
+        png_write_row(pp, (png_byte *)idata);
+        idata += iwd;
+    }
+
+    png_write_end(pp, info);
+    png_destroy_write_struct(&pp, 0);
+
+    /* cleanup */
+    //if(state.buffer) free(state.buffer);
+
+    *outbuffer = state.buffer;
+       outlen = state.size;
+
+//     FILE* outfile = fopen("test.png", "wb");
+//     if (outfile){
+//         fwrite(*outbuffer, outlen,1 ,outfile);
+//         fclose(outfile);
+//     }
+       return 0;
+#else
+    return -1;
+#endif // HAVEHAVE_LIBPNG && HAVE_LIBZ
+}
+
 //
 // End of "$Id$".
 //
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to