G
George
I cannot understand how to fix my code so that it will work. It needs
to draw a series of lines in different colors and save it to a ppm
file. Could someone tell me what I have done wrong. Thanks a lot.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct Pixel {
unsigned char R, G, B; // Red, Green, Blue
};
class ColorImage {
Pixel *pPixel;
int xRes, yRes;
public:
ColorImage();
~ColorImage();
void init(int xSize, int ySize);
void clear(Pixel background);
Pixel readPixel(int x, int y);
void writePixel(int x, int y, Pixel p);
void outputPPM(char *filename);
void DrawLine(int x1, int y1, int x2, int y2, Pixel p);
bool Inside(int x0, int x1, int x2, int x3, int y0, int y1, int y2,
int y3);
void DrawTriangle(int x1, int x2, int x3, int y1, int y2, int y3,
Pixel p);
};
ColorImage::ColorImage()
{
pPixel = 0;
}
ColorImage::~ColorImage()
{
if (pPixel) delete[] pPixel;
pPixel = 0;
}
void ColorImage::init(int xSize, int ySize)
{
Pixel p = {0,0,0};
xRes = xSize;
yRes = ySize;
pPixel = new Pixel[xSize*ySize];
clear(p);
}
void ColorImage::clear(Pixel background)
{
int i;
if (! pPixel) return;
for (i=0; i<xRes*yRes; i++) pPixel = background;
}
Pixel ColorImage::readPixel(int x, int y)
{
assert(pPixel); // die if image not initialized
return pPixel[x + y*yRes];
}
void ColorImage::writePixel(int x, int y, Pixel p)
{
assert(pPixel); // die if image not initialized
pPixel[x + y*yRes] = p;
}
void ColorImage:utputPPM(char *filename)
{
FILE *outFile = fopen(filename, "wb");
assert(outFile); // die if file can't be opened
fprintf(outFile, "P3 %d %d 255\n", xRes, yRes);
fwrite(pPixel, 1, 3*xRes*yRes, outFile );
fclose(outFile);
}
void ColorImage:rawLine(int x1, int y1, int x2, int y2, Pixel p)
{
int dx, dy, d, incE, incNE, x, y;
dx = x2 - x1;
dy = y2 - y1;
d = 2*dy - dx;
incE = 2*dy;
incNE = 2*(dy - dx);
y = y1;
for (x=x1; x<=x2; x++) {
writePixel(x, y, p);
if (d>0) {
d = d + incNE;
y = y + 1;
} else {
d = d + incE;
}
}
}
// A test program that generates varying shades of reds.
int main(int argc, char* argv[])
{
ColorImage image;
Pixel p={0,0,0};
Pixel red={1,0,0};
Pixel blue={0,0,1};
Pixel green={0,1,0};
Pixel white={1,1,1};
Pixel yellow={1,1,0};
Pixel purple={1,0,1};
image.init(512, 512);
image.DrawLine(299,300,299,400,white);
image.outputPPM("output.ppm");
return 0;
}
to draw a series of lines in different colors and save it to a ppm
file. Could someone tell me what I have done wrong. Thanks a lot.
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
struct Pixel {
unsigned char R, G, B; // Red, Green, Blue
};
class ColorImage {
Pixel *pPixel;
int xRes, yRes;
public:
ColorImage();
~ColorImage();
void init(int xSize, int ySize);
void clear(Pixel background);
Pixel readPixel(int x, int y);
void writePixel(int x, int y, Pixel p);
void outputPPM(char *filename);
void DrawLine(int x1, int y1, int x2, int y2, Pixel p);
bool Inside(int x0, int x1, int x2, int x3, int y0, int y1, int y2,
int y3);
void DrawTriangle(int x1, int x2, int x3, int y1, int y2, int y3,
Pixel p);
};
ColorImage::ColorImage()
{
pPixel = 0;
}
ColorImage::~ColorImage()
{
if (pPixel) delete[] pPixel;
pPixel = 0;
}
void ColorImage::init(int xSize, int ySize)
{
Pixel p = {0,0,0};
xRes = xSize;
yRes = ySize;
pPixel = new Pixel[xSize*ySize];
clear(p);
}
void ColorImage::clear(Pixel background)
{
int i;
if (! pPixel) return;
for (i=0; i<xRes*yRes; i++) pPixel = background;
}
Pixel ColorImage::readPixel(int x, int y)
{
assert(pPixel); // die if image not initialized
return pPixel[x + y*yRes];
}
void ColorImage::writePixel(int x, int y, Pixel p)
{
assert(pPixel); // die if image not initialized
pPixel[x + y*yRes] = p;
}
void ColorImage:utputPPM(char *filename)
{
FILE *outFile = fopen(filename, "wb");
assert(outFile); // die if file can't be opened
fprintf(outFile, "P3 %d %d 255\n", xRes, yRes);
fwrite(pPixel, 1, 3*xRes*yRes, outFile );
fclose(outFile);
}
void ColorImage:rawLine(int x1, int y1, int x2, int y2, Pixel p)
{
int dx, dy, d, incE, incNE, x, y;
dx = x2 - x1;
dy = y2 - y1;
d = 2*dy - dx;
incE = 2*dy;
incNE = 2*(dy - dx);
y = y1;
for (x=x1; x<=x2; x++) {
writePixel(x, y, p);
if (d>0) {
d = d + incNE;
y = y + 1;
} else {
d = d + incE;
}
}
}
// A test program that generates varying shades of reds.
int main(int argc, char* argv[])
{
ColorImage image;
Pixel p={0,0,0};
Pixel red={1,0,0};
Pixel blue={0,0,1};
Pixel green={0,1,0};
Pixel white={1,1,1};
Pixel yellow={1,1,0};
Pixel purple={1,0,1};
image.init(512, 512);
image.DrawLine(299,300,299,400,white);
image.outputPPM("output.ppm");
return 0;
}