-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgV.c
More file actions
117 lines (105 loc) · 2.93 KB
/
Copy pathimgV.c
File metadata and controls
117 lines (105 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <SDL2/SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
typedef struct {
int16_t maxVal;
int16_t height;
int16_t width;
char header[3];
char *body;
} ppmImg;
size_t get_file_size(const char *filename) {
struct stat st;
if (stat(filename, &st) != 0) {
return 0; // Error
}
return st.st_size;
}
int nxt_pow2(int n) {
int power = 1;
while (power < n) {
power *= 2;
}
return power;
}
void loadImg(char *filepath, ppmImg *accessedImg) {
FILE *fptr = fopen(filepath, "rb");
size_t filesize = get_file_size(filepath);
char *buff = malloc(filesize);
size_t bytes_read = fread(buff, 1, filesize, fptr);
if (bytes_read != filesize) {
fprintf(stderr, "Error reading the complete file");
}
strncpy(accessedImg->header, buff, 2);
accessedImg->header[2] = '\0';
char *tempint = malloc(5);
strncpy(tempint, buff + 3, 4);
accessedImg->width = atoi(tempint);
strncpy(tempint, buff + 7, 4);
accessedImg->height = atoi(tempint);
strncpy(tempint, buff + 11, 4);
accessedImg->maxVal = atoi(tempint);
free(tempint);
size_t pixel_data_size = filesize - 15;
accessedImg->body = malloc(pixel_data_size);
memcpy(accessedImg->body, buff + 15, pixel_data_size);
free(buff);
int rec = fclose(fptr);
if (rec != 0) {
fprintf(stderr, "Error closing the file");
} else {
printf("File closed successfully\n");
}
}
int main(int argc, char *argv[]) {
ppmImg loadedImg;
char *filepath = "images/images.ppm";
if (argc > 1) {
filepath = argv[1];
}
loadImg(filepath, &loadedImg);
int32_t width = loadedImg.width;
int32_t height = loadedImg.height;
int32_t delay_time;
if(argc >= 3){
delay_time = atoi(argv[2])*1000; // temp creation
}else{ delay_time = 10000;}
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *pwindow =
SDL_CreateWindow("Image Viewer", SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, width, height, 0);
SDL_Surface *psurf = SDL_GetWindowSurface(pwindow);
// Init Pixel
int x, y;
SDL_Rect pixel =
(SDL_Rect){0, 0, 1, 1}; // x and y are specified and width and height is
// specified as 1 pixel each
for (x = 0; x < width; x++) {
for (y = 0; y < height; y++) {
int idx = (y * loadedImg.width + x) * 3;
uint8_t r = loadedImg.body[idx];
uint8_t g = loadedImg.body[idx + 1];
uint8_t b = loadedImg.body[idx + 2];
uint32_t color = SDL_MapRGB(psurf->format, r, g, b);
pixel.x = x;
pixel.y = y;
SDL_FillRect(psurf, &pixel, color);
}
}
SDL_UpdateWindowSurface(pwindow);
uint32_t start = SDL_GetTicks();
SDL_Event e;
while (SDL_GetTicks() - start < delay_time) {
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) {
SDL_DestroyWindow(pwindow);
SDL_Quit();
free(loadedImg.body);
return 0;
}
}
}
}