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;
}
========================================
No.4
- 回答日時:
1データが24ビットなので
fprintf(output, "%x ", *(int * ) &buffData[j*3]);
これだとintが4バイトなので,32ビット分表示してしまうかなと。
なので、0xffffffで&を取れば24ビット分にできたなぁって。
でも深く考えていないので0xffffff00かもしれない。
No.3
- 回答日時:
補足
画像ファイルなので
10進数より16進数のが分かりやすいかなと思い。%x表示しています。
また、printfだと見にくかったのでファイル(ファイル名output)に出力してしまいました。
それと24ビットなのにintで扱った為、上位8bitに前のデータが入ってしまっています。
0xffffffでマスクすればよかった(汗)
この回答への補足
>>それと24ビットなのにintで扱った為、上位8bitに前のデータが入ってしまっています。
>>0xffffffでマスクすればよかった(汗)
の意味が分からないのですが,どういう意味なのでしょうか?
No.2ベストアンサー
- 回答日時:
時間ないので全て解析しきれませんでしたが、
やった成果だけでも、参考になればどぞ
#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;
}
No.1
- 回答日時:
中身をまだ理解出来ていませんがとりあえず。
/************************************
* 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がいくつ読み込めばいいのか困ってしまうと思います。
お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!
似たような質問が見つかりました
- C言語・C++・C# c言語の問題の説明、各所ごとに 5 2023/07/26 11:03
- C言語・C++・C# バイナリファイルをコピーするのにかかる時間を測りたいのですが実行するとFatel error:gli 2 2022/11/03 01:10
- C言語・C++・C# このプログラミング誰か教えてくれませんか 1 2022/06/02 15:27
- C言語・C++・C# プログラミングの授業の課題です 1 2023/01/17 22:15
- C言語・C++・C# 宣言する関数の形が決まっている状態で、 str1とstr2の文字列をこの順に引っ付けてstrに保存し 2 2022/05/30 18:21
- C言語・C++・C# 並列プログラミングのπ計算について 1 2022/07/16 22:30
- C言語・C++・C# c言語 プログラムのエラー 1 2023/02/11 20:31
- C言語・C++・C# Cのdoubleの浮動小数点表示について 3 2023/04/17 13:14
- C言語・C++・C# c言語の問題です 3 2023/01/10 16:15
- C言語・C++・C# C言語のエラーについて 2 2022/07/11 13:56
関連するカテゴリからQ&Aを探す
おすすめ情報
デイリーランキングこのカテゴリの人気デイリーQ&Aランキング
-
#define _CRT_SECURE_NO_WARNIN...
-
std::set<int> で、ある値が何...
-
卒業研究でよく分からないとこ...
-
(int *)の意味
-
C言語 逆順の配列の仕方を教え...
-
初項a_0=aとし、漸化式 a_n+1=(...
-
c++でサインカーブ、コサインカ...
-
C言語での平滑化フィルタ
-
外積のプログラムについて質問...
-
C言語でlookupのような関数って?
-
C言語で関数に渡す値をScanfか...
-
C言語での引数の省略方法
-
足して100になるような乱数のア...
-
「指定されたキャストは有効で...
-
複数桁10進数の*桁目だけを抽出...
-
万年カレンダで祝日にマークを...
-
BMPを白黒画像にする方法。
-
C言語による簡易電卓の作成
-
C言語の配列をC++のvectorに高...
-
C言語で、数値の桁数を求めるに...
マンスリーランキングこのカテゴリの人気マンスリーQ&Aランキング
-
「指定されたキャストは有効で...
-
C言語での引数の省略方法
-
複数桁10進数の*桁目だけを抽出...
-
#define _CRT_SECURE_NO_WARNIN...
-
ラップ関数とはどんなものですか?
-
卒業研究でよく分からないとこ...
-
【C++】関数ポインタの使い方
-
実数の整数部,小数部の取得
-
std::set<int> で、ある値が何...
-
C言語 エラーの原因がわからな...
-
c言語
-
system関数がうまくいかない
-
C++でvectorにテキストファイル...
-
acceptをalarmでタイムアウトさ...
-
if と配列の組み合わせ
-
return 1L
-
「{ } で囲むだけ」は正しい?
-
(マルチスレッド)_beginthrea...
-
PowerShellがうまくいかない
-
このプログラミング誰か教えて...
おすすめ情報