Initial checkin of Pika from heckimp

This commit is contained in:
2023-09-25 15:35:21 -07:00
commit 891e999216
6761 changed files with 5240685 additions and 0 deletions

View File

@ -0,0 +1,101 @@
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* exr-attribute-blob.h
* copyright (c) 2012 johannes hanika
*
* 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 <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <ciso646>
#include <inttypes.h>
#if defined(_LIBCPP_VERSION)
#include <memory>
#else
#include <tr1/memory>
#endif
#include <OpenEXR/ImfFrameBuffer.h>
#include <OpenEXR/ImfTestFile.h>
#include <OpenEXR/ImfInputFile.h>
#include <OpenEXR/ImfTiledInputFile.h>
#include <OpenEXR/ImfChannelList.h>
#include <OpenEXR/ImfStandardAttributes.h>
#ifdef OPENEXR_IMF_INTERNAL_NAMESPACE
#define IMF_NS OPENEXR_IMF_INTERNAL_NAMESPACE
#else
#define IMF_NS Imf
#endif
// this stores our exif data as a blob.
template <typename T> struct array_deleter
{
void operator()(T const *p)
{
delete[] p;
}
};
namespace IMF_NS
{
class Blob
{
public:
Blob() : size(0), data((uint8_t *)NULL)
{
}
Blob(uint32_t _size, uint8_t *_data) : size(_size)
{
uint8_t *tmp_ptr = new uint8_t[_size];
memcpy(tmp_ptr, _data, _size);
data.reset(tmp_ptr, array_deleter<uint8_t>());
}
uint32_t size;
#if defined(_LIBCPP_VERSION)
std::shared_ptr<uint8_t> data;
#else
std::tr1::shared_ptr<uint8_t> data;
#endif
};
typedef IMF_NS::TypedAttribute<IMF_NS::Blob> BlobAttribute;
template <> const char *BlobAttribute::staticTypeName()
{
return "blob";
}
template <> void BlobAttribute::writeValueTo(OStream &os, int version) const
{
Xdr::write<StreamIO>(os, _value.size);
Xdr::write<StreamIO>(os, (char *)(_value.data.get()), _value.size);
}
template <> void BlobAttribute::readValueFrom(IStream &is, int size, int version)
{
Xdr::read<StreamIO>(is, _value.size);
_value.data.reset(new uint8_t[_value.size], array_deleter<uint8_t>());
Xdr::read<StreamIO>(is, (char *)(_value.data.get()), _value.size);
}
}

View File

