-
Notifications
You must be signed in to change notification settings - Fork 1
/
ImageStack.c
481 lines (328 loc) · 10.6 KB
/
ImageStack.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pixel.h"
#include "ImageStack.h"
#include "psharplib.h"
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
//Defines ImageStack type -- which is used for the Photo# "Image" type.
//Also defines ImageLayer type, which is the type used for each layer in an
//ImageStack.
//Uses public domain headers "stb_image.h" and "stb_image_write.h". See those files
//for more details on their author.
// Defines the maximim number of versions to be stored in an ImageStack
#define STACKSIZE 5
//Return a pointer to a specific pixel in the top layer
//of an image stack -- used for iteration
struct pixel *getPixel(struct ImageStack *img, int index) {
struct ImageLayer *topLay = img->imgArray[img->top - 1];
return topLay->imgPixelData[index];
}
//Return the size of the top layer of an ImageStack
int ImageSize(struct ImageStack *img) {
int size;
struct ImageLayer *topLay = img->imgArray[img->top - 1];
size = ((topLay->w) * (topLay->h));
return(size);
}
//push a previously malloc-ed ImageLayer onto a previously malloc-ed ImageStack
void pushLayer(struct ImageStack *img, struct ImageLayer *lay) {
img->imgArray[img->top] = lay;
img->top++;
if(img->top == STACKSIZE) {
img->top = 0;
}
}
//pop the last ImageLayer off the ImageStack. Function will return the popped layer.
//NOTE: it is the responsibility of the calling function to free the popped layer.
struct ImageLayer *popLayer(struct ImageStack *img) {
struct ImageLayer *lay;
lay = img->imgArray[img->top -1];
if(img->top == 0) {
if(img->imgArray[4] == NULL) {
img->top = 0;
} else {
img->top = 4;
}
} else {
(img->top)--;
}
return lay;
}
//Free an ImageLayer and all pixel structures associated with it
void freeLayer(struct ImageLayer *lay) {
for(int i = 0; i < (lay->h * lay->w); i++) {
free(lay->imgPixelData[i]);
}
free(lay);
}
//Free all ImageLayers belonging to an ImageStack
void freeImageStack(struct ImageStack *stack){
for (int i = 0; i < stack->top; i++){
if(stack->imgArray[i] != NULL) {
freeLayer(stack->imgArray[i]);
}
}
free(stack);
}
void ImageRevert(struct ImageStack *im){
freeLayer(popLayer(im));
}
//create a new Image layer of the requested dimensions
//to handle memory allocation in image editing functions
struct ImageLayer *newImageLayer(int hi, int wi) {
struct ImageLayer *lay = (struct ImageLayer *)malloc(sizeof(struct ImageLayer));
struct pixel* temp;
int x = 0;
lay->h = hi;
lay->w = wi;
int j = 0;
struct pixel **buf;
buf = (struct pixel **)malloc(sizeof(struct pixel *)*(wi * hi));
lay->imgPixelData = buf;
int next = 0;
while(j < (hi * wi)) {
temp = makePix();
lay->imgPixelData[j] = temp;
j++;
}
return lay;
}
// dir is either 0 or 1. 0: top-down, 1: bottom-up.
struct ImageGradient *newImageGradient(struct ImageStack* s, unsigned char dir){
struct ImageLayer *lay = s->imgArray[s->top-1];
struct ImageGradient *grad = (struct ImageGradient *)malloc(sizeof(struct ImageGradient));
struct pixel *temp;
struct pixel **buf;
buf = (struct pixel **)malloc(sizeof(struct pixel *) * (lay->h * lay->w));
grad->imgPixelData = buf;
int j = 0;
while(j < (lay->h*lay->w)) {
temp = makePix();
temp->red = lay->imgPixelData[j]->red;
temp->green= lay->imgPixelData[j]->green;
temp->blue = lay->imgPixelData[j]->blue;
temp->alpha = lay->imgPixelData[j]->alpha;
buf[j] = temp;
j++;
}
grad->direction = dir;
grad->h = lay->h;
grad->w = lay->w;
grad->imgPixelData = buf;
return grad;
}
struct ImageGradient *newGradFromGrad(struct ImageGradient* lay){
struct ImageGradient *grad = (struct ImageGradient *)malloc(sizeof(struct ImageGradient));
struct pixel *temp;
struct pixel **buf;
buf = (struct pixel **)malloc(sizeof(struct pixel *) * (lay->h * lay->w));
for (int i = 0 ; i < lay->h * lay->w; i++){
buf[i] = lay->imgPixelData[i];
}
grad->direction = lay->direction;
grad->imgPixelData = buf;
grad->h = lay->h;
grad->w = lay->w;
return grad;
}
void GradToLayer(struct ImageStack *s, struct ImageGradient *grad){
struct ImageLayer *lay = (struct ImageLayer *)malloc(sizeof(struct ImageLayer));
lay->h = grad->h;
lay->w = grad->w;
struct pixel **buf;
buf = (struct pixel **)malloc(sizeof(struct pixel *) * (lay->h * lay->w));
for (int i = 0; i < grad->w * grad-> h ; i++){
struct pixel *tmp = makePix();
tmp->red = grad->imgPixelData[i]->red;
tmp->green = grad->imgPixelData[i]->green;
tmp->blue = grad->imgPixelData[i]->blue;
buf[i] = tmp;
}
lay->imgPixelData = buf;
pushLayer(s, lay);
}
void freeGradient(struct ImageGradient *grad){
for(int i = 0 ; i< grad->h*grad->w; i++){
free(grad->imgPixelData[i]);
}
free(grad);
}
//create new, empty, ImageStack
struct ImageStack *newImageStack() {
struct ImageStack* img = (struct ImageStack *)malloc(sizeof(struct ImageStack));
struct ImageLayer **buf;
buf = (struct ImageLayer **)malloc(sizeof(struct ImageLayer *) * (STACKSIZE));
img->imgArray = buf;
img->top = 0;
return img;
}
//save the top layer of an image to a designated file name
int save(char const *filename, struct ImageStack *img){
int topLayer = img->top - 1;
struct pixel **data = img->imgArray[topLayer]->imgPixelData;
unsigned char *temp;
int hi = img->imgArray[topLayer]->h;
int wi = img->imgArray[topLayer]->w;
temp = (unsigned char *)malloc(sizeof(int)*(wi * hi * 4));
int x = 0;
for(int i = 0; i < wi * hi; i++)
{
temp[x++] = (unsigned char)data[i]->red;
temp[x++] = (unsigned char)data[i]->green;
temp[x++] = (unsigned char)data[i]->blue;
temp[x++] = (unsigned char)data[i]->alpha;
}
return(stbi_write_jpg(filename, wi, hi, 4, temp, 100));
}
//open a file and save to a new ImageLayer
struct ImageLayer *openFile(char *fileName){
int hi;
int wi;
int chan;
unsigned char *data = stbi_load(fileName, &wi, &hi, &chan, 4);
struct ImageLayer *lay = newImageLayer(hi, wi);
struct pixel* temp;
int x = 0;
lay->h = hi;
lay->w = wi;
int j = 0;
int next = 0;
while(j < (hi * wi * 4))
{
if(next == 0) {
lay->imgPixelData[x]->red = (int)data[j];
next = 1;
} else if(next == 1) {
lay->imgPixelData[x]->green = (int)data[j];
next = 2;
} else if(next == 2) {
lay->imgPixelData[x]->blue = (int)data[j];
next = 3;
} else if(next == 3) {
lay->imgPixelData[x]->alpha = (int)data[j];
next = 0;
x = x + 1;
}
j++;
}
stbi_image_free(data);
return lay;
}
//Open a file and set the first layer of an ImageStack to this file. For now, we can initialize to opened file only
struct ImageStack *open(char *fileName) {
struct ImageStack *img = newImageStack();
struct ImageLayer *base;
base = openFile(fileName);
pushLayer(img, base);
return img;
}
//Print the top layer of an ImageStack
void PrintImage(struct ImageStack *img) {
int topLayer = img->top - 1;
int imgSize = (img->imgArray[topLayer]->h)*(img->imgArray[topLayer]->w);
int i;
for (i = 0; i<imgSize; i++){
printPix(img->imgArray[topLayer]->imgPixelData[i]);
}
printf("\n");
}
//Codegen interface functions
//Equivalent function calls which do the heavy lifting are in the psharplib.c file
void ImageContrast(struct ImageStack *img, int lev) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = Contrast(img->imgArray[topLevel], lev);
pushLayer(img, lay);
}
void ImageSaturate(struct ImageStack *img, int lev) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = HSL(img->imgArray[topLevel], lev, 2, 0);
pushLayer(img, lay);
}
void ImageRotate90(struct ImageStack *img) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = Rotate90(img->imgArray[topLevel]);
pushLayer(img, lay);
}
void ImageAddNoise(struct ImageStack *img, double var, double mean) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = addNoise(img->imgArray[topLevel], (float)var, (float)mean);
pushLayer(img, lay);
}
void ImageKelvin(struct ImageStack *img, double lev) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = Kelvin(img->imgArray[topLevel], (float)lev);
pushLayer(img, lay);
}
void ImageReflectY(struct ImageStack *img) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = ReflectY(img->imgArray[topLevel]);
pushLayer(img, lay);
}
void ImageReflectX(struct ImageStack *img) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = ReflectX(img->imgArray[topLevel]);
pushLayer(img, lay);
}
void ImageTint(struct ImageStack *img, int lev) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = Tint(img->imgArray[topLevel], lev);
pushLayer(img, lay);
}
void ImageCrop(struct ImageStack *img, double p) {
int topLevel = img->top - 1;
float f = (float)p;
struct ImageLayer *lay;
lay = Crop(img->imgArray[topLevel], f);
pushLayer(img, lay);
}
void ImageHSL(struct ImageStack *img, int a, int b, int c) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = HSL(img->imgArray[topLevel], a, b, c);
pushLayer(img, lay);
}
void ImageBrightness(struct ImageStack *img, int amt) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = Brightness(img->imgArray[topLevel], amt);
pushLayer(img, lay);
}
void ImageRGBImage(struct ImageStack *img, int value) {
int topLevel = img->top - 1;
struct ImageLayer *lay;
lay = RGBImage(img->imgArray[topLevel], value);
pushLayer(img, lay);
}
void GradientContrast(struct ImageGradient *img, int lev) {
struct ImageGradient *grad;
GradContrast(grad, lev);
}
void GradientHSL(struct ImageGradient *img, int lev, int hsl, int channel){
struct ImageGradient *grad;
GradHSL(grad, lev, hsl, channel);
}
/*
int main() {
struct ImageStack* img;
img = open("test.jpg");
struct ImageGradient* g;
//g = newImageGradient(img, 1);
//GradientHSL(g, 140, 0, 0);
//GradToLayer(img, g);
//ImageHSL(img, 120, 0, 0);
ImageKelvin(img, 6800.0);
save("ttt.jpg", img);
}
*/