-
Notifications
You must be signed in to change notification settings - Fork 26
/
imageReader.cpp
515 lines (395 loc) · 18.3 KB
/
imageReader.cpp
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
// TO COMPILE: g++ -std=c++11 imageReader.cpp -o imageReader -L/usr/lib64 -lOpenImageIO -L/opt/appleseed/lib -ltiff
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> // std::sort
#include <functional> // std::greater
#include <random>
#include <cstring>
#include <OpenImageIO/imageio.h>
//#include <OpenImageIO/imagebuf.h>
//#include <OpenImageIO/imagebufalgo.h>
#include <stdint.h>
struct imageData{
int x, y;
int nchannels;
std::vector<uint8_t> pixelData;
float* cdfRow;
float* cdfColumn;
float* summedRowValues;
float* normalizedValuesPerRow;
std::vector<int> rowIndices;
std::vector<int> columnIndices;
};
// for graphing
std::vector<float> plotDataSamplesX;
std::vector<float> plotDataSamplesY;
std::vector<float> pdf;
std::vector<float> cdf;
bool debug = false;
// make sure to resize image to odd res before all operations
imageData* readImage(char const *bokeh_kernel_filename){
imageData* img = new imageData;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cerr << "Reading image <" << bokeh_kernel_filename << "> with OpenImageIO" << std::endl;
/* Search for an ImageIO plugin that is capable of reading the file ("foo.jpg"), first by
trying to deduce the correct plugin from the file extension, but if that fails, by opening
every ImageIO plugin it can find until one will open the file without error. When it finds
the right plugin, it creates a subclass instance of ImageInput that reads the right kind of
file format, and tries to fully open the file. */
OpenImageIO::ImageInput *in = OpenImageIO::ImageInput::open (bokeh_kernel_filename);
if (! in){
return nullptr; // Return a null pointer if we have issues
}
const OpenImageIO::ImageSpec &spec = in->spec();
img->x = spec.width;
img->y = spec.height;
img->nchannels = spec.nchannels;
img->pixelData.reserve(img->x * img->y * img->nchannels);
in->read_image (OpenImageIO::TypeDesc::UINT8, &img->pixelData[0]);
in->close ();
delete in;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cerr << "Image Width: " << img->x << std::endl;
std::cerr << "Image Height: " << img->y << std::endl;
std::cerr << "Image Channels: " << img->nchannels << std::endl;
std::cout << "Total amount of pixels: " << img->x * img->y << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
if (debug == true){
// print out raw pixel data
for (int i = 0; i < img->x * img->y * img->nchannels; i++){
int j = 0;
if(img->nchannels == 3){
if (j == 0){
std::cout << "Channel Value [R]: " << (int)img->pixelData[i] << std::endl;
j += 1;
}
if (j == 1){
std::cout << "Channel Value [G]: " << (int)img->pixelData[i] << std::endl;
j += 1;
}
if (j == 2){
std::cout << "Channel Value [B]: " << (int)img->pixelData[i] << std::endl;
j = 0;
}
}
else if(img->nchannels == 4){
if (j == 0){
std::cout << "Channel Value [R]: " << (int)img->pixelData[i] << std::endl;
j += 1;
}
if (j == 1){
std::cout << "Channel Value [G]: " << (int)img->pixelData[i] << std::endl;
j += 1;
}
if (j == 2){
std::cout << "Channel Value [B]: " << (int)img->pixelData[i] << std::endl;
j += 1;
}
if (j == 3){
std::cout << "Channel Value [A]: " << (int)img->pixelData[i] << std::endl;
j = 0;
}
}
}
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
return img;
}
//imageData* rotateImage(char const *bokeh_kernel_filename){
// OpenImageIO::ImageBuf src (bokeh_kernel_filename);
// OpenImageIO::ImageBuf dst; // will be the output image
// bool ok = OpenImageIO::ImageBufAlgo::transpose (dst, src);
// if (! ok) {
// std::cout << "Error" << std::endl;
// }
//}
void bokehProbability(imageData *img){
if(img){
// initialize arrays
float pixelValues[img->x * img->y];
float normalizedPixelValues[img->x * img->y];
// for every pixel, stuff going wrong here
int tmpPixelCounter = 0;
for(int i=0; i < img->x * img->y; ++i){
// store pixel value in array
// calculate luminance [Y = 0.3 R + 0.59 G + 0.11 B]
pixelValues[i] = (img->pixelData[tmpPixelCounter] * 0.3) + (img->pixelData[tmpPixelCounter+1] * 0.59) + (img->pixelData[tmpPixelCounter+2] * 0.11f);
if (debug == true){
// print array
std::cout << "Pixel Luminance: " << i << " -> " << pixelValues[i] << std::endl;
}
if(img->nchannels == 3){
tmpPixelCounter += 3;
}
else if(img->nchannels == 4){
tmpPixelCounter += 4;
}
}
// calculate sum of all pixel values
float totalValue = 0.0f;
for(int i=0; i < img->x * img->y; ++i){
totalValue += pixelValues[i];
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
std::cout << "DEBUG: Total Pixel Value: " << totalValue << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// normalize pixel values so sum = 1
for(int i=0; i < img->x * img->y; ++i){
normalizedPixelValues[i] = pixelValues[i] / totalValue;
if (debug == true){
// print array
std::cout << "Normalized Pixel Value: " << i << ": " << normalizedPixelValues[i] << std::endl;
}
}
// calculate sum of all normalized pixel values, to check
float totalNormalizedValue = 0.0f;
for(int i=0; i < img->x * img->y; ++i){
totalNormalizedValue += normalizedPixelValues[i];
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
std::cout << "DEBUG: Total Normalized Pixel Value: " << totalNormalizedValue << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// calculate sum for each row
img->summedRowValues = new float [img->y]();
float summedHorizontalNormalizedValues;
int counterRow = 0;
for(int i=0; i < img->y; ++i){
summedHorizontalNormalizedValues = 0.0f;
for(int j=0; j < img->x; ++j){
summedHorizontalNormalizedValues += normalizedPixelValues[counterRow];
counterRow += 1;
}
img->summedRowValues[i] = summedHorizontalNormalizedValues;
if (debug == true){
std::cout << "Summed Values row [" << i << "]: " << img->summedRowValues[i] << std::endl;
}
}
// calculate sum of all row values, just to debug
float totalNormalizedRowValue = 0.0f;
for(int i=0; i < img->y; ++i){
totalNormalizedRowValue += img->summedRowValues[i];
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
std::cout << "Debug: Summed Row Value: " << totalNormalizedRowValue << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// sort row values from highest to lowest (probability density function)
// needed to make a copy of array, can't use the one in struct for some reason?
float summedRowValueCopy[img->y];
for(int i = 0; i < img->y; i++){
summedRowValueCopy[i] = img->summedRowValues[i];
}
// make array of indices
size_t summedRowValueCopyIndices[img->y];
for(int i = 0; i < img->y; i++){
summedRowValueCopyIndices[i] = i;
}
std::sort(summedRowValueCopyIndices, summedRowValueCopyIndices + img->y, [&summedRowValueCopy]( size_t _lhs, size_t _rhs){
return summedRowValueCopy[_lhs] > summedRowValueCopy[_rhs];
});
if (debug == true){
// print values
for(int i = 0; i < img->y; ++i){
std::cout << "PDF row [" << summedRowValueCopyIndices[i] << "]: " << summedRowValueCopy[summedRowValueCopyIndices[i]] << std::endl;
}
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// for graph output
for(int i = 0; i < img->y; i++){
pdf.push_back(summedRowValueCopy[summedRowValueCopyIndices[i]]);
}
// For every row, add the sum of all previous row (cumulative distribution function)
img->cdfRow = new float [img->y]();
img->rowIndices.reserve(img->y);
for (int i = 0; i < img->y; ++i){
if(i == 0){
img->cdfRow[i] = img->cdfRow[i] + summedRowValueCopy[summedRowValueCopyIndices[i]];
}
else{
img->cdfRow[i] = img->cdfRow[i-1] + summedRowValueCopy[summedRowValueCopyIndices[i]];
}
img->rowIndices[i] = summedRowValueCopyIndices[i];
if (debug == true){
std::cout << "CDF row [" << img->rowIndices[i] << "]: " << img->cdfRow[i] << std::endl;
}
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// for graph output
for(int i = 0; i < img->y; i++){
cdf.push_back(img->cdfRow[i]);
}
// divide pixel values of each pixel by the sum of the pixel values of that row (Normalize)
int rowCounter = 0;
int tmpCounter = 0;
img->normalizedValuesPerRow = new float [img->x * img->y]();
for (int i = 0; i < img->x * img->y; ++i){
// avoid division by 0
if ((normalizedPixelValues[i] != 0) && (img->summedRowValues[rowCounter] != 0)){
img->normalizedValuesPerRow[i] = normalizedPixelValues[i] / img->summedRowValues[rowCounter];
}
else{
img->normalizedValuesPerRow[i] = 0;
}
tmpCounter += 1;
// silly counter, there must be faster ways to do this but i'm not exactly a genius
if (tmpCounter == img->x){
rowCounter += 1;
tmpCounter = 0;
}
if (debug == true){
std::cout << "Normalized Pixel value per row: " << i << ": " << img->normalizedValuesPerRow[i] << std::endl;
}
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// sort column values from highest to lowest per row (probability density function)
// needed to make a copy of array, can't use the one in struct for some reason?
float summedColumnValueCopy[img->x * img->y];
for(int i = 0; i < img->x * img->y; ++i){
summedColumnValueCopy[i] = img->normalizedValuesPerRow[i];
}
// make array of indices
size_t summedColumnValueCopyIndices[img->x * img->y];
for(int i = 0; i < img->x * img->y; i++){
summedColumnValueCopyIndices[i] = i;
}
for (int i = 0; i < img->x * img->y; i+=img->x){
std::sort(summedColumnValueCopyIndices + i, summedColumnValueCopyIndices + i + img->x, [&summedColumnValueCopy]( size_t _lhs, size_t _rhs){
return summedColumnValueCopy[_lhs] > summedColumnValueCopy[_rhs];
});
}
if (debug == true){
// print values
for(int i = 0; i < img->x * img->y; ++i){
std::cout << "PDF column [" << summedColumnValueCopyIndices[i] << "]: " << summedColumnValueCopy[summedColumnValueCopyIndices[i]] << std::endl;
}
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// For every column per row, add the sum of all previous columns (cumulative distribution function)
img->cdfColumn = new float [img->x * img->y]();
img->columnIndices.reserve(img->x * img->y);
int cdfCounter = 0;
for (int i = 0; i < img->x * img->y; ++i){
if (cdfCounter == img->x) {
img->cdfColumn[i] = summedColumnValueCopy[summedColumnValueCopyIndices[i]];
cdfCounter = 0;
}
else {
img->cdfColumn[i] = img->cdfColumn[i-1] + summedColumnValueCopy[summedColumnValueCopyIndices[i]];
}
cdfCounter += 1;
img->columnIndices[i] = summedColumnValueCopyIndices[i];
if (debug == true){
std::cout << "CDF column [" << img->columnIndices[i] << "]: " << img->cdfColumn[i] << std::endl;
}
}
if (debug == true){
std::cout << "----------------------------------------------" << std::endl;
}
}
}
void bokehSample(imageData *img, float randomNumberRow, float randomNumberColumn){
if (debug == true){
// print random number between 0 and 1
std::cout << "RANDOM NUMBER ROW: " << randomNumberRow << std::endl;
}
// find upper bound of random number in the array
float *pUpperBound = std::upper_bound(img->cdfRow, img->cdfRow + img->y, randomNumberRow);
if (debug == true){
std::cout << "UPPER BOUND: " << *pUpperBound << std::endl;
}
// find which element of the array the upper bound is
int x = std::distance(img->cdfRow, std::find(img->cdfRow, img->cdfRow + img->y, *pUpperBound));
// find actual pixel row
int actualPixelRow = img->rowIndices[x];
// recalculate pixel row so that the center pixel is (0,0) - might run into problems with images of dimensions like 2x2, 4x4, 6x6, etc
int recalulatedPixelRow = actualPixelRow - ((img->x - 1) / 2);
if (debug == true){
// print values
std::cout << "INDEX IN CDF ROW: " << x << std::endl;
std::cout << "ACTUAL PIXEL ROW: " << actualPixelRow << std::endl;
std::cout << "RECALCULATED PIXEL ROW: " << recalulatedPixelRow << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
// print random number between 0 and 1
std::cout << "RANDOM NUMBER COLUMN: " << randomNumberColumn << std::endl;
}
int startPixel = actualPixelRow * img->x;
if (debug == true){
std::cout << "START PIXEL: " << startPixel << std::endl;
}
float *pUpperBoundColumn = std::upper_bound(img->cdfColumn + startPixel, img->cdfColumn + startPixel + img->x, randomNumberColumn);
if (debug == true){
std::cout << "UPPER BOUND: " << *pUpperBoundColumn << std::endl;
}
// find which element of the array the upper bound is
int y = std::distance(img->cdfColumn, std::find(img->cdfColumn + startPixel, img->cdfColumn + startPixel + img->x, *pUpperBoundColumn));
// find actual pixel column
int actualPixelColumn = img->columnIndices[y];
int relativePixelColumn = actualPixelColumn - startPixel;
int recalulatedPixelColumn = relativePixelColumn - ((img->y - 1) / 2);
if (debug == true){
// print values
std::cout << "INDEX IN CDF COLUMN: " << y << std::endl;
std::cout << "ACTUAL PIXEL COLUMN: " << actualPixelColumn << std::endl;
std::cout << "RELATIVE PIXEL COLUMN (starting from 0): " << relativePixelColumn << std::endl;
std::cout << "RECALCULATED PIXEL COLUMN: " << recalulatedPixelColumn << std::endl;
std::cout << "----------------------------------------------" << std::endl;
std::cout << "----------------------------------------------" << std::endl;
}
// plot the data
plotDataSamplesX.push_back((float)actualPixelRow / (float)img->x);
plotDataSamplesY.push_back((float)relativePixelColumn / (float)img->x);
}
int main(){
imageData *image = nullptr;
image = readImage("/home/i7210038/Desktop/teapot.png");
// Check if image is valid (is the pointer null?)
if(!image){
std::cout << "Couldn't open image, shit\n";
exit(1);
}
bokehProbability(image);
// random number
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> distribution (0.0, 1.0);
bokehSample(image, distribution(gen), distribution(gen));
// stuff for scatter plotting
plotDataSamplesX.reserve(20000);
plotDataSamplesY.reserve(20000);
for(int i =0; i < 500; i++){
std::uniform_real_distribution<float> distribution (0.0, 1.0);
bokehSample(image, distribution(gen), distribution(gen));
}
for (int i=0; i<500; i++){
std::cout << "[" << plotDataSamplesY[i] << ", " << plotDataSamplesX[i] << "], ";
}
// pdf.reserve(20000);
// for (int i = 0; i < image->y; i++){
// std::cout << pdf[i] << ", ";
// }
cdf.reserve(20000);
for (int i = 0; i < image->y; i++){
std::cout << cdf[i] << ", ";
}
}