@ -0,0 +1,455 @@
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <libpika/pika.h>
#include <libpika/pikaui.h>
#include "libpika/stdplugins-intl.h"
#include "openexr-wrapper.h"
#define LOAD_PROC "file-exr-load"
#define PLUG_IN_BINARY "file-exr"
#define PLUG_IN_VERSION "0.0.0"
typedef struct _Exr Exr;
typedef struct _ExrClass ExrClass;
struct _Exr
{
PikaPlugIn parent_instance;
};
struct _ExrClass
{
PikaPlugInClass parent_class;
};
#define EXR_TYPE (exr_get_type ())
#define EXR (obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), EXR_TYPE, Exr))
GType exr_get_type (void) G_GNUC_CONST;
static GList * exr_query_procedures (PikaPlugIn *plug_in);
static PikaProcedure * exr_create_procedure (PikaPlugIn *plug_in,
const gchar *name);
static PikaValueArray * exr_load (PikaProcedure *procedure,
PikaRunMode run_mode,
GFile *file,
const PikaValueArray *args,
gpointer run_data);
static PikaImage * load_image (GFile *file,
gboolean interactive,
GError **error);
static void sanitize_comment (gchar *comment);
void load_dialog (void);
G_DEFINE_TYPE (Exr, exr, PIKA_TYPE_PLUG_IN)
PIKA_MAIN (EXR_TYPE)
DEFINE_STD_SET_I18N
static void
exr_class_init (ExrClass *klass)
{
PikaPlugInClass *plug_in_class = PIKA_PLUG_IN_CLASS (klass);
plug_in_class->query_procedures = exr_query_procedures;
plug_in_class->create_procedure = exr_create_procedure;
plug_in_class->set_i18n = STD_SET_I18N;
}
static void
exr_init (Exr *exr)
{
}
static GList *
exr_query_procedures (PikaPlugIn *plug_in)
{
return g_list_append (NULL, g_strdup (LOAD_PROC));
}
static PikaProcedure *
exr_create_procedure (PikaPlugIn *plug_in,
const gchar *name)
{
PikaProcedure *procedure = NULL;
if (! strcmp (name, LOAD_PROC))
{
procedure = pika_load_procedure_new (plug_in, name,
PIKA_PDB_PROC_TYPE_PLUGIN,
exr_load, NULL, NULL);
pika_procedure_set_menu_label (procedure, _("OpenEXR image"));
pika_procedure_set_documentation (procedure,
_("Loads files in the OpenEXR file format"),
"This plug-in loads OpenEXR files. ",
name);
pika_procedure_set_attribution (procedure,
"Dominik Ernst <dernst@gmx.de>, "
"Mukund Sivaraman <muks@banu.com>",
"Dominik Ernst <dernst@gmx.de>, "
"Mukund Sivaraman <muks@banu.com>",
NULL);
pika_file_procedure_set_mime_types (PIKA_FILE_PROCEDURE (procedure),
"image/x-exr");
pika_file_procedure_set_extensions (PIKA_FILE_PROCEDURE (procedure),
"exr");
pika_file_procedure_set_magics (PIKA_FILE_PROCEDURE (procedure),
"0,long,0x762f3101");
}
return procedure;
}
static PikaValueArray *
exr_load (PikaProcedure *procedure,
PikaRunMode run_mode,
GFile *file,
const PikaValueArray *args,
gpointer run_data)
{
PikaValueArray *return_vals;
PikaImage *image;
GError *error = NULL;
gegl_init (NULL, NULL);
image = load_image (file, run_mode == PIKA_RUN_INTERACTIVE,
&error);
if (! image)
return pika_procedure_new_return_values (procedure,
PIKA_PDB_EXECUTION_ERROR,
error);
return_vals = pika_procedure_new_return_values (procedure,
PIKA_PDB_SUCCESS,
NULL);
PIKA_VALUES_SET_IMAGE (return_vals, 1, image);
return return_vals;
}
static PikaImage *
load_image (GFile *file,
gboolean interactive,
GError **error)
{
EXRLoader *loader;
gint width;
gint height;
gboolean has_alpha;
PikaImageBaseType image_type;
PikaPrecision image_precision;
PikaImage *image = NULL;
PikaImageType layer_type;
PikaLayer *layer;
const Babl *format;
GeglBuffer *buffer = NULL;
gint bpp;
gint tile_height;
gchar *pixels = NULL;
gint begin;
gint32 success = FALSE;
gchar *comment = NULL;
PikaColorProfile *profile = NULL;
guchar *exif_data;
guint exif_size;
guchar *xmp_data;
guint xmp_size;
pika_progress_init_printf (_("Opening '%s'"),
pika_file_get_utf8_name (file));
loader = exr_loader_new (g_file_peek_path (file));
if (! loader)
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error opening file '%s' for reading"),
pika_file_get_utf8_name (file));
goto out;
}
width = exr_loader_get_width (loader);
height = exr_loader_get_height (loader);
if ((width < 1) || (height < 1))
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error querying image dimensions from '%s'"),
pika_file_get_utf8_name (file));
goto out;
}
has_alpha = exr_loader_has_alpha (loader) ? TRUE : FALSE;
switch (exr_loader_get_precision (loader))
{
case PREC_UINT:
image_precision = PIKA_PRECISION_U32_LINEAR;
break;
case PREC_HALF:
image_precision = PIKA_PRECISION_HALF_LINEAR;
break;
case PREC_FLOAT:
image_precision = PIKA_PRECISION_FLOAT_LINEAR;
break;
default:
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error querying image precision from '%s'"),
pika_file_get_utf8_name (file));
goto out;
}
switch (exr_loader_get_image_type (loader))
{
case IMAGE_TYPE_RGB:
image_type = PIKA_RGB;
layer_type = has_alpha ? PIKA_RGBA_IMAGE : PIKA_RGB_IMAGE;
break;
case IMAGE_TYPE_GRAY:
case IMAGE_TYPE_UNKNOWN_1_CHANNEL:
image_type = PIKA_GRAY;
layer_type = has_alpha ? PIKA_GRAYA_IMAGE : PIKA_GRAY_IMAGE;
break;
default:
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error querying image type from '%s'"),
pika_file_get_utf8_name (file));
goto out;
}
image = pika_image_new_with_precision (width, height,
image_type, image_precision);
if (! image)
{
g_set_error (error, 0, 0,
_("Could not create new image for '%s': %s"),
pika_file_get_utf8_name (file),
pika_pdb_get_last_error (pika_get_pdb ()));
goto out;
}
if (exr_loader_get_image_type (loader) == IMAGE_TYPE_UNKNOWN_1_CHANNEL &&
interactive)
load_dialog ();
/* try to load an icc profile, it will be generated on the fly if
* chromaticities are given
*/
if (image_type == PIKA_RGB)
{
profile = exr_loader_get_profile (loader);
if (profile)
pika_image_set_color_profile (image, profile);
}
layer = pika_layer_new (image, _("Background"), width, height,
layer_type, 100,
pika_image_get_default_new_layer_mode (image));
pika_image_insert_layer (image, layer, NULL, 0);
buffer = pika_drawable_get_buffer (PIKA_DRAWABLE (layer));
format = pika_drawable_get_format (PIKA_DRAWABLE (layer));
bpp = babl_format_get_bytes_per_pixel (format);
tile_height = pika_tile_height ();
pixels = g_new0 (gchar, tile_height * width * bpp);
for (begin = 0; begin < height; begin += tile_height)
{
gint end;
gint num;
gint i;
end = MIN (begin + tile_height, height);
num = end - begin;
for (i = 0; i < num; i++)
{
gint retval;
retval = exr_loader_read_pixel_row (loader,
pixels + (i * width * bpp),
bpp, begin + i);
if (retval < 0)
{
g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
_("Error reading pixel data from '%s'"),
pika_file_get_utf8_name (file));
goto out;
}
}
gegl_buffer_set (buffer, GEGL_RECTANGLE (0, begin, width, num),
0, NULL, pixels, GEGL_AUTO_ROWSTRIDE);
pika_progress_update ((gdouble) begin / (gdouble) height);
}
/* try to read the file comment */
comment = exr_loader_get_comment (loader);
if (comment)
{
PikaParasite *parasite;
sanitize_comment (comment);
parasite = pika_parasite_new ("pika-comment",
PIKA_PARASITE_PERSISTENT,
strlen (comment) + 1,
comment);
pika_image_attach_parasite (image, parasite);
pika_parasite_free (parasite);
}
/* check if the image contains Exif or Xmp data and read it */
exif_data = exr_loader_get_exif (loader, &exif_size);
xmp_data = exr_loader_get_xmp (loader, &xmp_size);
if (exif_data || xmp_data)
{
PikaMetadata *metadata = pika_metadata_new ();
PikaMetadataLoadFlags flags = PIKA_METADATA_LOAD_ALL;
if (exif_data)
{
pika_metadata_set_from_exif (metadata, exif_data, exif_size, NULL);
g_free (exif_data);
}
if (xmp_data)
{
pika_metadata_set_from_xmp (metadata, xmp_data, xmp_size, NULL);
g_free (xmp_data);
}
if (comment)
flags &= ~PIKA_METADATA_LOAD_COMMENT;
if (profile)
flags &= ~PIKA_METADATA_LOAD_COLORSPACE;
pika_image_metadata_load_finish (image, "image/exr",
metadata, flags);
g_object_unref (metadata);
}
pika_progress_update (1.0);
success = TRUE;
out:
g_clear_object (&profile);
g_clear_object (&buffer);
g_clear_pointer (&pixels, g_free);
g_clear_pointer (&comment, g_free);
g_clear_pointer (&loader, exr_loader_unref);
if (success)
return image;
if (image)
pika_image_delete (image);
return NULL;
}
/* copy & pasted from file-jpeg/jpeg-load.c */
static void
sanitize_comment (gchar *comment)
{
const gchar *start_invalid;
if (! g_utf8_validate (comment, -1, &start_invalid))
{
guchar *c;
for (c = (guchar *) start_invalid; *c; c++)
{
if (*c > 126 || (*c < 32 && *c != '\t' && *c != '\n' && *c != '\r'))
*c = '?';
}
}
}
void
load_dialog (void)
{
GtkWidget *dialog;
GtkWidget *label;
GtkWidget *vbox;
gchar *label_text;
pika_ui_init (PLUG_IN_BINARY);
dialog = pika_dialog_new (_("Import OpenEXR"),
"openexr-notice",
NULL, 0, NULL, NULL,
_("_OK"), GTK_RESPONSE_OK,
NULL);
pika_window_set_transient (GTK_WINDOW (dialog));
vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 2);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 12);
gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
vbox, TRUE, TRUE, 0);
gtk_widget_show (vbox);
label_text = g_strdup_printf ("<b>%s</b>\n%s", _("Unknown Channel Name"),
_("The image contains a single unknown channel.\n"
"It has been converted to grayscale."));
label = gtk_label_new (NULL);
gtk_label_set_markup (GTK_LABEL (label), label_text);
gtk_label_set_selectable (GTK_LABEL (label), TRUE);
gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
gtk_label_set_yalign (GTK_LABEL (label), 0.0);
gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0);
gtk_widget_show (label);
g_free (label_text);
gtk_widget_show (dialog);
/* run the dialog */
pika_dialog_run (PIKA_DIALOG (dialog));
gtk_widget_destroy (dialog);
}

