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.
This commit is contained in:
Albrecht Schlosser 2021-11-09 19:55:35 +01:00
parent 5c5244b162
commit 500e470d39
6 changed files with 157 additions and 26 deletions

View File

@ -1,7 +1,7 @@
//
// PNG image header file for the Fast Light Tool Kit (FLTK).
//
// Copyright 1998-2010 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
@ -37,4 +37,10 @@ private:
void load_png_(const char *name_png, const unsigned char *buffer_png, int datasize);
};
// Support functions to write PNG image files (since 1.4.0)
FL_EXPORT int fl_write_png(const char *filename, int w, int h, int d, const unsigned char *pixels);
FL_EXPORT int fl_write_png(const char *filename, Fl_RGB_Image *img);
#endif

View File

@ -347,34 +347,13 @@ void save_template_cb(Fl_Widget *, void *) {
// Save to a PNG file...
strcpy(ext, ".png");
FILE *fp;
if ((fp = fl_fopen(filename, "wb")) == NULL) {
errno = 0;
if (fl_write_png(filename, w, h, 3, pixels) != 0) {
delete[] pixels;
fl_alert("Error writing %s: %s", filename, strerror(errno));
return;
}
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 * 3) {
png_write_row(pptr, ptr);
}
png_write_end(pptr, iptr);
png_destroy_write_struct(&pptr, &iptr);
fclose(fp);
# if 0 // The original PPM output code...
strcpy(ext, ".ppm");
fp = fl_fopen(filename, "wb");

View File

@ -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

View File

@ -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 \

136
src/fl_write_png.cxx Normal file
View File

@ -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
}

View File

@ -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