forked from coderand/photoshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
waveletDecompose.jsx
179 lines (146 loc) · 5.56 KB
/
waveletDecompose.jsx
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
// https://github.com/coderand/photoshop/waveletDecompose.jsx
// Wavelet decompose for Photoshop.
// Inspired by GIMP's Wavelet plug-in.
// Created by Dmitry "AND" Andreev 2016.
// License Creative Commons Zero v1.0 Universal.
#target photoshop
app;
function openUrl( url ) {
if ( $.os.indexOf( "Windows" ) != -1 ) {
// Windows
app.system( "start " + url );
} else {
// Mac
system.callSystem( "open " + url );
}
}
function addBrightnessContrastLayer() {
var idMk = charIDToTypeID( "Mk " );
var desc2 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref1 = new ActionReference();
var idAdjL = charIDToTypeID( "AdjL" );
ref1.putClass( idAdjL );
desc2.putReference( idnull, ref1 );
var idUsng = charIDToTypeID( "Usng" );
var desc3 = new ActionDescriptor();
var idType = charIDToTypeID( "Type" );
var desc4 = new ActionDescriptor();
var iduseLegacy = stringIDToTypeID( "useLegacy" );
desc4.putBoolean( iduseLegacy, true );
var idBrgC = charIDToTypeID( "BrgC" );
desc3.putObject( idType, idBrgC, desc4 );
var idAdjL = charIDToTypeID( "AdjL" );
desc2.putObject( idUsng, idAdjL, desc3 );
executeAction( idMk, desc2, DialogModes.NO );
var idDlt = charIDToTypeID( "Dlt " );
var desc5 = new ActionDescriptor();
var idnull = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var idChnl = charIDToTypeID( "Chnl" );
var idOrdn = charIDToTypeID( "Ordn" );
var idTrgt = charIDToTypeID( "Trgt" );
ref2.putEnumerated( idChnl, idOrdn, idTrgt );
desc5.putReference( idnull, ref2 );
executeAction( idDlt, desc5, DialogModes.NO );
}
function generateDocWavelets( waveletDoc, levelCount, addExtraControls ) {
waveletDoc.paste();
waveletDoc.activeLayer.name = "Original";
var original = waveletDoc.activeLayer;
for ( var i = 0; i < levelCount; i++ ) {
var src = waveletDoc.activeLayer;
var octave1 = original.duplicate()
octave1.move( src, ElementPlacement.PLACEBEFORE );
octave1.applyGaussianBlur( Math.pow( 2.0, i ) - 0.5 );
octave1.name = "Blurred " + String(i + 1);
var octave1_blr = octave1.duplicate();
octave1_blr.visible = false;
octave1.mixChannels( [[50, 0, 0, 0], [0, 50, 0, 0], [0, 0, 50, 0]], false );
var src2 = src.duplicate();
src2.move( src, ElementPlacement.PLACEBEFORE );
src2.mixChannels( [[50, 0, 0, 50], [0, 50, 0, 50], [0, 0, 50, 50]], false );
octave1.move( src2, ElementPlacement.PLACEBEFORE );
octave1.blendMode = BlendMode.SUBTRACT;
waveletDoc.selection.selectAll();
waveletDoc.selection.copy( true );
var res = waveletDoc.paste();
res.name = "Scale " + String(i + 1);
res.move( src2, ElementPlacement.PLACEBEFORE );
res.blendMode = BlendMode.LINEARLIGHT;
src2.remove();
octave1.remove();
if ( i > 0 ) {
src.remove();
}
octave1_blr.visible = true;
waveletDoc.activeLayer = octave1_blr;
}
var residual = waveletDoc.activeLayer;
residual.name = "Residual";
var group = app.activeDocument.layerSets.add();
group.name = "Wavelet";
if ( addExtraControls ) {
app.activeDocument.artLayers[ 0 ].move( group, ElementPlacement.INSIDE );
for ( var i = 0; i < levelCount; i++ ) {
var groupScale = group.layerSets.add();
groupScale.blendMode = BlendMode.LINEARLIGHT;
groupScale.name = "Scale " + String(levelCount - i);
app.activeDocument.artLayers[ 0 ].move( groupScale, ElementPlacement.INSIDE );
addBrightnessContrastLayer();
waveletDoc.activeLayer.name = "Adjust";
}
} else {
for ( var i = 0; i <= levelCount; i++ ) {
app.activeDocument.artLayers[ 0 ].move( group, ElementPlacement.INSIDE );
}
}
}
function generateWavelets( levelCount, addExtraControls ) {
levelCount = parseInt( levelCount, 10 );
var activeLayer = app.activeDocument.activeLayer;
app.activeDocument.selection.selectAll();
try { app.activeDocument.selection.copy( true ); }
catch (e) {
try { app.activeDocument.selection.copy(); }
catch (e) { alert(e); }
}
var waveletDoc = app.documents.add(
app.activeDocument.width,
app.activeDocument.height,
app.activeDocument.resolution,
app.activeDocument.name + "-" + activeLayer.name + "-Wavelet",
NewDocumentMode.RGB, DocumentFill.TRANSPARENT,
app.activeDocument.pixelAspectRatio,
BitsPerChannelType.SIXTEEN
);
try { app.activeDocument.suspendHistory("Wavelet", "generateDocWavelets( waveletDoc, levelCount, addExtraControls )") }
catch(e) { alert(e); }
}
function showDialog() {
var dlg = new Window( "dialog", "Wavelet decompose for Photoshop" );
dlg.main = dlg.add( "group", undefined, );
dlg.group = dlg.main.add( "panel", undefined );
dlg.group.alignment = "top";
dlg.group.orientation = "column";
dlg.group3 = dlg.group.add( "group", undefined, );
dlg.group3.orientation = "row";
dlg.panel_text = dlg.group3.add( "statictext", undefined, "Number of wavelet detail scales:" );
dlg.panel_list = dlg.group3.add( "dropdownlist", undefined, ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"] );
dlg.panel_list.selection = 4;
dlg.panel_chk_extra = dlg.group.add( "checkbox", undefined, "Add extra Brightness/Contrast controls" );
dlg.group2 = dlg.main.add( "group", undefined, );
dlg.group2.orientation = "column";
dlg.btn_ok = dlg.group2.add( "button", undefined, "OK", {name:'ok'} );
dlg.btn_cancel = dlg.group2.add( "button", undefined, "Cancel", {name:"close"} );
dlg.btn_help = dlg.group2.add( "button", undefined, "Help", {name:"help"} );
dlg.btn_ok.onClick = function() {
dlg.close();
generateWavelets( dlg.panel_list.selection + 1, dlg.panel_chk_extra.value );
}
dlg.btn_help.onClick = function() {
openUrl( "http://registry.gimp.org/node/11742" );
}
dlg.show();
}
showDialog();