Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
imageio_gm.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 Hubert Kowalski.
10 Copyright (C) 2020-2021 Pascal Obry.
11 Copyright (C) 2022 Martin Baƙinka.
12 Copyright (C) 2023 Alynx Zhou.
13
14 darktable is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, either version 3 of the License, or
17 (at your option) any later version.
18
19 darktable is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with darktable. If not, see <http://www.gnu.org/licenses/>.
26*/
27
28#ifdef HAVE_GRAPHICSMAGICK
29#include "imageio_gm.h"
30#include "common/colorspaces.h"
31#include "common/darktable.h"
32#include "common/exif.h"
33#include "control/conf.h"
34#include "develop/develop.h"
35#include "imageio.h"
36
37#include <assert.h>
38#include <inttypes.h>
39#include <magick/api.h>
40#include <memory.h>
41#include <stdio.h>
42#include <strings.h>
43
44
45// we only support images with certain filename extensions via GraphicsMagick;
46// RAWs are excluded as GraphicsMagick would render them with third party
47// libraries in reduced quality - slow and only 8-bit
48static gboolean _supported_image(const gchar *filename)
49{
50 const char *extensions_whitelist[] = { "tif", "tiff", "gif", "jpc", "jp2", "bmp", "dcm", "jng",
51 "miff", "mng", "pbm", "pnm", "ppm", "pgm", "webp", NULL };
52 gboolean supported = FALSE;
53 char *ext = g_strrstr(filename, ".");
54 if(IS_NULL_PTR(ext)) return FALSE;
55 ext++;
56 for(const char **i = extensions_whitelist; !IS_NULL_PTR(*i); i++)
57 if(!g_ascii_strncasecmp(ext, *i, strlen(*i)))
58 {
60 break;
61 }
62 return supported;
63}
64
65
67{
69 ExceptionInfo exception;
70 Image *image = NULL;
71 ImageInfo *image_info = NULL;
72 uint32_t width, height;
73
74 if(!_supported_image(filename)) return DT_IMAGEIO_FILE_CORRUPTED;
75
76 if(!img->exif_inited) (void)dt_exif_read(img, filename);
77
78 GetExceptionInfo(&exception);
79 image_info = CloneImageInfo((ImageInfo *)NULL);
80
81 g_strlcpy(image_info->filename, filename, sizeof(image_info->filename));
82
83 image = ReadImage(image_info, &exception);
84 if(exception.severity != UndefinedException) CatchException(&exception);
85 if(IS_NULL_PTR(image))
86 {
87 fprintf(stderr, "[GraphicsMagick_open] image `%s' not found\n", img->filename);
89 goto error;
90 }
91
92 dt_print(DT_DEBUG_IMAGEIO, "[GraphicsMagick_open] image `%s' loading\n", img->filename);
93
94 if(IsCMYKColorspace(image->colorspace))
95 {
96 fprintf(stderr, "[GraphicsMagick_open] error: CMYK images are not supported.\n");
98 goto error;
99 }
100
101 width = image->columns;
102 height = image->rows;
103
104 img->width = width;
105 img->height = height;
106
107 img->dsc.channels = 4;
108 img->dsc.datatype = TYPE_FLOAT;
109 img->dsc.bpp = 4 * sizeof(float);
110 img->dsc.cst = IOP_CS_RGB;
111 img->dsc.filters = 0u;
112 img->flags &= ~DT_IMAGE_RAW;
113 img->flags &= ~DT_IMAGE_HDR;
114 img->flags &= ~DT_IMAGE_S_RAW;
115 img->flags |= DT_IMAGE_LDR;
116
117 img->loader = LOADER_GM;
118
119 if(IS_NULL_PTR(mbuf))
120 {
121 if(image) DestroyImage(image);
122 if(image_info) DestroyImageInfo(image_info);
123 DestroyExceptionInfo(&exception);
124 return DT_IMAGEIO_OK;
125 }
126
127 float *mipbuf = (float *)dt_mipmap_cache_alloc(mbuf, img);
128 if(IS_NULL_PTR(mipbuf))
129 {
130 fprintf(stderr, "[GraphicsMagick_open] could not alloc full buffer for image `%s'\n", img->filename);
132 goto error;
133 }
134
135 for(uint32_t row = 0; row < height; row++)
136 {
137 float *bufprt = mipbuf + (size_t)4 * row * img->width;
138 int ret = DispatchImage(image, 0, row, width, 1, "RGBP", FloatPixel, bufprt, &exception);
139 if(exception.severity != UndefinedException) CatchException(&exception);
140 if(ret != MagickPass)
141 {
142 fprintf(stderr, "[GraphicsMagick_open] error reading image `%s'\n", img->filename);
144 goto error;
145 }
146 }
147
148 size_t profile_length;
149 const uint8_t *profile_data = (const uint8_t *)GetImageProfile(image, "ICM", &profile_length);
150 if(profile_data)
151 {
152 img->profile_size = profile_length;
153 img->profile = (uint8_t *)g_malloc0(profile_length);
154 memcpy(img->profile, profile_data, profile_length);
155 }
156
157 if(image) DestroyImage(image);
158 if(image_info) DestroyImageInfo(image_info);
159 DestroyExceptionInfo(&exception);
160 return DT_IMAGEIO_OK;
161
162error:
163 if(image) DestroyImage(image);
164 if(image_info) DestroyImageInfo(image_info);
165 DestroyExceptionInfo(&exception);
166 return err;
167}
168#endif
169
170// clang-format off
171// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
172// vim: shiftwidth=2 expandtab tabstop=2 cindent
173// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
174// 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
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
@ IOP_CS_RGB
static const int row
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_GM
Definition image.h:230
dt_imageio_retval_t dt_imageio_open_gm(dt_image_t *img, const char *filename, dt_mipmap_buffer_t *buf)
Definition imageio_gm.h:33
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