-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantization.c
More file actions
150 lines (117 loc) · 3.33 KB
/
quantization.c
File metadata and controls
150 lines (117 loc) · 3.33 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
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
149
150
#include "quantization.h"
#include "cluster.h"
#include <png.h>
#include <stdio.h>
#include <stdlib.h>
png_bytep readImage(char *filename, ImageInfo *imageInfo, png_bytepp *rp);
void generateImage(png_bytepp rowPointers, int width, int height);
void quantize(char *filename, int k, int maxIterations) {
ImageInfo info;
int i;
png_bytep data;
png_bytepp rowPointers;
Cluster *clusters;
Pixel *pix, *head, *prox;
info.k = k;
info.maxInterations = maxIterations;
data = readImage(filename, &info, &rowPointers);
printf("Running...\n");
clusters = lloydsAlgorithm(data, &info, &head);
pix = head;
while (pix) {
for (i = 0; i < info.channels; i++) {
pix->colors[i] = clusters[pix->centroidIndex].centroid[i];
}
pix = pix->successor;
}
generateImage(rowPointers, info.width, info.height);
for (i = 0; i < info.k; i++) {
free(clusters[i].centroid);
}
free(clusters);
pix = head;
while (pix) {
prox = pix->successor;
free(pix);
pix = prox;
}
free(data);
}
png_bytep readImage(char *filename, ImageInfo *imageInfo, png_bytepp *rp) {
png_FILE_p file;
png_structp png;
png_infop info;
png_bytep data;
png_bytepp rowPointers;
int width, height, channels, i;
file = fopen(filename, "rb");
if (!file) return NULL;
png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) {
fclose(file);
return NULL;
}
info = png_create_info_struct(png);
if (!info) {
fclose(file);
png_destroy_read_struct(&png, NULL, NULL);
return NULL;
}
if (setjmp(png_jmpbuf(png))) {
fclose(file);
png_destroy_read_struct(&png, &info, NULL);
return NULL;
}
png_init_io(png, file);
png_read_info(png, info);
width = png_get_image_width(png, info);
height = png_get_image_height(png, info);
channels = png_get_channels(png, info);
imageInfo->width = width;
imageInfo->height = height;
imageInfo->channels = channels;
data = (png_bytep)malloc(width * height * channels);
rowPointers = (png_bytepp)malloc(height * sizeof(png_bytep));
*rp = rowPointers;
for (i = 0; i < height; i++)
rowPointers[i] = data + (width * channels * i);
png_read_image(png, rowPointers);
png_destroy_read_struct(&png, &info, NULL);
fclose(file);
return data;
}
void generateImage(png_bytepp rowPointers, int width, int height) {
png_structp png;
png_infop info;
png_FILE_p file;
png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png) {
return;
}
info = png_create_info_struct(png);
if (!info) {
png_destroy_write_struct(&png, NULL);
return;
}
if (setjmp(png_jmpbuf(png))) {
png_destroy_write_struct(&png, &info);
return;
}
file = fopen("output.png", "wb");
if (!file) {
png_destroy_write_struct(&png, &info);
return;
}
png_init_io(png, file);
png_set_IHDR(png, info, width, height, 8,
PNG_COLOR_TYPE_RGB_ALPHA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_BASE,
PNG_FILTER_TYPE_BASE);
png_write_info(png, info);
png_write_image(png, rowPointers);
png_write_end(png, NULL);
png_destroy_write_struct(&png, &info);
fclose(file);
free(rowPointers);
}