3 * ***** BEGIN GPL 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.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 /** \file blender/imbuf/intern/readimage.c
40 #include <sys/types.h>
47 #include "BLI_blenlib.h"
50 #include "IMB_imbuf_types.h"
51 #include "IMB_imbuf.h"
52 #include "IMB_filetype.h"
54 static ImBuf *imb_ibImageFromFile(const char *filepath, int flags)
59 for(type=IMB_FILE_TYPES; type->is_a; type++) {
60 if(type->load_filepath) {
61 ibuf= type->load_filepath(filepath, flags);
63 if(flags & IB_premul) {
64 IMB_premultiply_alpha(ibuf);
65 ibuf->flags |= IB_premul;
76 ImBuf *IMB_ibImageFromMemory(unsigned char *mem, size_t size, int flags)
82 printf("Error in ibImageFromMemory: NULL pointer\n");
86 for(type=IMB_FILE_TYPES; type->is_a; type++) {
88 ibuf= type->load(mem, size, flags);
90 if(flags & IB_premul) {
91 IMB_premultiply_alpha(ibuf);
92 ibuf->flags |= IB_premul;
100 fprintf(stderr, "Unknown fileformat\n");
105 ImBuf *IMB_loadifffile(int file, int flags)
111 if(file == -1) return NULL;
113 size= BLI_file_descriptor_size(file);
115 mem= mmap(NULL, size, PROT_READ, MAP_SHARED, file, 0);
116 if(mem==(unsigned char*)-1) {
117 fprintf(stderr, "Couldn't get mapping\n");
121 ibuf= IMB_ibImageFromMemory(mem, size, flags);
123 if(munmap(mem, size))
124 fprintf(stderr, "Couldn't unmap file.\n");
129 static void imb_cache_filename(char *filename, const char *name, int flags)
131 /* read .tx instead if it exists and is not older */
132 if(flags & IB_tilecache) {
133 BLI_strncpy(filename, name, IB_FILENAME_SIZE);
134 if(!BLI_replace_extension(filename, IB_FILENAME_SIZE, ".tx"))
137 if(BLI_file_older(name, filename))
141 BLI_strncpy(filename, name, IB_FILENAME_SIZE);
144 ImBuf *IMB_loadiffname(const char *name, int flags)
148 char filename[IB_FILENAME_SIZE];
150 imb_cache_filename(filename, name, flags);
152 ibuf= imb_ibImageFromFile(name, flags);
155 file = open(filename, O_BINARY|O_RDONLY);
156 if(file < 0) return NULL;
158 ibuf= IMB_loadifffile(file, flags);
163 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
164 BLI_strncpy(ibuf->cachename, filename, sizeof(ibuf->cachename));
165 for(a=1; a<ibuf->miptot; a++)
166 BLI_strncpy(ibuf->mipmap[a-1]->cachename, filename, sizeof(ibuf->cachename));
167 if(flags & IB_fields) IMB_de_interlace(ibuf);
173 ImBuf *IMB_testiffname(char *name, int flags)
177 char filename[IB_FILENAME_SIZE];
179 imb_cache_filename(filename, name, flags);
181 file = open(filename,O_BINARY|O_RDONLY);
182 if(file < 0) return NULL;
184 ibuf=IMB_loadifffile(file, flags|IB_test|IB_multilayer);
186 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
187 BLI_strncpy(ibuf->cachename, filename, sizeof(ibuf->cachename));
195 static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int *rect)
201 if(file == -1) return;
203 size= BLI_file_descriptor_size(file);
205 mem= mmap(NULL, size, PROT_READ, MAP_SHARED, file, 0);
206 if(mem==(unsigned char*)-1) {
207 fprintf(stderr, "Couldn't get memory mapping for %s\n", ibuf->cachename);
211 for(type=IMB_FILE_TYPES; type->is_a; type++)
212 if(type->load_tile && type->ftype && type->ftype(type, ibuf))
213 type->load_tile(ibuf, mem, size, tx, ty, rect);
215 if(munmap(mem, size))
216 fprintf(stderr, "Couldn't unmap memory for %s.\n", ibuf->cachename);
219 void imb_loadtile(ImBuf *ibuf, int tx, int ty, unsigned int *rect)
223 file = open(ibuf->cachename, O_BINARY|O_RDONLY);
226 imb_loadtilefile(ibuf, file, tx, ty, rect);