Commit c1021a3e authored by Benoit Touchette's avatar Benoit Touchette Committed by Michael Natterer

Bug 769651 - Add webp file format support to gimp master

Patch to add webp file format support. Includes supports for
loading/saving, icc profiles, exif, and xmp data.
Co-authored with Nathan Osman.
parent 98e43b94
......@@ -75,6 +75,9 @@ m4_define([openexr_required_version], [1.6.1])
m4_define([gtk_mac_integration_required_version], [2.0.0])
m4_define([intltool_required_version], [0.40.1])
m4_define([python2_required_version], [2.5.0])
m4_define([webp_required_version], [0.5.1])
m4_define([webpmux_required_version], [0.5.1])
m4_define([webpdemux_required_version], [0.5.1])
# Current test considers only 2 version numbers. If we update the recommended
# version of gettext with more version numbers, please update the tests.
......@@ -158,6 +161,9 @@ POPPLER_DATA_REQUIRED_VERSION=poppler_data_required_version
OPENEXR_REQUIRED_VERSION=openexr_required_version
INTLTOOL_REQUIRED_VERSION=intltool_required_version
PYTHON2_REQUIRED_VERSION=python2_required_version
WEBP_REQUIRED_VERSION=webp_required_version
WEBPMUX_REQUIRED_VERSION=webpmux_required_version
WEBPDEMUX_REQUIRED_VERSION=webpdemux_required_version
XGETTEXT_RECOMMENDED_VERSION=xgettext_recommended_version
AC_SUBST(GLIB_REQUIRED_VERSION)
AC_SUBST(GDK_PIXBUF_REQUIRED_VERSION)
......@@ -185,6 +191,9 @@ AC_SUBST(POPPLER_DATA_REQUIRED_VERSION)
AC_SUBST(OPENEXR_REQUIRED_VERSION)
AC_SUBST(INTLTOOL_REQUIRED_VERSION)
AC_SUBST(PYTHON2_REQUIRED_VERSION)
AC_SUBST(WEBP_REQUIRED_VERSION)
AC_SUBST(WEBPMUX_REQUIRED_VERSION)
AC_SUBST(WEBPDEMUX_REQUIRED_VERSION)
AC_SUBST(XGETTEXT_RECOMMENDED_VERSION)
# The symbol GIMP_UNSTABLE is defined above for substitution in
......@@ -1548,6 +1557,32 @@ AC_SUBST(FILE_EXR)
AM_CONDITIONAL(HAVE_OPENEXR, test "x$have_openexr" = xyes)
################
# Check for WebP
################
AC_ARG_WITH(webp, [ --without-webp build without WebP support])
have_webp=no
if test "x$with_webp" != xno; then
have_webp=yes
PKG_CHECK_MODULES(WEBP, libwebp >= webp_required_version,,
[have_webp="no (WebP not found)"])
PKG_CHECK_MODULES(WEBPMUX, libwebpmux >= webpmux_required_version,,
[have_webp="no (WebP not found)"])
PKG_CHECK_MODULES(WEBPDEMUX, libwebpdemux >= webpdemux_required_version,,
[have_webp="no (WebP not found)"])
fi
if test "x$have_webp" = xyes; then
MIME_TYPES="$MIME_TYPES;image/x-webp"
fi
AM_CONDITIONAL(HAVE_WEBP, test "x$have_webp" = xyes)
######################
# Check for libmypaint
######################
......@@ -2400,6 +2435,7 @@ plug-ins/file-jpeg/Makefile
plug-ins/file-psd/Makefile
plug-ins/file-sgi/Makefile
plug-ins/file-tiff/Makefile
plug-ins/file-webp/Makefile
plug-ins/flame/Makefile
plug-ins/fractal-explorer/Makefile
plug-ins/fractal-explorer/examples/Makefile
......@@ -2554,6 +2590,7 @@ Optional Plug-Ins:
JPEG 2000: $have_jp2
MNG: $have_libmng
OpenEXR: $have_openexr
WebP: $have_webp
PDF (import): $have_poppler
PDF (export): $have_cairo_pdf
Print: $enable_print
......
......@@ -25,6 +25,10 @@ if OS_WIN32
twain = twain
endif
if HAVE_WEBP
file_webp = file-webp
endif
SUBDIRS = \
$(script_fu) \
$(pygimp) \
......@@ -39,6 +43,7 @@ SUBDIRS = \
file-psd \
file-sgi \
file-tiff \
$(file_webp) \
flame \
fractal-explorer \
gfig \
......
/Makefile.in
/Makefile
/.deps
/_libs
/.libs
/file-webp
/file-webp.exe
## Process this file with automake to produce Makefile.in
libgimpui = $(top_builddir)/libgimp/libgimpui-$(GIMP_API_VERSION).la
libgimpconfig = $(top_builddir)/libgimpconfig/libgimpconfig-$(GIMP_API_VERSION).la
libgimpwidgets = $(top_builddir)/libgimpwidgets/libgimpwidgets-$(GIMP_API_VERSION).la
libgimp = $(top_builddir)/libgimp/libgimp-$(GIMP_API_VERSION).la
libgimpcolor = $(top_builddir)/libgimpcolor/libgimpcolor-$(GIMP_API_VERSION).la
libgimpbase = $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la
libgimpmath = $(top_builddir)/libgimpmath/libgimpmath-$(GIMP_API_VERSION).la
if OS_WIN32
mwindows = -mwindows
endif
if HAVE_WINDRES
include $(top_srcdir)/build/windows/gimprc-plug-ins.rule
file_webp_RC = file-webp.rc.o
endif
AM_LDFLAGS = $(mwindows)
libexecdir = $(gimpplugindir)/plug-ins
AM_CPPFLAGS = \
-I$(top_srcdir) \
$(GTK_CFLAGS) \
$(EXIF_CFLAGS) \
$(GEGL_CFLAGS) \
$(GEXIV2_CFLAGS) \
$(WEBP_CFLAGS) \
$(WEBPMUX_CFLAGS) \
$(WEBPDEMUX_CFLAGS) \
-I$(includedir)
libexec_PROGRAMS = file-webp
file_webp_SOURCES = \
file-webp.c \
file-webp.h \
file-webp-dialog.c \
file-webp-dialog.h \
file-webp-load.c \
file-webp-load.h \
file-webp-save.c \
file-webp-save.h
file_webp_LDADD = \
$(libgimpui) \
$(libgimpwidgets) \
$(libgimpconfig) \
$(libgimp) \
$(libgimpcolor) \
$(libgimpmath) \
$(libgimpbase) \
$(webp_LIBS) \
$(GTK_LIBS) \
$(GEGL_LIBS) \
$(GEXIV2_LIBS) \
$(WEBP_LIBS) \
$(WEBPMUX_LIBS) \
$(WEBPDEMUX_LIBS) \
$(RT_LIBS) \
$(INTLLIBS) \
$(file_webp_RC)
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* file-webp - WebP file format plug-in for the GIMP
* Copyright (C) 2015 Nathan Osman
* Copyright (C) 2016 Ben Touchette
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include "file-webp.h"
#include "file-webp-dialog.h"
#include "libgimp/stdplugins-intl.h"
void save_dialog_response (GtkWidget *widget,
gint response_id,
gpointer data);
GtkListStore * save_dialog_presets (void);
void save_dialog_set_preset (GtkWidget *widget,
gpointer data);
void save_dialog_toggle_scale (GtkWidget *widget,
gpointer data);
struct
{
const gchar *id;
const gchar *label;
} presets[] =
{
{ "default", "Default" },
{ "picture", "Picture" },
{ "photo", "Photo" },
{ "drawing", "Drawing" },
{ "icon", "Icon" },
{ "text", "Text" },
{ 0 }
};
void
save_dialog_response (GtkWidget *widget,
gint response_id,
gpointer data)
{
/* Store the response */
*(GtkResponseType *)data = response_id;
/* Close the dialog */
gtk_widget_destroy (widget);
}
GtkListStore *
save_dialog_presets (void)
{
GtkListStore *list_store;
int i;
/* Create the model */
list_store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING);
/* Insert the entries */
for (i = 0; presets[i].id; ++i)
{
gtk_list_store_insert_with_values (list_store,
NULL,
-1,
0, presets[i].id,
1, presets[i].label,
-1);
}
return list_store;
}
void
save_dialog_set_preset (GtkWidget *widget,
gpointer data)
{
*(gchar **) data =
gimp_string_combo_box_get_active (GIMP_STRING_COMBO_BOX (widget));
}
void
save_dialog_toggle_scale (GtkWidget *widget,
gpointer data)
{
gimp_scale_entry_set_sensitive (GTK_OBJECT (data),
! gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (widget)));
}
GtkResponseType
save_dialog (WebPSaveParams *params,
gint32 image_ID,
gint32 nLayers)
{
GtkWidget *dialog;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *table;
GtkWidget *preset_label;
GtkListStore *preset_list;
GtkWidget *preset_combo;
GtkWidget *lossless_checkbox;
GtkWidget *animation_checkbox;
GtkWidget *loop_anim_checkbox;
GtkObject *quality_scale;
GtkObject *alpha_quality_scale;
GtkResponseType response;
gboolean animation_supported = FALSE;
int slider1 , slider2;
animation_supported = nLayers > 1;
/* Create the dialog */
dialog = gimp_export_dialog_new (_("WebP"),BINARY_NAME,
SAVE_PROCEDURE);
/* Store the response when the dialog is closed */
g_signal_connect (dialog, "response",
G_CALLBACK (save_dialog_response),
&response);
/* Quit the main loop when the dialog is closed */
g_signal_connect (dialog, "destroy",
G_CALLBACK (gtk_main_quit),
NULL);
/* Create the vbox */
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 6);
gtk_box_pack_start (GTK_BOX (gimp_export_dialog_get_content_area (dialog)),
vbox, FALSE, FALSE, 0);
gtk_widget_show (vbox);
/* Create the descriptive label at the top */
label = gtk_label_new (_("Use the options below to customize the image."));
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
/* Create the table */
table = gtk_table_new (4, 5, FALSE);
gtk_table_set_row_spacings (GTK_TABLE (table), 6);
gtk_table_set_col_spacings (GTK_TABLE (table), 6);
gtk_box_pack_start (GTK_BOX (vbox), table, FALSE, FALSE, 0);
gtk_widget_show (table);
/* Create the label for the selecting a preset */
preset_label = gtk_label_new (_("Preset:"));
gtk_table_attach (GTK_TABLE (table), preset_label,
0, 1, 0, 1,
0, 0, 0, 0);
gtk_widget_show (preset_label);
/* Create the combobox containing the presets */
preset_list = save_dialog_presets ();
preset_combo = gimp_string_combo_box_new (GTK_TREE_MODEL (preset_list), 0, 1);
g_object_unref (preset_list);
gimp_string_combo_box_set_active (GIMP_STRING_COMBO_BOX (preset_combo),
params->preset);
gtk_table_attach (GTK_TABLE (table), preset_combo,
1, 3, 0, 1,
GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show (preset_combo);
g_signal_connect (preset_combo, "changed",
G_CALLBACK (save_dialog_set_preset),
&params->preset);
/* Create the lossless checkbox */
lossless_checkbox = gtk_check_button_new_with_label (_("Lossless"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (lossless_checkbox),
params->lossless);
gtk_table_attach (GTK_TABLE (table), lossless_checkbox,
1, 3, 1, 2,
GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show (lossless_checkbox);
g_signal_connect (lossless_checkbox, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&params->lossless);
slider1 = 2;
slider2 = 3;
if (animation_supported)
{
slider1 = 4;
slider2 = 5;
/* Create the animation checkbox */
animation_checkbox = gtk_check_button_new_with_label (_("Use animation"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (animation_checkbox),
params->animation);
gtk_table_attach (GTK_TABLE (table), animation_checkbox,
1, 3, 2, 3,
GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show (animation_checkbox);
g_signal_connect (animation_checkbox, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&params->animation);
/* Create the loop animation checkbox */
loop_anim_checkbox = gtk_check_button_new_with_label (_("Loop infinitely"));
gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (loop_anim_checkbox),
params->loop);
gtk_table_attach (GTK_TABLE (table), loop_anim_checkbox,
1, 3, 3, 4,
GTK_FILL, GTK_FILL, 0, 0);
gtk_widget_show (loop_anim_checkbox);
g_signal_connect (loop_anim_checkbox, "toggled",
G_CALLBACK (gimp_toggle_button_update),
&params->loop);
}
/* Create the slider for image quality */
quality_scale = gimp_scale_entry_new (GTK_TABLE (table),
0, slider1,
_("Image quality:"),
125,
0,
params->quality,
0.0, 100.0,
1.0, 10.0,
0, TRUE,
0.0, 0.0,
_("Image quality"),
NULL);
gimp_scale_entry_set_sensitive (quality_scale, ! params->lossless);
g_signal_connect (quality_scale, "value-changed",
G_CALLBACK (gimp_float_adjustment_update),
&params->quality);
/* Create the slider for alpha channel quality */
alpha_quality_scale = gimp_scale_entry_new (GTK_TABLE (table),
0, slider2,
_("Alpha quality:"),
125,
0,
params->alpha_quality,
0.0, 100.0,
1.0, 10.0,
0, TRUE,
0.0, 0.0,
_("Alpha channel quality"),
NULL);
gimp_scale_entry_set_sensitive (alpha_quality_scale, ! params->lossless);
g_signal_connect (alpha_quality_scale, "value-changed",
G_CALLBACK (gimp_float_adjustment_update),
&params->alpha_quality);
/* Enable and disable the sliders when the lossless option is selected */
g_signal_connect (lossless_checkbox, "toggled",
G_CALLBACK (save_dialog_toggle_scale),
quality_scale);
g_signal_connect (lossless_checkbox, "toggled",
G_CALLBACK (save_dialog_toggle_scale),
alpha_quality_scale);
/* Display the dialog and enter the main event loop */
gtk_widget_show (dialog);
gtk_main ();
return response;
}
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* file-webp - WebP file format plug-in for the GIMP
* Copyright (C) 2015 Nathan Osman
* Copyright (C) 2016 Ben Touchette
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __WEBP_DIALOG_H__
#define __WEBP_DIALOG_H__
#include "file-webp-save.h"
GtkResponseType save_dialog (WebPSaveParams *params,
gint32 image_ID,
gint32 nLayers);
#endif /* __WEBP_DIALOG_H__ */
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* file-webp - WebP file format plug-in for the GIMP
* Copyright (C) 2015 Nathan Osman
* Copyright (C) 2016 Ben Touchette
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <gegl.h>
#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>
#include <webp/decode.h>
#include <webp/demux.h>
#include <webp/mux.h>
#include "file-webp-load.h"
#include "libgimp/stdplugins-intl.h"
static void
create_layer (gint32 image_ID,
uint8_t *layer_data,
gint32 position,
gchar *name,
gint width,
gint height,
gint32 offsetx,
gint32 offsety)
{
gint32 layer_ID;
GeglBuffer *geglbuffer;
GeglRectangle extent;
layer_ID = gimp_layer_new (image_ID, name,
width, height,
GIMP_RGBA_IMAGE,
100,
GIMP_NORMAL_MODE);
gimp_image_insert_layer (image_ID, layer_ID, -1, position);
if (offsetx > 0 || offsety > 0)
{
gimp_layer_set_offsets (layer_ID, offsetx, offsety);
}
/* Retrieve the buffer for the layer */
geglbuffer = gimp_drawable_get_buffer (layer_ID);
/* Copy the image data to the region */
gegl_rectangle_set (&extent, 0, 0, width, height);
gegl_buffer_set (geglbuffer, &extent, 0, NULL, layer_data,
GEGL_AUTO_ROWSTRIDE);
/* Flush the drawable and detach */
gegl_buffer_flush (geglbuffer);
if (geglbuffer)
g_object_unref (geglbuffer);
}
gint32
load_image (const gchar *filename,
gboolean interactive,
GError **error)
{
uint8_t *indata = NULL;
uint8_t *outdata;
gsize indatalen;
gint width;
gint height;
gint32 image_ID;
GFile *file;
GimpMetadata *metadata;
WebPMux *mux;
WebPBitstreamFeatures features;
WebPData wp_data;
uint32_t flag;
int animation = FALSE;
int icc = FALSE;
int exif = FALSE;
int xmp = FALSE;
int frames = 0;
gchar *name;
/* Attempt to read the file contents from disk */
if (! g_file_get_contents (filename,
(gchar **) &indata,
&indatalen,
error))
{
return -1;
}
g_printerr ("Loading WebP file %s\n", filename);
/* Validate WebP data */
if (! WebPGetInfo (indata, indatalen, &width, &height))
{
g_printerr ("Invalid WebP file\n");
return -1;
}
gegl_init (NULL, NULL);
wp_data.bytes = indata;
wp_data.size = indatalen;
mux = WebPMuxCreate (&wp_data, 1);
if (! mux)
return -1;
WebPMuxGetFeatures (mux, &flag);
if (flag == 0)
{
animation = FALSE;
icc = FALSE;
exif = FALSE;
xmp = FALSE;
frames = 0;
}
else
{
if (flag & ANIMATION_FLAG)
animation = TRUE;
if (flag & ICCP_FLAG)
icc = TRUE;
if (flag & EXIF_FLAG)
exif = TRUE;
if (flag & XMP_FLAG)
xmp = TRUE;
}
/* TODO: decode the image in "chunks" or "tiles" */
/* TODO: check if an alpha channel is present */
/* Create the new image and associated layer */
image_ID = gimp_image_new (width, height, GIMP_RGB);
if (! animation)
{
/* Attempt to decode the data as a WebP image */
outdata = WebPDecodeRGBA (indata, indatalen, &width, &height);
/* Free the original compressed data */
g_free (indata);
/* Check to ensure the image data was loaded correctly */
if (! outdata)
return -1;
create_layer (image_ID, outdata, 0, _("Background"),
width, height, 0, 0);
/* Free the image data */
free (outdata);
}
else
{
const WebPChunkId id = WEBP_CHUNK_ANMF;
WebPMuxAnimParams params;
gint loop;
WebPMuxGetAnimationParams (mux, &params);
WebPMuxNumChunks (mux, id, &frames);
/* Attempt to decode the data as a WebP animation image */
for (loop = 0; loop < frames; loop++)
{
WebPMuxFrameInfo thisframe;
gint i = loop;
if (WebPMuxGetFrame (mux, i, &thisframe) == WEBP_MUX_OK)
{
WebPGetFeatures (thisframe.bitstream.bytes,
thisframe.bitstream.size, &features);
outdata = WebPDecodeRGBA (thisframe.bitstream.bytes,
thisframe.bitstream.size,
&width, &height);
if (! outdata)
return -1;
name = g_strdup_printf (_("Frame %d"), loop + 1);
create_layer (image_ID, outdata, 0,
name, width, height,
thisframe.x_offset,
thisframe.y_offset);
g_free (name);
/* Free the image data */
free (outdata);
}
WebPDataClear (&thisframe.bitstream);
}
WebPDataClear (&wp_data);
}
if (icc)
{
WebPData icc_profile;
GimpColorProfile *profile;
WebPMuxGetChunk (mux, "ICCP", &icc_profile);
profile = gimp_color_profile_new_from_icc_profile (icc_profile.bytes,
icc_profile.size, NULL);
if (profile)
{
gimp_image_set_color_profile (image_ID, profile);
g_object_unref (profile);
}
}
if (exif || xmp)
{
<