//========================================== // 関数 : LoadRGB // 目的 : RGB情報を読み込む // 戻り値: unsigned char * RGB情報が格納されたメモリのアドレス。読み込みに失敗した場合NULLを返す // 引数 : FILE * pf ファイルポインタ // int nWidth 画像の横幅(ピクセル数) // int nHeight 画像の縦幅(ピクセル数) //========================================== unsigned char * LoadRGB(FILE * pf, int nWidth, int nHeight) { unsigned int nSizeOfRGBDatas = nWidth * nHeight * sizeof(unsigned char)* 3/* R, G, B それぞれ1バイトずつで合計3バイト*/; // RGB情報を格納するメモリのサイズ unsigned char * pbyRGBDatas = (unsigned char *)malloc(nSizeOfRGBDatas); // RGB情報を格納するメモリを動的確保し、その先頭アドレスを保持する long lPaddingByte = ComputePaddingByte(nWidth); // 不要なデータのバイト数 long lColByte = nWidth * 3/* R, G, B それぞれ1バイトずつで合計3バイト*/; // 画像の横一列分のデータのバイト数 int nColIndex; // 画像の横何列目のデータかを示す。また、今まで横何列分読み込んだかも示す。 for (nColIndex = 0; nColIndex<nHeight; ++nColIndex) // 横1列ずつ画像のRGB情報を読み込んでいく { if (fread(pbyRGBDatas + lColByte * nColIndex, lColByte, 1, pf) == 0) // 画像の横1列分を読み込む { free(pbyRGBDatas); // 読み込みに失敗した場合メモリを解放し、NULLを返して関数から脱出する return NULL; } fseek(pf, lPaddingByte, SEEK_CUR); // 不要なデータを読み飛ばす } BGR_to_RGB(pbyRGBDatas, nWidth * nHeight); return pbyRGBDatas; }
bool loader::tga::Load(File *pIO,Image *map) { ImageOpener image(map); struct TGA_Head header; pIO->seek(File::SET,0); pIO->read(&header, sizeof(struct TGA_Head)); uint32_t width =(uint32_t)header.width; uint32_t height=(uint32_t)header.height; /* toy::Log(_T("struct TGA_Head\n")); toy::Log(_T("{\n")); toy::Log(_T(" idlength :%d\n"), header.idlength); toy::Log(_T(" colourmaptype :%d\n"), header.colourmaptype); toy::Log(_T(" datatypecode :%d\n"), header.datatypecode); toy::Log(_T(" x_origin :%d\n"), header.x_origin); toy::Log(_T(" y_origin :%d\n"), header.y_origin); toy::Log(_T(" width :%d\n"), header.width); toy::Log(_T(" height :%d\n"), header.height); toy::Log(_T(" bitsperpixel :%d\n"), header.bitsperpixel); toy::Log(_T(" imagedescriptor:%d\n"), header.imagedescriptor); toy::Log(_T("};\n")); */ if(header.idlength!=0) return 0; if(header.colourmaptype!=0) return 0; if(header.datatypecode!=2) return 0; if(header.bitsperpixel!=24 && header.bitsperpixel!=32) return 0; image.width(width); image.height(height); if(header.bitsperpixel==32)//GBRA format { uint32_t size = width * height * 4; image.allocator()->size(size); uint8_t* data=(uint8_t*)image.data(); if( (header.imagedescriptor & 0x20) == 0)// bottom-left { pIO->read(data,size); BGRA_to_RGBA(data,size); } else// top-left(upside down) { uint8_t* temp=(uint8_t*)malloc(size); pIO->read(temp,size); width*=4; for( int32_t k=0,j=size-width ; j>=0 ; j-=width,k+=width) { for( decltype(width) i=0 ; i<width ; i+=4) { data[i+k] =temp[i+j+2]; data[i+k+1]=temp[i+j+1]; data[i+k+2]=temp[i+j]; data[i+k+3]=temp[i+j+3]; } } free(temp); } } else if(header.bitsperpixel==24)//GBR format { uint32_t size = width * height * 3; image.allocator()->size(width*height*4);// Finally, we want RBGA format not RBG. uint8_t* data=(uint8_t*)image.data(); if( (header.imagedescriptor & 0x20) == 0)// bottom-left { pIO->read(data,size); BGR_to_RGB(data,size); RGB_to_RGBA(data,size,0); } else// top-left(upside down) { uint8_t* temp=(uint8_t*)malloc(size); pIO->read(temp,size); width*=3; for( int32_t k=0,j=size-width ; j>=0 ; j-=width,k+=width) { for( uint32_t i=0 ; i<width ; i+=3) { data[i+k] =temp[i+j+2]; data[i+k+1]=temp[i+j+1]; data[i+k+2]=temp[i+j]; } } free(temp); RGB_to_RGBA(data,size,0); } } return 1; }