summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlbrecht Schlosser <albrechts.fltk@online.de>2021-11-09 19:55:35 +0100
committerAlbrecht Schlosser <albrechts.fltk@online.de>2021-11-09 20:36:23 +0100
commit500e470d392a547e6b72ac3fec92e0cd638c0d2f (patch)
tree7e308cfe1aaa4881ac75fdafeffe6bc9518efd62 /src
parent5c5244b1622561809173cbfc41fa9c1290d55fe2 (diff)
Refactor fluid: make fl_write_png() public
The new function fl_write_png() was moved to its own file and is now publicly available ("exported") so other programs can use it. This function was used in fluid to write a window screenshot (.png) together with a template (.fl) to preferences storage.
Diffstat (limited to 'src')
-rw-r--r--src/CMakeLists.txt3
-rw-r--r--src/Makefile3
-rw-r--r--src/fl_write_png.cxx136
-rw-r--r--src/makedepend8
4 files changed, 148 insertions, 2 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 274df8e02..0efbfede4 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,7 +1,7 @@
#
# CMakeLists.txt to build the FLTK library using CMake (www.cmake.org)
#
-# Copyright 1998-2020 by Bill Spitzak and others.
+# Copyright 1998-2021 by Bill Spitzak and others.
#
# This library is free software. Distribution and use rights are outlined in
# the file "COPYING" which should have been included with this file. If this
@@ -424,6 +424,7 @@ set (GLCPPFILES
set (IMGCPPFILES
fl_images_core.cxx
+ fl_write_png.cxx
Fl_BMP_Image.cxx
Fl_File_Icon2.cxx
Fl_GIF_Image.cxx
diff --git a/src/Makefile b/src/Makefile
index 1415be133..0b7b89a92 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,7 +1,7 @@
#
# Library Makefile for the Fast Light Tool Kit (FLTK).
#
-# Copyright 1998-2020 by Bill Spitzak and others.
+# Copyright 1998-2021 by Bill Spitzak and others.
#
# This library is free software. Distribution and use rights are outlined in
# the file "COPYING" which should have been included with this file. If this
@@ -210,6 +210,7 @@ GLCPPFILES += $(GLCPPFILES_$(BUILD))
IMGCPPFILES = \
fl_images_core.cxx \
+ fl_write_png.cxx \
Fl_BMP_Image.cxx \
Fl_File_Icon2.cxx \
Fl_GIF_Image.cxx \
diff --git a/src/fl_write_png.cxx b/src/fl_write_png.cxx
new file mode 100644
index 000000000..e3fa56034
--- /dev/null
+++ b/src/fl_write_png.cxx
@@ -0,0 +1,136 @@
+//
+// Fl_PNG_Image support functions for the Fast Light Tool Kit (FLTK).
+//
+// Copyright 2021 by Bill Spitzak and others.
+//
+// This library is free software. Distribution and use rights are outlined in
+// the file "COPYING" which should have been included with this file. If this
+// file is missing or damaged, see the license at:
+//
+// https://www.fltk.org/COPYING.php
+//
+// Please see the following page on how to report bugs and issues:
+//
+// https://www.fltk.org/bugs.php
+//
+
+#include <config.h>
+#include <FL/Fl_PNG_Image.H>
+#include <FL/Fl_RGB_Image.H>
+#include <FL/fl_string.h>
+#include <FL/fl_utf8.h> // fl_fopen()
+#include <stdio.h>
+
+// PNG library include files
+
+extern "C" {
+#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
+#include <zlib.h>
+#ifdef HAVE_PNG_H
+#include <png.h>
+#else
+#include <libpng/png.h>
+#endif // HAVE_PNG_H
+#endif // HAVE_LIBPNG && HAVE_LIBZ
+} // extern "C"
+
+/**
+ \file fl_write_png.cxx
+
+ PNG image support functions.
+
+*/
+
+/**
+ Write a RGB(A) image to a PNG image file.
+
+ This is a very basic and restricted function to create a PNG image file
+ from an RGB image (Fl_RGB_Image).
+
+ The image data must be aligned w/o gaps, i.e. ld() \b MUST be zero or
+ equal to data_w() * data_h().
+
+ The image file is always written with the original image size data_w()
+ and data_h(), even if the image has been scaled.
+
+ Image depth 3 (RGB) and 4 (RGBA) are supported.
+
+ \note Behavior of grayscale images (depth 1 and 2) is currently undefined
+ and may or may not work. There is no error handling except for opening
+ the file. This function may be changed in the future.
+
+ \param[in] filename Output filename, extension should be '.png'
+ \param[in] img RGB image to be written
+
+ \return success (0) or error code
+
+ \retval 0 success, file has been written
+ \retval -1 png or zlib library not available
+ \retval -2 file open error
+
+ \see fl_write_png(const char *, int, int, int, const unsigned char *)
+*/
+
+int fl_write_png(const char *filename, Fl_RGB_Image *img) {
+ return fl_write_png(filename,
+ img->data_w(), img->data_h(), img->d(),
+ (const unsigned char *)img->data()[0]);
+}
+
+/**
+ Write raw image data to a PNG image file.
+
+ This is a very basic and restricted function to create a PNG image file
+ from raw image data, e.g. a screenshot.
+
+ The image data must be aligned w/o gaps after each row.
+
+ For further restrictions and return values please see
+ fl_write_png(const char *filename, Fl_RGB_Image *img).
+
+ \param[in] filename Output filename, extension should be '.png'
+ \param[in] w Image data width
+ \param[in] h Image data height
+ \param[in] d Image depth (3 = RGB, 4 = RGBA)
+ \param[in] pixels Image data (\p w * \p h * \p d bytes)
+
+ \return success (0) or error code, see ...
+
+ \see fl_write_png(const char *filename, Fl_RGB_Image *img)
+ */
+int fl_write_png(const char *filename, int w, int h, int d, const unsigned char *pixels) {
+
+#if defined(HAVE_LIBPNG) && defined(HAVE_LIBZ)
+
+ FILE *fp;
+
+ if ((fp = fl_fopen(filename, "wb")) == NULL) {
+ return -2;
+ }
+
+ png_structp pptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, 0, 0);
+ png_infop iptr = png_create_info_struct(pptr);
+ png_bytep ptr = (png_bytep)pixels;
+
+ png_init_io(pptr, fp);
+ png_set_IHDR(pptr, iptr, w, h, 8,
+ PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE,
+ PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ png_set_sRGB(pptr, iptr, PNG_sRGB_INTENT_PERCEPTUAL);
+
+ png_write_info(pptr, iptr);
+
+ for (int i = h; i > 0; i--, ptr += w * d) {
+ png_write_row(pptr, ptr);
+ }
+
+ png_write_end(pptr, iptr);
+ png_destroy_write_struct(&pptr, &iptr);
+
+ fclose(fp);
+ return 0;
+
+#else
+ return -1;
+#endif
+}
diff --git a/src/makedepend b/src/makedepend
index 328e057cc..137bc943d 100644
--- a/src/makedepend
+++ b/src/makedepend
@@ -3502,6 +3502,14 @@ Fl_Wizard.o: ../FL/Fl_Widget.H
Fl_Wizard.o: ../FL/Fl_Window.H
Fl_Wizard.o: ../FL/Fl_Wizard.H
Fl_Wizard.o: ../FL/platform_types.h
+fl_write_png.o: ../config.h
+fl_write_png.o: ../FL/Fl_Export.H
+fl_write_png.o: ../FL/Fl_Image.H
+fl_write_png.o: ../FL/Fl_PNG_Image.H
+fl_write_png.o: ../FL/Fl_RGB_Image.H
+fl_write_png.o: ../FL/fl_string.h
+fl_write_png.o: ../FL/fl_types.h
+fl_write_png.o: ../FL/fl_utf8.h
Fl_x.o: ../config.h
Fl_x.o: ../FL/abi-version.h
Fl_x.o: ../FL/Enumerations.H