3 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version. The Blender
9 * Foundation also sells licenses for use in proprietary software under
10 * the Blender License. See http://www.blender.org/BL/ for information
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 * All rights reserved.
25 * The Original Code is: all of this file.
27 * Contributor(s): none yet.
29 * ***** END GPL/BL DUAL LICENSE BLOCK *****
39 #include "BLI_blenlib.h"
42 #include "imbuf_patch.h"
43 #include "IMB_imbuf_types.h"
44 #include "IMB_imbuf.h"
46 #include "IMB_amiga.h"
48 #include "IMB_targa.h"
54 #include "IMB_radiance_hdr.h"
55 #include "BKE_global.h"
58 #include "openexr/openexr_api.h"
62 #if defined(_WIN32) || defined (__APPLE__)
63 #include "quicktime_import.h"
67 /* actually hard coded endianness */
68 #define GET_BIG_LONG(x) (((uchar *) (x))[0] << 24 | ((uchar *) (x))[1] << 16 | ((uchar *) (x))[2] << 8 | ((uchar *) (x))[3])
69 #define GET_LITTLE_LONG(x) (((uchar *) (x))[3] << 24 | ((uchar *) (x))[2] << 16 | ((uchar *) (x))[1] << 8 | ((uchar *) (x))[0])
70 #define SWAP_L(x) (((x << 24) & 0xff000000) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) | ((x >> 24) & 0xff))
71 #define SWAP_S(x) (((x << 8) & 0xff00) | ((x >> 8) & 0xff))
73 /* more endianness... should move to a separate file... */
74 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
75 #define GET_ID GET_BIG_LONG
76 #define LITTLE_LONG SWAP_LONG
78 #define GET_ID GET_LITTLE_LONG
79 #define LITTLE_LONG ENDIAN_NOP
82 /* from misc_util: flip the bytes from x */
83 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
85 /* this one is only def-ed once, strangely... */
86 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
88 int IB_verbose = TRUE;
90 ImBuf *IMB_ibImageFromMemory(int *mem, int size, int flags) {
95 printf("Error in ibImageFromMemory: NULL pointer\n");
97 if ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)){
98 return (imb_loadiris((uchar *) mem, flags));
99 } else if ((BIG_LONG(mem[0]) & 0xfffffff0) == 0xffd8ffe0) {
100 return (imb_ibJpegImageFromMemory((uchar *)mem, size, flags));
103 if (GET_ID(mem) == CAT){
107 if (GET_ID(mem) == FORM){
108 len = ((GET_BIG_LONG(mem+1) + 1) & ~1) + 8;
109 if ((GET_ID(mem+2) == ILBM) || (GET_ID(mem+2) == ANIM)) break;
110 mem = (int *)((uchar *)mem +len);
117 if (GET_ID(mem) == FORM){
118 if (GET_ID(mem+2) == ILBM){
119 return (imb_loadamiga(mem, flags));
120 } else if (GET_ID(mem+5) == ILBM){ /* animaties */
121 return (imb_loadamiga(mem+3, flags));
122 } else if (GET_ID(mem+2) == ANIM){
123 return (imb_loadanim(mem, flags));
128 ibuf = imb_loadpng((uchar *)mem, size, flags);
129 if (ibuf) return(ibuf);
131 ibuf = imb_bmp_decode((uchar *)mem, size, flags);
132 if (ibuf) return(ibuf);
134 ibuf = imb_loadtarga((uchar *)mem, flags);
135 if (ibuf) return(ibuf);
137 if (G.have_libtiff) {
138 ibuf = imb_loadtiff((uchar *)mem, size, flags);
139 if (ibuf) return(ibuf);
142 ibuf = imb_loadhdr((uchar*)mem, size, flags);
143 if (ibuf) return (ibuf);
146 ibuf = imb_load_openexr((uchar *)mem, size, flags);
147 if (ibuf) return (ibuf);
150 #ifdef WITH_QUICKTIME
151 #if defined(_WIN32) || defined (__APPLE__)
152 if(G.have_quicktime) {
153 ibuf = imb_quicktime_decode((uchar *)mem, size, flags);
154 if (ibuf) return(ibuf);
159 if (IB_verbose) fprintf(stderr, "Unknown fileformat\n");
166 struct ImBuf *IMB_loadiffmem(int *mem, int flags) {
170 // IMB_loadiffmem shouldn't be used anymore in new development
171 // it's still here to be backwards compatible...
173 maxlen= (GET_BIG_LONG(mem+1) + 1) & ~1;
175 if (GET_ID(mem) == CAT){
179 if (GET_ID(mem) == FORM){
180 len = ((GET_BIG_LONG(mem+1) + 1) & ~1) + 8;
181 if ((GET_ID(mem+2) == ILBM) || (GET_ID(mem+2) == ANIM)) break;
182 mem = (int *)((uchar *)mem +len);
189 if (GET_ID(mem) == FORM){
190 if (GET_ID(mem+2) == ILBM){
191 return (imb_loadamiga(mem, flags));
192 } else if (GET_ID(mem+5) == ILBM){ /* animaties */
193 return (imb_loadamiga(mem+3, flags));
194 } else if (GET_ID(mem+2) == ANIM){
195 return (imb_loadanim(mem, flags));
197 } else if ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)){
198 return (imb_loadiris((uchar *) mem,flags));
199 } else if ((BIG_LONG(mem[0]) & 0xfffffff0) == 0xffd8ffe0) {
204 ibuf = imb_loadtarga((uchar *) mem,flags);
205 if (ibuf) return(ibuf);
207 if (IB_verbose) fprintf(stderr,"Unknown fileformat\n");
211 struct ImBuf *IMB_loadifffile(int file, int flags) {
215 if (file == -1) return (0);
217 size = BLI_filesize(file);
219 #if defined(AMIGA) || defined(__BeOS) || defined(WIN32)
220 mem= (int *)malloc(size);
222 printf("Out of mem\n");
226 if (read(file, mem, size)!=size){
227 printf("Read Error\n");
232 ibuf = IMB_ibImageFromMemory(mem, size, flags);
236 lseek(file, 0L, SEEK_SET);
239 mem= (int *)mmap(0,size,PROT_READ,MAP_SHARED,file,0);
241 printf("Couldn't get mapping\n");
245 ibuf = IMB_ibImageFromMemory(mem, size, flags);
247 if (munmap( (void *) mem, size)){
248 printf("Couldn't unmap file.\n");
255 struct ImBuf *IMB_loadiffname(char *naam, int flags) {
260 file = open(naam, O_BINARY|O_RDONLY);
262 if (file == -1) return (0);
264 ibuf= IMB_loadifffile(file, flags);
267 if (read(file, buf, 4) != 4) buf[0] = 0;
268 if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
269 ibuf = imb_ibJpegImageFromFilename(naam, flags);
273 strncpy(ibuf->name, naam, sizeof(ibuf->name));
274 if (flags & IB_fields) IMB_de_interlace(ibuf);
280 struct ImBuf *IMB_testiffname(char *naam,int flags) {
285 file = open(naam,O_BINARY|O_RDONLY);
287 if (file<=0) return (0);
289 ibuf=IMB_loadifffile(file,flags);
291 strncpy(ibuf->name, naam, sizeof(ibuf->name));