Blender V2.27

readimage.c

00001 
00032 #ifdef _WIN32
00033 #include <io.h>
00034 #include <stddef.h>
00035 #include <sys/types.h>
00036 #include "mmap_win.h"
00037 #define open _open
00038 #define read _read
00039 #define close _close
00040 #endif
00041 
00042 #include "BLI_blenlib.h"
00043 
00044 #include "imbuf.h"
00045 #include "IMB_imbuf_types.h"
00046 #include "IMB_imbuf.h"
00047 #include "IMB_filetype.h"
00048 
00049 ImBuf *IMB_ibImageFromMemory(unsigned char *mem, size_t size, int flags)
00050 {
00051         ImBuf *ibuf;
00052         ImFileType *type;
00053 
00054         if(mem == NULL) {
00055                 printf("Error in ibImageFromMemory: NULL pointer\n");
00056                 return NULL;
00057         }
00058 
00059         for(type=IMB_FILE_TYPES; type->is_a; type++) {
00060                 if(type->load) {
00061                         ibuf= type->load(mem, size, flags);
00062                         if(ibuf) {
00063                                 if(flags & IB_premul) {
00064                                         IMB_premultiply_alpha(ibuf);
00065                                         ibuf->flags |= IB_premul;
00066                                 }
00067 
00068                                 return ibuf;
00069                         }
00070                 }
00071         }
00072 
00073         fprintf(stderr, "Unknown fileformat\n");
00074         
00075         return NULL;
00076 }
00077 
00078 ImBuf *IMB_loadifffile(int file, int flags)
00079 {
00080         ImBuf *ibuf;
00081         unsigned char *mem;
00082         size_t size;
00083 
00084         if(file == -1) return 0;
00085 
00086         size= BLI_filesize(file);
00087 
00088         mem= mmap(0, size, PROT_READ, MAP_SHARED, file, 0);
00089         if(mem==(unsigned char*)-1) {
00090                 fprintf(stderr, "Couldn't get mapping\n");
00091                 return 0;
00092         }
00093 
00094         ibuf= IMB_ibImageFromMemory(mem, size, flags);
00095 
00096         if(munmap(mem, size))
00097                 fprintf(stderr, "Couldn't unmap file.\n");
00098 
00099         return ibuf;
00100 }
00101 
00102 static void imb_cache_filename(char *filename, const char *name, int flags)
00103 {
00104         /* read .tx instead if it exists and is not older */
00105         if(flags & IB_tilecache) {
00106                 BLI_strncpy(filename, name, IB_FILENAME_SIZE);
00107                 if(!BLI_replace_extension(filename, IB_FILENAME_SIZE, ".tx"))
00108                         return;
00109 
00110                 if(BLI_file_older(name, filename))
00111                         return;
00112         }
00113 
00114         BLI_strncpy(filename, name, IB_FILENAME_SIZE);
00115 }
00116 
00117 ImBuf *IMB_loadiffname(const char *name, int flags)
00118 {
00119         ImBuf *ibuf;
00120         int file, a;
00121         char filename[IB_FILENAME_SIZE];
00122 
00123         imb_cache_filename(filename, name, flags);
00124 
00125         file = open(filename, O_BINARY|O_RDONLY);
00126         if(file < 0) return 0;
00127 
00128         ibuf= IMB_loadifffile(file, flags);
00129 
00130         if(ibuf) {
00131                 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
00132                 BLI_strncpy(ibuf->cachename, filename, sizeof(ibuf->cachename));
00133                 for(a=1; a<ibuf->miptot; a++)
00134                         BLI_strncpy(ibuf->mipmap[a-1]->cachename, filename, sizeof(ibuf->cachename));
00135                 if(flags & IB_fields) IMB_de_interlace(ibuf);
00136         }
00137 
00138         close(file);
00139 
00140         return ibuf;
00141 }
00142 
00143 ImBuf *IMB_testiffname(char *name, int flags)
00144 {
00145         ImBuf *ibuf;
00146         int file;
00147         char filename[IB_FILENAME_SIZE];
00148 
00149         imb_cache_filename(filename, name, flags);
00150 
00151         file = open(filename,O_BINARY|O_RDONLY);
00152         if(file < 0) return 0;
00153 
00154         ibuf=IMB_loadifffile(file, flags|IB_test|IB_multilayer);
00155         if(ibuf) {
00156                 BLI_strncpy(ibuf->name, name, sizeof(ibuf->name));
00157                 BLI_strncpy(ibuf->cachename, filename, sizeof(ibuf->cachename));
00158         }
00159 
00160         close(file);
00161 
00162         return ibuf;
00163 }
00164 
00165 static void imb_loadtilefile(ImBuf *ibuf, int file, int tx, int ty, unsigned int *rect)
00166 {
00167         ImFileType *type;
00168         unsigned char *mem;
00169         size_t size;
00170 
00171         if(file == -1) return;
00172 
00173         size= BLI_filesize(file);
00174 
00175         mem= mmap(0, size, PROT_READ, MAP_SHARED, file, 0);
00176         if(mem==(unsigned char*)-1) {
00177                 fprintf(stderr, "Couldn't get memory mapping for %s\n", ibuf->cachename);
00178                 return;
00179         }
00180 
00181         for(type=IMB_FILE_TYPES; type->is_a; type++)
00182                 if(type->load_tile && type->ftype(type, ibuf))
00183                         type->load_tile(ibuf, mem, size, tx, ty, rect);
00184 
00185         if(munmap(mem, size))
00186                 fprintf(stderr, "Couldn't unmap memory for %s.\n", ibuf->cachename);
00187 }
00188 
00189 void imb_loadtile(ImBuf *ibuf, int tx, int ty, unsigned int *rect)
00190 {
00191         int file;
00192 
00193         file = open(ibuf->cachename, O_BINARY|O_RDONLY);
00194         if(file < 0) return;
00195 
00196         imb_loadtilefile(ibuf, file, tx, ty, rect);
00197 
00198         close(file);
00199 }
00200