File size: 17,828 Bytes
d01a8a4
 
 
42ec444
d01a8a4
 
 
 
 
42ec444
 
 
d01a8a4
 
 
 
 
 
f08a4ef
d01a8a4
f08a4ef
d01a8a4
42ec444
 
 
 
 
 
 
 
 
 
d01a8a4
 
f08a4ef
d01a8a4
 
 
42ec444
 
 
f08a4ef
 
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01a8a4
42ec444
 
f08a4ef
42ec444
d01a8a4
42ec444
 
d01a8a4
42ec444
 
 
d01a8a4
 
42ec444
f08a4ef
d01a8a4
 
 
 
 
f08a4ef
 
 
 
 
42ec444
f08a4ef
 
 
 
 
 
 
 
 
 
 
d01a8a4
f08a4ef
d01a8a4
 
 
 
 
f08a4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42ec444
f08a4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42ec444
 
f08a4ef
 
 
42ec444
 
 
 
f08a4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08a4ef
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01a8a4
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01a8a4
 
42ec444
 
 
 
 
 
 
 
 
 
 
 
f08a4ef
42ec444
 
 
 
 
f08a4ef
42ec444
 
 
 
 
 
 
 
 
 
d01a8a4
 
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08a4ef
 
42ec444
d01a8a4
 
 
 
 
42ec444
 
 
d01a8a4
 
 
 
 
 
42ec444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f08a4ef
42ec444
 
 
f08a4ef
 
 
42ec444
d01a8a4
42ec444
d01a8a4
 
 
 
 
42ec444
d01a8a4
 
