Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <assert.h>
- #include "image2.hpp"
- #include <stdint.h> // for uint8_t
- #include <cstddef>
- #include <stdio.h>
- /* Constructs an image of 0x0 pixels. */
- Image::Image(): cols(0), rows(0), pixels(NULL)
- {
- }
- /* Frees all memory allocated for img */
- Image::~Image()
- {
- printf("inside destructor\n");
- if (pixels != NULL)
- {
- printf("Inside if statement in destructor\n");
- delete[] pixels;
- printf("past delete in destructor\n");
- }
- }
- /* Changes the size of an image, allocating memory as necessary, and
- setting all pixels to fillcolour. Returns 0 on success, or a non-zero error code.*/
- int Image::resize( unsigned int width, unsigned int height, uint8_t fillcolour )
- {
- // printf("Inside image resize method\n");
- if (pixels != NULL)
- {
- delete [] pixels;
- }
- pixels = new uint8_t[width * height];
- if (pixels == NULL)
- return 1;
- // printf("Memory succesfully allocated\n");
- cols = width;
- rows = height;
- for (unsigned int i = 0; i < cols * rows; i++)
- {
- // printf("For loop, i = %d\n", i);
- pixels[i] = fillcolour;
- }
- return 0;
- }
- /* Sets the colour of the pixel at (x,y) to colour. Returns 0 on success, else a non-zero
- error code. If (x,y) is not a valid pixel, the call fails and the image does not change.*/
- int Image::set_pixel( unsigned int x, unsigned int y, uint8_t colour )
- {
- if (x >= cols)
- return 1;
- if (y >= rows)
- return 2;
- if (pixels == NULL)
- return 3;
- // printf("Inside set_pixel, x = %d, y = %d\n", x, y);
- pixels[(cols * y) + x] = colour;
- printf("inside set pixel: colour = %d\n", colour);
- printf("inside set pixel: pixels[%d] = %d\n", (cols * y + x), pixels[(cols * y) + x]);
- return 0;
- }
- /* Gets the colour of the pixel at (x,y) and stores at the address pointed to
- by colourp. Returns 0 on success, else a non-zero error code. */
- int Image::get_pixel( unsigned int x, unsigned int y, uint8_t* colourp )
- {
- if (x >= cols)
- return 1;
- if (y >= rows)
- return 2;
- if (pixels == NULL)
- return 3;
- if (colourp == NULL)
- return 4;
- *colourp = pixels[cols * y + x];
- return 0;
- }
- /* Saves the image in the file filename. In a format that can be
- loaded by load(). Returns 0 on success, else a non-zero error
- code. */
- int Image::save( const char* filename )
- {
- if (pixels == NULL)
- return 1;
- if (filename == NULL)
- return 2;
- FILE *f = fopen(filename, "w");
- if (f == NULL)
- {
- return 3;
- }
- if (fwrite(&cols, sizeof(unsigned int), 1, f) != 1)
- {
- return 4;
- }
- if (fwrite(&rows, sizeof(unsigned int), 1, f) != 1)
- {
- return 5;
- }
- fwrite(pixels, sizeof(uint8_t), cols * rows, f);
- return 0;
- }
- /* Load the an image from the file filename, replacing the current
- image size and data. The file is in a format that was saved by
- save(). Returns 0 success, else a non-zero error code . */
- int Image::load( const char* filename )
- {
- if (pixels == NULL)
- return 1;
- if (filename == NULL)
- return 2;
- FILE *f = fopen(filename, "r");
- if (f == NULL)
- {
- return 3;
- }
- if (fread(&cols, sizeof(unsigned int), 1, f) != 1)
- {
- return 4;
- }
- if (fread(&rows, sizeof(unsigned int), 1, f) != 1)
- {
- return 5;
- }
- printf("Inside load, past first two reads\n");
- printf("Inside load, cols = %d\n", cols);
- printf("Inside load, rows = %d\n", rows);
- if (resize(cols, rows, 0) != 0)
- {
- printf("Error in resize in load\n");
- }
- fread(pixels, sizeof(uint8_t), cols * rows, f);
- fclose(f);
- return 0;
- }
- /*
- * testDriver.c
- * Adam Dee
- * CMPT 127 Lab 10
- */
- #include <assert.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include "image2.hpp"
- int main ()
- {
- // const int j = 5;
- Image img;
- uint8_t temp;
- if( img.resize( 640, 480, 0 ) != 0 )
- {
- exit(1); // quit
- }
- printf("From main, past resize\n");
- for( int i=0; i<100; i++ )
- {
- int randx = rand() % 640;
- int randy = rand() % 480;
- int randc = rand() % 256;
- img.set_pixel(randx, randy, randc);
- img.get_pixel(randx, randy, &temp);
- printf("set pixel[%d] to %d\n", (640 * randy + randx), temp);
- }
- printf("From main, past set pixel\n");
- printf("img.cols = %d\n", img.cols);
- printf("img.rows = %d\n", img.rows);
- for (unsigned int x = 0; x < img.cols; x++)
- {
- for (unsigned int y = 0; y < img.rows; y++)
- {
- img.get_pixel(x, y, &temp);
- // printf("get pixel: [%d] = %d\n", (640 * y + x), temp);
- }
- }
- img.save("whatever.bin");
- printf("SAVED\n");
- printf("Loading now\n");
- img.load("whatever.bin");
- printf("past load\n");
- // for (unsigned int i = 0; i < img.cols * img.rows; i++)
- // {
- // // printf("img.pixels[%d] = %d\n", i, img.pixels[i]);
- // }
- delete &img;
- printf("past delete in main\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement