📄 ppmpgm.cpp
字号:
// PpmPgm.cpp: implementation of the PpmPgm class.
//
//////////////////////////////////////////////////////////////////////
//#include "stdafx.h"
//#include "p1.h"
#include "PpmPgm.h"
#include <ctype.h>
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
PpmPgm::PpmPgm()
{
pixel = red = green = blue = gray = NULL;
width = height = 0;
max_value = 255;
type = PPMRAW;
}
PpmPgm::PpmPgm(PpmPgm &p2)
{
pixel = red = green = blue = gray = NULL;
*this = p2;
}
PpmPgm::PpmPgm(int w, int h, FILE_TYPE t)
{
pixel = red = green = blue = gray = NULL;
width = w;
height = h;
type = t;
max_value = 255;
if ((type == PPMRAW) || (type == PPMASCII))
{
red = new unsigned int *[height];
green = new unsigned int *[height];
blue = new unsigned int *[height];
pixel = new unsigned int *[height];
for (int i=0; i<height; i++)
{
red[i] = new unsigned int[width];
green[i] = new unsigned int[width];
blue[i] = new unsigned int[width];
pixel[i] = new unsigned int[width];
}
}
else
{
gray = new unsigned int *[height];
pixel = new unsigned int *[height];
for (int i=0; i<height; i++)
{
gray[i] = new unsigned int[width];
pixel[i] = new unsigned int[width];
}
}
Fill(0);
}
PpmPgm::~PpmPgm()
{
ClearData();
}
bool PpmPgm::Read(CFile &fin)
{
char buffer[100];
int i;
unsigned char *line;
// Read the magic number
fin.Read(buffer, 2);
if (buffer[0] != 'P')
return false;
switch (buffer[1])
{
case '2':
type = PGMASCII;
break;
case '3':
type = PPMASCII;
break;
case '5':
type = PGMRAW;
break;
case '6':
type = PPMRAW;
break;
default:
return false;
}
// Read the width
fin.Read(buffer, 1);
do
{
if (buffer[0] == '#')
buffer[0] = SkipComment(fin);
while (isspace(buffer[0]))
fin.Read(buffer, 1);
} while (buffer[0] == '#');
char *temp = buffer;
while (!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
width = atoi(buffer);
// Read the height
buffer[0] = *temp;
do
{
if (buffer[0] == '#')
buffer[0] = SkipComment(fin);
while (isspace(buffer[0]))
fin.Read(buffer, 1);
} while (buffer[0] == '#');
temp = buffer;
while(!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
height = atoi(buffer);
// Read the maximum value
buffer[0] = *temp;
do
{
if (buffer[0] == '#')
buffer[0] = SkipComment(fin);
while (isspace(buffer[0]))
fin.Read(buffer, 1);
} while (buffer[0] == '#');
temp = buffer;
while(!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
max_value = atoi(buffer);
// Allocate memory for the pixel values
pixel = new unsigned int *[height];
for (i=0; i<height; i++)
pixel[i] = new unsigned int[width];
if ((type == PPMASCII) || (type == PPMRAW))
{
red = new unsigned int *[height];
green = new unsigned int *[height];
blue = new unsigned int *[height];
for (i=0; i<height; i++)
{
red[i] = new unsigned int[width];
green[i] = new unsigned int[width];
blue[i] = new unsigned int[width];
}
}
else
{
gray = new unsigned int *[height];
for (i=0; i<height; i++)
gray[i] = new unsigned int[width];
}
// Read in the pixel values
switch (type)
{
case PPMASCII:
for (i=0; i<height; i++)
{
for (int j=0; j<width; j++)
{
// Read red value
buffer[0] = *temp;
while (isspace(buffer[0]))
fin.Read(buffer, 1);
temp = buffer;
while (!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
red[i][j] = atoi(buffer);
// Read green value
buffer[0] = *temp;
while (isspace(buffer[0]))
fin.Read(buffer, 1);
temp = buffer;
while (!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
green[i][j] = atoi(buffer);
// Read blue value
buffer[0] = *temp;
while (isspace(buffer[0]))
fin.Read(buffer, 1);
temp = buffer;
while (!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
blue[i][j] = atoi(buffer);
// set the pixel value
pixel[i][j] =
((unsigned int)(((double)blue[i][j]/(double)max_value)*255.0)<<16) +
((unsigned int)(((double)green[i][j]/(double)max_value)*255.0)<<8) +
((unsigned int)(((double)red[i][j]/(double)max_value)*255.0));
}
}
break;
case PPMRAW:
// allocate memory for a complete scan line
line = new unsigned char[width*3];
for (i=0; i<height; i++)
{
// read in a horizontal line of the image
fin.Read(line, width*3);
// pick out the r,g,b values from the line
for (int j=0; j<width; j++)
{
red[i][j] = (unsigned int)line[j*3];
green[i][j] = (unsigned int)line[j*3+1];
blue[i][j] = (unsigned int)line[j*3+2];
pixel[i][j] =
((unsigned int)line[j*3+2]<<16) +
((unsigned int)line[j*3+1]<<8) +
((unsigned int)line[j*3]);
}
}
delete line;
break;
case PGMASCII:
for (i=0; i<height; i++)
{
for (int j=0; j<width; j++)
{
// read the gray value
buffer[0] = *temp;
while (isspace(buffer[0]))
fin.Read(buffer, 1);
temp = buffer;
while (!isspace(*temp))
{
temp++;
fin.Read(temp, 1);
}
gray[i][j] = atoi(buffer);
// set the pixel value
pixel[i][j] =
((unsigned int)(((double)gray[i][j]/(double)max_value)*255.0)<<16) +
((unsigned int)(((double)gray[i][j]/(double)max_value)*255.0)<<8) +
((unsigned int)(((double)gray[i][j]/(double)max_value)*255.0));
}
}
break;
case PGMRAW:
// allocate memory for a complete scan line
line = new unsigned char[width*3];
for (i=0; i<height; i++)
{
// read in a horizontal line of the image
fin.Read(line, width);
// pick out the gray values for each pixel along the line
for (int j=0; j<width; j++)
{
gray[i][j] = (unsigned int)line[j];
pixel[i][j] = (line[j]<<16) + (line[j]<<8) + (line[j]);
}
}
delete line;
break;
}
return true;
}
bool PpmPgm::Write(CFile &fout, FILE_TYPE t)
{
char buffer[100];
int i;
unsigned char *line;
// convert the image to type t
switch (type)
{
case PPMASCII:
switch(t)
{
case PPMASCII:
break;
case PPMRAW:
type = PPMRAW;
break;
case PGMASCII:
Convert_To_Gray();
type = PGMASCII;
break;
case PGMRAW:
Convert_To_Gray();
type = PGMRAW;
break;
}
break;
case PPMRAW:
switch(t)
{
case PPMASCII:
type = PPMASCII;
break;
case PPMRAW:
break;
case PGMASCII:
Convert_To_Gray();
type = PGMASCII;
break;
case PGMRAW:
Convert_To_Gray();
type = PGMRAW;
break;
}
break;
case PGMASCII:
switch(t)
{
case PPMASCII:
Convert_To_Color();
type = PPMASCII;
break;
case PPMRAW:
Convert_To_Color();
type = PPMRAW;
break;
case PGMASCII:
break;
case PGMRAW:
type = PGMRAW;
break;
}
break;
case PGMRAW:
switch(t)
{
case PPMASCII:
Convert_To_Color();
type = PPMASCII;
break;
case PPMRAW:
Convert_To_Color();
type = PPMRAW;
break;
case PGMASCII:
type = PGMASCII;
break;
case PGMRAW:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -