OSDN Git Service

6127947573fcdc23a35d35738e996aa0b2dab095
[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 int[] hdr_img_r;
38 int[] hdr_img_g;
39 int[] 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("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, 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     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 //    hdr_img_r[i] = hdr_img_r[i]/3*color_gain;
241 //    hdr_img_g[i] = hdr_img_g[i]/3*color_gain;
242 //    hdr_img_b[i] = hdr_img_b[i]/3*color_gain;
243   }
244
245   //debug-----
246   /*
247   for(int y = 0; y < img0.height; y++){
248     for(int x = 0; x < img0.width; x++){
249       int pos = x + y*img0.width;
250       color tmp_color = color(hdr_img_r[pos], hdr_img_g[pos], hdr_img_b[pos]);
251       set(x, y, tmp_color);
252     }
253   }
254   */
255   //----debug  
256 }
257
258 void ToneMapping(){
259   float lum_sum;
260   int sum_numb;
261
262   int scope = (int)(sqrt(img0.height*img0.width) * scope_ratio/100);
263   int scope_speed = (int)(scope * scope_speed_ratio/100)+1;
264   int average_speed = (int)(sqrt(img0.height*img0.width) * average_speed_ratio/100);
265
266   // debug
267 //  println("scope= " + scope);
268 //  println("scope_speed= " + scope_speed);
269 //  println("average_speede= " + average_speed);
270
271   //ToneMapping----
272   int tmp = average_speed;
273   float lum_sum_w = 0;
274
275   float[] lum = new float[img0.height*img0.width];
276   float[] lum_local = new float[img0.height*img0.width];
277   int[] lum_class = new int[img0.height*img0.width];
278   float[] u = new float[img0.height*img0.width];
279   float[] v = new float[img0.height*img0.width];
280
281   for(int y = 0; y < img0.height; y++){
282     for(int x = 0; x < img0.width; x++){
283       int pos = x + y*img0.width;
284       lum[pos] = 0.3*hdr_img_r[pos] + 0.59*hdr_img_g[pos] + 0.11*hdr_img_b[pos];
285       lum_local[pos] = log((0.3*hdr_img_r[pos] + 0.59*hdr_img_g[pos] + 0.11*hdr_img_b[pos])/256+delta);
286       u[pos] = -0.17*hdr_img_r[pos] - 0.33*hdr_img_g[pos] + 0.5*hdr_img_b[pos];
287       v[pos] = 0.5*hdr_img_r[pos] -0.42*hdr_img_g[pos] - 0.08*hdr_img_b[pos];
288     }
289   }
290   
291   int max_lum = (int)max(lum);
292   for(int y = 0; y < img0.height; y++){
293     for(int x = 0; x < img0.width; x++){
294       int pos = x + y*img0.width;
295       lum_class[pos] = (int)(lum[pos]/((max_lum+1)/max_lum_class));
296     }
297   }
298
299   for(int y = 0; y < img0.height; y++){
300     tmp = average_speed;
301     for(int x = 0; x < img0.width; x++){
302       int pos = x + y*img0.width;
303       lum_sum = 0;
304       sum_numb = 0;
305       tmp++;
306       if(tmp > average_speed){
307         tmp = 0;
308         for(int y_2 = y-scope; y_2 < y+scope; y_2 += scope_speed){
309           for(int x_2 = x-scope; x_2 < x+scope; x_2 += scope_speed){
310             if(y_2 >= 0 && y_2 < img0.height && x_2 >=0 && x_2 < img0.width){
311                 int pos_2 = x_2 + y_2*img0.width;
312                 if(abs(lum_class[pos] - lum_class[pos_2]) < class_th){
313                   sum_numb++;
314                   lum_sum += lum_local[pos_2];
315               }
316             }
317           }
318         }
319         lum_sum_w = exp(lum_sum/(float)sum_numb);
320       }
321
322       float lum_w = lum[pos]/lum_sum_w*a_value;
323       float r = lum_w + 1.4*v[pos];
324       float g = lum_w -0.34*u[pos] -0.71*v[pos];
325       float b = lum_w + 1.77*u[pos];
326       
327       writeImg.pixels[pos] = color(r,g,b);
328     }
329   }
330   writeImg.updatePixels();
331 }
332