deallocate();
}
-void RGBImage::write_ppm(const char *filename) {
- FILE *outfile;
-
- if ((outfile = fopen (filename, "wb")) == 0) {
- fprintf (stderr, "Can't open %s for reading\n", filename);
- exit(1);
- }
-
- fprintf(outfile, "P6\n%d %d\n255\n", _width, _height);
-
- char *raw = new char[_width * _height * 3];
-
- int k = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- raw[k++] = _bit_map[x + _width * (y + _height * RED)];
- raw[k++] = _bit_map[x + _width * (y + _height * GREEN)];
- raw[k++] = _bit_map[x + _width * (y + _height * BLUE)];
- }
-
- fwrite((void *) raw, sizeof(unsigned char), _width * _height * 3, outfile);
- fclose(outfile);
-
- delete[] raw;
-}
-
-void RGBImage::read_ppm(const char *filename) {
- const int buffer_size = 1024;
- FILE *infile;
- char buffer[buffer_size];
- int max;
-
- deallocate();
-
- if((infile = fopen (filename, "r")) == 0) {
- fprintf (stderr, "Can't open %s for reading\n", filename);
- exit(1);
- }
-
- fgets(buffer, buffer_size, infile);
-
- if(strncmp(buffer, "P6", 2) == 0) {
-
- do {
- fgets(buffer, buffer_size, infile);
- } while((buffer[0] < '0') || (buffer[0] > '9'));
- sscanf(buffer, "%d %d", &_width, &_height);
- fgets(buffer, buffer_size, infile);
- sscanf(buffer, "%d", &max);
-
- allocate();
-
- unsigned char *raw = new unsigned char[_width * _height * RGB_DEPTH];
- fread(raw, sizeof(unsigned char), _width * _height * RGB_DEPTH, infile);
-
- int k = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- _bit_plans[RED][y][x] = raw[k++];
- _bit_plans[GREEN][y][x] = raw[k++];
- _bit_plans[BLUE][y][x] = raw[k++];
- }
-
- delete[] raw;
-
- } else if(strncmp(buffer, "P5", 2) == 0) {
-
- do {
- fgets(buffer, buffer_size, infile);
- } while((buffer[0] < '0') || (buffer[0] > '9'));
- sscanf(buffer, "%d %d", &_width, &_height);
- fgets(buffer, buffer_size, infile);
- sscanf(buffer, "%d", &max);
-
- allocate();
-
- unsigned char *pixbuf = new unsigned char[_width * _height];
- fread(buffer, sizeof(unsigned char), _width * _height, infile);
-
- int k = 0, l = 0;
- for(int y = 0; y < _height; y++) for(int x = 0; x < _width; x++) {
- unsigned char c = pixbuf[k++];
- _bit_map[l++] = c;
- _bit_map[l++] = c;
- _bit_map[l++] = c;
- }
-
- delete[] pixbuf;
-
- } else {
- cerr << "Can not read ppm of type [" << buffer << "] from " << filename << ".\n";
- exit(1);
- }
-}
-
void RGBImage::read_png(const char *name) {
// This is the number of bytes the read_png routine will read to
// decide if the file is a PNG or not. According to the png