f08a4ef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d01a8a4
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
document.addEventListener('DOMContentLoaded', function() {
    // 要素の取得
    const imageUpload = document.getElementById('image-upload');
    const imageCanvas = document.getElementById('image-canvas');
    const brightnessSlider = document.getElementById('brightness');
    const contrastSlider = document.getElementById('contrast');
    const saturationSlider = document.getElementById('saturation');
    const shadowsSlider = document.getElementById('shadows');
    const highlightsSlider = document.getElementById('highlights');
    const hueRedSlider = document.getElementById('hue-red');
    const hueGreenSlider = document.getElementById('hue-green');
    const hueBlueSlider = document.getElementById('hue-blue');
    const brightnessValue = document.getElementById('brightness-value');
    const contrastValue = document.getElementById('contrast-value');
    const saturationValue = document.getElementById('saturation-value');
    const shadowsValue = document.getElementById('shadows-value');
    const highlightsValue = document.getElementById('highlights-value');
    const resetBtn = document.getElementById('reset-btn');
    const applyBtn = document.getElementById('apply-btn');
    const downloadBtn = document.getElementById('download-btn');
    const loadingOverlay = document.getElementById('loading-overlay');
    
    // RGBカーブ用のキャンバス
    const redCurveCanvas = document.getElementById('red-curve');
    const greenCurveCanvas = document.getElementById('green-curve');
    const blueCurveCanvas = document.getElementById('blue-curve');
    
    // カーブ制御用の変数
    let redCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
    let greenCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
    let blueCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
    
    let camanInstance = null;
    let originalImageData = null;
    let currentFilters = {};
    
    // 画像アップロードの処理
    imageUpload.addEventListener('change', function(e) {
        const file = e.target.files[0];
        if (!file) return;
        
        showLoading();
        
        const reader = new FileReader();
        reader.onload = function(event) {
            const img = new Image();
            img.onload = function() {
                // キャンバスのサイズを画像に合わせる
                const maxWidth = 800;
                const maxHeight = 600;
                let width = img.width;
                let height = img.height;
                
                if (width > maxWidth) {
                    height = (maxWidth / width) * height;
                    width = maxWidth;
                }
                
                if (height > maxHeight) {
                    width = (maxHeight / height) * width;
                    height = maxHeight;
                }
                
                imageCanvas.width = width;
                imageCanvas.height = height;
                
                // CamanJSで画像を読み込む
                Caman(imageCanvas, function() {
                    this.revert(false);
                    this.render();
                    camanInstance = this;
                    originalImageData = this.canvas.toDataURL();
                    
                    // RGBカーブを初期化
                    initCurves();
                    hideLoading();
                });
                
                const ctx = imageCanvas.getContext('2d');
                ctx.drawImage(img, 0, 0, width, height);
            };
            img.src = event.target.result;
        };
        reader.readAsDataURL(file);
    });
    
    // スライダーイベントの設定
    function setupSlider(slider, valueElement) {
        slider.addEventListener('input', function() {
            valueElement.textContent = this.value;
        });
    }
    
    setupSlider(brightnessSlider, brightnessValue);
    setupSlider(contrastSlider, contrastValue);
    setupSlider(saturationSlider, saturationValue);
    setupSlider(shadowsSlider, shadowsValue);
    setupSlider(highlightsSlider, highlightsValue);
    
    // 適用ボタンのイベントリスナー
    applyBtn.addEventListener('click', function() {
        if (!camanInstance) return;
        showLoading();
        
        // 非同期で処理を行う
        setTimeout(function() {
            applyFilters();
            hideLoading();
        }, 100);
    });
    
    // フィルターを適用する関数(強化版)
    function applyFilters() {
        if (!camanInstance) return;
        
        camanInstance.revert(false);
        
        // 現在のフィルター設定を保存
        currentFilters = {
            brightness: parseInt(brightnessSlider.value),
            contrast: parseInt(contrastSlider.value),
            saturation: parseInt(saturationSlider.value),
            shadows: parseInt(shadowsSlider.value),
            highlights: parseInt(highlightsSlider.value),
            hueRed: parseInt(hueRedSlider.value),
            hueGreen: parseInt(hueGreenSlider.value),
            hueBlue: parseInt(hueBlueSlider.value)
        };
        
        // 明るさ(範囲拡大: -100 to 100 → -150 to 150)
        camanInstance.brightness(currentFilters.brightness * 1.5);
        
        // コントラスト(効果を強化)
        camanInstance.contrast(currentFilters.contrast * 1.5);
        
        // 彩度
        camanInstance.saturation(currentFilters.saturation);
        
        // シャドウ(効果を強化、範囲拡大)
        if (currentFilters.shadows > 0) {
            camanInstance.shadows(currentFilters.shadows * 2);
        } else {
            camanInstance.exposure(currentFilters.shadows / 5);
        }
        
        // ハイライト(効果を強化、範囲拡大)
        if (currentFilters.highlights > 0) {
            camanInstance.highlights(currentFilters.highlights * 2);
        } else {
            camanInstance.exposure(currentFilters.highlights / 5);
        }
        
        // HSL調整
        camanInstance.hue(currentFilters.hueRed / 2);
        camanInstance.hue(currentFilters.hueGreen / 2);
        camanInstance.hue(currentFilters.hueBlue / 2);
        
        // RGBカーブを適用
        applyRgbCurves();
        
        // 追加フィルター: シャープネス
        if (currentFilters.sharpness) {
            camanInstance.sharpen(currentFilters.sharpness);
        }
        
        // 追加フィルター: ノイズ
        if (currentFilters.noise) {
            camanInstance.noise(currentFilters.noise);
        }
        
        // 追加フィルター: ビネット
        if (currentFilters.vignette) {
            camanInstance.vignette("10%", currentFilters.vignette * 2);
        }
        
        camanInstance.render();
    }
    
    // RGBカーブを初期化
    function initCurves() {
        drawCurve(redCurveCanvas, redCurvePoints, 'red');
        drawCurve(greenCurveCanvas, greenCurvePoints, 'green');
        drawCurve(blueCurveCanvas, blueCurvePoints, 'blue');
        
        setupCurveInteraction(redCurveCanvas, redCurvePoints, 'red');
        setupCurveInteraction(greenCurveCanvas, greenCurvePoints, 'green');
        setupCurveInteraction(blueCurveCanvas, blueCurvePoints, 'blue');
    }
    
    // カーブを描画
    function drawCurve(canvas, points, color) {
        const ctx = canvas.getContext('2d');
        const width = canvas.width;
        const height = canvas.height;
        
        // クリア
        ctx.clearRect(0, 0, width, height);
        
        // グリッドを描画
        ctx.strokeStyle = '#eee';
        ctx.lineWidth = 1;
        
        // 水平線
        for (let y = 0; y <= height; y += height / 4) {
            ctx.beginPath();
            ctx.moveTo(0, y);
            ctx.lineTo(width, y);
            ctx.stroke();
        }
        
        // 垂直線
        for (let x = 0; x <= width; x += width / 4) {
            ctx.beginPath();
            ctx.moveTo(x, 0);
            ctx.lineTo(x, height);
            ctx.stroke();
        }
        
        // 対角線
        ctx.strokeStyle = '#ccc';
        ctx.beginPath();
        ctx.moveTo(0, height);
        ctx.lineTo(width, 0);
        ctx.stroke();
        
        // カーブを描画
        ctx.strokeStyle = color;
        ctx.lineWidth = 2;
        ctx.beginPath();
        
        // 点をソート
        points.sort((a, b) => a.x - b.x);
        
        // 最初の点
        const firstPoint = points[0];
        ctx.moveTo(firstPoint.x / 255 * width, (255 - firstPoint.y) / 255 * height);
        
        // 中間の点
        for (let i = 1; i < points.length; i++) {
            const point = points[i];
            ctx.lineTo(point.x / 255 * width, (255 - point.y) / 255 * height);
        }
        
        ctx.stroke();
        
        // 制御点を描画
        ctx.fillStyle = color;
        points.forEach(point => {
            const x = point.x / 255 * width;
            const y = (255 - point.y) / 255 * height;
            ctx.beginPath();
            ctx.arc(x, y, 5, 0, Math.PI * 2);
            ctx.fill();
        });
    }
    
    // カーブのインタラクションを設定
    function setupCurveInteraction(canvas, points, color) {
        let isDragging = false;
        let draggedPoint = null;
        
        canvas.addEventListener('mousedown', function(e) {
            const rect = canvas.getBoundingClientRect();
            const x = (e.clientX - rect.left) / rect.width * 255;
            const y = 255 - (e.clientY - rect.top) / rect.height * 255;
            
            // 既存の点をチェック
            for (let i = 0; i < points.length; i++) {
                const point = points[i];
                const distance = Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
                
                if (distance < 15) {
                    isDragging = true;
                    draggedPoint = point;
                    return;
                }
            }
            
            // 新しい点を追加
            if (x > 0 && x < 255 && y > 0 && y < 255) {
                points.push({x, y});
                points.sort((a, b) => a.x - b.x);
                isDragging = true;
                draggedPoint = {x, y};
            }
            
            drawCurve(canvas, points, color);
        });
        
        canvas.addEventListener('mousemove', function(e) {
            if (!isDragging || !draggedPoint) return;
            
            const rect = canvas.getBoundingClientRect();
            let x = (e.clientX - rect.left) / rect.width * 255;
            let y = 255 - (e.clientY - rect.top) / rect.height * 255;
            
            // 境界チェック
            x = Math.max(0, Math.min(255, x));
            y = Math.max(0, Math.min(255, y));
            
            // 最初と最後の点はx座標を固定
            if (points.indexOf(draggedPoint) === 0) {
                x = 0;
            } else if (points.indexOf(draggedPoint) === points.length - 1) {
                x = 255;
            }
            
            draggedPoint.x = x;
            draggedPoint.y = y;
            
            drawCurve(canvas, points, color);
        });
        
        canvas.addEventListener('mouseup', function() {
            isDragging = false;
            draggedPoint = null;
        });
        
        canvas.addEventListener('mouseleave', function() {
            isDragging = false;
            draggedPoint = null;
        });
        
        // ポイントを削除するダブルクリック
        canvas.addEventListener('dblclick', function(e) {
            if (points.length <= 2) return;
            
            const rect = canvas.getBoundingClientRect();
            const x = (e.clientX - rect.left) / rect.width * 255;
            const y = 255 - (e.clientY - rect.top) / rect.height * 255;
            
            for (let i = 1; i < points.length - 1; i++) {
                const point = points[i];
                const distance = Math.sqrt(Math.pow(point.x - x, 2) + Math.pow(point.y - y, 2));
                
                if (distance < 15) {
                    points.splice(i, 1);
                    drawCurve(canvas, points, color);
                    return;
                }
            }
        });
    }
    
    // RGBカーブを適用
    function applyRgbCurves() {
        if (!camanInstance) return;
        
        // カーブデータを準備
        const redCurve = createCurveData(redCurvePoints);
        const greenCurve = createCurveData(greenCurvePoints);
        const blueCurve = createCurveData(blueCurvePoints);
        
        // カーブを適用
        camanInstance.process("rgbCurve", function(rgba) {
            rgba.r = redCurve[rgba.r];
            rgba.g = greenCurve[rgba.g];
            rgba.b = blueCurve[rgba.b];
            return rgba;
        });
    }
    
    // カーブデータを作成
    function createCurveData(points) {
        points.sort((a, b) => a.x - b.x);
        const curve = new Array(256);
        
        for (let i = 0; i < points.length - 1; i++) {
            const start = points[i];
            const end = points[i + 1];
            
            const x1 = Math.round(start.x);
            const y1 = Math.round(start.y);
            const x2 = Math.round(end.x);
            const y2 = Math.round(end.y);
            
            // 線形補間
            for (let x = x1; x <= x2; x++) {
                const t = (x - x1) / (x2 - x1);
                curve[x] = Math.round(y1 + t * (y2 - y1));
            }
        }
        
        return curve;
    }
    
    // リセットボタン
    resetBtn.addEventListener('click', function() {
        if (!camanInstance) return;
        
        showLoading();
        
        // スライダーをリセット
        brightnessSlider.value = 0;
        contrastSlider.value = 0;
        saturationSlider.value = 0;
        shadowsSlider.value = 0;
        highlightsSlider.value = 0;
        hueRedSlider.value = 0;
        hueGreenSlider.value = 0;
        hueBlueSlider.value = 0;
        
        brightnessValue.textContent = '0';
        contrastValue.textContent = '0';
        saturationValue.textContent = '0';
        shadowsValue.textContent = '0';
        highlightsValue.textContent = '0';
        
        // カーブをリセット
        redCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
        greenCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
        blueCurvePoints = [{x: 0, y: 0}, {x: 255, y: 255}];
        
        drawCurve(redCurveCanvas, redCurvePoints, 'red');
        drawCurve(greenCurveCanvas, greenCurvePoints, 'green');
        drawCurve(blueCurveCanvas, blueCurvePoints, 'blue');
        
        // 画像をリセット
        const img = new Image();
        img.onload = function() {
            const ctx = imageCanvas.getContext('2d');
            ctx.drawImage(img, 0, 0, imageCanvas.width, imageCanvas.height);
            
            Caman(imageCanvas, function() {
                this.revert(false);
                this.render();
                camanInstance = this;
                hideLoading();
            });
        };
        img.src = originalImageData;
        
        // フィルター設定をリセット
        currentFilters = {};
    });
    
    // ダウンロードボタン
    downloadBtn.addEventListener('click', function() {
        if (!camanInstance) return;
        
        const link = document.createElement('a');
        link.download = 'edited-image.png';
        link.href = imageCanvas.toDataURL('image/png');
        link.click();
    });
    
    // 読み込み表示を制御する関数
    function showLoading() {
        loadingOverlay.style.display = 'flex';
    }
    
    function hideLoading() {
        loadingOverlay.style.display = 'none';
    }
    
    // 追加機能: キーボードショートカット
    document.addEventListener('keydown', function(e) {
        if (e.ctrlKey && e.key === 'z') {
            resetBtn.click();
        } else if (e.ctrlKey && e.key === 's') {
            downloadBtn.click();
            e.preventDefault();
        } else if (e.key === 'Enter') {
            applyBtn.click();
        }
    });
    
    // 追加機能: プリセットフィルター
    function applyPreset(presetName) {
        switch(presetName) {
            case 'vintage':
                brightnessSlider.value = 10;
                contrastSlider.value = 20;
                saturationSlider.value = -30;
                shadowsSlider.value = 15;
                highlightsSlider.value = -10;
                hueRedSlider.value = 10;
                break;
            case 'dramatic':
                brightnessSlider.value = -10;
                contrastSlider.value = 50;
                saturationSlider.value = -20;
                shadowsSlider.value = 30;
                highlightsSlider.value = -20;
                break;
            case 'bright':
                brightnessSlider.value = 30;
                contrastSlider.value = 20;
                saturationSlider.value = 40;
                shadowsSlider.value = 20;
                highlightsSlider.value = 10;
                break;
        }
        
        // スライダー値を更新
        brightnessValue.textContent = brightnessSlider.value;
        contrastValue.textContent = contrastSlider.value;
        saturationValue.textContent = saturationSlider.value;
        shadowsValue.textContent = shadowsSlider.value;
        highlightsValue.textContent = highlightsSlider.value;
        
        applyBtn.click();
    }
});