summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore4
-rw-r--r--LICENSE165
-rw-r--r--Makefile12
-rw-r--r--README76
-rw-r--r--cairo_jpg.c623
-rw-r--r--cairo_jpg.h70
-rw-r--r--genclockimg.c45
7 files changed, 973 insertions, 22 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..b8c641d
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+*~
+*.o
+*.jpg
+*.png
diff --git a/LICENSE b/LICENSE
index 9922fec..0a04128 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,12 +1,165 @@
+ GNU LESSER GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
-Copyright 2022, Stijn Buys
+ Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
-Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
-1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
+ This version of the GNU Lesser General Public License incorporates
+the terms and conditions of version 3 of the GNU General Public
+License, supplemented by the additional permissions listed below.
-2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
+ 0. Additional Definitions.
-3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
+ As used herein, "this License" refers to version 3 of the GNU Lesser
+General Public License, and the "GNU GPL" refers to version 3 of the GNU
+General Public License.
-THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ "The Library" refers to a covered work governed by this License,
+other than an Application or a Combined Work as defined below.
+
+ An "Application" is any work that makes use of an interface provided
+by the Library, but which is not otherwise based on the Library.
+Defining a subclass of a class defined by the Library is deemed a mode
+of using an interface provided by the Library.
+
+ A "Combined Work" is a work produced by combining or linking an
+Application with the Library. The particular version of the Library
+with which the Combined Work was made is also called the "Linked
+Version".
+
+ The "Minimal Corresponding Source" for a Combined Work means the
+Corresponding Source for the Combined Work, excluding any source code
+for portions of the Combined Work that, considered in isolation, are
+based on the Application, and not on the Linked Version.
+
+ The "Corresponding Application Code" for a Combined Work means the
+object code and/or source code for the Application, including any data
+and utility programs needed for reproducing the Combined Work from the
+Application, but excluding the System Libraries of the Combined Work.
+
+ 1. Exception to Section 3 of the GNU GPL.
+
+ You may convey a covered work under sections 3 and 4 of this License
+without being bound by section 3 of the GNU GPL.
+
+ 2. Conveying Modified Versions.
+
+ If you modify a copy of the Library, and, in your modifications, a
+facility refers to a function or data to be supplied by an Application
+that uses the facility (other than as an argument passed when the
+facility is invoked), then you may convey a copy of the modified
+version:
+
+ a) under this License, provided that you make a good faith effort to
+ ensure that, in the event an Application does not supply the
+ function or data, the facility still operates, and performs
+ whatever part of its purpose remains meaningful, or
+
+ b) under the GNU GPL, with none of the additional permissions of
+ this License applicable to that copy.
+
+ 3. Object Code Incorporating Material from Library Header Files.
+
+ The object code form of an Application may incorporate material from
+a header file that is part of the Library. You may convey such object
+code under terms of your choice, provided that, if the incorporated
+material is not limited to numerical parameters, data structure
+layouts and accessors, or small macros, inline functions and templates
+(ten or fewer lines in length), you do both of the following:
+
+ a) Give prominent notice with each copy of the object code that the
+ Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the object code with a copy of the GNU GPL and this license
+ document.
+
+ 4. Combined Works.
+
+ You may convey a Combined Work under terms of your choice that,
+taken together, effectively do not restrict modification of the
+portions of the Library contained in the Combined Work and reverse
+engineering for debugging such modifications, if you also do each of
+the following:
+
+ a) Give prominent notice with each copy of the Combined Work that
+ the Library is used in it and that the Library and its use are
+ covered by this License.
+
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
+ document.
+
+ c) For a Combined Work that displays copyright notices during
+ execution, include the copyright notice for the Library among
+ these notices, as well as a reference directing the user to the
+ copies of the GNU GPL and this license document.
+
+ d) Do one of the following:
+
+ 0) Convey the Minimal Corresponding Source under the terms of this
+ License, and the Corresponding Application Code in a form
+ suitable for, and under terms that permit, the user to
+ recombine or relink the Application with a modified version of
+ the Linked Version to produce a modified Combined Work, in the
+ manner specified by section 6 of the GNU GPL for conveying
+ Corresponding Source.
+
+ 1) Use a suitable shared library mechanism for linking with the
+ Library. A suitable mechanism is one that (a) uses at run time
+ a copy of the Library already present on the user's computer
+ system, and (b) will operate properly with a modified version
+ of the Library that is interface-compatible with the Linked
+ Version.
+
+ e) Provide Installation Information, but only if you would otherwise
+ be required to provide such information under section 6 of the
+ GNU GPL, and only to the extent that such information is
+ necessary to install and execute a modified version of the
+ Combined Work produced by recombining or relinking the
+ Application with a modified version of the Linked Version. (If
+ you use option 4d0, the Installation Information must accompany
+ the Minimal Corresponding Source and Corresponding Application
+ Code. If you use option 4d1, you must provide the Installation
+ Information in the manner specified by section 6 of the GNU GPL
+ for conveying Corresponding Source.)
+
+ 5. Combined Libraries.
+
+ You may place library facilities that are a work based on the
+Library side by side in a single library together with other library
+facilities that are not Applications and are not covered by this
+License, and convey such a combined library under terms of your
+choice, if you do both of the following:
+
+ a) Accompany the combined library with a copy of the same work based
+ on the Library, uncombined with any other library facilities,
+ conveyed under the terms of this License.
+
+ b) Give prominent notice with the combined library that part of it
+ is a work based on the Library, and explaining where to find the
+ accompanying uncombined form of the same work.
+
+ 6. Revised Versions of the GNU Lesser General Public License.
+
+ The Free Software Foundation may publish revised and/or new versions
+of the GNU Lesser General Public License from time to time. Such new
+versions will be similar in spirit to the present version, but may
+differ in detail to address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Library as you received it specifies that a certain numbered version
+of the GNU Lesser General Public License "or any later version"
+applies to it, you have the option of following the terms and
+conditions either of that published version or of any later version
+published by the Free Software Foundation. If the Library as you
+received it does not specify a version number of the GNU Lesser
+General Public License, you may choose any version of the GNU Lesser
+General Public License ever published by the Free Software Foundation.
+
+ If the Library as you received it specifies that a proxy can decide
+whether future versions of the GNU Lesser General Public License shall
+apply, that proxy's public statement of acceptance of any version is
+permanent authorization for you to choose that version for the
+Library.
diff --git a/Makefile b/Makefile
index 3ec2468..9faee5b 100644
--- a/Makefile
+++ b/Makefile
@@ -1,8 +1,8 @@
-CFLAGS = -I/usr/include/cairo -D_XOPEN_SOURCE -g
-LDDFLAGS = -lcairo -lpopt -lm
+CFLAGS = `pkg-config cairo libjpeg popt --cflags` -D_XOPEN_SOURCE -g
+LDDFLAGS = `pkg-config cairo libjpeg popt --libs` -lm
-SRC = genclockimg.o
+SRC = cairo_jpg.o genclockimg.o
TARGET = genclockimg
$(TARGET): $(SRC)
@@ -13,3 +13,9 @@ all:
clean:
rm *.o $(TARGET)
+install:
+ install -o root -g root -m 755 $(target) /usr/bin
+
+uninstall:
+ rm /usr/bin/$(TARGET)
+
diff --git a/README b/README
new file mode 100644
index 0000000..5011153
--- /dev/null
+++ b/README
@@ -0,0 +1,76 @@
+
+GENCLOCKIMG
+
+ genclockimg is a small program to generate clock face PNG images.
+ I made it to generate a time index watermark for my timelapse videos.
+
+ It is written in C and uses cairo and libpopt. These are fairly common
+ and chances are high you already have this installed on your system.
+
+ I used rahra's cairo_jpg library to handle JPEG images.
+ See https://github.com/rahra/cairo_jpg
+
+BUILDING
+
+ To build genclockimg you need libcairo and libpopt and
+ their accompanying dev packages installed. You'll also need
+ a C compiler and GNU Make.
+
+ To build the program:
+
+ make
+
+ To test:
+
+ ./genclockimg
+
+ A file 'out.png' should appear.
+
+ To install:
+
+ make install
+
+ To uninstall:
+
+ make uninstall
+
+RUNNING
+
+ By default, running the program will create a 128x128 PNG image called 'out.png'
+ in the current directory. The image will contain a simple analog clock indicating
+ the current time.
+
+OPTIONS
+
+ -o filename
+ Output filename. The .png extension is NOT added automaticly!
+ The output is always PNG, even if you name the file differently.
+
+ -t HH:MM
+ Time to show on the clock
+
+ -w width
+ Width of the clock image in pixels
+
+ -h height
+ Height of the clock image in pixels
+
+
+EXAMPLES
+
+ Generate a series of clock images based on the timestamps of a series of source images:
+
+ for file in *.jpg; do genclockimg -t `date -r "${file}" +"%H:%M"` -o "${file}-clock.png"; done
+
+LICENSE
+
+ genclockimg is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser
+ General Public License as published by the Free Software Foundation, either version 3 of the License,
+ or (at your option) any later version.
+
+ genclockimg is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along with genclockimg.
+ If not, see https://www.gnu.org/licenses/.
+
+ Copyright (c) 2022, Stijn Buys
diff --git a/cairo_jpg.c b/cairo_jpg.c
new file mode 100644
index 0000000..90759be
--- /dev/null
+++ b/cairo_jpg.c
@@ -0,0 +1,623 @@
+/* Copyright 2018 Bernhard R. Fischer, 4096R/8E24F29D <bf@abenteuerland.at>
+ *
+ * This file is part of Cairo_JPG.
+ *
+ * Cairo_JPG is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Cairo_JPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Cairo_JPG. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+/*! \file cairo_jpg.c
+ * This file contains two functions for reading and writing JPEG files from
+ * and to Cairo image surfaces. It uses the functions from the libjpeg.
+ * Most of the code is directly derived from the online example at
+ * http://libjpeg-turbo.virtualgl.org/Documentation/Documentation
+ *
+ * All prototypes are defined in cairo_jpg.h All functions and their parameters
+ * and return values are described below directly at the functions. You may
+ * also have a look at the preprocessor macros defined below.
+ *
+ * To compile this code you need to have installed the packages libcairo2-dev
+ * and libjpeg-dev. Compile with the following to create an object file to link
+ * with your code:
+ * gcc -std=c99 -Wall -c `pkg-config cairo libjpeg --cflags --libs` cairo_jpg.c
+ * Use the following command to include the main() function and create an
+ * executable for testing of this code:
+ * gcc -std=c99 -Wall -o cairo_jpg -DCAIRO_JPEG_MAIN `pkg-config cairo libjpeg --cflags --libs` cairo_jpg.c
+ *
+ * @author Bernhard R. Fischer, 4096R/8E24F29D bf@abenteuerland.at
+ * @version 2020/01/18
+ * @license LGPL3.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <stdio.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <cairo.h>
+#include <jpeglib.h>
+
+#include "cairo_jpg.h"
+
+/*! Macro to activate main() function. This is only used for testing. Comment
+ * it out (#undef) if you link this file to your own program.
+ */
+//#define CAIRO_JPEG_MAIN
+//
+/*! Define this to use an alternate implementation of
+ * cairo_image_surface_create_from_jpeg() which fstat(3)s the file before
+ * reading (see below). For huge files this /may/ be slightly faster.
+ */
+#undef CAIRO_JPEG_USE_FSTAT
+
+/*! This is the read block size for the stream reader
+ * cairo_image_surface_create_from_jpeg_stream().
+ */
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+#define CAIRO_JPEG_IO_BLOCK_SIZE 4096
+#else
+/*! Block size has to be one if cairo_read_func_t is in use because of the lack
+ * to detect EOF (truncated reads).
+ */
+#define CAIRO_JPEG_IO_BLOCK_SIZE 1
+/*! In case of original cairo_read_func_t is used fstat() should be used for
+ * performance reasons (see CAIRO_JPEG_USE_FSTAT above).
+ */
+#define CAIRO_JPEG_USE_FSTAT
+#endif
+
+/*! Define this to test jpeg creation with non-image surfaces. This is only for
+ * testing and is to be used together with CAIRO_JPEG_MAIN.
+ */
+#undef CAIRO_JPEG_TEST_SIMILAR
+#if defined(CAIRO_JPEG_TEST_SIMILAR) && defined(CAIRO_JPEG_MAIN)
+#include <cairo-pdf.h>
+#endif
+
+
+#ifndef LIBJPEG_TURBO_VERSION
+/*! This function makes a covnersion for "odd" pixel sizes which typically is a
+ * conversion from a 3-byte to a 4-byte (or more) pixel size or vice versa.
+ * The conversion is done from the source buffer src to the destination buffer
+ * dst. The caller MUST ensure that src and dst have the correct memory size.
+ * This is dw * num for dst and sw * num for src. src and dst may point to the
+ * same memory address.
+ * @param dst Pointer to destination buffer.
+ * @param dw Pixel width (in bytes) of pixels in destination buffer, dw >= 3.
+ * @param src Pointer to source buffer.
+ * @param sw Pixel width (in bytes) of pixels in source buffer, sw >= 3.
+ * @param num Number of pixels to convert, num >= 1;
+ */
+static void pix_conv(unsigned char *dst, int dw, const unsigned char *src, int sw, int num)
+{
+ int si, di;
+
+ // safety check
+ if (dw < 3 || sw < 3 || dst == NULL || src == NULL)
+ return;
+
+ num--;
+ for (si = num * sw, di = num * dw; si >= 0; si -= sw, di -= dw)
+ {
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ dst[di + 2] = src[si ];
+ dst[di + 1] = src[si + 1];
+ dst[di + 0] = src[si + 2];
+#else
+ // FIXME: This is untested, it may be wrong.
+ dst[di - 3] = src[si - 3];
+ dst[di - 2] = src[si - 2];
+ dst[di - 1] = src[si - 1];
+#endif
+ }
+}
+#endif
+
+
+/*! This function creates a JPEG file in memory from a Cairo image surface.
+ * @param sfc Pointer to a Cairo surface. It should be an image surface of
+ * either CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_RGB24. Other formats are
+ * converted to CAIRO_FORMAT_RGB24 before compression.
+ * Please note that this may give unexpected results because JPEG does not
+ * support transparency. Thus, default background color is used to replace
+ * transparent regions. The default background color is black if not specified
+ * explicitly. Thus converting e.g. PDF surfaces without having any specific
+ * background color set will apear with black background and not white as you
+ * might expect. In such cases it is suggested to manually convert the surface
+ * to RGB24 before calling this function.
+ * @param data Pointer to a memory pointer. This parameter receives a pointer
+ * to the memory area where the final JPEG data is found in memory. This
+ * function reserves the memory properly and it has to be freed by the caller
+ * with free(3).
+ * @param len Pointer to a variable of type size_t which will receive the final
+ * lenght of the memory buffer.
+ * @param quality Compression quality, 0-100.
+ * @return On success the function returns CAIRO_STATUS_SUCCESS. In case of
+ * error CAIRO_STATUS_INVALID_FORMAT is returned.
+ */
+cairo_status_t cairo_image_surface_write_to_jpeg_mem(cairo_surface_t *sfc, unsigned char **data, size_t *len, int quality)
+{
+ struct jpeg_compress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ JSAMPROW row_pointer[1];
+ cairo_surface_t *other = NULL;
+
+ // check valid input format (must be IMAGE_SURFACE && (ARGB32 || RGB24))
+ if (cairo_surface_get_type(sfc) != CAIRO_SURFACE_TYPE_IMAGE ||
+ (cairo_image_surface_get_format(sfc) != CAIRO_FORMAT_ARGB32 &&
+ cairo_image_surface_get_format(sfc) != CAIRO_FORMAT_RGB24))
+ {
+ // create a similar surface with a proper format if supplied input format
+ // does not fulfill the requirements
+ double x1, y1, x2, y2;
+ other = sfc;
+ cairo_t *ctx = cairo_create(other);
+ // get extents of original surface
+ cairo_clip_extents(ctx, &x1, &y1, &x2, &y2);
+ cairo_destroy(ctx);
+
+ // create new image surface
+ sfc = cairo_surface_create_similar_image(other, CAIRO_FORMAT_RGB24, x2 - x1, y2 - y1);
+ if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
+ return CAIRO_STATUS_INVALID_FORMAT;
+
+ // paint original surface to new surface
+ ctx = cairo_create(sfc);
+ cairo_set_source_surface(ctx, other, 0, 0);
+ cairo_paint(ctx);
+ cairo_destroy(ctx);
+ }
+
+ // finish queued drawing operations
+ cairo_surface_flush(sfc);
+
+ // init jpeg compression structures
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_compress(&cinfo);
+
+ // set compression parameters
+ jpeg_mem_dest(&cinfo, data, len);
+ cinfo.image_width = cairo_image_surface_get_width(sfc);
+ cinfo.image_height = cairo_image_surface_get_height(sfc);
+#ifdef LIBJPEG_TURBO_VERSION
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ //cinfo.in_color_space = JCS_EXT_BGRX;
+ cinfo.in_color_space = cairo_image_surface_get_format(sfc) == CAIRO_FORMAT_ARGB32 ? JCS_EXT_BGRA : JCS_EXT_BGRX;
+#else
+ //cinfo.in_color_space = JCS_EXT_XRGB;
+ cinfo.in_color_space = cairo_image_surface_get_format(sfc) == CAIRO_FORMAT_ARGB32 ? JCS_EXT_ARGB : JCS_EXT_XRGB;
+#endif
+ cinfo.input_components = 4;
+#else
+ cinfo.in_color_space = JCS_RGB;
+ cinfo.input_components = 3;
+#endif
+ jpeg_set_defaults(&cinfo);
+ jpeg_set_quality(&cinfo, quality, TRUE);
+
+ // start compressor
+ jpeg_start_compress(&cinfo, TRUE);
+
+ // loop over all lines and compress
+ while (cinfo.next_scanline < cinfo.image_height)
+ {
+#ifdef LIBJPEG_TURBO_VERSION
+ row_pointer[0] = cairo_image_surface_get_data(sfc) + (cinfo.next_scanline
+ * cairo_image_surface_get_stride(sfc));
+#else
+ unsigned char row_buf[3 * cinfo.image_width];
+ pix_conv(row_buf, 3, cairo_image_surface_get_data(sfc) +
+ (cinfo.next_scanline * cairo_image_surface_get_stride(sfc)), 4, cinfo.image_width);
+ row_pointer[0] = row_buf;
+#endif
+ (void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
+ }
+
+ // finalize and close everything
+ jpeg_finish_compress(&cinfo);
+ jpeg_destroy_compress(&cinfo);
+
+ // destroy temporary image surface (if available)
+ if (other != NULL)
+ cairo_surface_destroy(sfc);
+
+ return CAIRO_STATUS_SUCCESS;
+}
+
+
+/*! This is the internal write function which is called by
+ * cairo_image_surface_write_to_jpeg(). It is not exported.
+ */
+static cairo_status_t cj_write(void *closure, const unsigned char *data, unsigned int length)
+{
+ return write((intptr_t) closure, data, length) < length ?
+ CAIRO_STATUS_WRITE_ERROR : CAIRO_STATUS_SUCCESS;
+}
+
+
+/*! This function writes JPEG file data from a Cairo image surface by using the
+ * user-supplied stream writer function write_func().
+ * @param sfc Pointer to a Cairo *image* surface. Its format must either be
+ * CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_RGB24. Other formats are not supported
+ * by this function, yet.
+ * @param write_func Function pointer to a function which is actually writing
+ * the data.
+ * @param closure Pointer to user-supplied variable which is directly passed to
+ * write_func().
+ * @param quality Compression quality, 0-100.
+ * @return This function calles cairo_image_surface_write_to_jpeg_mem() and
+ * returns its return value.
+ */
+cairo_status_t cairo_image_surface_write_to_jpeg_stream(cairo_surface_t *sfc, cairo_write_func_t write_func, void *closure, int quality)
+{
+ cairo_status_t e;
+ unsigned char *data = NULL;
+ size_t len = 0;
+
+ // create JPEG data in memory from surface
+ if ((e = cairo_image_surface_write_to_jpeg_mem(sfc, &data, &len, quality)) != CAIRO_STATUS_SUCCESS)
+ return e;
+
+ // write whole memory block with stream function
+ e = write_func(closure, data, len);
+
+ // free JPEG memory again and return the return value
+ free(data);
+ return e;
+
+}
+
+
+/*! This function creates a JPEG file from a Cairo image surface.
+ * @param sfc Pointer to a Cairo *image* surface. Its format must either be
+ * CAIRO_FORMAT_ARGB32 or CAIRO_FORMAT_RGB24. Other formats are not supported
+ * by this function, yet.
+ * @param filename Pointer to the filename.
+ * @param quality Compression quality, 0-100.
+ * @return In case of success CAIRO_STATUS_SUCCESS is returned. If an error
+ * occured while opening/creating the file CAIRO_STATUS_DEVICE_ERROR is
+ * returned. The error can be tracked down by inspecting errno(3). The function
+ * internally calles cairo_image_surface_write_to_jpeg_stream() and returnes
+ * its return value respectively (see there).
+ */
+cairo_status_t cairo_image_surface_write_to_jpeg(cairo_surface_t *sfc, const char *filename, int quality)
+{
+ cairo_status_t e;
+ int outfile;
+
+ // Open/create new file
+ if ((outfile = open(filename, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) == -1)
+ return CAIRO_STATUS_DEVICE_ERROR;
+
+ // write surface to file
+ e = cairo_image_surface_write_to_jpeg_stream(sfc, cj_write, (void*)(intptr_t) outfile, quality);
+
+ // close file again and return
+ close(outfile);
+ return e;
+}
+
+
+
+/*! This function decompresses a JPEG image from a memory buffer and creates a
+ * Cairo image surface.
+ * @param data Pointer to JPEG data (i.e. the full contents of a JPEG file read
+ * into this buffer).
+ * @param len Length of buffer in bytes.
+ * @return Returns a pointer to a cairo_surface_t structure. It should be
+ * checked with cairo_surface_status() for errors.
+ */
+cairo_surface_t *cairo_image_surface_create_from_jpeg_mem(void *data, size_t len)
+{
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ JSAMPROW row_pointer[1];
+ cairo_surface_t *sfc;
+
+ // initialize jpeg decompression structures
+ cinfo.err = jpeg_std_error(&jerr);
+ jpeg_create_decompress(&cinfo);
+ jpeg_mem_src(&cinfo, data, len);
+ (void) jpeg_read_header(&cinfo, TRUE);
+
+#ifdef LIBJPEG_TURBO_VERSION
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ cinfo.out_color_space = JCS_EXT_BGRA;
+#else
+ cinfo.out_color_space = JCS_EXT_ARGB;
+#endif
+#else
+ cinfo.out_color_space = JCS_RGB;
+#endif
+
+ // start decompressor
+ (void) jpeg_start_decompress(&cinfo);
+
+ // create Cairo image surface
+ sfc = cairo_image_surface_create(CAIRO_FORMAT_RGB24, cinfo.output_width, cinfo.output_height);
+ if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
+ {
+ jpeg_destroy_decompress(&cinfo);
+ return sfc;
+ }
+
+ // loop over all scanlines and fill Cairo image surface
+ while (cinfo.output_scanline < cinfo.output_height)
+ {
+ unsigned char *row_address = cairo_image_surface_get_data(sfc) +
+ (cinfo.output_scanline * cairo_image_surface_get_stride(sfc));
+ row_pointer[0] = row_address;
+ (void) jpeg_read_scanlines(&cinfo, row_pointer, 1);
+#ifndef LIBJPEG_TURBO_VERSION
+ pix_conv(row_address, 4, row_address, 3, cinfo.output_width);
+#endif
+ }
+
+ // finish and close everything
+ cairo_surface_mark_dirty(sfc);
+ (void) jpeg_finish_decompress(&cinfo);
+ jpeg_destroy_decompress(&cinfo);
+
+ // set jpeg mime data
+ cairo_surface_set_mime_data(sfc, CAIRO_MIME_TYPE_JPEG, data, len, free, data);
+
+ return sfc;
+}
+
+
+/*! This function reads a JPEG image from a stream and creates a Cairo image
+ * surface.
+ * @param read_func Pointer to function which reads data.
+ * @param closure Pointer which is passed to read_func().
+ * @return Returns a pointer to a cairo_surface_t structure. It should be
+ * checked with cairo_surface_status() for errors.
+ * @note If the surface returned is invalid you can use errno(3) to determine
+ * further reasons. Errno is set according to realloc(3). If you
+ * intend to check errno you shall set it to 0 before calling this function
+ * because it modifies errno only in case of an error.
+ */
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+cairo_surface_t *cairo_image_surface_create_from_jpeg_stream(cairo_read_func_len_t read_func, void *closure)
+#else
+cairo_surface_t *cairo_image_surface_create_from_jpeg_stream(cairo_read_func_t read_func, void *closure)
+#endif
+{
+ cairo_surface_t *sfc;
+ void *data, *tmp;
+ ssize_t len, rlen;
+ int eof = 0;
+
+ // read all data into memory buffer in blocks of CAIRO_JPEG_IO_BLOCK_SIZE
+ for (len = 0, data = NULL; !eof; len += rlen)
+ {
+ // grow memory buffer and check for error
+ if ((tmp = realloc(data, len + CAIRO_JPEG_IO_BLOCK_SIZE)) == NULL)
+ break;
+ data = tmp;
+
+ // read bytes into buffer and check for error
+ rlen = read_func(closure, data + len, CAIRO_JPEG_IO_BLOCK_SIZE);
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+ // check for error
+ if (rlen == -1)
+ break;
+
+ // check if EOF occured
+ if (rlen < CAIRO_JPEG_IO_BLOCK_SIZE)
+ eof++;
+#else
+ // check for error
+ if (rlen == CAIRO_STATUS_READ_ERROR)
+ eof++;
+#endif
+ }
+
+ // check for error in read loop
+ if (!eof)
+ {
+ free(data);
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+ }
+
+ // call jpeg decompression and return surface
+ sfc = cairo_image_surface_create_from_jpeg_mem(data, len);
+ if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
+ free(data);
+
+ return sfc;
+}
+
+
+#ifdef CAIRO_JPEG_USE_FSTAT
+/*! This function reads an JPEG image from a file an creates a Cairo image
+ * surface. Internally the filesize is determined with fstat(2) and then the
+ * whole data is read at once.
+ * @param filename Pointer to filename of JPEG file.
+ * @return Returns a pointer to a cairo_surface_t structure. It should be
+ * checked with cairo_surface_status() for errors.
+ * @note If the returned surface is invalid you can use errno to determine
+ * further reasons. Errno is set according to fopen(3) and malloc(3). If you
+ * intend to check errno you shall set it to 0 before calling this function
+ * because it does not modify errno itself.
+ */
+cairo_surface_t *cairo_image_surface_create_from_jpeg(const char *filename)
+{
+ void *data;
+ int infile;
+ struct stat stat;
+
+ // open input file
+ if ((infile = open(filename, O_RDONLY)) == -1)
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+
+ // get stat structure for file size
+ if (fstat(infile, &stat) == -1)
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+
+ // allocate memory
+ if ((data = malloc(stat.st_size)) == NULL)
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+
+ // read data
+ if (read(infile, data, stat.st_size) < stat.st_size)
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+
+ close(infile);
+
+ return cairo_image_surface_create_from_jpeg_mem(data, stat.st_size);
+}
+
+#else
+
+
+/*! This is the read function which is called by
+ * cairo_image_surface_create_from_jpeg_stream() (non-fstat-version below). It
+ * is not exported.
+ */
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+static ssize_t cj_read(void *closure, unsigned char *data, unsigned int length)
+{
+ return read((intptr_t) closure, data, length);
+}
+#else
+static cairo_status_t cj_read(void *closure, unsigned char *data, unsigned int length)
+{
+ return read((intptr_t) closure, data, length) < length ? CAIRO_STATUS_READ_ERROR : CAIRO_STATUS_SUCCESS;
+}
+#endif
+
+
+/*! This function reads an JPEG image from a file an creates a Cairo image
+ * surface. Internally the function calls
+ * cairo_image_surface_create_from_jpeg_stream() to actually read the data.
+ * @param filename Pointer to filename of JPEG file.
+ * @return Returns a pointer to a cairo_surface_t structure. It should be
+ * checked with cairo_surface_status() for errors.
+ * @note If the returned surface is invalid you can use errno to determine
+ * further reasons. Errno is set according to fopen(3) and malloc(3). If you
+ * intend to check errno you shall set it to 0 before calling this function
+ * because it does not modify errno itself.
+ */
+cairo_surface_t *cairo_image_surface_create_from_jpeg(const char *filename)
+{
+ cairo_surface_t *sfc;
+ int infile;
+
+ // open input file
+ if ((infile = open(filename, O_RDONLY)) == -1)
+ return cairo_image_surface_create(CAIRO_FORMAT_INVALID, 0, 0);
+
+ // call stream loading function
+ sfc = cairo_image_surface_create_from_jpeg_stream(cj_read, (void*)(intptr_t) infile);
+ close(infile);
+
+ return sfc;
+}
+
+#endif
+
+
+#ifdef CAIRO_JPEG_MAIN
+#include <string.h>
+#include <strings.h>
+
+int strrcasecmp(const char *s1, const char *s2)
+{
+ int off = (int) strlen(s1) - (int) strlen(s2); // typecast size_t to int because size_t typically is unsigned
+ return strcasecmp(s1 + (off < 0 ? 0 : off), s2);
+}
+
+/*! Main routine, only for testing. #undef CAIRO_JPEG_MAIN or simply delete
+ * this part if you link this file to your own program.
+ */
+int main(int argc, char **argv)
+{
+ cairo_surface_t *sfc;
+
+#ifndef CAIRO_JPEG_TEST_SIMILAR
+ if (argc < 3)
+ {
+ fprintf(stderr, "usage: %s <infile> <outfile>\n", argv[0]);
+ return 1;
+ }
+
+ // test input file type and read file
+ if (!strrcasecmp(argv[1], ".png"))
+ {
+ // read PNG file
+ sfc = cairo_image_surface_create_from_png(argv[1]);
+ }
+ else if (!strrcasecmp(argv[1], ".jpg"))
+ {
+ // read JPEG file
+ sfc = cairo_image_surface_create_from_jpeg(argv[1]);
+ }
+ else
+ {
+ fprintf(stderr, "source file is neither JPG nor PNG\n");
+ return 1;
+ }
+
+ // check surface status
+ if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
+ {
+ fprintf(stderr, "error loading image: %s", cairo_status_to_string(cairo_surface_status(sfc)));
+ return 2;
+ }
+
+ // test output file type and write file
+ if (!strrcasecmp(argv[2], ".png"))
+ {
+ // write PNG file
+ cairo_surface_write_to_png(sfc, argv[2]);
+ }
+ else if (!strrcasecmp(argv[2], ".jpg"))
+ {
+ // write JPEG file
+ cairo_image_surface_write_to_jpeg(sfc, argv[2], 90);
+ }
+ else
+ {
+ fprintf(stderr, "destination file is neither JPG nor PNG\n");
+ return 1;
+ }
+
+ cairo_surface_destroy(sfc);
+
+#else
+ sfc = cairo_pdf_surface_create("xyz.pdf", 595.276, 841.890);
+
+ cairo_t *ctx = cairo_create(sfc);
+ cairo_set_source_rgb(ctx, 1, 1, 1);
+ cairo_paint(ctx);
+ cairo_move_to(ctx, 100, 100);
+ cairo_set_source_rgb(ctx, 1, 0, 0);
+ cairo_set_line_width(ctx, 3);
+ cairo_line_to(ctx, 400, 400);
+ cairo_stroke(ctx);
+ cairo_destroy(ctx);
+
+ cairo_image_surface_write_to_jpeg(sfc, "xyz.jpg", 90);
+ cairo_surface_destroy(sfc);
+#endif
+
+ return 0;
+}
+
+#endif
+
diff --git a/cairo_jpg.h b/cairo_jpg.h
new file mode 100644
index 0000000..da73220
--- /dev/null
+++ b/cairo_jpg.h
@@ -0,0 +1,70 @@
+/* Copyright 2018 Bernhard R. Fischer, 4096R/8E24F29D <bf@abenteuerland.at>
+ *
+ * This file is part of Cairo_JPG.
+ *
+ * Cairo_JPG is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Cairo_JPG is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Cairo_JPG. If not, see <https://www.gnu.org/licenses/>.
+ */
+
+#ifndef CAIRO_JPEG_H
+#define CAIRO_JPEG_H
+
+/*! \file cairo_jpg.h
+ * This file contains all prototypes for the Cairo-JPEG functions implemented
+ * in cairo_jpg.c. See there for the function documentation.
+ *
+ * @author Bernhard R. Fischer, 4096R/8E24F29D <bf@abenteuerland.at>
+ * @version 2018/12/11
+ * @license LGPL3
+ */
+
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+#include <cairo.h>
+
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+/*! This is the type for the stream read function. Which must be implemented by
+ * the user if cairo_image_surface_create_from_jpeg_stream() is used. Please
+ * note that this prototype is slightly different from cairo_read_func_t which
+ * is used by cairo_image_surface_create_from_png_stream().
+ * This new prototype is defined because the original prototype
+ * cairo_read_func_t does not allow to detect truncated reads. This issue was
+ * discussed on the cairographics mailinglist, see
+ * https://lists.cairographics.org/archives/cairo/2016-March/027298.html
+ * @param closure This parameter is directly passed through by
+ * cairo_image_surface_create_from_jpeg_stream().
+ * @param data Pointer to data buffer which will receive the data.
+ * @param length Size of the data buffer in bytes.
+ * @return This function must return the actually length that was read into the
+ * buffer. This may actually be less than length which indicates an EOF. In
+ * case of any fatal unrecoverable error on the input stream -1 shall be
+ * returned.
+ */
+typedef ssize_t (*cairo_read_func_len_t) (void *closure, unsigned char *data, unsigned int length);
+#endif
+
+
+cairo_status_t cairo_image_surface_write_to_jpeg_mem(cairo_surface_t *sfc, unsigned char **data, size_t *len, int quality);
+cairo_status_t cairo_image_surface_write_to_jpeg_stream(cairo_surface_t *sfc, cairo_write_func_t write_func, void *closure, int quality);
+cairo_status_t cairo_image_surface_write_to_jpeg(cairo_surface_t *sfc, const char *filename, int quality);
+cairo_surface_t *cairo_image_surface_create_from_jpeg_mem(void *data, size_t len);
+#ifdef USE_CAIRO_READ_FUNC_LEN_T
+cairo_surface_t *cairo_image_surface_create_from_jpeg_stream(cairo_read_func_len_t read_func, void *closure);
+#else
+cairo_surface_t *cairo_image_surface_create_from_jpeg_stream(cairo_read_func_t read_func, void *closure);
+#endif
+cairo_surface_t *cairo_image_surface_create_from_jpeg(const char *filename);
+
+#endif
+
diff --git a/genclockimg.c b/genclockimg.c
index e85a4ee..cb303d4 100644
--- a/genclockimg.c
+++ b/genclockimg.c
@@ -2,15 +2,17 @@
genclockimg
generates a clock face PNG image
*/
+
#include <cairo.h>
#include <popt.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
-
#include <math.h>
+#include "cairo_jpg.h"
+
int main (int argc, const char **argv)
{
@@ -21,6 +23,8 @@ int main (int argc, const char **argv)
char img_filename_default[] = "out.png\0";
char *timestr;
+ int use_jpeg;
+
/* timestamp represeting what time to draw */
time_t clock_timestamp;
struct tm *clock_localtime;
@@ -39,6 +43,7 @@ int main (int argc, const char **argv)
{"hours", '\0', POPT_ARG_INT, &h, 0, "hour to display", NULL },
{"minutes", '\0', POPT_ARG_INT, &m, 0, "minutes to display", NULL },
{"time", 't', POPT_ARG_STRING, &timestr, 0, "time to display as HH:MM", NULL},
+ {"jpg", 'j', POPT_ARG_NONE, &use_jpeg, 0, "Write a JPEG file", NULL},
POPT_AUTOHELP
{NULL, '\0', 0, NULL, 0, NULL, NULL}
};
@@ -46,9 +51,9 @@ int main (int argc, const char **argv)
poptContext options_context;
/* default filename is out.png */
- img_filename = NULL;
-
+ img_filename = NULL;
timestr = NULL;
+ use_jpeg = 0;
/* default image size is 128x128 */
img_width = 128;
@@ -64,12 +69,18 @@ int main (int argc, const char **argv)
options_context = poptGetContext(NULL, argc, argv, options_table, 0);
int result;
- while ((result = poptGetNextOpt(options_context)) > 0 ) {
+ while ((result = poptGetNextOpt(options_context)) > 0 ) {
}
if (result < -1) {
- /* an error occurred during option processing */
- fprintf(stderr, "%s: %s\n", poptBadOption(options_context, POPT_BADOPTION_NOALIAS), poptStrerror(result));
- exit(1);
+ /* an error occurred during option processing */
+ fprintf(stderr, "%s: %s\n", poptBadOption(options_context, POPT_BADOPTION_NOALIAS),poptStrerror(result));
+ exit(1);
+ }
+
+ if (use_jpeg) {
+ img_filename_default[4] = 'j';
+ img_filename_default[5] = 'p';
+ img_filename_default[6] = 'g';
}
if (img_filename == NULL) {
@@ -92,7 +103,7 @@ int main (int argc, const char **argv)
context = cairo_create(surface);
/* draw image */
- cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 0.0);
+ cairo_set_source_rgba(context, 0.0, 0.0, 0.0, 1.0);
cairo_rectangle(context, 0, 0, img_width, img_height);
cairo_fill(context);
@@ -107,18 +118,20 @@ int main (int argc, const char **argv)
}
float angle = 2.0f * M_PI / 12.0f;
- /* draw clock face */
+ /* draw clock background */
+ /*
cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0);
cairo_arc(context, x1, y1, r-4, 0, 2 * M_PI);
cairo_fill(context);
-
- cairo_set_source_rgba (context, 0.0, 0.0, 0.0, 1.0);
+ */
+ /* draw clock face */
+ cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0);
cairo_set_line_width (context, 1);
cairo_arc(context, x1, y1, r-4, 0, 2 * M_PI);
cairo_stroke(context);
/* draw markers */
- cairo_set_source_rgba (context, 0.0, 0.0, 0.0, 1.0);
+ cairo_set_source_rgba (context, 1.0, 1.0, 1.0, 1.0);
cairo_set_line_width (context, 2);
for (int h = 0; h < 12; h++) {
cairo_move_to(context, x1 + (r - 10) * cosf(angle * h), y1 + (r - 10) * sinf(angle * h));
@@ -140,7 +153,13 @@ int main (int argc, const char **argv)
/* write image file */
- if (cairo_surface_write_to_png(surface, img_filename) != CAIRO_STATUS_SUCCESS) {
+ cairo_status_t status = CAIRO_STATUS_SUCCESS;
+ if (use_jpeg) {
+ status = cairo_image_surface_write_to_jpeg(surface, img_filename, 100);
+ } else {
+ status = cairo_surface_write_to_png(surface, img_filename);
+ }
+ if (status != CAIRO_STATUS_SUCCESS) {
fprintf(stderr, "Error saving %s\n", img_filename);
}