读取和编辑位图文件 [英] reading in and editing bitmap files

查看:68
本文介绍了读取和编辑位图文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,最近我一直在做的事情让我头疼不已,我想我需要一些帮助.我想做的只是从一个普通的位图文件中读取它的宽度,高度,然后将每个像素的所有数据馈送到结构数组中.我想出的是以下内容:

okay, what i''ve been doing recently has been giving me a massive headache, and i think i need a little help. what i''m trying to do, is just read from a normal bitmap file, it''s width, height, and than feed all the data for each individual pixel into an array of structures. what i came up with is below:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;


struct PIXEL
{
	unsigned char r, g, b;
};

int main(int argc, char *argv[])
{
	ifstream image;
	int width, height;
	PIXEL * pixeldata;
	int imageIdx = 0;
	unsigned char temp;
	// picture.bmp is just a random picture i have
	image.open("picture.bmp", ios::in | ios::binary);
	image.seekg(18);
	image.read((char*) &width, 4);
	image.read((char*) &height, 4);
	image.seekg(54);

	pixeldata = new PIXEL[width - 1, height - 1 ];
	int rowcount = 0;
	for(int y = height - 1; y >= 0; y--)
	{
		rowcount = 0;
		for (int x = 0; x < width; x++)
		{
			if(image.eof() ) break;
			image.read((char*) &pixeldata[x, y].b, 1);
			image.read((char*) &pixeldata[x, y].g, 1);
			image.read((char*) &pixeldata[x, y].r, 1);
			rowcount += 3;
		}

		while(rowcount % 4)
		{
			image.read((char*) &temp, 1);
			rowcount++;
		}
	}
	image.close();

        // this just prints out the data so i can compare it to the original file
	for (int i = 0; i< 50; i++)
	{
		for( int y = 0; y< 20; y++)
		{
			cout << "  pixel: (" << i << "," << y << ")   "
				 << (int) pixeldata[i,y].r << "   " << (int) pixeldata[i,y].g << "   " << (int) pixeldata[i,y].b << "\n";
		}
	}
	system("PAUSE");
}



这是完全无法正常工作的:(.我认为必须有一种比c ++或Windows提供的更直接的方法.任何人都可以对此提供任何见解吗?我的最终目标是能够加载位图及其所有内容.像我一样,将像素数据转换成多维数组,x和y值从左上角开始,然后可以更改各个像素的一些rgb值,然后将新的像素数据输出到相同的位图文件.我知道我将如何更改像素数据,但是我实际上不知道如何进行输出,因此除了像素数据之外,什么都没有改变:(,我们将不胜感激.



this is utterly disfunctional :( .I figure there has to be a more straightforward way than this that either c++ or windows provides. can anyone provide any insight into this? my ultimate goal here is be able to load a bitmap and all of it''s pixel data into a multidimentional array like i did, the x and y values starting in the top left, and then be able to change some rgb values of the individual pixels, and then output the new pixel data to the same bitmap file. i know how i would go about changing the pixel data, but i literally have no idea how to do the output so that nothing is changed besides the pixel data :( , any help is appreciated.

推荐答案

您应该研究 BMP文件格式 [ -请注意,并非每个BMP都是每个像素24位,即红色1字节,绿色1字节和蓝色1字节.
-请注意:
You should study the BMP file format[^] if you want to do the whole thing "by hand". Looking at your code i''d say:
-note that not every BMP is 24 bits per pixel, that is, 1 byte for red, 1 byte for green and 1 byte for blue.
-note that this:
pixeldata = new PIXEL[width - 1, height - 1 ];

不会分配(宽度-1)乘(高度-1)PIXEL结构的缓冲区,您可能想要在这里是:

does not allocate a buffer of (width - 1) by (height - 1) PIXEL structures, what you probably want here is:

pixeldata = new PIXEL[(width - 1) * (height - 1)];

,然后为什么减一?不应该

and then, why minus one? Shouldn''t it be

pixeldata = new PIXEL[width * height]

?


如果您可以自由使用第三方库,我建议您看看例如openCV或ITK,因为它们与C ++高度兼容,并且大大简化了图像的工作.我非常确定,还有更多的开源库可以提供您正在寻找的功能.
If you are free to use third party libraries i would suggest you to have a look into e.g. openCV or ITK since they are highly compatible with C++ and ease the work with images a lot. Im pretty sure that there are even more open source libraries out there which provide the functionality you''re looking for.


这篇关于读取和编辑位图文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