Welcome to mirror list, hosted at ThFree Co, Russian Federation.

openimageio_api.cpp « oiio « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 838343dce35cf21108e16a809d9928c1358b78c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * ***** BEGIN GPL LICENSE BLOCK *****
 *
 * 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 2
 * 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, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): Dalai Felinto and Brecht van Lommel
 *
 * ***** END GPL LICENSE BLOCK *****
 */

/** \file blender/imbuf/intern/oiio/openimageio_api.cpp
 *  \ingroup openimageio
 */

#include <set>

#include <openimageio_api.h>
#include <OpenImageIO/imageio.h>

OIIO_NAMESPACE_USING

#if defined(WIN32) && !defined(FREE_WINDOWS)
#include "utfconv.h"
#endif

extern "C"
{
#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"
#include "IMB_allocimbuf.h"
#include "IMB_colormanagement.h"
#include "IMB_colormanagement_intern.h"
}

using namespace std;

typedef unsigned char uchar;

template <class T, class Q>
static void fill_all_channels(T *pixels, int width, int height, int components, Q alpha)
{
	if (components == 2) {
		for (int i = width * height - 1; i >= 0; i--) {
			pixels[i * 4 + 3] = pixels[i * 2 + 1];
			pixels[i * 4 + 2] = pixels[i * 2 + 0];
			pixels[i * 4 + 1] = pixels[i * 2 + 0];
			pixels[i * 4 + 0] = pixels[i * 2 + 0];
		}
	}
	else if (components == 3) {
		for (int i = width * height - 1; i >= 0; i--) {
			pixels[i * 4 + 3] = alpha;
			pixels[i * 4 + 2] = pixels[i * 3 + 2];
			pixels[i * 4 + 1] = pixels[i * 3 + 1];
			pixels[i * 4 + 0] = pixels[i * 3 + 0];

		}
	}
	else if (components == 1) {
		for (int i = width * height - 1; i >= 0; i--) {
			pixels[i * 4 + 3] = alpha;
			pixels[i * 4 + 2] = pixels[i];
			pixels[i * 4 + 1] = pixels[i];
			pixels[i * 4 + 0] = pixels[i];
		}
	}

}

static ImBuf *imb_oiio_load_image(ImageInput *in, int width, int height, int components, int flags, bool is_alpha)
{
	ImBuf *ibuf;
	int scanlinesize = width * components * sizeof(uchar);

	/* allocate the memory for the image */
	ibuf = IMB_allocImBuf(width, height, is_alpha ? 32 : 24, flags | IB_rect);

	try
	{
		in->read_image(TypeDesc::UINT8,
		               (uchar *)ibuf->rect + (height - 1) * scanlinesize,
		               AutoStride,
		               -scanlinesize,
		               AutoStride);
	}
	catch (const std::exception &exc)
	{
		std::cerr << exc.what() << std::endl;
		if (ibuf) IMB_freeImBuf(ibuf);

		return NULL;
	}

	/* ImBuf always needs 4 channels */
	fill_all_channels((uchar *)ibuf->rect, width, height, components, 0xFF);

	return ibuf;
}

static ImBuf *imb_oiio_load_image_float(ImageInput *in, int width, int height, int components, int flags, bool is_alpha)
{
	ImBuf *ibuf;
	int scanlinesize = width * components * sizeof(float);

	/* allocate the memory for the image */
	ibuf = IMB_allocImBuf(width, height, is_alpha ? 32 : 24, flags | IB_rectfloat);

	try
	{
		in->read_image(TypeDesc::FLOAT,
		               (uchar *)ibuf->rect_float + (height - 1) * scanlinesize,
		               AutoStride,
		               -scanlinesize,
		               AutoStride);
	}
	catch (const std::exception &exc)
	{
		std::cerr << exc.what() << std::endl;
		if (ibuf) IMB_freeImBuf(ibuf);

		return NULL;
	}

	/* ImBuf always needs 4 channels */
	fill_all_channels((float *)ibuf->rect_float, width, height, components, 1.0f);

	/* note: Photoshop 16 bit files never has alpha with it, so no need to handle associated/unassociated alpha */
	return ibuf;
}

extern "C"
{

int imb_is_a_photoshop(const char *filename)
{
	const char *photoshop_extension[] = {
		".psd",
		".pdd",
		".psb",
		NULL
	};

	return BLI_testextensie_array(filename, photoshop_extension);
}

int imb_save_photoshop(struct ImBuf *ibuf, const char *name, int flags)
{
	if (flags & IB_mem) {
		printf("Photoshop PSD-save: Create PSD in memory CURRENTLY NOT SUPPORTED !\n");
		imb_addencodedbufferImBuf(ibuf);
		ibuf->encodedsize = 0;
		return(0);
	}

	return(0);
}

struct ImBuf *imb_load_photoshop(const char *filename, int flags, char colorspace[IM_MAX_SPACE])
{
	ImageInput *in = NULL;
	struct ImBuf *ibuf = NULL;
	int width, height, components;
	bool is_float, is_alpha;
	TypeDesc typedesc;
	int basesize;

	/* load image from file through OIIO */
	if (imb_is_a_photoshop(filename) == 0) return (NULL);

	colorspace_set_default_role(colorspace, IM_MAX_SPACE, COLOR_ROLE_DEFAULT_BYTE);

	in = ImageInput::create(filename);
	if (!in) return NULL;

	ImageSpec spec, config;
	config.attribute("oiio:UnassociatedAlpha", (int) 1);

	if (!in->open(filename, spec, config)) {
		delete in;
		return NULL;
	}

	string ics = spec.get_string_attribute("oiio:ColorSpace");
	BLI_strncpy(colorspace, ics.c_str(), IM_MAX_SPACE);

	width = spec.width;
	height = spec.height;
	components = spec.nchannels;
	is_alpha = spec.alpha_channel != -1;
	basesize = spec.format.basesize();
	is_float = basesize > 1;

	/* we only handle certain number of components */
	if (!(components >= 1 && components <= 4)) {
		if (in) {
			in->close();
			delete in;
		}
		return NULL;
	}

	if (is_float)
		ibuf = imb_oiio_load_image_float(in, width, height, components, flags, is_alpha);
	else
		ibuf = imb_oiio_load_image(in, width, height, components, flags, is_alpha);

	if (in) {
		in->close();
		delete in;
	}

	if (!ibuf)
		return NULL;

	/* ImBuf always needs 4 channels */
	ibuf->ftype = PSD;
	ibuf->channels = 4;
	ibuf->planes = (3 + (is_alpha ? 1 : 0)) * 4 << basesize;

	try
	{
		return ibuf;
	}
	catch (const std::exception &exc)
	{
		std::cerr << exc.what() << std::endl;
		if (ibuf) IMB_freeImBuf(ibuf);

		return NULL;
	}
}

} // export "C"