-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.h
83 lines (67 loc) · 1.92 KB
/
image.h
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
#ifndef CANDLEBROT_IMAGE_H
#define CANDLEBROT_IMAGE_H
#ifndef PNG_H
#include <png.h>
#endif
#ifndef _STDLIB_H
#include <stdlib.h>
#endif
typedef struct
{
__uint8_t red;
__uint8_t green;
__uint8_t blue;
} pixel_t;
typedef struct
{
size_t width;
size_t height;
pixel_t **pixels;
} bitmap_t;
static bitmap_t *new_bitmap(size_t width, size_t height)
{
bitmap_t *bitmap = malloc(sizeof(bitmap_t));
bitmap->width = width;
bitmap->height = height;
bitmap->pixels = calloc(height, sizeof(pixel_t *));
for(int y = 0; y < height; y++)
{
bitmap->pixels[y] = calloc(width, sizeof(pixel_t));
}
return bitmap;
}
static void free_bitmap(bitmap_t *bitmap)
{
for(int y = 0; y < bitmap->height; y++)
{
free(bitmap->pixels[y]);
}
free(bitmap->pixels);
free(bitmap);
}
static void set_pixel(bitmap_t *bitmap, int x, int y, __uint32_t color)
{
bitmap->pixels[y][x].red = (__uint8_t) (color >> 16 & 0xFF);
bitmap->pixels[y][x].green = (__uint8_t) (color >> 8 & 0xFF);
bitmap->pixels[y][x].blue = (__uint8_t) (color & 0xFF);
}
static void save_png_to_file(bitmap_t *bitmap, char *path)
{
FILE *image = fopen(path, "wb");
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop info_ptr = png_create_info_struct(png_ptr);
png_set_IHDR(png_ptr, info_ptr,
(png_uint_32) bitmap->width,
(png_uint_32) bitmap->height,
8 /* depth */,
PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
png_init_io(png_ptr, image);
png_set_rows(png_ptr, info_ptr, (unsigned char **) bitmap->pixels);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(image);
}
#endif //CANDLEBROT_IMAGE_H