OSDN Git Service

size changed
[hdrholic/HDRHolic.git] / HDRHolic.pde
1 import controlP5.*;
2
3 ControlP5 cp5;
4 ControlWindow controlWindow;
5 ControlWindow viewWindow;
6 Textlabel readmeText;
7
8 PImage img0;
9 PImage img1;
10 PImage img2;
11 PImage writeImg;
12
13 float scope_ratio = 5; // 1 to 100%
14
15 final int low_scope_speed_ratio = 20;
16 final float low_average_speed_ratio = 0.1;
17
18 final int high_scope_speed_ratio = 5;
19 final float high_average_speed_ratio = 0.1;
20
21 int scope_speed_ratio = low_scope_speed_ratio; // 1 to scope
22 float average_speed_ratio = low_average_speed_ratio; // 0 to picture width
23
24 final int max_lum_class = 30; // 1 to 256
25 int class_th = 20; // class threshold
26 float a_value = 0.27;
27 float gamma_u = 0.5;
28 float gamma_n = 1;
29 float gamma_o = 0.5;
30 float color_gain = 1;
31 final float delta = 0.01;
32 float[] lut_u = new float[256];
33 float[] lut_n = new float[256];
34 float[] lut_o = new float[256];
35
36 // hdr image
37 int[] hdr_img_r;
38 int[] hdr_img_g;
39 int[] hdr_img_b;
40
41 //Window Size
42 int size_x = 800;
43 int size_y = 600;
44 int view_width, view_height;
45
46 void setup(){
47   size(size_x, size_y);
48
49   cp5 = new ControlP5(this);
50
51   controlWindow = cp5.addControlWindow("Tunewindow", 100, 100, 360, 600)
52     .hideCoordinates()
53     .setBackground(color(40))
54     ;
55
56   cp5.addTextlabel("guide")
57       .setText("Guide:")
58       .setPosition(40,40)
59       .setColorValue(0xffffffff)
60       .setFont(createFont("Georgia",20))
61       .moveTo(controlWindow)
62       ;
63
64   readmeText = cp5.addTextlabel("label")
65                .setText("Select an under exposed photo.")
66                .setPosition(40,80)
67                .setColorValue(0xffffffff)
68                .setFont(createFont("Georgia",18))
69                .moveTo(controlWindow)
70                 ;
71
72   cp5.addSlider("gamma_u")
73      .setRange(0, 2)
74      .setPosition(40, 140)
75      .setSize(200, 29)
76      .moveTo(controlWindow)
77      ;
78
79   cp5.addSlider("gamma_n")
80      .setRange(0, 2)
81      .setPosition(40, 180)
82      .setSize(200, 29)
83      .moveTo(controlWindow)
84      ;
85
86   cp5.addSlider("gamma_o")
87      .setRange(0, 2)
88      .setPosition(40, 220)
89      .setSize(200, 29)
90      .moveTo(controlWindow)
91      ;
92
93   cp5.addSlider("a_value")
94      .setRange(0, 0.5)
95      .setPosition(40, 300)
96      .setSize(200, 29)
97      .moveTo(controlWindow)
98      ;
99
100   cp5.addSlider("color_gain")
101      .setRange(0, 5)
102      .setPosition(40, 340)
103      .setSize(200, 29)
104      .moveTo(controlWindow)
105      ;
106
107   cp5.addSlider("scope_ratio")
108      .setRange(0, 20)
109      .setPosition(40, 380)
110      .setSize(200, 29)
111      .moveTo(controlWindow)
112      ;
113
114   cp5.addSlider("class_th")
115      .setRange(0, 40)
116      .setPosition(40, 420)
117      .setSize(200, 29)
118      .moveTo(controlWindow)
119      ;
120
121   cp5.addButton("Save Image")
122      .setPosition(40,500)
123      .setSize(100,39)
124      .moveTo(controlWindow)
125      ;
126
127   cp5.addButton("Exit")
128      .setPosition(160,500)
129      .setSize(100,39)
130      .moveTo(controlWindow)
131      ;
132
133   String imgPath = selectInput();
134   img0 = loadImage(imgPath);
135   readmeText.setText("Select a normal exposed photo.");
136
137   imgPath = selectInput();
138   img1 = loadImage(imgPath);
139
140   readmeText.setText("Select an over exposed photo.");
141
142   imgPath = selectInput();
143   img2 = loadImage(imgPath);
144
145   writeImg = createImage(img0.width, img0.height, RGB);
146
147   MakeHDR();
148   ToneMapping();
149
150   readmeText.setText("Completed.");
151
152   if(img0.width > size_x || img0.height > size_y){
153     float k_width = (float)img0.width / (float)size_x;
154     float k_height = (float)img0.height / (float)size_y;
155     float k_max;
156     if(k_width > k_height){
157       k_max = k_width;
158     }else{
159       k_max = k_height;
160     }
161     view_width = (int)(img0.width/k_max);
162     view_height = (int)(img0.height/k_max);
163   }else{
164     view_width = img0.width;
165     view_height = img0.height;
166   }
167 }
168
169 public void controlEvent(ControlEvent theEvent) {
170   if(theEvent.isFrom("color_gain")) {
171     MakeHDR();
172   }
173
174   if(theEvent.isFrom("gamma_u")) {
175     MakeHDR();
176   }
177
178   if(theEvent.isFrom("gamma_n")) {
179     MakeHDR();
180   }
181
182   if(theEvent.isFrom("gamma_o")) {
183     MakeHDR();
184   }
185
186   if(theEvent.isFrom("Save Image")) {
187     String imgPath = selectOutput();
188     writeImg.save(imgPath);
189   }
190
191   if(theEvent.isFrom("Exit")) {
192     exit();
193   }
194 }
195
196
197 void draw(){
198   //  MakeHDR();
199   ToneMapping();
200   image(writeImg, 0, 0, view_width, view_height);
201 }
202
203 void MakeGamma(){
204   for (int i = 0; i < 256; i++){
205     lut_u[i] = 255*pow(((float)i/255),(1/gamma_u));
206   }
207
208   for (int i = 0; i < 256; i++){
209     lut_n[i] = 255*pow(((float)i/255),(1/gamma_n));
210   }
211
212   for (int i = 0; i < 256; i++){
213     lut_o[i] = 255*pow(((float)i/255),(1/gamma_o));
214   }  
215 }
216
217 void MakeHDR(){
218   MakeGamma();
219
220   hdr_img_r = new int[img0.height*img0.width];
221   hdr_img_g = new int[img0.height*img0.width];
222   hdr_img_b = new int[img0.height*img0.width];
223
224   img0.loadPixels();
225   img1.loadPixels();
226   img2.loadPixels();
227
228   for(int i = 0; i < img0.width*img0.height; i++){
229     color tmp_color0 = img0.pixels[i];
230     color tmp_color1 = img1.pixels[i];
231     color tmp_color2 = img2.pixels[i];
232
233     hdr_img_r[i] =
234       (int)((lut_u[(int)red(tmp_color0)] + lut_n[(int)red(tmp_color1)] + lut_o[(int)red(tmp_color2)])*color_gain/3);
235     hdr_img_g[i] =
236       (int)((lut_u[(int)green(tmp_color0)] + lut_n[(int)green(tmp_color1)] + lut_o[(int)green(tmp_color2)])*color_gain/3);
237     hdr_img_b[i] =
238       (int)((lut_u[(int)blue(tmp_color0)] + lut_n[(int)blue(tmp_color1)] + lut_o[(int)blue(tmp_color2)])*color_gain/3);
239   }
240 }
241
242 void ToneMapping(){
243   float lum_sum;
244   int sum_numb;
245
246   int scope = (int)(sqrt(img0.height*img0.width) * scope_ratio/100);
247   int scope_speed = (int)(scope * scope_speed_ratio/100)+1;
248   int average_speed = (int)(sqrt(img0.height*img0.width) * average_speed_ratio/100);
249
250   //ToneMapping----
251   int tmp = average_speed;
252   float lum_sum_w = 0;
253
254   int[] lum = new int[img0.height*img0.width];
255   float[] lum_local = new float[img0.height*img0.width];
256   int[] lum_class = new int[img0.height*img0.width];
257   int[] u = new int[img0.height*img0.width];
258   int[] v = new int[img0.height*img0.width];
259
260   for(int y = 0; y < img0.height; y++){
261     for(int x = 0; x < img0.width; x++){
262       int pos = x + y*img0.width;
263       lum[pos] = (307*hdr_img_r[pos] + 604*hdr_img_g[pos] + 113*hdr_img_b[pos])  >> 10;
264       lum_local[pos] = log((float)(lum[pos]) / 256 + delta);
265
266       u[pos] = (-174*hdr_img_r[pos] - 338*hdr_img_g[pos] + 512*hdr_img_b[pos]) >> 10;
267       v[pos] = (512*hdr_img_r[pos] -430*hdr_img_g[pos] - 82*hdr_img_b[pos]) >> 10;
268     }
269   }
270   
271   int max_lum = (int)max(lum);
272   for(int y = 0; y < img0.height; y++){
273     for(int x = 0; x < img0.width; x++){
274       int pos = x + y*img0.width;
275       lum_class[pos] = (int)(float(lum[pos])/((max_lum+1)/max_lum_class));
276     }
277   }
278
279   for(int y = 0; y < img0.height; y++){
280     tmp = average_speed;
281     for(int x = 0; x < img0.width; x++){
282       int pos = x + y*img0.width;
283       lum_sum = 0;
284       sum_numb = 0;
285       tmp++;
286       if(tmp > average_speed){
287         tmp = 0;
288         for(int y_2 = y-scope; y_2 < y+scope; y_2 += scope_speed){
289           for(int x_2 = x-scope; x_2 < x+scope; x_2 += scope_speed){
290             if(y_2 >= 0 && y_2 < img0.height && x_2 >=0 && x_2 < img0.width){
291                 int pos_2 = x_2 + y_2*img0.width;
292                 if(abs(lum_class[pos] - lum_class[pos_2]) < class_th){
293                   sum_numb++;
294                   lum_sum += lum_local[pos_2];
295               }
296             }
297           }
298         }
299         lum_sum_w = exp(lum_sum/(float)sum_numb);
300       }
301
302       float lum_w = lum[pos]/lum_sum_w*a_value;
303
304       int r = (int)(1024*lum_w + 1433*v[pos]) >> 10;
305       int g = (int)(1024*lum_w -348*u[pos] -727*v[pos]) >> 10;
306       int b = (int)(1024*lum_w + 1812*u[pos]) >> 10;
307       
308       writeImg.pixels[pos] = color(r,g,b);
309     }
310   }
311   writeImg.updatePixels();
312 }
313