-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscreen.c
148 lines (113 loc) · 4.04 KB
/
screen.c
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* File name: prgsem.c
* Date: 2022/05/7 6:03 PM
* Author: Aleksandr Zelik
* Tusk text: https://cw.fel.cvut.cz/wiki/courses/b3b36prg/semestral-project/start
*/
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <stdbool.h>
#include <string.h>
#include <png.h>
#include "screen.h"
#include "xwin_sdl.h"
void __SavePNGImage__ (Image *img) {
FILE * fd = fopen("fracktal_screen.png","wb");
if (!fd) exit(1);
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
size_t x, y;
png_byte ** row_pointers = NULL;
int depth = 8;
png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) goto png_create_write_struct_failed;
info_ptr = png_create_info_struct (png_ptr);
if (info_ptr == NULL) goto png_create_info_struct_failed;
if (setjmp (png_jmpbuf (png_ptr)))goto png_failure;
//image attributes
png_set_IHDR (png_ptr,
info_ptr,
img->width,
img->height,
depth,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
// Initialize rows of PNG.
row_pointers = png_malloc (png_ptr, img->height * sizeof (png_byte *));
for (y = 0; y < img->height; y++) {
png_byte *row = png_malloc (png_ptr, img->width * PIXEL_SIZE);
row_pointers[y] = row;
for (x = 0; x < img->width; x++) {
Pixel pixel = img->pixels[img->width *y +x];
*row++ = pixel.R;
*row++ = pixel.G;
*row++ = pixel.B;
}
}
// Write the image data to "fp".
png_init_io (png_ptr, fd);
png_set_rows (png_ptr, info_ptr, row_pointers);
png_write_png (png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
// The routine has successfully written the file, so we set "status" to a value which indicates success.
for (y = 0; y < img->height; y++) {
png_free (png_ptr, row_pointers[y]);
}
png_free (png_ptr, row_pointers);
png_failure:
png_create_info_struct_failed:
png_destroy_write_struct (&png_ptr, &info_ptr);
png_create_write_struct_failed:
fclose (fd);
}
Image* __InitImage__(int width, int height, int chunk_size) {
Pixel* pixels_of_img = (Pixel*) malloc(width * height * PIXEL_SIZE);
Image* img = (Image*) malloc(IMAGE_SIZE);
// calculate how many chunks we will need in each row
if (width % chunk_size == 0) {
img->chunks_in_row = (uint8_t)(width / chunk_size);
} else {
img->chunks_in_row = (uint8_t)((width / chunk_size) + 1);
}
// calculate how many chunks we will need in each column
if (height / chunk_size == 0) {
img->chunks_in_col = (uint8_t)(height / chunk_size);
} else {
img->chunks_in_col = (uint8_t)((height / chunk_size) + 1);
}
// calculate how many chunks we will need to have
img->q_chunks = img->chunks_in_row * img->chunks_in_col;
// inicializate others parameters
img->chunk_size = chunk_size;
img->height = height;
img->width = width;
img->pixels = pixels_of_img;
img->refresh_required = false;
// inicilizate window
xwin_init(img->width, img->height);
// fill image buffer by black pixels
img = __SetBlackScreen__(img);
return img;
}
Image* __SetBlackScreen__(Image *image) {
// fill image buffer by black pixels
for (int index_pixel = 0; index_pixel < image->width * image->height; index_pixel++) {
image->pixels[index_pixel] = (Pixel) { .R = 0, .G = 0, .B = 0};
}
// redraw image in the window
xwin_redraw(image->width, image->height, (unsigned char*) image->pixels);
return image;
}
void __FreeImage__(Image* img) {
free(img->pixels);
free(img);
}
void __RepaintScreen__(Image* image) {
xwin_redraw(image->width, image->height, (unsigned char*) image->pixels);
}
void __CloseImage__() {
xwin_close();
}