Created
August 11, 2016 02:05
-
-
Save karl-zylinski/c7422cc435c6687ae35db88aa38f559b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <assert.h> | |
#include <string.h> | |
struct Color | |
{ | |
unsigned char r, g, b; | |
}; | |
int main() | |
{ | |
const unsigned num_pixels = 64 * 64; | |
Color colors[num_pixels]; | |
memset(colors, 0, num_pixels * sizeof(Color)); | |
for (unsigned i = 0; i < num_pixels; ++i) | |
{ | |
colors[i].r = rand()%255; | |
} | |
FILE* write_handle = fopen("red_noise.data", "w"); | |
fwrite(colors, 1, num_pixels * sizeof(Color), write_handle); | |
fclose(write_handle); | |
FILE* read_handle = fopen("red_noise.data", "r"); | |
fseek(read_handle, 0, SEEK_END); | |
size_t filesize = ftell(read_handle); | |
fseek(read_handle, 0, SEEK_SET); | |
Color* read_colors = (Color*)malloc(filesize); | |
fread(read_colors, 1, filesize, read_handle); | |
fclose(read_handle); | |
for (unsigned i = 0; i < num_pixels; ++i) | |
{ | |
if(read_colors[i].r != colors[i].r | |
|| read_colors[i].g != colors[i].g | |
|| read_colors[i].b != colors[i].b) | |
{ | |
printf("At pixel %u: Expected [%u %u %u] Got [%u %u %u]", i, | |
colors[i].r, colors[i].g, colors[i].b, | |
read_colors[i].r, read_colors[i].g, read_colors[i].b); | |
return 1; | |
} | |
} | |
free(read_colors); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment