テキストファイルでの範囲指定
Posted: 2009年12月03日(木) 18:41
ビットマップ画像からRGBの値を抽出するプログラムを作成しましたが、画像の周りの余白の部分がいらないので、画像の4隅の座標をテキストファイルで指定してその中のRGBの値を抽出するにはどうしたらいいでしょうか?
ご教授の程、よろしくお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <malloc.h>
int main( int argc, char **argv )
{
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
char *filename;
FILE *fp;
RGBTRIPLE *buf, **data;
unsigned char R,G,B;
int LineSize,BufferSize;
int x, y;
//ファイルを開く
if ( argc != 2){
filename = "bitmap2.bmp";
} else {
filename = argv[1];
}
//ファイルを開く
fp = fopen( filename, "rb");
fread( &bf, sizeof(BITMAPFILEHEADER), 1, fp );
fread( &bi, sizeof(BITMAPINFOHEADER), 1, fp );
if (bi.biBitCount !=24){
printf("このファイルは24bit画像専用です\n");
return 0;
}
if((bi.biWidth % 4) == 0){
LineSize = sizeof(RGBTRIPLE) * bi.biWidth ;
} else {
LineSize = sizeof(RGBTRIPLE) * bi.biWidth + (4 - (bi.biWidth * 3) % 4) ;
}
BufferSize = LineSize * bi.biHeight;
//変換元のデータ
buf = (RGBTRIPLE *)malloc( BufferSize );
data = (RGBTRIPLE **)malloc( sizeof(RGBTRIPLE *) * bi.biHeight );
//RGB別に出力する
for ( y = 0; y <= bi.biHeight; y++ ) {
data[ y ] = (RGBTRIPLE *)((char*)buf + LineSize * y);
}
fread( buf, BufferSize, 1, fp );
for ( y = 0; y <= bi.biHeight; y++ ) {
for ( x = 0; x <= bi.biWidth; x++) {
R = data[ y ][ x ].rgbtRed;
G = data[ y ][ x ].rgbtGreen;
B = data[ y ][ x ].rgbtBlue;
printf("X軸:%d,Y軸:%d, R:%d, G:%d, B:%d\n", x, y, R, G, B);
}
}
return 0;
}
ご教授の程、よろしくお願いします。
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <malloc.h>
int main( int argc, char **argv )
{
BITMAPFILEHEADER bf;
BITMAPINFOHEADER bi;
char *filename;
FILE *fp;
RGBTRIPLE *buf, **data;
unsigned char R,G,B;
int LineSize,BufferSize;
int x, y;
//ファイルを開く
if ( argc != 2){
filename = "bitmap2.bmp";
} else {
filename = argv[1];
}
//ファイルを開く
fp = fopen( filename, "rb");
fread( &bf, sizeof(BITMAPFILEHEADER), 1, fp );
fread( &bi, sizeof(BITMAPINFOHEADER), 1, fp );
if (bi.biBitCount !=24){
printf("このファイルは24bit画像専用です\n");
return 0;
}
if((bi.biWidth % 4) == 0){
LineSize = sizeof(RGBTRIPLE) * bi.biWidth ;
} else {
LineSize = sizeof(RGBTRIPLE) * bi.biWidth + (4 - (bi.biWidth * 3) % 4) ;
}
BufferSize = LineSize * bi.biHeight;
//変換元のデータ
buf = (RGBTRIPLE *)malloc( BufferSize );
data = (RGBTRIPLE **)malloc( sizeof(RGBTRIPLE *) * bi.biHeight );
//RGB別に出力する
for ( y = 0; y <= bi.biHeight; y++ ) {
data[ y ] = (RGBTRIPLE *)((char*)buf + LineSize * y);
}
fread( buf, BufferSize, 1, fp );
for ( y = 0; y <= bi.biHeight; y++ ) {
for ( x = 0; x <= bi.biWidth; x++) {
R = data[ y ][ x ].rgbtRed;
G = data[ y ][ x ].rgbtGreen;
B = data[ y ][ x ].rgbtBlue;
printf("X軸:%d,Y軸:%d, R:%d, G:%d, B:%d\n", x, y, R, G, B);
}
}
return 0;
}