From 4087b8cd9a139664420468c57489cae73c8b830a Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Fri, 23 Nov 2001 12:06:36 +0000 Subject: Add code for PNG and JPEG images. git-svn-id: file:///fltk/svn/fltk/branches/branch-1.1@1713 ea41ed52-d2ee-0310-a9c1-e6b18d33e121 --- FL/Fl_Image.H | 5 ++-- src/Fl_Bitmap.cxx | 9 +++++-- src/Fl_Image.cxx | 19 ++++++++++++-- src/Fl_JPEG_Image.cxx | 61 +++++++++++++-------------------------------- src/Fl_PNG_Image.cxx | 68 +++++++++++++++++++++------------------------------ src/Fl_Pixmap.cxx | 14 ++++++++--- 6 files changed, 82 insertions(+), 94 deletions(-) diff --git a/FL/Fl_Image.H b/FL/Fl_Image.H index 596b268a8..8432aaa5e 100644 --- a/FL/Fl_Image.H +++ b/FL/Fl_Image.H @@ -1,5 +1,5 @@ // -// "$Id: Fl_Image.H,v 1.5.2.3.2.4 2001/11/22 15:35:01 easysw Exp $" +// "$Id: Fl_Image.H,v 1.5.2.3.2.5 2001/11/23 12:06:35 easysw Exp $" // // Image header file for the Fast Light Tool Kit (FLTK). // @@ -41,6 +41,7 @@ class FL_EXPORT Fl_Image { void h(int H) {h_ = H;} void d(int D) {d_ = D;} void data(const char * const *p, int c) {data_ = p; count_ = c;} + void draw_empty(int X, int Y); public: @@ -88,5 +89,5 @@ class FL_EXPORT Fl_RGB_Image : public Fl_Image { #endif // -// End of "$Id: Fl_Image.H,v 1.5.2.3.2.4 2001/11/22 15:35:01 easysw Exp $". +// End of "$Id: Fl_Image.H,v 1.5.2.3.2.5 2001/11/23 12:06:35 easysw Exp $". // diff --git a/src/Fl_Bitmap.cxx b/src/Fl_Bitmap.cxx index 887d57603..304f16029 100644 --- a/src/Fl_Bitmap.cxx +++ b/src/Fl_Bitmap.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_Bitmap.cxx,v 1.5.2.4.2.4 2001/11/19 20:59:59 easysw Exp $" +// "$Id: Fl_Bitmap.cxx,v 1.5.2.4.2.5 2001/11/23 12:06:36 easysw Exp $" // // Bitmap drawing routines for the Fast Light Tool Kit (FLTK). // @@ -161,6 +161,11 @@ void fl_delete_bitmask(Fl_Bitmask bm) { #endif // WIN32 void Fl_Bitmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) { + if (!array) { + draw_empty(XP, YP); + return; + } + // account for current clip region (faster on Irix): int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H); cx += X-XP; cy += Y-YP; @@ -273,5 +278,5 @@ Fl_Image *Fl_Bitmap::copy(int W, int H) { } // -// End of "$Id: Fl_Bitmap.cxx,v 1.5.2.4.2.4 2001/11/19 20:59:59 easysw Exp $". +// End of "$Id: Fl_Bitmap.cxx,v 1.5.2.4.2.5 2001/11/23 12:06:36 easysw Exp $". // diff --git a/src/Fl_Image.cxx b/src/Fl_Image.cxx index b6c61a727..909a2a7d5 100644 --- a/src/Fl_Image.cxx +++ b/src/Fl_Image.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_Image.cxx,v 1.5.2.3.2.6 2001/11/22 15:35:01 easysw Exp $" +// "$Id: Fl_Image.cxx,v 1.5.2.3.2.7 2001/11/23 12:06:36 easysw Exp $" // // Image drawing code for the Fast Light Tool Kit (FLTK). // @@ -37,6 +37,16 @@ Fl_Image::~Fl_Image() { } void Fl_Image::draw(int XP, int YP, int WP, int HP, int cx, int cy) { + draw_empty(XP, YP); +} + +void Fl_Image::draw_empty(int X, int Y) { + if (w() > 0 && h() > 0) { + fl_color(FL_BLACK); + fl_rect(X, Y, w(), h()); + fl_line(X, Y, X + w() - 1, Y + h() - 1); + fl_line(X, Y + h() - 1, X + w() - 1, Y); + } } Fl_Image *Fl_Image::copy(int W, int H) { @@ -222,6 +232,11 @@ void Fl_RGB_Image::desaturate() { } void Fl_RGB_Image::draw(int XP, int YP, int WP, int HP, int cx, int cy) { + if (!array) { + draw_empty(XP, YP); + return; + } + // account for current clip region (faster on Irix): int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H); cx += X-XP; cy += Y-YP; @@ -341,5 +356,5 @@ void Fl_RGB_Image::label(Fl_Menu_Item* m) { // -// End of "$Id: Fl_Image.cxx,v 1.5.2.3.2.6 2001/11/22 15:35:01 easysw Exp $". +// End of "$Id: Fl_Image.cxx,v 1.5.2.3.2.7 2001/11/23 12:06:36 easysw Exp $". // diff --git a/src/Fl_JPEG_Image.cxx b/src/Fl_JPEG_Image.cxx index 0c38b4709..73df5f24b 100644 --- a/src/Fl_JPEG_Image.cxx +++ b/src/Fl_JPEG_Image.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_JPEG_Image.cxx,v 1.1.2.1 2001/11/19 01:06:45 easysw Exp $" +// "$Id: Fl_JPEG_Image.cxx,v 1.1.2.2 2001/11/23 12:06:36 easysw Exp $" // // Fl_JPEG_Image routines. // @@ -32,27 +32,9 @@ // #include -#include "config.h" +#include #include #include -#include -#include -#ifdef HAVE_STRINGS_H -# include -#endif /* HAVE_STRINGS_H */ -#include - -#if defined(WIN32) && ! defined(__CYGWIN__) -# include -# include -# define strcasecmp(s,t) stricmp((s), (t)) -# define strncasecmp(s,t,n) strnicmp((s), (t), (n)) -#elif defined(__EMX__) -# define strcasecmp(s,t) stricmp((s), (t)) -# define strncasecmp(s,t,n) strnicmp((s), (t), (n)) -#else -# include -#endif // WIN32 extern "C" { @@ -61,24 +43,22 @@ extern "C" #endif // HAVE_LIBJPEG } -#define MAX_COLUMNS 200 - -#if 0 -#ifdef HAVE_LIBJPEG // -// 'Fl_Help_View::load_jpeg()' - Load a JPEG image file. +// 'Fl_JPEG_Image::Fl_JPEG_Image()' - Load a JPEG image file. // -int // O - 0 = success, -1 = fail -Fl_Help_View::load_jpeg(Fl_Help_Image *img, // I - Image pointer - FILE *fp) // I - File to load from -{ +Fl_JPEG_Image::Fl_JPEG_Image(const char *jpeg) // I - File to load + : Fl_RGB_Image(0,0,0) { +#ifdef HAVE_LIBJPEG + FILE *fp; // File pointer struct jpeg_decompress_struct cinfo; // Decompressor info struct jpeg_error_mgr jerr; // Error handler info JSAMPROW row; // Sample row pointer + if ((fp = fopen(jpeg, "rb")) == NULL) return; + cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, fp); @@ -91,22 +71,17 @@ Fl_Help_View::load_jpeg(Fl_Help_Image *img, // I - Image pointer jpeg_calc_output_dimensions(&cinfo); - img->w = cinfo.output_width; - img->h = cinfo.output_height; - img->d = cinfo.output_components; - img->data = (unsigned char *)malloc(img->w * img->h * img->d); + w(cinfo.output_width); + h(cinfo.output_height); + d(cinfo.output_components); - if (img->data == NULL) - { - jpeg_destroy_decompress(&cinfo); - return (0); - } + array = new uchar[w() * h() * d()]; jpeg_start_decompress(&cinfo); while (cinfo.output_scanline < cinfo.output_height) { - row = (JSAMPROW)(img->data + + row = (JSAMPROW)(array + cinfo.output_scanline * cinfo.output_width * cinfo.output_components); jpeg_read_scanlines(&cinfo, &row, (JDIMENSION)1); @@ -115,12 +90,10 @@ Fl_Help_View::load_jpeg(Fl_Help_Image *img, // I - Image pointer jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); - return (1); -} + fclose(fp); #endif // HAVE_LIBJPEG - -#endif // 0 +} // -// End of "$Id: Fl_JPEG_Image.cxx,v 1.1.2.1 2001/11/19 01:06:45 easysw Exp $". +// End of "$Id: Fl_JPEG_Image.cxx,v 1.1.2.2 2001/11/23 12:06:36 easysw Exp $". // diff --git a/src/Fl_PNG_Image.cxx b/src/Fl_PNG_Image.cxx index 36ede18ff..e12cd5c77 100644 --- a/src/Fl_PNG_Image.cxx +++ b/src/Fl_PNG_Image.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_PNG_Image.cxx,v 1.1.2.1 2001/11/19 01:06:45 easysw Exp $" +// "$Id: Fl_PNG_Image.cxx,v 1.1.2.2 2001/11/23 12:06:36 easysw Exp $" // // Fl_PNG_Image routines. // @@ -32,15 +32,9 @@ // #include -#include "config.h" +#include #include #include -#include -#include -#ifdef HAVE_STRINGS_H -# include -#endif /* HAVE_STRINGS_H */ -#include extern "C" { @@ -51,23 +45,24 @@ extern "C" } -#if 0 -#ifdef HAVE_LIBPNG // -// 'Fl_Help_View::load_png()' - Load a PNG image file. +// 'Fl_PNG_Image::Fl_PNG_Image()' - Load a PNG image file. // -int // O - 0 = success, -1 = fail -Fl_Help_View::load_png(Fl_Help_Image *img,// I - Image pointer - FILE *fp) // I - File to read from -{ +Fl_PNG_Image::Fl_PNG_Image(const char *png) // I - File to read + : Fl_RGB_Image(0,0,0) { +#ifdef HAVE_LIBPNG int i; // Looping var + FILE *fp; // File pointer + int channels; // Number of color channels png_structp pp; // PNG read pointer png_infop info; // PNG info pointers png_bytep *rows; // PNG row pointers - png_color_16 bg; // Background color + // Open the PNG file... + if ((fp = fopen(png, "rb")) == NULL) return; + // Setup the PNG data structures... pp = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); info = png_create_info_struct(pp); @@ -82,12 +77,16 @@ Fl_Help_View::load_png(Fl_Help_Image *img,// I - Image pointer png_set_expand(pp); if (info->color_type & PNG_COLOR_MASK_COLOR) - img->d = 3; + channels = 3; else - img->d = 1; + channels = 1; if ((info->color_type & PNG_COLOR_MASK_ALPHA) || info->num_trans) - img->d ++; + channels ++; + + w(info->width); + h(info->height); + d(channels); if (info->bit_depth < 8) { @@ -103,31 +102,20 @@ Fl_Help_View::load_png(Fl_Help_Image *img,// I - Image pointer png_set_tRNS_to_alpha(pp); #endif // HAVE_PNG_GET_VALID && HAVE_SET_TRNS_TO_ALPHA - img->w = (int)info->width; - img->h = (int)info->height; - img->data = (unsigned char *)malloc(img->w * img->h * img->d); - - // Background color... - unsigned rgba = fltk_colors[bgcolor_]; - - bg.red = 65535 * (rgba >> 24) / 255; - bg.green = 65535 * ((rgba >> 16) & 255) / 255; - bg.blue = 65535 * ((rgba >> 8) & 255) / 255; - - png_set_background(pp, &bg, PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); + array = new uchar[w() * h() * d()]; // Allocate pointers... - rows = (png_bytep *)calloc(info->height, sizeof(png_bytep)); + rows = new png_bytep[h()]; - for (i = 0; i < (int)info->height; i ++) - rows[i] = img->data + i * img->w * img->d; + for (i = 0; i < h(); i ++) + rows[i] = (png_bytep)(array + i * w() * d()); // Read the image, handling interlacing as needed... for (i = png_set_interlace_handling(pp); i > 0; i --) - png_read_rows(pp, rows, NULL, img->h); + png_read_rows(pp, rows, NULL, h()); // Free memory and return... - free(rows); + delete rows; png_read_end(pp, info); # ifdef HAVE_PNG_READ_DESTROY @@ -136,11 +124,11 @@ Fl_Help_View::load_png(Fl_Help_Image *img,// I - Image pointer png_destroy_read_struct(&pp, &info, NULL); # endif // HAVE_PNG_READ_DESTROY - return (1); -} + fclose(fp); #endif // HAVE_LIBPNG -#endif // 0 +} + // -// End of "$Id: Fl_PNG_Image.cxx,v 1.1.2.1 2001/11/19 01:06:45 easysw Exp $". +// End of "$Id: Fl_PNG_Image.cxx,v 1.1.2.2 2001/11/23 12:06:36 easysw Exp $". // diff --git a/src/Fl_Pixmap.cxx b/src/Fl_Pixmap.cxx index 743e2f1fa..0c6b5b74a 100644 --- a/src/Fl_Pixmap.cxx +++ b/src/Fl_Pixmap.cxx @@ -1,5 +1,5 @@ // -// "$Id: Fl_Pixmap.cxx,v 1.9.2.4.2.7 2001/11/22 15:35:01 easysw Exp $" +// "$Id: Fl_Pixmap.cxx,v 1.9.2.4.2.8 2001/11/23 12:06:36 easysw Exp $" // // Pixmap drawing code for the Fast Light Tool Kit (FLTK). // @@ -56,13 +56,19 @@ void Fl_Pixmap::measure() { void Fl_Pixmap::draw(int XP, int YP, int WP, int HP, int cx, int cy) { // ignore empty or bad pixmap data: - if (!data()) return; + if (!data()) { + draw_empty(XP, YP); + return; + } if (w()<0) measure(); if (WP==-1) { WP = w(); HP = h(); } - if (!w()) return; + if (!w()) { + draw_empty(XP, YP); + return; + } // account for current clip region (faster on Irix): int X,Y,W,H; fl_clip_box(XP,YP,WP,HP,X,Y,W,H); cx += X-XP; cy += Y-YP; @@ -461,5 +467,5 @@ void Fl_Pixmap::desaturate() { } // -// End of "$Id: Fl_Pixmap.cxx,v 1.9.2.4.2.7 2001/11/22 15:35:01 easysw Exp $". +// End of "$Id: Fl_Pixmap.cxx,v 1.9.2.4.2.8 2001/11/23 12:06:36 easysw Exp $". // -- cgit v1.2.3