|
Blender V2.27
|
00001 00030 #include "BLI_blenlib.h" 00031 00032 #include "imbuf.h" 00033 00034 #include "IMB_imbuf_types.h" 00035 #include "IMB_imbuf.h" 00036 #include "IMB_allocimbuf.h" 00037 #include "IMB_filetype.h" 00038 00039 /* some code copied from article on microsoft.com, copied 00040 here for enhanced BMP support in the future 00041 http://www.microsoft.com/msj/defaultframe.asp?page=/msj/0197/mfcp1/mfcp1.htm&nav=/msj/0197/newnav.htm 00042 */ 00043 00044 typedef struct BMPINFOHEADER{ 00045 unsigned int biSize; 00046 unsigned int biWidth; 00047 unsigned int biHeight; 00048 unsigned short biPlanes; 00049 unsigned short biBitCount; 00050 unsigned int biCompression; 00051 unsigned int biSizeImage; 00052 unsigned int biXPelsPerMeter; 00053 unsigned int biYPelsPerMeter; 00054 unsigned int biClrUsed; 00055 unsigned int biClrImportant; 00056 } BMPINFOHEADER; 00057 00058 typedef struct BMPHEADER { 00059 unsigned short biType; 00060 unsigned int biSize; 00061 unsigned short biRes1; 00062 unsigned short biRes2; 00063 unsigned int biOffBits; 00064 } BMPHEADER; 00065 00066 #define BMP_FILEHEADER_SIZE 14 00067 00068 static int checkbmp(unsigned char *mem) 00069 { 00070 int ret_val = 0; 00071 BMPINFOHEADER bmi; 00072 unsigned int u; 00073 00074 if (mem) { 00075 if ((mem[0] == 'B') && (mem[1] == 'M')) { 00076 /* skip fileheader */ 00077 mem += BMP_FILEHEADER_SIZE; 00078 } else { 00079 } 00080 00081 /* for systems where an int needs to be 4 bytes aligned */ 00082 memcpy(&bmi, mem, sizeof(bmi)); 00083 00084 u = LITTLE_LONG(bmi.biSize); 00085 /* we only support uncompressed 24 or 32 bits images for now */ 00086 if (u >= sizeof(BMPINFOHEADER)) { 00087 if ((bmi.biCompression == 0) && (bmi.biClrUsed == 0)) { 00088 u = LITTLE_SHORT(bmi.biBitCount); 00089 if (u >= 16) { 00090 ret_val = 1; 00091 } 00092 } 00093 } 00094 } 00095 00096 return(ret_val); 00097 } 00098 00099 int imb_is_a_bmp(unsigned char *buf) { 00100 00101 return checkbmp(buf); 00102 } 00103 00104 struct ImBuf *imb_bmp_decode(unsigned char *mem, size_t size, int flags) 00105 { 00106 struct ImBuf *ibuf = 0; 00107 BMPINFOHEADER bmi; 00108 int x, y, depth, skip, i; 00109 unsigned char *bmp, *rect; 00110 unsigned short col; 00111 00112 (void)size; /* unused */ 00113 00114 if (checkbmp(mem) == 0) return(0); 00115 00116 if ((mem[0] == 'B') && (mem[1] == 'M')) { 00117 /* skip fileheader */ 00118 mem += BMP_FILEHEADER_SIZE; 00119 } 00120 00121 /* for systems where an int needs to be 4 bytes aligned */ 00122 memcpy(&bmi, mem, sizeof(bmi)); 00123 00124 skip = LITTLE_LONG(bmi.biSize); 00125 x = LITTLE_LONG(bmi.biWidth); 00126 y = LITTLE_LONG(bmi.biHeight); 00127 depth = LITTLE_SHORT(bmi.biBitCount); 00128 00129 /* printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, 00130 depth, bmi.biBitCount); */ 00131 /* printf("skip: %d, x: %d y: %d, depth: %d (%x)\n", skip, x, y, 00132 depth, bmi.biBitCount); */ 00133 if (flags & IB_test) { 00134 ibuf = IMB_allocImBuf(x, y, depth, 0); 00135 } else { 00136 ibuf = IMB_allocImBuf(x, y, depth, IB_rect); 00137 bmp = mem + skip; 00138 rect = (unsigned char *) ibuf->rect; 00139 00140 if (depth == 16) { 00141 for (i = x * y; i > 0; i--) { 00142 col = bmp[0] + (bmp[1] << 8); 00143 rect[0] = ((col >> 10) & 0x1f) << 3; 00144 rect[1] = ((col >> 5) & 0x1f) << 3; 00145 rect[2] = ((col >> 0) & 0x1f) << 3; 00146 00147 rect[3] = 255; 00148 rect += 4; bmp += 2; 00149 } 00150 00151 } else if (depth == 24) { 00152 for (i = y; i > 0; i--) { 00153 int j; 00154 for (j = x ; j > 0; j--) { 00155 rect[0] = bmp[2]; 00156 rect[1] = bmp[1]; 00157 rect[2] = bmp[0]; 00158 00159 rect[3] = 255; 00160 rect += 4; bmp += 3; 00161 } 00162 /* for 24-bit images, rows are padded to multiples of 4 */ 00163 bmp += x % 4; 00164 } 00165 } else if (depth == 32) { 00166 for (i = x * y; i > 0; i--) { 00167 rect[0] = bmp[2]; 00168 rect[1] = bmp[1]; 00169 rect[2] = bmp[0]; 00170 rect[3] = bmp[3]; 00171 rect += 4; bmp += 4; 00172 } 00173 } 00174 } 00175 00176 if (ibuf) { 00177 ibuf->ftype = BMP; 00178 ibuf->profile = IB_PROFILE_SRGB; 00179 } 00180 00181 return(ibuf); 00182 } 00183 00184 /* Couple of helper functions for writing our data */ 00185 static int putIntLSB(unsigned int ui,FILE *ofile) { 00186 putc((ui>>0)&0xFF,ofile); 00187 putc((ui>>8)&0xFF,ofile); 00188 putc((ui>>16)&0xFF,ofile); 00189 return putc((ui>>24)&0xFF,ofile); 00190 } 00191 00192 static int putShortLSB(unsigned short us,FILE *ofile) { 00193 putc((us>>0)&0xFF,ofile); 00194 return putc((us>>8)&0xFF,ofile); 00195 } 00196 00197 /* Found write info at http://users.ece.gatech.edu/~slabaugh/personal/c/bitmapUnix.c */ 00198 int imb_savebmp(struct ImBuf *ibuf, const char *name, int flags) { 00199 00200 BMPINFOHEADER infoheader; 00201 int bytesize, extrabytes, x, y, t, ptr; 00202 uchar *data; 00203 FILE *ofile; 00204 00205 (void)flags; /* unused */ 00206 00207 extrabytes = (4 - ibuf->x*3 % 4) % 4; 00208 bytesize = (ibuf->x * 3 + extrabytes) * ibuf->y; 00209 00210 data = (uchar *) ibuf->rect; 00211 ofile = fopen(name,"wb"); 00212 if (!ofile) return 0; 00213 00214 putShortLSB(19778,ofile); /* "BM" */ 00215 putIntLSB(0,ofile); /* This can be 0 for BI_RGB bitmaps */ 00216 putShortLSB(0,ofile); /* Res1 */ 00217 putShortLSB(0,ofile); /* Res2 */ 00218 putIntLSB(BMP_FILEHEADER_SIZE + sizeof(infoheader),ofile); 00219 00220 putIntLSB(sizeof(infoheader),ofile); 00221 putIntLSB(ibuf->x,ofile); 00222 putIntLSB(ibuf->y,ofile); 00223 putShortLSB(1,ofile); 00224 putShortLSB(24,ofile); 00225 putIntLSB(0,ofile); 00226 putIntLSB(bytesize + BMP_FILEHEADER_SIZE + sizeof(infoheader),ofile); 00227 putIntLSB(0,ofile); 00228 putIntLSB(0,ofile); 00229 putIntLSB(0,ofile); 00230 putIntLSB(0,ofile); 00231 00232 /* Need to write out padded image data in bgr format */ 00233 for (y=0;y<ibuf->y;y++) { 00234 for (x=0;x<ibuf->x;x++) { 00235 ptr=(x + y * ibuf->x) * 4; 00236 if (putc(data[ptr+2],ofile) == EOF) return 0; 00237 if (putc(data[ptr+1],ofile) == EOF) return 0; 00238 if (putc(data[ptr],ofile) == EOF) return 0; 00239 } 00240 /* add padding here */ 00241 for (t=0;t<extrabytes;t++) if (putc(0,ofile) == EOF) return 0; 00242 } 00243 if (ofile) { 00244 fflush(ofile); 00245 fclose(ofile); 00246 } 00247 return 1; 00248 }
1.7.3