--- /dev/null
+// Copyright (C) 2009 Masaki Saito <rezoolab@gmail.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+#include "pdutility.h"
+#include <stdio.h>
+
+using namespace pd;
+
+IplImage* loadTGAImage(const char* filename) {
+ unsigned char format;
+ unsigned short width, height;
+ unsigned char depth_color;
+ char* imageData;
+ IplImage* img;
+
+ FILE* fp = fopen(filename, "rb");
+
+ unsigned char tgaheader[18];
+ int size = fread(tgaheader, 1, 18, fp);
+ if(size != 18) return NULL;
+ format = tgaheader[2];
+ width = *(short*)(tgaheader + 12);
+ height = *(short*)(tgaheader + 14);
+ depth_color = tgaheader[14];
+
+ int imgsize = depth_color/8*width*height;
+ imageData = new char[imgsize];
+
+ if(format == TGA_TYPE_FULLCOLOR) {
+ size = fread(imageData, 1, imgsize, fp);
+ if(size != imgsize) return NULL;
+
+ if(depth_color == TGA_DEPTH_24BIT){
+ img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 3);
+ }else if(depth_color == TGA_DEPTH_32BIT){
+ img = cvCreateImageHeader(cvSize(width, height), IPL_DEPTH_8U, 4);
+ }else return NULL;
+
+ img->imageData = imageData;
+
+ } else if(format == TGA_TYPE_FULLCOLOR_RLE) {
+ return NULL; // »ÃÄêŪ¡£
+ } else {
+ fclose(fp);
+ return NULL;
+ }
+ fclose(fp);
+
+ return img;
+}
+
--- /dev/null
+// Copyright (C) 2009 Masaki Saito <rezoolab@gmail.com>
+//
+// This program is free software; you can redistribute it and/or
+// modify it under the terms of the GNU General Public License
+// as published by the Free Software Foundation; either version 2
+// of the License, or (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+#include <cv.h>
+
+namespace pd {
+
+const int TGA_TYPE_NOIMAGE = 0;
+const int TGA_TYPE_INDEXCOLOR = 1;
+const int TGA_TYPE_FULLCOLOR = 2;
+const int TGA_TYPE_MONOCHROME = 3;
+const int TGA_TYPE_INDEXCOLOR_RLE = 9;
+const int TGA_TYPE_FULLCOLOR_RLE = 10;
+const int TGA_TYPE_MONOCHROME_RLE = 11;
+const int TGA_DEPTH_24BIT = 24;
+const int TGA_DEPTH_32BIT = 32;
+
+IplImage* loadTGAImage(const char* filename);
+
+}