Ledger Nano X - The secure hardware wallet
SHARE
TWEET

Untitled

a guest Mar 30th, 2020 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. #include <assert.h>
  3. #include "image2.hpp"
  4. #include <stdint.h> // for uint8_t
  5. #include <cstddef>
  6. #include <stdio.h>
  7.  
  8. /* Constructs an image of 0x0 pixels. */
  9. Image::Image(): cols(0), rows(0), pixels(NULL)
  10. {
  11.  
  12. }
  13. /* Frees all memory allocated for img */
  14. Image::~Image()
  15. {
  16.     printf("inside destructor\n");
  17.     if (pixels != NULL)
  18.     {
  19.         printf("Inside if statement in destructor\n");
  20.         delete[] pixels;
  21.         printf("past delete in destructor\n");
  22.     }
  23. }
  24.  
  25.  
  26. /* Changes the size of an image, allocating memory as necessary, and
  27.     setting all pixels to fillcolour. Returns 0 on success, or a non-zero error code.*/
  28. int Image::resize( unsigned int width,  unsigned int height, uint8_t fillcolour )
  29. {
  30.     // printf("Inside image resize method\n");
  31.     if (pixels != NULL)
  32.     {
  33.         delete [] pixels;
  34.     }
  35.  
  36.     pixels = new uint8_t[width * height];
  37.     if (pixels == NULL)
  38.         return 1;
  39.     // printf("Memory succesfully allocated\n");
  40.  
  41.     cols = width;
  42.     rows = height;
  43.  
  44.     for (unsigned int i = 0; i < cols * rows; i++)
  45.     {
  46.         // printf("For loop, i = %d\n", i);
  47.         pixels[i] = fillcolour;
  48.     }
  49.     return 0;
  50.  
  51. }
  52.  
  53. /* Sets the colour of the pixel at (x,y) to colour. Returns 0 on success, else a non-zero
  54.     error code. If (x,y) is not a valid pixel, the call fails and the image does not change.*/
  55. int Image::set_pixel( unsigned int x, unsigned int y, uint8_t colour )
  56. {
  57.     if (x >= cols)
  58.         return 1;
  59.     if (y >= rows)
  60.         return 2;
  61.     if (pixels == NULL)
  62.         return 3;
  63.     // printf("Inside set_pixel, x = %d, y = %d\n", x, y);
  64.     pixels[(cols * y) + x] = colour;
  65.     printf("inside set pixel: colour =  %d\n", colour);
  66.     printf("inside set pixel: pixels[%d] = %d\n", (cols * y + x), pixels[(cols * y) + x]);
  67.     return 0;
  68. }
  69.  
  70.  
  71. /* Gets the colour of the pixel at (x,y) and stores at the address pointed to
  72. by colourp. Returns 0 on success, else a non-zero error code. */
  73. int Image::get_pixel( unsigned int x, unsigned int y, uint8_t* colourp )
  74. {
  75.     if (x >= cols)
  76.         return 1;
  77.     if (y >= rows)
  78.         return 2;
  79.     if (pixels == NULL)
  80.         return 3;
  81.     if (colourp == NULL)
  82.         return 4;
  83.     *colourp = pixels[cols * y + x];
  84.     return 0;
  85. }
  86.  
  87.  
  88. /* Saves the image in the file filename. In a format that can be
  89.     loaded by load(). Returns 0 on success, else a non-zero error
  90.     code. */
  91. int Image::save( const char* filename )
  92. {
  93.     if (pixels == NULL)
  94.         return 1;
  95.     if (filename == NULL)
  96.         return 2;
  97.  
  98.     FILE *f = fopen(filename, "w");
  99.  
  100.     if (f == NULL)
  101.     {
  102.         return 3;
  103.     }
  104.  
  105.     if (fwrite(&cols, sizeof(unsigned int), 1, f) != 1)
  106.     {
  107.         return 4;
  108.     }
  109.     if (fwrite(&rows, sizeof(unsigned int), 1, f) != 1)
  110.     {
  111.         return 5;
  112.     }
  113.     fwrite(pixels, sizeof(uint8_t), cols * rows, f);
  114.     return 0;
  115. }
  116.  
  117. /* Load the an image from the file filename, replacing the current
  118.     image size and data. The file is in a format that was saved by
  119.     save(). Returns 0 success, else a non-zero error code . */
  120. int Image::load( const char* filename )
  121. {
  122.     if (pixels == NULL)
  123.         return 1;
  124.     if (filename == NULL)
  125.         return 2;
  126.  
  127.     FILE *f = fopen(filename, "r");
  128.  
  129.     if (f == NULL)
  130.     {
  131.         return 3;
  132.     }
  133.     if (fread(&cols, sizeof(unsigned int), 1, f) != 1)
  134.     {  
  135.         return 4;
  136.     }
  137.     if (fread(&rows, sizeof(unsigned int), 1, f) != 1)
  138.     {
  139.         return 5;
  140.     }
  141.     printf("Inside load, past first two reads\n");
  142.     printf("Inside load, cols = %d\n", cols);
  143.     printf("Inside load, rows = %d\n", rows);
  144.     if (resize(cols, rows, 0) != 0)
  145.     {
  146.         printf("Error in resize in load\n");
  147.     }
  148.     fread(pixels, sizeof(uint8_t), cols * rows, f);
  149.     fclose(f);
  150.     return 0;
  151. }
  152.  
  153.  
  154. /*
  155. * testDriver.c
  156. * Adam Dee
  157. * CMPT 127 Lab 10
  158. */
  159.  
  160. #include <assert.h>
  161. #include <stdint.h>
  162. #include <stdio.h>
  163. #include <stdlib.h>
  164. #include "image2.hpp"
  165.  
  166. int main ()
  167. {
  168.     // const int j = 5;
  169.     Image img;
  170.     uint8_t temp;
  171.     if( img.resize( 640, 480, 0 ) != 0 )
  172.     {
  173.         exit(1); // quit
  174.     }
  175.     printf("From main, past resize\n");
  176.     for( int i=0; i<100; i++ )
  177.     {
  178.         int randx = rand() % 640;
  179.         int randy = rand() % 480;
  180.         int randc = rand() % 256;
  181.         img.set_pixel(randx, randy, randc);
  182.         img.get_pixel(randx, randy, &temp);
  183.         printf("set pixel[%d] to %d\n", (640 * randy + randx), temp);
  184.     }  
  185.     printf("From main, past set pixel\n");
  186.     printf("img.cols = %d\n", img.cols);
  187.     printf("img.rows = %d\n", img.rows);
  188.     for (unsigned int x = 0; x < img.cols; x++)
  189.     {
  190.         for (unsigned int y = 0; y < img.rows; y++)
  191.         {
  192.             img.get_pixel(x, y, &temp);
  193.             // printf("get pixel: [%d] = %d\n", (640 * y + x), temp);
  194.  
  195.         }
  196.     }
  197.     img.save("whatever.bin");
  198.     printf("SAVED\n");
  199.     printf("Loading now\n");
  200.     img.load("whatever.bin");
  201.     printf("past load\n");
  202.     // for (unsigned int i = 0; i < img.cols * img.rows; i++)
  203.     // {
  204.     //  // printf("img.pixels[%d] = %d\n", i, img.pixels[i]);
  205.     // }
  206.  
  207.     delete &img;
  208.     printf("past delete in main\n");
  209. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top