// Copyright (C) 2009 Masaki Saito <rezoolab@gmail.com>
//
// pd::Utility
-// \82»\82ê\82¼\82ê\82Ì\83N\83\89\83X\82Ì\96ð\8a\84\82É\82Í\93\96\82Ä\82Í\82Ü\82ç\82È\82¢\82ª\81A\95K\97v\82Æ\82È\82é\8aÖ\90\94\8cQ\82ð\97ñ\8b\93\82µ\82Ü\82·\81B
+// それぞれのクラスの役割には当てはまらないが、必要となる関数群を列挙します。
#include "pdutility.h"
#include <cv.h>
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
+ unsigned char format; // TGA画像のフォーマット
+ unsigned short width, height; // 幅、高さ
+ unsigned char depth_color; // 色深度
+ char* imageData; // 生のデータ
+ bool direction_x; // 格納方向(x方向,false=左から右,true=右から左)
+ bool direction_y; // 格納方向(y方向,false=下から上,true=上から下)
+ IplImage* img; // 最終的に格納するIplImage
FILE* fp = fopen(filename, "rb");
- // \83w\83b\83_\8fî\95ñ\82Ì\93Ç\82Ý\8eæ\82è
+ // ヘッダ情報の読み取り
unsigned char tgaheader[18];
if(fread(tgaheader, sizeof(char), sizeof(tgaheader), fp)
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¢
+ if(direction_x == true){ //右から左に格納してあるデータは読み込まない
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
+ // フルカラー画像(RLE圧縮なし)の場合
if(fread(imageData, sizeof(char), imgsize, fp) != imgsize){
delete [] imageData;
fclose(fp);
return NULL;
}
} else if(format == TGA_TYPE_FULLCOLOR_RLE) {
- // \83t\83\8b\83J\83\89\81[\89æ\91\9c(RLE\88³\8fk\82 \82è)\82Ì\8fê\8d\87
+ // フルカラー画像(RLE圧縮あり)の場合
if((depth_color != TGA_DEPTH_32BIT) &&
(depth_color != TGA_DEPTH_24BIT)){
fclose(fp);
return NULL;
}
- // \98A\91±\83f\81[\83^\82©\82»\82¤\82Å\82È\82¢\82©\8c\9f\8fØ
+ // 連続データかそうでないか検証
if((first_byte >> 7) == 1){
- // \98A\91±\82·\82é\83f\81[\83^\82Æ\82µ\82Ä\8f\88\97\9d
+ // 連続するデータとして処理
unsigned char num = (first_byte & 127) + 1;
char b[4];
}
i += num*channel;
}else{
- // \98A\91±\82µ\82È\82¢\83f\81[\83^\82Æ\82µ\82Ä\8f\88\97\9d
+ // 連続しないデータとして処理
unsigned char num = first_byte + 1;
unsigned int read_byte = num*channel;
if(fread((imageData + i), sizeof(char), read_byte, fp) != read_byte){
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;