アプリ版:「スタンプのみでお礼する」機能のリリースについて

C言語で24bit Windowsbitmapの画像データ部を読み込んで10進数でダンプするプログラムを書きましたが000…と表示されます.
バイナリファイルの扱いは初めてなので勘違いを多々しているとおもいます.ご指導,ご指摘宜しくお願いします.
=======ソース===========================
#include<stdio.h>
#include<stdlib.h>
int main(int argc,char *argv[]){
FILE *fp;
char buff[40];
char buffData[2560];
size_t size = 1;
size_t n = 40;
int biSize;
int biBitCount;
int biCompression;
int biWidth;
int biHeight;
int bfSize;
int bfOffBits;
int lineByte;
int i;
int position;
int line;
if( argc != 2){
printf("Run this way !! %s [bit map image] \n",argv[0]);
return 0;
}
fp = fopen(argv[1],"rb");
if(fp == NULL){
printf("%s No such file or directory !!\n",argv[1]);
return 0;
}
/*********************************
* check header infomation *
*********************************/
// check input file type
fread(buff,size,n,fp);
if('B' != buff[0] || 'M' != buff[1]){
printf("## Warning ## %s is not bit map file !!\n",argv[1]);
return 0;
}
// check bitmap type
biSize = *(int*)(buff + 14);
if( biSize != 40){
printf("%s isn`t Windows bitmap !!\n",argv[1]);
return 0;
}
// check the size(bit) of 1 pixel
biBitCount = *(int*)(buff + 28);
if(biBitCount != 24){
printf("%s isn't 24bit Windows bitmap !!\n",argv[1]);
return 0;
}
// check the type of compression
biCompression = *(int*)(buff + 30);
if(biCompression != 0){
printf("%s is compressed Windows bitmap !!",argv[1]);
return 0;
}
// check the width of image (pixel)
biWidth = *(int*)(buff + 18);
//printf("Width : %d\n",biWidth);
// check the height of image (pixel)
biHeight = *(int*)(buff + 22);
//printf("Height : %d\n",biHeight);
// check the file size
bfSize = *(int*)(buff + 2);
// check the offset to image data
bfOffBits = *(int*)(buff + 10);
/************************************
* reading image data *
************************************/
// the size of one line
lineByte = (biWidth * biBitCount) / 8;
printf("LineByte = %d\n",lineByte);
for(i=0;i < biHeight ;i++){
position = bfOffBits + lineByte * (biHeight - (i + 1));
fseek(fp,position,SEEK_SET);
fread(buffData,line,1,fp);
printf("%d\n",*(int*)buffData);
}
fclose(fp);
return 0;
}
========================================

A 回答 (4件)

中身をまだ理解出来ていませんがとりあえず。



/************************************
* reading image data *
************************************/
// the size of one line
lineByte = (biWidth * biBitCount) / 8;
printf("LineByte = %d\n",lineByte);
for(i=0;i < biHeight ;i++){
position = bfOffBits + lineByte * (biHeight - (i + 1));
fseek(fp,position,SEEK_SET);
fread(buffData,line,1,fp);
printf("%d\n",*(int*)buffData);
}


fread(buffData,line,1,fp);
lineに値が入っていないようですよ。

freadがいくつ読み込めばいいのか困ってしまうと思います。

この回答への補足

回答ありがとうございます。lineByteにすべきですね。修正して試してみます。

補足日時:2009/12/11 23:12
    • good
    • 0

時間ないので全て解析しきれませんでしたが、


やった成果だけでも、参考になればどぞ

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

int main(int argc, char * argv[]) {
BITMAPFILEHEADER BMFH;
BITMAPINFOHEADER BMIH;
FILE * fp;
char buffData[2560];
size_t size = 1;
size_t n = 40;
int lineByte;
int i;
if (argc != 2) {
printf("Run this way !! %s [bit map image] \n", argv[0]);
return 0;
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("%s No such file or directory !!\n", argv[1]);
return 0;
}
/*********************************
* check header infomation *
*********************************/
// check input file type
fread(&BMFH, sizeof(BMFH), 1, fp);
if (memcmp((char*)&BMFH.bfType, "BM", sizeof(BMFH.bfType))) {
printf("## Warning ## %s is not bit map file !!\n", argv[1]);
return 0;
}

// check bitmap type
fread(&BMIH, sizeof(BMIH), 1, fp);
if (BMIH.biSize != 40) {
printf("%s isn`t Windows bitmap !!\n", argv[1]);
return 0;
}
// check the size(bit) of 1 pixel
if (BMIH.biBitCount != 24) {
printf("%s isn't 24bit Windows bitmap !!\n", argv[1]);
return 0;
}
// check the type of compression
if (BMIH.biCompression != 0) {
printf("%s is compressed Windows bitmap !!", argv[1]);
return 0;
}
/*
// check the width of image (pixel)
biWidth = *(int * )(buff + 18);
//printf("Width : %d\n",biWidth);
// check the height of image (pixel)
biHeight = *(int * )(buff + 22);
//printf("Height : %d\n",biHeight);
// check the file size
bfSize = *(int * )(buff + 2);
// check the offset to image data
bfOffBits = *(int * )(buff + 10);
*/

/************************************
* reading image data *
************************************/
// the size of one line
lineByte = (BMIH.biWidth * BMIH.biBitCount) / 8;
printf("LineByte = %d\n", lineByte);

int j;
FILE *output;
output = fopen("output.txt", "w");
for (i = 0; i < BMIH.biHeight; i++) {
fread(buffData, lineByte, 1, fp);
for(j = 0; j < BMIH.biWidth; j++) {
fprintf(output, "%x ", *(int * ) &buffData[j*3]);
}
fprintf(output, "\n");
}
fclose(fp);
return 0;
}
    • good
    • 0

補足


画像ファイルなので
10進数より16進数のが分かりやすいかなと思い。%x表示しています。
また、printfだと見にくかったのでファイル(ファイル名output)に出力してしまいました。

それと24ビットなのにintで扱った為、上位8bitに前のデータが入ってしまっています。
0xffffffでマスクすればよかった(汗)

この回答への補足

>>それと24ビットなのにintで扱った為、上位8bitに前のデータが入ってしまっています。
>>0xffffffでマスクすればよかった(汗)
の意味が分からないのですが,どういう意味なのでしょうか?

補足日時:2009/12/14 16:21
    • good
    • 0

1データが24ビットなので


fprintf(output, "%x ", *(int * ) &buffData[j*3]);
これだとintが4バイトなので,32ビット分表示してしまうかなと。

なので、0xffffffで&を取れば24ビット分にできたなぁって。
でも深く考えていないので0xffffff00かもしれない。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!