OSDN Git Service

名前空間をよりわかりやすく、かつTGA右->左に対応
[paldema/paldema.git] / src / cpp / pdutility.cpp
index 4791f71..4888adf 100644 (file)
 #include <cv.h>
 #include <stdio.h>
 
-IplImage* pd::loadTGAImage(const char* filename) {
-  unsigned char format;
-  unsigned short width, height;
-  unsigned char depth_color;
-  char* imageData;
-  IplImage* img;
+namespace pd {
+
+IplImage* loadTGAImage(const char* filename) {
+  unsigned char format;         // TGA\89æ\91\9c\82Ì\83t\83H\81[\83}\83b\83g
+  unsigned short width, height; // \95\9d\81A\8d\82\82³
+  unsigned char depth_color;    // \90F\90[\93x
+  char* imageData;              // \90\82Ì\83f\81[\83^
+  bool direction_x;             // \8ai\94[\95û\8cü(x\95û\8cü,false=\8d\82©\82ç\89E,true=\89E\82©\82ç\8d¶)
+  bool direction_y;             // \8ai\94[\95û\8cü(y\95û\8cü,false=\89º\82©\82ç\8fã,true=\8fã\82©\82ç\89º)
+  IplImage* img;                // \8dÅ\8fI\93I\82É\8ai\94[\82·\82éIplImage
 
   FILE* fp = fopen(filename, "rb");
   
+  // \83w\83b\83_\8fî\95ñ\82Ì\93Ç\82Ý\8eæ\82è
+  
   unsigned char tgaheader[18];
   if(fread(tgaheader, 1, 18, fp) != 18){
     fclose(fp);
@@ -39,9 +45,19 @@ IplImage* pd::loadTGAImage(const char* filename) {
   depth_color = tgaheader[16];
   unsigned char channel = depth_color/8;
   
+  unsigned char direction_byte = tgaheader[17];
+  direction_x = ((0x01 << 4) & direction_byte) >> 4;
+  direction_y = ((0x01 << 5) & direction_byte) >> 5;
+  if(direction_x == true){ //\89E\82©\82ç\8d\82É\8ai\94[\82µ\82Ä\82 \82é\83f\81[\83^\82Í\93Ç\82Ý\8d\9e\82Ü\82È\82¢
+    fclose(fp);
+    return NULL;
+  }
+  
   unsigned int imgsize = channel*width*height;
   imageData = new char[imgsize];
   
+  // \82»\82ê\82¼\82ê\82Ì\95û\8e®\82É\91Î\89\9e\82µ\82½\93Ç\82Ý\8eæ\82è
+  
   if(format == TGA_TYPE_FULLCOLOR) {
     // \83t\83\8b\83J\83\89\81[\89æ\91\9c(RLE\88³\8fk\82È\82µ)\82Ì\8fê\8d\87
     if(fread(imageData, 1, imgsize, fp) != imgsize){
@@ -113,6 +129,11 @@ IplImage* pd::loadTGAImage(const char* filename) {
       img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 4);
     img->imageData = imageData;
     
+    // \83w\83b\83_\82Ì\90Ý\92è
+    if(direction_y == true) img->origin = 0;
+    else img->origin = 1;
+    img->widthStep = width*channel;
+    
   } else {
     fclose(fp);
     return NULL;
@@ -121,3 +142,5 @@ IplImage* pd::loadTGAImage(const char* filename) {
   
   return img;
 }
+
+}