OSDN Git Service

09cad58ffed18e5485ed75b914c34731db3bbc11
[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 = 15; // 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 float[] hdr_img_r;
38 float[] hdr_img_g;
39 float[] hdr_img_b;
40
41 //Window Size
42 int size_x = 1024;
43 int size_y = 768;
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("controlP5window", 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, 30)
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 = img0.width / size_x;
154     float k_height = img0.height /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     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 MakeHDR(){
204   hdr_img_r = new float[img0.height*img0.width];
205   hdr_img_g = new float[img0.height*img0.width];
206   hdr_img_b = new float[img0.height*img0.width];
207
208   img0.loadPixels();
209   img1.loadPixels();
210   img2.loadPixels();
211
212   for (int i = 0; i < 256; i++){
213     lut_u[i] = 255*pow(((float)i/255),(1/gamma_u));
214   }
215
216   for (int i = 0; i < 256; i++){
217     lut_n[i] = 255*pow(((float)i/255),(1/gamma_n));
218   }
219
220   for (int i = 0; i < 256; i++){
221     lut_o[i] = 255*pow(((float)i/255),(1/gamma_o));
222   }
223
224   for(int i = 0; i < img0.width*img0.height; i++){
225     color tmp_color0 = img0.pixels[i];
226     color tmp_color1 = img1.pixels[i];
227     color tmp_color2 = img2.pixels[i];
228
229     hdr_img_r[i] =
230       lut_u[(int)red(tmp_color0)] + lut_n[(int)red(tmp_color1)] + lut_o[(int)red(tmp_color2)];
231     hdr_img_g[i] =
232       lut_u[(int)green(tmp_color0)] + lut_n[(int)green(tmp_color1)] + lut_o[(int)green(tmp_color2)];
233     hdr_img_b[i] =
234       lut_u[(int)blue(tmp_color0)] + lut_n[(int)blue(tmp_color1)] + lut_o[(int)blue(tmp_color2)];
235
236     hdr_img_r[i] = hdr_img_r[i]/3*color_gain;
237     hdr_img_g[i] = hdr_img_g[i]/3*color_gain;
238     hdr_img_b[i] = hdr_img_b[i]/3*color_gain;
239   }
240
241   //debug-----
242   /*
243   for(int y = 0; y < img0.height; y++){
244     for(int x = 0; x < img0.width; x++){
245       int pos = x + y*img0.width;
246       color tmp_color = color(hdr_img_r[pos], hdr_img_g[pos], hdr_img_b[pos]);
247       set(x, y, tmp_color);
248     }
249   }
250   */
251   //----debug  
252 }
253
254 void ToneMapping(){
255   float lum_sum;
256   int sum_numb;
257
258   int scope = (int)(sqrt(img0.height*img0.width) * scope_ratio/100);
259   int scope_speed = (int)(scope * scope_speed_ratio/100)+1;
260   int average_speed = (int)(sqrt(img0.height*img0.width) * average_speed_ratio/100);
261
262   // debug
263 //  println("scope= " + scope);
264 //  println("scope_speed= " + scope_speed);
265 //  println("average_speede= " + average_speed);
266
267   //ToneMapping----
268   color[] tmp_img = new color[img0.height*img0.width];
269   int tmp = average_speed;
270   float lum_sum_w = 0;
271
272   float[] lum = new float[img0.height*img0.width];
273   float[] lum_local = new float[img0.height*img0.width];
274   int[] lum_class = new int[img0.height*img0.width];
275   float[] u = new float[img0.height*img0.width];
276   float[] v = new float[img0.height*img0.width];
277
278   for(int y = 0; y < img0.height; y++){
279     for(int x = 0; x < img0.width; x++){
280       int pos = x + y*img0.width;
281       lum[pos] = 0.3*hdr_img_r[pos] + 0.59*hdr_img_g[pos] + 0.11*hdr_img_b[pos];
282       lum_local[pos] = log((0.3*hdr_img_r[pos] + 0.59*hdr_img_g[pos] + 0.11*hdr_img_b[pos])/256+delta);
283       u[pos] = -0.17*hdr_img_r[pos] - 0.33*hdr_img_g[pos] + 0.5*hdr_img_b[pos];
284       v[pos] = 0.5*hdr_img_r[pos] -0.42*hdr_img_g[pos] - 0.08*hdr_img_b[pos];
285     }
286   }
287   
288   int max_lum = (int)max(lum);
289   for(int y = 0; y < img0.height; y++){
290     for(int x = 0; x < img0.width; x++){
291       int pos = x + y*img0.width;
292       lum_class[pos] = (int)(lum[pos]/((max_lum+1)/max_lum_class));
293     }
294   }
295
296   for(int y = 0; y < img0.height; y++){
297     tmp = average_speed;
298     for(int x = 0; x < img0.width; x++){
299       int pos = x + y*img0.width;
300       lum_sum = 0;
301       sum_numb = 0;
302       tmp++;
303       if(tmp > average_speed){
304         tmp = 0;
305         for(int y_2 = y-scope; y_2 < y+scope; y_2 += scope_speed){
306           for(int x_2 = x-scope; x_2 < x+scope; x_2 += scope_speed){
307             if(y_2 >= 0 && y_2 < img0.height && x_2 >=0 && x_2 < img0.width){
308                 int pos_2 = x_2 + y_2*img0.width;
309                 if(abs(lum_class[pos] - lum_class[pos_2]) < class_th){
310                   sum_numb++;
311                   lum_sum += lum_local[pos_2];
312               }
313             }
314           }
315         }
316         lum_sum_w = exp(lum_sum/(float)sum_numb);
317       }
318
319       float lum_w = lum[pos]/lum_sum_w*a_value;
320       float r = lum_w + 1.4*v[pos];
321       float g = lum_w -0.34*u[pos] -0.71*v[pos];
322       float b = lum_w + 1.77*u[pos];
323       
324       writeImg.pixels[pos] = color(r,g,b);
325     }
326   }
327   writeImg.updatePixels();
328 }
329