Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
imageio_im.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2012-2014 Ulrich Pegelow.
4 Copyright (C) 2013-2014, 2016 Roman Lebedev.
5 Copyright (C) 2013-2014, 2016 Tobias Ellinghaus.
6 Copyright (C) 2014 johannes hanika.
7 Copyright (C) 2014 Pascal de Bruijn.
8 Copyright (C) 2019 Hanno Schwalm.
9 Copyright (C) 2020 Aurélien PIERRE.
10 Copyright (C) 2020 Hubert Kowalski.
11 Copyright (C) 2020 Matthieu Volat.
12 Copyright (C) 2020-2021 Pascal Obry.
13 Copyright (C) 2022 Martin Bařinka.
14 Copyright (C) 2023 Alynx Zhou.
15
16 darktable is free software: you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation, either version 3 of the License, or
19 (at your option) any later version.
20
21 darktable is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with darktable. If not, see <http://www.gnu.org/licenses/>.
28*/
29
30#ifdef HAVE_IMAGEMAGICK
31#include "common/darktable.h"
32#include "imageio.h"
33#include "imageio_gm.h"
34#include "develop/develop.h"
35#include "common/exif.h"
36#include "common/colorspaces.h"
37#include "control/conf.h"
38
39#include <memory.h>
40#include <stdio.h>
41#include <inttypes.h>
42#include <strings.h>
43#include <assert.h>
44
45#include <MagickWand/MagickWand.h>
46
47
48/* we only support images with certain filename extensions via ImageMagick,
49 * derived from what it declared as "supported" with GraphicsMagick; RAWs
50 * are excluded as ImageMagick would render them with third party libraries
51 * in reduced quality - slow and only 8-bit */
52static gboolean _supported_image(const gchar *filename)
53{
54 const char *extensions_whitelist[] = { "tif", "tiff", "gif", "jpc", "jp2", "bmp", "dcm", "jng",
55 "miff", "mng", "pbm", "pnm", "ppm", "pgm", "webp", NULL };
56 gboolean supported = FALSE;
57 char *ext = g_strrstr(filename, ".");
58 if(IS_NULL_PTR(ext)) return FALSE;
59 ext++;
60 for(const char **i = extensions_whitelist; !IS_NULL_PTR(*i); i++)
61 if(!g_ascii_strncasecmp(ext, *i, strlen(*i)))
62 {
64 break;
65 }
66 return supported;
67}
68
69
71{
73 MagickWand *image = NULL;
74 MagickBooleanType ret;
75
76 if(!_supported_image(filename)) return DT_IMAGEIO_FILE_CORRUPTED;
77
78 if(!img->exif_inited) (void)dt_exif_read(img, filename);
79
80 image = NewMagickWand();
81 if (IS_NULL_PTR(image)) goto error;
82
83 ret = MagickReadImage(image, filename);
84 if (ret != MagickTrue) {
85 fprintf(stderr, "[ImageMagick_open] cannot open `%s'\n", img->filename);
87 goto error;
88 }
89 dt_print(DT_DEBUG_IMAGEIO, "[ImageMagick_open] image `%s' loading\n", img->filename);
90
91 ColorspaceType colorspace;
92
93 colorspace = MagickGetImageColorspace(image);
94
95 if((colorspace == CMYColorspace) || (colorspace == CMYKColorspace))
96 {
97 fprintf(stderr, "[ImageMagick_open] error: CMY(K) images are not supported.\n");
99 goto error;
100 }
101
102 img->width = MagickGetImageWidth(image);
103 img->height = MagickGetImageHeight(image);
104
105 img->dsc.channels = 4;
106 img->dsc.datatype = TYPE_FLOAT;
107 img->dsc.bpp = 4 * sizeof(float);
108 img->dsc.cst = IOP_CS_RGB;
109 img->dsc.filters = 0u;
110 img->flags &= ~DT_IMAGE_RAW;
111 img->flags &= ~DT_IMAGE_S_RAW;
112 img->flags &= ~DT_IMAGE_HDR;
113 img->flags |= DT_IMAGE_LDR;
114
115 img->loader = LOADER_IM;
116
117 if(IS_NULL_PTR(mbuf))
118 {
119 DestroyMagickWand(image);
120 return DT_IMAGEIO_OK;
121 }
122
123 float *mipbuf = dt_mipmap_cache_alloc(mbuf, img);
124 if (IS_NULL_PTR(mipbuf)) {
125 fprintf(stderr,
126 "[ImageMagick_open] could not alloc full buffer for image `%s'\n",
127 img->filename);
129 goto error;
130 }
131
132 ret = MagickExportImagePixels(image, 0, 0, img->width, img->height, "RGBP", FloatPixel, mipbuf);
133 if (ret != MagickTrue) {
134 fprintf(stderr,
135 "[ImageMagick_open] error reading image `%s'\n", img->filename);
136 goto error;
137 }
138
139 size_t profile_length;
140 uint8_t *profile_data = (uint8_t *)MagickGetImageProfile(image, "icc", &profile_length);
141 /* no alias support like GraphicsMagick, have to check both locations */
142 if(IS_NULL_PTR(profile_data))
143 profile_data = (uint8_t *)MagickGetImageProfile(image, "icm", &profile_length);
144 if(profile_data)
145 {
146 img->profile_size = profile_length;
147 img->profile = (uint8_t *)g_malloc0(profile_length);
148 memcpy(img->profile, profile_data, profile_length);
149 MagickRelinquishMemory(profile_data);
150 }
151
152 DestroyMagickWand(image);
153 return DT_IMAGEIO_OK;
154
155error:
156 DestroyMagickWand(image);
157 return err;
158}
159#endif
160
161// clang-format off
162// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
163// vim: shiftwidth=2 expandtab tabstop=2 cindent
164// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
165// clang-format on
static void error(char *msg)
Definition ashift_lsd.c:202
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
@ IOP_CS_RGB
typedef void((*dt_cache_allocate_t)(void *userdata, dt_cache_entry_t *entry))
void dt_print(dt_debug_thread_t thread, const char *msg,...)
Definition darktable.c:1542
@ DT_DEBUG_IMAGEIO
Definition darktable.h:733
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition darktable.h:281
int supported(struct dt_imageio_module_storage_t *storage, struct dt_imageio_module_format_t *format)
Definition example.c:274
int dt_exif_read(dt_image_t *img, const char *path)
Definition exif.cc:1753
@ TYPE_FLOAT
Definition format.h:46
dt_imageio_retval_t
Definition image.h:78
@ DT_IMAGEIO_OK
Definition image.h:79
@ DT_IMAGEIO_CACHE_FULL
Definition image.h:82
@ DT_IMAGEIO_FILE_CORRUPTED
Definition image.h:81
@ DT_IMAGEIO_FILE_NOT_FOUND
Definition image.h:80
@ DT_IMAGE_LDR
Definition image.h:109
@ LOADER_IM
Definition image.h:234
dt_imageio_retval_t dt_imageio_open_im(dt_image_t *img, const char *filename, dt_mipmap_buffer_t *buf)
Definition imageio_im.h:34
void * dt_mipmap_cache_alloc(dt_mipmap_buffer_t *buf, const dt_image_t *img)
int32_t height
Definition image.h:315
dt_image_loader_t loader
Definition image.h:335
int32_t exif_inited
Definition image.h:283
int32_t flags
Definition image.h:319
int32_t width
Definition image.h:315
uint32_t profile_size
Definition image.h:341
dt_iop_buffer_dsc_t dsc
Definition image.h:337
uint8_t * profile
Definition image.h:340
char filename[DT_MAX_FILENAME_LEN]
Definition image.h:304
uint32_t filters
Definition format.h:60
unsigned int channels
Definition format.h:54
dt_iop_buffer_type_t datatype
Definition format.h:56