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"
53 #include "BKE_global.h"
56 #if defined(_WIN32) || defined (__APPLE__)
57 #include "quicktime_import.h"
58 #elif defined (__linux__)
59 #include "quicktime_import_linux.h"
63 #include "IMB_freeimage.h"
66 /* actually hard coded endianness */
67 #define GET_BIG_LONG(x) (((uchar *) (x))[0] << 24 | ((uchar *) (x))[1] << 16 | ((uchar *) (x))[2] << 8 | ((uchar *) (x))[3])
68 #define GET_LITTLE_LONG(x) (((uchar *) (x))[3] << 24 | ((uchar *) (x))[2] << 16 | ((uchar *) (x))[1] << 8 | ((uchar *) (x))[0])
69 #define SWAP_L(x) (((x << 24) & 0xff000000) | ((x << 8) & 0xff0000) | ((x >> 8) & 0xff00) | ((x >> 24) & 0xff))
70 #define SWAP_S(x) (((x << 8) & 0xff00) | ((x >> 8) & 0xff))
72 /* more endianness... should move to a separate file... */
73 #if defined(__sgi) || defined (__sparc) || defined (__sparc__) || defined (__PPC__) || defined (__ppc__) || defined (__BIG_ENDIAN__)
74 #define GET_ID GET_BIG_LONG
75 #define LITTLE_LONG SWAP_LONG
77 #define GET_ID GET_LITTLE_LONG
78 #define LITTLE_LONG ENDIAN_NOP
81 /* from misc_util: flip the bytes from x */
82 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
84 /* this one is only def-ed once, strangely... */
85 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
87 int IB_verbose = TRUE;
89 ImBuf *IMB_ibImageFromMemory(int *mem, int size, int flags) {
94 printf("Error in ibImageFromMemory: NULL pointer\n");
96 if ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)){
97 return (imb_loadiris((uchar *) mem, flags));
98 } else if ((BIG_LONG(mem[0]) & 0xfffffff0) == 0xffd8ffe0) {
99 return (imb_ibJpegImageFromMemory((uchar *)mem, size, flags));
102 if (GET_ID(mem) == CAT){
106 if (GET_ID(mem) == FORM){
107 len = ((GET_BIG_LONG(mem+1) + 1) & ~1) + 8;
108 if ((GET_ID(mem+2) == ILBM) || (GET_ID(mem+2) == ANIM)) break;
109 mem = (int *)((uchar *)mem +len);
116 if (GET_ID(mem) == FORM){
117 if (GET_ID(mem+2) == ILBM){
118 return (imb_loadamiga(mem, flags));
119 } else if (GET_ID(mem+5) == ILBM){ /* animaties */
120 return (imb_loadamiga(mem+3, flags));
121 } else if (GET_ID(mem+2) == ANIM){
122 return (imb_loadanim(mem, flags));
127 /* let quicktime handle png's, skips error messages ;)
128 * but only on windows
131 if(G.have_quicktime == FALSE) {
135 ibuf = imb_loadpng((uchar *)mem, size, flags);
136 if (ibuf) return(ibuf);
139 ibuf = imb_bmp_decode((uchar *)mem, size, flags);
140 if (ibuf) return(ibuf);
142 ibuf = imb_loadtarga((uchar *)mem, flags);
143 if (ibuf) return(ibuf);
145 #ifdef WITH_QUICKTIME
146 #if defined(_WIN32) || defined (__APPLE__)
147 if(G.have_quicktime) {
148 ibuf = imb_quicktime_decode((uchar *)mem, size, flags);
149 if (ibuf) return(ibuf);
153 #ifdef WITH_FREEIMAGE
154 ibuf = imb_freeimage_decode((uchar *)mem, size, flags);
155 if (ibuf) return(ibuf);
157 #ifdef WITH_IMAGEMAGICK
158 ibuf = imb_imagick_decode((uchar *)mem, size, flags);
159 if (ibuf) return(ibuf);
162 if (IB_verbose) fprintf(stderr, "Unknown fileformat\n");
169 struct ImBuf *IMB_loadiffmem(int *mem, int flags) {
173 // IMB_loadiffmem shouldn't be used anymore in new development
174 // it's still here to be backwards compatible...
176 maxlen= (GET_BIG_LONG(mem+1) + 1) & ~1;
178 if (GET_ID(mem) == CAT){
182 if (GET_ID(mem) == FORM){
183 len = ((GET_BIG_LONG(mem+1) + 1) & ~1) + 8;
184 if ((GET_ID(mem+2) == ILBM) || (GET_ID(mem+2) == ANIM)) break;
185 mem = (int *)((uchar *)mem +len);
192 if (GET_ID(mem) == FORM){
193 if (GET_ID(mem+2) == ILBM){
194 return (imb_loadamiga(mem, flags));
195 } else if (GET_ID(mem+5) == ILBM){ /* animaties */
196 return (imb_loadamiga(mem+3, flags));
197 } else if (GET_ID(mem+2) == ANIM){
198 return (imb_loadanim(mem, flags));
200 } else if ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC)){
201 return (imb_loadiris((uchar *) mem,flags));
202 } else if ((BIG_LONG(mem[0]) & 0xfffffff0) == 0xffd8ffe0) {
207 ibuf = imb_loadtarga((uchar *) mem,flags);
208 if (ibuf) return(ibuf);
210 if (IB_verbose) fprintf(stderr,"Unknown fileformat\n");
214 struct ImBuf *IMB_loadifffile(int file, int flags) {
218 if (file == -1) return (0);
220 size = BLI_filesize(file);
222 #if defined(AMIGA) || defined(__BeOS) || defined(WIN32)
223 mem= (int *)malloc(size);
225 printf("Out of mem\n");
229 if (read(file, mem, size)!=size){
230 printf("Read Error\n");
235 ibuf = IMB_ibImageFromMemory(mem, size, flags);
239 lseek(file, 0L, SEEK_SET);
242 mem= (int *)mmap(0,size,PROT_READ,MAP_SHARED,file,0);
244 printf("Couldn't get mapping\n");
248 ibuf = IMB_ibImageFromMemory(mem, size, flags);
250 if (munmap( (void *) mem, size)){
251 printf("Couldn't unmap file.\n");
258 struct ImBuf *IMB_loadiffname(char *naam, int flags) {
263 file = open(naam, O_BINARY|O_RDONLY);
265 if (file == -1) return (0);
267 ibuf= IMB_loadifffile(file, flags);
270 if (read(file, buf, 4) != 4) buf[0] = 0;
271 if ((BIG_LONG(buf[0]) & 0xfffffff0) == 0xffd8ffe0)
272 ibuf = imb_ibJpegImageFromFilename(naam, flags);
276 strncpy(ibuf->name, naam, sizeof(ibuf->name));
277 if (flags & IB_fields) IMB_de_interlace(ibuf);
283 struct ImBuf *IMB_testiffname(char *naam,int flags) {
288 file = open(naam,O_BINARY|O_RDONLY);
290 if (file<=0) return (0);
292 ibuf=IMB_loadifffile(file,flags);
294 strncpy(ibuf->name, naam, sizeof(ibuf->name));