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

openimageio.cpp « intern « imbuf « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2e5dd5b6613d6d88fa7daa8505e839663d8b404c (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
/*
 *
 * ***** 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) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL LICENSE BLOCK *****
 * $Id: png.c 36777 2011-05-19 11:54:03Z blendix $
 */

/** \file blender/imbuf/intern/png.c
 *  \ingroup imbuf
 */

#ifdef WITH_OPENIMAGEIO

#include "MEM_sys_types.h"

#include "imbuf.h"

extern "C" {

#include "IMB_imbuf_types.h"
#include "IMB_imbuf.h"

#include "IMB_allocimbuf.h"
#include "IMB_metadata.h"
#include "IMB_filetype.h"

}

#include <OpenImageIO/imageio.h>

OIIO_NAMESPACE_USING

int imb_is_a_openimageio(unsigned char *buf)
{
	return 0;
}

int imb_is_a_filepath_openimageio(const char *filepath)
{
	ImageInput *in = ImageInput::create(filepath);
	ImageSpec spec;
	int recognized = in->open(filepath, spec);
	in->close();

	return recognized;
}

template<typename T> static void pack_pixels(T *pixels, int width, int height, int components, T alpha)
{
	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];
		}
	}
}

int imb_ftype_openimageio(ImFileType *type, ImBuf *ibuf)
{
	return ibuf->ftype & (PNG|TGA|JPG|BMP|RADHDR|TIF|OPENEXR|CINEON|DPX|DDS|JP2);
}

static int format_name_to_ftype(const char *format_name)
{
	if(strcmp(format_name, "png") == 0)
		return PNG;
	else if(strcmp(format_name, "targa") == 0)
		return TGA; /* RAWTGA */
	else if(strcmp(format_name, "jpeg") == 0)
		return JPG;
	else if(strcmp(format_name, "bmp") == 0)
		return BMP;
	else if(strcmp(format_name, "hdr") == 0)
		return RADHDR;
	else if(strcmp(format_name, "tiff") == 0)
		return TIF; /* TIF_16BIT */
	else if(strcmp(format_name, "openexr") == 0)
		return OPENEXR; /* OPENEXR_HALF, OPENEXR_COMPRESS */
	else if(strcmp(format_name, "cineon") == 0)
		return CINEON;
	else if(strcmp(format_name, "dpx") == 0)
		return DPX;
	else if(strcmp(format_name, "dds") == 0)
		return DDS;
	else if(strcmp(format_name, "jpeg2000") == 0)
		return JP2; /* JP2_12BIT, JP2_16BIT, JP2_YCC , JP2_CINE , JP2_CINE_48FPS */

	/* not handled: "field3d", "fits", "ico", "iff", "pnm", "ptex", "sgi", "zfile" */

	return 0;
}

ImBuf *imb_load_openimageio(const char *filepath, int flags)
{
	ImageInput *in = ImageInput::create(filepath);
	ImageSpec spec;
	bool success;

	if(!in->open(filepath, spec)) {
		delete in;
		return NULL;
	}
	
	/* we only handle certain number of components */
	int width = spec.width;
	int height = spec.height;
	int components = spec.nchannels;

	if(!(components == 1 || components == 3 || components == 4)) {
		delete in;
		return NULL;
	}

	ImBuf *ibuf = IMB_allocImBuf(width, height, 32, 0);
	ibuf->ftype = format_name_to_ftype(in->format_name());

	/* TODO: handle oiio:ColorSpace, oiio:Gamma, metadata, multilayer, size_t_safe */

	/* read RGBA pixels */
	if(spec.format == TypeDesc::UINT8 || spec.format == TypeDesc::INT8) {
		//if(in->get_string_attribute("oiio:ColorSpace") == "sRGB")
		ibuf->profile = IB_PROFILE_SRGB;

		imb_addrectImBuf(ibuf);

		uint8_t *pixels = (uint8_t*)ibuf->rect;
		int scanlinesize = width*components;

		success = in->read_image(TypeDesc::UINT8,
			pixels + (height-1)*scanlinesize,
			AutoStride,
			-scanlinesize*sizeof(uint8_t),
			AutoStride);

		pack_pixels<uint8_t>(pixels, width, height, components, 255);
	}
	else {
		ibuf->profile = IB_PROFILE_LINEAR_RGB; /* XXX assumption */

		imb_addrectfloatImBuf(ibuf);

		float *pixels = ibuf->rect_float;
		int scanlinesize = width*components;

		success = in->read_image(TypeDesc::FLOAT,
			pixels + (height-1)*scanlinesize,
			AutoStride,
			-scanlinesize*sizeof(float),
			AutoStride);

		pack_pixels<float>(pixels, width, height, components, 1.0f);
	}

	if(!success)
		fprintf(stderr, "OpenImageIO: error loading image: %s\n", in->geterror().c_str());

	in->close();
	delete in;

	return ibuf;
}

int imb_save_openimageio(struct ImBuf *ibuf, const char *filepath, int flags)
{
	ImageOutput *out = ImageOutput::create(filepath);

	if(ibuf->rect_float) {
		/* XXX profile */

		/* save as float image XXX works? */
		ImageSpec spec(ibuf->x, ibuf->y, 4, TypeDesc::FLOAT);
		int scanlinesize = ibuf->x*4;

		out->open(filepath, spec);

		/* conversion for different top/bottom convention */
		out->write_image(TypeDesc::FLOAT,
			ibuf->rect_float + (ibuf->y-1)*scanlinesize,
			AutoStride,
			-scanlinesize*sizeof(float),
			AutoStride);
	}
	else {
		/* save as 8bit image */
		ImageSpec spec(ibuf->x, ibuf->y, 4, TypeDesc::UINT8);
		int scanlinesize = ibuf->x*4;

		out->open(filepath, spec);

		/* conversion for different top/bottom convention */
		out->write_image(TypeDesc::UINT8,
			(uint8_t*)ibuf->rect + (ibuf->y-1)*scanlinesize,
			AutoStride,
			-scanlinesize*sizeof(uint8_t),
			AutoStride);
	}

	out->close();
	delete out;

	return 1;
}

#endif /* WITH_OPENIMAGEIO */