View File

@ -0,0 +1,35 @@
if openexr.found()
plugin_name = 'file-exr'
plugin_sources = [
'file-exr.c',
'openexr-wrapper.cc',
]
if platform_windows
plugin_sources += windows.compile_resources(
pika_plugins_rc,
args: [
'--define', 'ORIGINALFILENAME_STR="@0@"'.format(plugin_name+'.exe'),
'--define', 'INTERNALNAME_STR="@0@"' .format(plugin_name),
'--define', 'TOP_SRCDIR="@0@"' .format(meson.project_source_root()),
],
include_directories: [
rootInclude, appInclude,
],
)
endif
executable(plugin_name,
plugin_sources,
dependencies: [
libpikaui_dep,
openexr,
lcms,
],
install: true,
install_dir: pikaplugindir / 'plug-ins' / plugin_name,
)
endif

View File

@ -0,0 +1,559 @@
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <string>
#include <lcms2.h>
/* These libpika includes are not needed here at all, but this is a
* convenient place to make sure the public libpika headers are
* C++-clean. The C++ compiler will choke on stuff like naming
* a struct member or parameter "private".
*/
#include "libpika/pika.h"
#include "libpika/pikaui.h"
#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"
#include "libpikacolor/pikacolor.h"
#include "libpikaconfig/pikaconfig.h"
#include "libpikamodule/pikamodule.h"
#include "libpikathumb/pikathumb.h"
#include "libpikawidgets/pikawidgets.h"
#if defined(__MINGW32__)
#ifndef FLT_EPSILON
#define FLT_EPSILON __FLT_EPSILON__
#endif
#ifndef DBL_EPSILON
#define DBL_EPSILON __DBL_EPSILON__
#endif
#ifndef LDBL_EPSILON
#define LDBL_EPSILON __LDBL_EPSILON__
#endif
#endif
/* ignore deprecated warnings from OpenEXR headers */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated"
#include <ImfInputFile.h>
#include <ImfChannelList.h>
#include <ImfRgbaFile.h>
#include <ImfRgbaYca.h>
#include <ImfStandardAttributes.h>
#pragma GCC diagnostic pop
#include "exr-attribute-blob.h"
#include "openexr-wrapper.h"
using namespace Imf;
using namespace Imf::RgbaYca;
using namespace Imath;
static bool XYZ_equal(cmsCIEXYZ *a, cmsCIEXYZ *b)
{
static const double epsilon = 0.0001;
// Y is encoding the luminance, we normalize that for comparison
return fabs ((a->X / a->Y * b->Y) - b->X) < epsilon &&
fabs ((a->Y / a->Y * b->Y) - b->Y) < epsilon &&
fabs ((a->Z / a->Y * b->Y) - b->Z) < epsilon;
}
struct _EXRLoader
{
_EXRLoader(const char* filename) :
refcount_(1),
file_(filename),
data_window_(file_.header().dataWindow()),
channels_(file_.header().channels())
{
const Channel* chan;
if (channels_.findChannel("R") ||
channels_.findChannel("G") ||
channels_.findChannel("B"))
{
format_string_ = "RGB";
image_type_ = IMAGE_TYPE_RGB;
if ((chan = channels_.findChannel("R")))
pt_ = chan->type;
else if ((chan = channels_.findChannel("G")))
pt_ = chan->type;
else
pt_ = channels_.findChannel("B")->type;
}
else if (channels_.findChannel("Y") &&
(channels_.findChannel("RY") ||
channels_.findChannel("BY")))
{
format_string_ = "RGB";
image_type_ = IMAGE_TYPE_RGB;
pt_ = channels_.findChannel("Y")->type;
// FIXME: no chroma handling for now.
throw;
}
else if (channels_.findChannel("Y"))
{
format_string_ = "Y";
image_type_ = IMAGE_TYPE_GRAY;
pt_ = channels_.findChannel("Y")->type;
}
else
{
int channel_count = 0;
const char *channel_name = NULL;
for (ChannelList::ConstIterator i = channels_.begin();
i != channels_.end(); ++i)
{
channel_count++;
pt_ = i.channel().type;
channel_name = i.name();
}
/* Assume single channel images are grayscale,
* no matter what the channel name is. */
if (channel_count == 1)
{
format_string_ = channel_name;
image_type_ = IMAGE_TYPE_UNKNOWN_1_CHANNEL;
unknown_channel_name_ = channel_name;
/* TODO: Pass this information back so it can be displayed
* in the UI. */
printf ("OpenEXR Warning: Single channel image with unknown "
"channel %s, loading as grayscale\n", channel_name);
}
else
{
throw;
}
}
if (channels_.findChannel("A"))
{
format_string_.append("A");
has_alpha_ = true;
}
else
{
has_alpha_ = false;
}
switch (pt_)
{
case UINT:
format_string_.append(" u32");
bpc_ = 4;
break;
case HALF:
format_string_.append(" half");
bpc_ = 2;
break;
case FLOAT:
default:
format_string_.append(" float");
bpc_ = 4;
}
}
int readPixelRow(char *pixels,
int bpp,
int row)
{
const int actual_row = data_window_.min.y + row;
FrameBuffer fb;
// This is necessary because OpenEXR expects the buffer to begin at
// (0, 0). Though it probably results in some unmapped address,
// hopefully OpenEXR will not make use of it. :/
char* base = pixels - (data_window_.min.x * bpp);
switch (image_type_)
{
case IMAGE_TYPE_UNKNOWN_1_CHANNEL:
fb.insert(unknown_channel_name_, Slice(pt_, base, bpp, 0, 1, 1, 0.5));
break;
case IMAGE_TYPE_GRAY:
fb.insert("Y", Slice(pt_, base, bpp, 0, 1, 1, 0.5));
if (hasAlpha())
{
fb.insert("A", Slice(pt_, base + bpc_, bpp, 0, 1, 1, 1.0));
}
break;
case IMAGE_TYPE_RGB:
default:
fb.insert("R", Slice(pt_, base + (bpc_ * 0), bpp, 0, 1, 1, 0.0));
fb.insert("G", Slice(pt_, base + (bpc_ * 1), bpp, 0, 1, 1, 0.0));
fb.insert("B", Slice(pt_, base + (bpc_ * 2), bpp, 0, 1, 1, 0.0));
if (hasAlpha())
{
fb.insert("A", Slice(pt_, base + (bpc_ * 3), bpp, 0, 1, 1, 1.0));
}
}
file_.setFrameBuffer(fb);
file_.readPixels(actual_row);
return 0;
}
int getWidth() const {
return data_window_.max.x - data_window_.min.x + 1;
}
int getHeight() const {
return data_window_.max.y - data_window_.min.y + 1;
}
EXRPrecision getPrecision() const {
EXRPrecision prec;
switch (pt_)
{
case UINT:
prec = PREC_UINT;
break;
case HALF:
prec = PREC_HALF;
break;
case FLOAT:
default:
prec = PREC_FLOAT;
}
return prec;
}
EXRImageType getImageType() const {
return image_type_;
}
int hasAlpha() const {
return has_alpha_ ? 1 : 0;
}
PikaColorProfile *getProfile() const {
Chromaticities chromaticities;
float whiteLuminance = 1.0;
PikaColorProfile *linear_srgb_profile;
cmsHPROFILE linear_srgb_lcms;
PikaColorProfile *profile;
cmsHPROFILE lcms_profile;
cmsCIEXYZ *pika_r_XYZ, *pika_g_XYZ, *pika_b_XYZ, *pika_w_XYZ;
cmsCIEXYZ exr_r_XYZ, exr_g_XYZ, exr_b_XYZ, exr_w_XYZ;
// get the color information from the EXR
if (hasChromaticities (file_.header ()))
chromaticities = Imf::chromaticities (file_.header ());
else
return NULL;
if (Imf::hasWhiteLuminance (file_.header ()))
whiteLuminance = Imf::whiteLuminance (file_.header ());
else
return NULL;
#if 0
std::cout << "hasChromaticities: "
<< hasChromaticities (file_.header ())
<< std::endl;
std::cout << "hasWhiteLuminance: "
<< hasWhiteLuminance (file_.header ())
<< std::endl;
std::cout << whiteLuminance << std::endl;
std::cout << chromaticities.red << std::endl;
std::cout << chromaticities.green << std::endl;
std::cout << chromaticities.blue << std::endl;
std::cout << chromaticities.white << std::endl;
std::cout << std::endl;
#endif
cmsCIExyY whitePoint = { chromaticities.white.x,
chromaticities.white.y,
whiteLuminance };
cmsCIExyYTRIPLE CameraPrimaries = { { chromaticities.red.x,
chromaticities.red.y,
whiteLuminance },
{ chromaticities.green.x,
chromaticities.green.y,
whiteLuminance },
{ chromaticities.blue.x,
chromaticities.blue.y,
whiteLuminance } };
// get the primaries + wp from PIKA's internal linear sRGB profile
linear_srgb_profile = pika_color_profile_new_rgb_srgb_linear ();
linear_srgb_lcms = pika_color_profile_get_lcms_profile (linear_srgb_profile);
pika_r_XYZ = (cmsCIEXYZ *) cmsReadTag (linear_srgb_lcms, cmsSigRedColorantTag);
pika_g_XYZ = (cmsCIEXYZ *) cmsReadTag (linear_srgb_lcms, cmsSigGreenColorantTag);
pika_b_XYZ = (cmsCIEXYZ *) cmsReadTag (linear_srgb_lcms, cmsSigBlueColorantTag);
pika_w_XYZ = (cmsCIEXYZ *) cmsReadTag (linear_srgb_lcms, cmsSigMediaWhitePointTag);
cmsxyY2XYZ(&exr_r_XYZ, &CameraPrimaries.Red);
cmsxyY2XYZ(&exr_g_XYZ, &CameraPrimaries.Green);
cmsxyY2XYZ(&exr_b_XYZ, &CameraPrimaries.Blue);
cmsxyY2XYZ(&exr_w_XYZ, &whitePoint);
// ... and check if the data stored in the EXR matches PIKA's internal profile
bool exr_is_linear_srgb = XYZ_equal (&exr_r_XYZ, pika_r_XYZ) &&
XYZ_equal (&exr_g_XYZ, pika_g_XYZ) &&
XYZ_equal (&exr_b_XYZ, pika_b_XYZ) &&
XYZ_equal (&exr_w_XYZ, pika_w_XYZ);
// using PIKA's linear sRGB profile allows to skip the conversion popup
if (exr_is_linear_srgb)
return linear_srgb_profile;
// nope, it's something else. Clean up and build a new profile
g_object_unref (linear_srgb_profile);
// TODO: maybe factor this out into libpikacolor/pikacolorprofile.h ?
double Parameters[2] = { 1.0, 0.0 };
cmsToneCurve *Gamma[3];
Gamma[0] = Gamma[1] = Gamma[2] = cmsBuildParametricToneCurve(0,
1,
Parameters);
lcms_profile = cmsCreateRGBProfile (&whitePoint, &CameraPrimaries, Gamma);
cmsFreeToneCurve (Gamma[0]);
if (lcms_profile == NULL) return NULL;
// cmsSetProfileVersion (lcms_profile, 2.1);
cmsMLU *mlu0 = cmsMLUalloc (NULL, 1);
cmsMLUsetASCII (mlu0, "en", "US", "(PIKA internal)");
cmsMLU *mlu1 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII (mlu1, "en", "US", "color profile from EXR chromaticities");
cmsMLU *mlu2 = cmsMLUalloc(NULL, 1);
cmsMLUsetASCII (mlu2, "en", "US", "color profile from EXR chromaticities");
cmsWriteTag (lcms_profile, cmsSigDeviceMfgDescTag, mlu0);
cmsWriteTag (lcms_profile, cmsSigDeviceModelDescTag, mlu1);
cmsWriteTag (lcms_profile, cmsSigProfileDescriptionTag, mlu2);
cmsMLUfree (mlu0);
cmsMLUfree (mlu1);
cmsMLUfree (mlu2);
profile = pika_color_profile_new_from_lcms_profile (lcms_profile,
NULL);
cmsCloseProfile (lcms_profile);
return profile;
}
gchar *getComment() const {
char *result = NULL;
const Imf::StringAttribute *comment = file_.header().findTypedAttribute<Imf::StringAttribute>("comment");
if (comment)
result = g_strdup (comment->value().c_str());
return result;
}
guchar *getExif(guint *size) const {
guchar jpeg_exif[] = "Exif\0\0";
guchar *exif_data = NULL;
*size = 0;
const Imf::BlobAttribute *exif = file_.header().findTypedAttribute<Imf::BlobAttribute>("exif");
if (exif)
{
exif_data = (guchar *)(exif->value().data.get());
*size = exif->value().size;
// darktable appends a jpg-compatible exif00 string, so get rid of that again:
if ( ! memcmp (jpeg_exif, exif_data, sizeof(jpeg_exif)))
{
*size -= 6;
exif_data += 6;
}
}
return (guchar *)g_memdup2 (exif_data, *size);
}
guchar *getXmp(guint *size) const {
guchar *result = NULL;
*size = 0;
const Imf::StringAttribute *xmp = file_.header().findTypedAttribute<Imf::StringAttribute>("xmp");
if (xmp)
{
*size = xmp->value().size();
result = (guchar *) g_memdup2 (xmp->value().data(), *size);
}
return result;
}
size_t refcount_;
InputFile file_;
const Box2i data_window_;
const ChannelList& channels_;
PixelType pt_;
int bpc_;
EXRImageType image_type_;
bool has_alpha_;
std::string format_string_;
std::string unknown_channel_name_;
};
EXRLoader*
exr_loader_new (const char *filename)
{
EXRLoader* file;
// Don't let any exceptions propagate to the C layer.
try
{
Imf::BlobAttribute::registerAttributeType();
file = new EXRLoader(filename);
}
catch (...)
{
file = NULL;
}
return file;
}
EXRLoader*
exr_loader_ref (EXRLoader *loader)
{
++loader->refcount_;
return loader;
}
void
exr_loader_unref (EXRLoader *loader)
{
if (--loader->refcount_ == 0)
{
delete loader;
}
}
int
exr_loader_get_width (EXRLoader *loader)
{
int width;
// Don't let any exceptions propagate to the C layer.
try
{
width = loader->getWidth();
}
catch (...)
{
width = -1;
}
return width;
}
int
exr_loader_get_height (EXRLoader *loader)
{
int height;
// Don't let any exceptions propagate to the C layer.
try
{
height = loader->getHeight();
}
catch (...)
{
height = -1;
}
return height;
}
EXRImageType
exr_loader_get_image_type (EXRLoader *loader)
{
// This does not throw.
return loader->getImageType();
}
EXRPrecision
exr_loader_get_precision (EXRLoader *loader)
{
// This does not throw.
return loader->getPrecision();
}
int
exr_loader_has_alpha (EXRLoader *loader)
{
// This does not throw.
return loader->hasAlpha();
}
PikaColorProfile *
exr_loader_get_profile (EXRLoader *loader)
{
return loader->getProfile ();
}
gchar *
exr_loader_get_comment (EXRLoader *loader)
{
return loader->getComment ();
}
guchar *
exr_loader_get_exif (EXRLoader *loader,
guint *size)
{
return loader->getExif (size);
}
guchar *
exr_loader_get_xmp (EXRLoader *loader,
guint *size)
{
return loader->getXmp (size);
}
int
exr_loader_read_pixel_row (EXRLoader *loader,
char *pixels,
int bpp,
int row)
{
int retval = -1;
// Don't let any exceptions propagate to the C layer.
try
{
retval = loader->readPixelRow(pixels, bpp, row);
}
catch (...)
{
retval = -1;
}
return retval;
}

