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"
38#include "control/conf.h"
39
40#include <memory.h>
41#include <stdio.h>
42#include <inttypes.h>
43#include <strings.h>
44#include <assert.h>
45
46#include <MagickWand/MagickWand.h>
47
48
49/* we only support images with certain filename extensions via ImageMagick,
50 * derived from what it declared as "supported" with GraphicsMagick; RAWs
51 * are excluded as ImageMagick would render them with third party libraries
52 * in reduced quality - slow and only 8-bit */
53static gboolean _supported_image(const gchar *filename)
54{
55 const char *extensions_whitelist[] = { "tif", "tiff", "gif", "jpc", "jp2", "bmp", "dcm", "jng",
56 "miff", "mng", "pbm", "pnm", "ppm", "pgm", "webp", NULL };
57 gboolean supported = FALSE;
58 char *ext = g_strrstr(filename, ".");
59 if(IS_NULL_PTR(ext)) return FALSE;
60 ext++;
61 for(const char **i = extensions_whitelist; !IS_NULL_PTR(*i); i++)
62 if(!g_ascii_strncasecmp(ext, *i, strlen(*i)))
63 {
65 break;
66 }
67 return supported;
68}
69
70
72{
74 MagickWand *image = NULL;
75 MagickBooleanType ret;
76
77 if(!_supported_image(filename)) return DT_IMAGEIO_FILE_CORRUPTED;
78
79 if(!img->exif_inited) (void)dt_exif_read(img, filename);
80
81 image = NewMagickWand();
82 if (IS_NULL_PTR(image)) return DT_IMAGEIO_FILE_CORRUPTED;
83
84 // ImageMagick calls assert() -> abort() on some malformed files instead of
85 // reporting through its normal error status. Recover instead of crashing
86 // the whole app; on recovery, `image` is NOT touched again (see
87 // imageio_magick_abort_guard.h) - we leak the wand and bail out directly.
88 DT_MAGICK_ABORT_GUARD("ImageMagick_open", filename, return DT_IMAGEIO_FILE_CORRUPTED);
89
90 ret = MagickReadImage(image, filename);
91 if (ret != MagickTrue) {
92 fprintf(stderr, "[ImageMagick_open] cannot open `%s'\n", img->filename);
94 goto error;
95 }
96 dt_print(DT_DEBUG_IMAGEIO, "[ImageMagick_open] image `%s' loading\n", img->filename);
97
98 ColorspaceType colorspace;
99
100 colorspace = MagickGetImageColorspace(image);
101
102 if((colorspace == CMYColorspace) || (colorspace == CMYKColorspace))
103 {
104 fprintf(stderr, "[ImageMagick_open] error: CMY(K) images are not supported.\n");
106 goto error;
107 }
108
109 img->width = MagickGetImageWidth(image);
110 img->height = MagickGetImageHeight(image);
111
112 img->dsc.channels = 4;
113 img->dsc.datatype = TYPE_FLOAT;
114 img->dsc.bpp = 4 * sizeof(float);
115 img->dsc.cst = IOP_CS_RGB;
116 img->dsc.filters = 0u;
117 img->flags &= ~DT_IMAGE_RAW;
118 img->flags &= ~DT_IMAGE_S_RAW;
119 img->flags &= ~DT_IMAGE_HDR;
120 img->flags |= DT_IMAGE_LDR;
121
122 img->loader = LOADER_IM;
123
124 if(IS_NULL_PTR(mbuf))
125 {
126 DT_MAGICK_ABORT_GUARD_DISARM();
127 DestroyMagickWand(image);
128 return DT_IMAGEIO_OK;
129 }
130
131 float *mipbuf = dt_mipmap_cache_alloc(mbuf, img);
132 if (IS_NULL_PTR(mipbuf)) {
133 fprintf(stderr,
134 "[ImageMagick_open] could not alloc full buffer for image `%s'\n",
135 img->filename);
137 goto error;
138 }
139
140 ret = MagickExportImagePixels(image, 0, 0, img->width, img->height, "RGBP", FloatPixel, mipbuf);
141 if (ret != MagickTrue) {
142 fprintf(stderr,
143 "[ImageMagick_open] error reading image `%s'\n", img->filename);
144 goto error;
145 }
146
147 size_t profile_length;
148 uint8_t *profile_data = (uint8_t *)MagickGetImageProfile(image, "icc", &profile_length);
149 /* no alias support like GraphicsMagick, have to check both locations */
150 if(IS_NULL_PTR(profile_data))
151 profile_data = (uint8_t *)MagickGetImageProfile(image, "icm", &profile_length);
152 if(profile_data)
153 {
154 img->profile_size = profile_length;
155 img->profile = (uint8_t *)g_malloc0(profile_length);
156 memcpy(img->profile, profile_data, profile_length);
157 MagickRelinquishMemory(profile_data);
158 }
159
160 DT_MAGICK_ABORT_GUARD_DISARM();
161 DestroyMagickWand(image);
162 return DT_IMAGEIO_OK;
163
164error:
165 DT_MAGICK_ABORT_GUARD_DISARM();
166 DestroyMagickWand(image);
167 return err;
168}
169#endif
170
171// clang-format off
172// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
173// vim: shiftwidth=2 expandtab tabstop=2 cindent
174// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
175// 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:1600
@ DT_DEBUG_IMAGEIO
Definition darktable.h:755
#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:293
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:1752
@ 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