Blender V2.27

dds_api.cpp

00001 
00025 #include <stddef.h>
00026 #include <dds_api.h>
00027 #include <Stream.h>
00028 #include <DirectDrawSurface.h>
00029 #include <stdio.h> // printf
00030 #include <fstream>
00031 
00032 extern "C" {
00033 
00034 #include "imbuf.h"
00035 #include "IMB_imbuf_types.h"
00036 #include "IMB_imbuf.h"
00037 #include "IMB_allocimbuf.h"
00038 
00039 
00040 int imb_save_dds(struct ImBuf * ibuf, const char *name, int flags)
00041 {
00042         return(0); /* todo: finish this function */
00043 
00044         /* check image buffer */
00045         if (ibuf == 0) return (0);
00046         if (ibuf->rect == 0) return (0);
00047 
00048         /* open file for writing */
00049         std::ofstream fildes(name);
00050 
00051         /* write header */
00052         fildes << "DDS ";
00053         fildes.close();
00054 
00055         return(1);
00056 }
00057 
00058 int imb_is_a_dds(unsigned char *mem) // note: use at most first 32 bytes
00059 {
00060         /* heuristic check to see if mem contains a DDS file */
00061         /* header.fourcc == FOURCC_DDS */
00062         if ((mem[0] != 'D') || (mem[1] != 'D') || (mem[2] != 'S') || (mem[3] != ' ')) return(0);
00063         /* header.size == 124 */
00064         if ((mem[4] != 124) || mem[5] || mem[6] || mem[7]) return(0);
00065         return(1);
00066 }
00067 
00068 struct ImBuf *imb_load_dds(unsigned char *mem, size_t size, int flags)
00069 {
00070         struct ImBuf * ibuf = 0;
00071         DirectDrawSurface dds(mem, size); /* reads header */
00072         unsigned char bits_per_pixel;
00073         unsigned int *rect;
00074         Image img;
00075         unsigned int numpixels = 0;
00076         int col;
00077         unsigned char *cp = (unsigned char *) &col;
00078         Color32 pixel;
00079         Color32 *pixels = 0;
00080 
00081         if(!imb_is_a_dds(mem))
00082                 return (0);
00083 
00084         /* check if DDS is valid and supported */
00085         if (!dds.isValid()) {
00086                 /* no need to print error here, just testing if it is a DDS */
00087                 if(flags & IB_test)
00088                         return (0);
00089 
00090                 printf("DDS: not valid; header follows\n");
00091                 dds.printInfo();
00092                 return(0);
00093         }
00094         if (!dds.isSupported()) {
00095                 printf("DDS: format not supported\n");
00096                 return(0);
00097         }
00098         if ((dds.width() > 65535) || (dds.height() > 65535)) {
00099                 printf("DDS: dimensions too large\n");
00100                 return(0);
00101         }
00102 
00103         /* convert DDS into ImBuf */
00104         // TODO use the image RGB or RGBA tag to determine the bits per pixel
00105         if (dds.hasAlpha()) bits_per_pixel = 32;
00106         else bits_per_pixel = 24;
00107         ibuf = IMB_allocImBuf(dds.width(), dds.height(), bits_per_pixel, 0);
00108         if (ibuf == 0) return(0); /* memory allocation failed */
00109 
00110         ibuf->ftype = DDS;
00111         ibuf->profile = IB_PROFILE_SRGB;
00112 
00113         if ((flags & IB_test) == 0) {
00114                 if (!imb_addrectImBuf(ibuf)) return(ibuf);
00115                 if (ibuf->rect == 0) return(ibuf);
00116 
00117                 rect = ibuf->rect;
00118                 dds.mipmap(&img, 0, 0); /* load first face, first mipmap */
00119                 pixels = img.pixels();
00120                 numpixels = dds.width() * dds.height();
00121                 cp[3] = 0xff; /* default alpha if alpha channel is not present */
00122 
00123                 for (unsigned int i = 0; i < numpixels; i++) {
00124                         pixel = pixels[i];
00125                         cp[0] = pixel.r; /* set R component of col */
00126                         cp[1] = pixel.g; /* set G component of col */
00127                         cp[2] = pixel.b; /* set B component of col */
00128                         if (bits_per_pixel == 32)
00129                                 cp[3] = pixel.a; /* set A component of col */
00130                         rect[i] = col;
00131                 }
00132                 IMB_flipy(ibuf);
00133         }
00134 
00135         return(ibuf);
00136 }
00137 
00138 } // extern "C"