View File

@ -0,0 +1,73 @@
/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef __OPENEXR_WRAPPER_H__
#define __OPENEXR_WRAPPER_H__
G_BEGIN_DECLS
/* This is fully opaque on purpose, as the calling C code must not be
* exposed to more than this.
*/
typedef struct _EXRLoader EXRLoader;
typedef enum
{
PREC_UINT,
PREC_HALF,
PREC_FLOAT
} EXRPrecision;
typedef enum
{
IMAGE_TYPE_RGB,
IMAGE_TYPE_GRAY,
IMAGE_TYPE_UNKNOWN_1_CHANNEL
} EXRImageType;
EXRLoader * exr_loader_new (const char *filename);
EXRLoader * exr_loader_ref (EXRLoader *loader);
void exr_loader_unref (EXRLoader *loader);
int exr_loader_get_width (EXRLoader *loader);
int exr_loader_get_height (EXRLoader *loader);
EXRPrecision exr_loader_get_precision (EXRLoader *loader);
EXRImageType exr_loader_get_image_type (EXRLoader *loader);
int exr_loader_has_alpha (EXRLoader *loader);
PikaColorProfile * exr_loader_get_profile (EXRLoader *loader);
gchar * exr_loader_get_comment (EXRLoader *loader);
guchar * exr_loader_get_exif (EXRLoader *loader,
guint *size);
guchar * exr_loader_get_xmp (EXRLoader *loader,
guint *size);
int exr_loader_read_pixel_row (EXRLoader *loader,
char *pixels,
int bpp,
int row);
G_END_DECLS
#endif /* __OPENEXR_WRAPPER_H__ */