java读取ppm图片_用C读取PPM文件(只有P6格式没有注释)并另存为图像

我有一个练习,我需要读取PPM文件并将其内容保存为C中的图像 . 然后我想计算平均亮度 . 我有3个头文件 . 我设法读取文件并检查P6值,宽度,高度和深度 . 如何从文件中提取图像数据以制作图像对象以计算平均亮度?

Color.h

namespace imaging

{

/*! An alias for the floating point representation of color components (32bit per color channel).

*/

typedef float component_t;

class Color

{

public:

// members

component_t r,

g,

b;

// member functions

component_t & operator [] (size_t index)

{

return *(&r + index);

}

Color operator + (Color & right)

{

Color left;

left.r = r + right.r;

left.g = g + right.g;

left.b = b + right.b;

return left;

}

// constructors

Color(component_t r, component_t g, component_t b) : r(r), g(g), b(b) {}

Color() : r(0), g(0), b(0) {}

};

image.h的

namespace imaging {

class Image

{

public:

enum channel_t {RED=0,GREEN, BLUE};

protected:

component_t * buffer;

unsigned int width,

height;

public:

// metric accessors

const unsigned int getWidth() const {return width;}

const unsigned int getHeight() const {return height;}

// data accessors

component_t * getRawDataPtr();

Color getPixel(unsigned int x, unsigned int y) const;

// data mutators

void setPixel(unsigned int x, unsigned int y, Color & value);

void setData(const component_t * & data_ptr);

void resize(unsigned int new_width, unsigned int new_height);

// constructors and destructor

/*! Default constructor.

*/

Image();

/*! Constructor with width and height specification.

*

*/

Image(unsigned int width, unsigned int height);

// Constructor with data initialization

Image(unsigned int width, unsigned int height, const component_t * data_ptr);

/*! Copy constructor.

*/

Image(const Image &src);

/*! The Image destructor.

*/

~Image();

/*! Copy assignment operator.*/

Image & operator = (const Image & right);

};

}

ppm_format.h

namespace imaging

{

/*! Reads a PPM image file and returns a pointer to a newly allocated Image object containing the image.

*/

Image * ReadPPM(const char * filename);

} //namespace imaging

我为读取PPM文件方法编写了这段代码:

Image * ReadPPM(const char* filename) {

ifstream ifs;

string v1; //for the P6 value

int v2,v3,v4; //for width, height and color depth

cin.getline(filename, 20);

ifs.open(filename,ios::binary);

if (ifs.is_open()) {

while (ifs.good()){

if (ifs>>v1>>v2>>v3>>v4){

int height = v2;

int width = v3;

if (v1!="P6") {

cout<

ifs.close();

exit(0);

}

if (v2<1){

cout<

ifs.close();

exit(0);

}

if (v3<1){

cout<

ifs.close();

exit(0);

}

if (v4<1 || v4>255){

cout<

ifs.close();

exit(0);

}

//cout<

}

}

}

else if (!ifs.is_open()) {

cout<

exit(EXIT_FAILURE);

}

ifs.close();

// end of reading the file

//start making the image

Image image = new Image(v2,v3);

return image;

}

我写了这段代码来读取和保存图像数据 . 问题是我想将图像数据提取为component_t类型(浮点数) .

char* data = new char[3*height*width]; int bytesPerLn = width*3; for (int i=0;i

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值