java读取bmp图片像素数据_读取bmp文件的像素值

How can I read the color value of 24bit BMP images at all the pixel [h*w] in C or C++ on Windows [better without any 3rd party library]. I got Dev-C++

A working code will be really appreciated as I've never worked on Image reading & have come to SO after Googling [if you can google better than me, plz provide a link].

解决方案

You can try this one:

unsigned char* readBMP(char* filename)

{

int i;

FILE* f = fopen(filename, "rb");

unsigned char info[54];

fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

// extract image height and width from header

int width = *(int*)&info[18];

int height = *(int*)&info[22];

int size = 3 * width * height;

unsigned char* data = new unsigned char[size]; // allocate 3 bytes per pixel

fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once

fclose(f);

for(i = 0; i < size; i += 3)

{

unsigned char tmp = data[i];

data[i] = data[i+2];

data[i+2] = tmp;

}

return data;

}

Now data should contain the (R, G, B) values of the pixels. The color of pixel (i, j) is stored at data[j * width + i], data[j * width + i + 1] and data[j * width + i + 2].

In the last part, the swap between every first and third pixel is done because windows stores the color values as (B, G, R) triples, not (R, G, B).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值