forked from imazen/imageflow-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imageflow.go
376 lines (322 loc) · 10.4 KB
/
imageflow.go
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
// Package imageflow is a fast image processing library
package imageflow
import (
"encoding/json"
)
// Steps is the builder for creating a operation
type Steps struct {
inputs []ioOperation
outputs []ioOperation
vertex []interface{}
last uint
innerGraph graph
ioID int
}
// Decode is used to import a image
func (steps *Steps) Decode(task ioOperation) *Steps {
steps.inputs = append(steps.inputs, task)
task.setIo(uint(steps.ioID))
steps.vertex = append(steps.vertex, decode{
IoID: steps.ioID,
}.toStep())
steps.ioID++
steps.last = uint(len(steps.vertex) - 1)
return steps
}
// ConstrainWithin is used to constraint a image
func (steps *Steps) ConstrainWithin(w float64, h float64) *Steps {
steps.input(constrainWithinMap(w, h))
return steps
}
// ConstrainWithinH is used to constraint a image
func (steps *Steps) ConstrainWithinH(h float64) *Steps {
steps.input(constrainWithinMap(nil, h))
return steps
}
// ConstrainWithinW is used to constraint a image
func (steps *Steps) ConstrainWithinW(w float64) *Steps {
steps.input(constrainWithinMap(w, nil))
return steps
}
func constrainWithinMap(w interface{}, h interface{}) map[string]interface{} {
constrainMap := make(map[string]interface{})
dataMap := make(map[string]interface{})
dataMap["mode"] = "within"
if w != nil {
dataMap["w"] = w
}
if h != nil {
dataMap["h"] = h
}
constrainMap["constrain"] = dataMap
return constrainMap
}
// Encode is used to convert the image
func (steps *Steps) Encode(task ioOperation, preset presetInterface) *Steps {
task.setIo(uint(steps.ioID))
steps.outputs = append(steps.outputs, task)
steps.input(encode{
IoID: steps.ioID,
Preset: preset.toPreset(),
}.toStep())
steps.ioID++
return steps
}
// Rotate90 is to used to rotate by 90 degrees
func (steps *Steps) Rotate90() *Steps {
rotate := rotate90{}
steps.input(rotate.toStep())
return steps
}
// Rotate180 is to used to rotate by 180 degrees
func (steps *Steps) Rotate180() *Steps {
rotate := rotate180{}
steps.input(rotate.toStep())
return steps
}
// Rotate270 is to used to rotate by 270 degrees
func (steps *Steps) Rotate270() *Steps {
rotate := rotate270{}
steps.input(rotate.toStep())
return steps
}
// FlipH is to used to flip image horizontally
func (steps *Steps) FlipH() *Steps {
rotate := flipH{}
steps.input(rotate.toStep())
return steps
}
// FlipV is to used to flip image horizontally
func (steps *Steps) FlipV() *Steps {
rotate := flipV{}
steps.input(rotate.toStep())
return steps
}
func (steps *Steps) input(step interface{}) {
steps.vertex = append(steps.vertex, step)
steps.innerGraph.AddEdge(steps.last, uint(len(steps.vertex)-1), "input")
steps.last = uint(len(steps.vertex) - 1)
}
func (steps *Steps) canvas(f func(*Steps), step stepInterface) *Steps {
last := steps.last
f(steps)
steps.vertex = append(steps.vertex, step.toStep())
steps.innerGraph.AddEdge(last, uint(len(steps.vertex)-1), "input")
steps.innerGraph.AddEdge(steps.last, uint(len(steps.vertex)-1), "canvas")
steps.last = uint(len(steps.vertex) - 1)
return steps
}
// CopyRectangle copy a image
func (steps *Steps) CopyRectangle(f func(steps *Steps), rect RectangleToCanvas) *Steps {
return steps.canvas(f, rect)
}
// DrawExact copy a image
func (steps *Steps) DrawExact(f func(steps *Steps), rect DrawExact) *Steps {
return steps.canvas(f, rect)
}
// Execute the graph
func (steps *Steps) Execute() (map[string][]byte, error) {
jsonMap := make(map[string]interface{})
graphMap := make(map[string]interface{})
nodeMap := make(map[int]interface{})
for i := 0; i < len(steps.vertex); i++ {
nodeMap[i] = steps.vertex[i]
}
frameWise := make(map[string]interface{})
graphMap["nodes"] = nodeMap
graphMap["edges"] = steps.innerGraph.edges
frameWise["graph"] = graphMap
jsonMap["framewise"] = frameWise
js, _ := json.Marshal(jsonMap)
job := newJob()
for i := 0; i < len(steps.inputs); i++ {
data, errorInBuffer := steps.inputs[i].toBuffer()
if errorInBuffer != nil {
return nil, errorInBuffer
}
errorInInput := job.AddInput(steps.inputs[i].getIo(), data)
if errorInInput != nil {
return nil, errorInInput
}
}
for i := 0; i < len(steps.outputs); i++ {
errorInOutput := job.AddOutput(steps.outputs[i].getIo())
if errorInOutput != nil {
return nil, errorInOutput
}
}
errorInMessage := job.Message(js)
if errorInMessage != nil {
return nil, errorInMessage
}
bufferMap := make(map[string][]byte)
for i := 0; i < len(steps.outputs); i++ {
data, errorInOutput := job.GetOutput(steps.outputs[i].getIo())
if errorInOutput != nil {
return nil, errorInOutput
}
bufferMap = steps.outputs[i].toOutput(data, bufferMap)
}
return bufferMap, nil
}
// Branch create a alternate path for the output
func (steps *Steps) Branch(f func(*Steps)) *Steps {
last := steps.last
f(steps)
steps.last = last
return steps
}
// Region is used to crop or add padding to image
func (steps *Steps) Region(region Region) *Steps {
steps.input(region.toStep())
return steps
}
// RegionPercentage is used to crop or add padding to image using percentage
func (steps *Steps) RegionPercentage(region RegionPercentage) *Steps {
steps.input(region.toStep())
return steps
}
// CropWhitespace is used to remove whitespace around the image
func (steps *Steps) CropWhitespace(threshold int, padding float64) *Steps {
steps.input(cropWhitespace{Threshold: threshold, PercentagePadding: padding}.toStep())
return steps
}
// FillRect is used create a rectangle on the image
func (steps *Steps) FillRect(x1 float64, y1 float64, x2 float64, y2 float64, color Color) *Steps {
steps.input(fillRect{X1: x1, Y1: y1, X2: x2, Y2: y2, Color: color}.toStep())
return steps
}
// ExpandCanvas is used create a rectangle on the image
func (steps *Steps) ExpandCanvas(canvas ExpandCanvas) *Steps {
steps.input(canvas.toStep())
return steps
}
// Watermark is used to watermark a image
func (steps *Steps) Watermark(data ioOperation, gravity interface{}, fitMode string, fitBox FitBox, opacity float32, hint interface{}) *Steps {
data.setIo(uint(steps.ioID))
steps.inputs = append(steps.inputs, data)
steps.input(watermark{
IoID: uint(steps.ioID),
Gravity: gravity,
FitMode: fitMode,
FitBox: fitBox,
}.toStep())
steps.ioID++
return steps
}
// Command is used to execute query like strings
func (steps *Steps) Command(cmd string) *Steps {
cmdMap := make(map[string]map[string]string)
dataMap := make(map[string]string)
dataMap["kind"] = "ir4"
dataMap["value"] = cmd
cmdMap["command_string"] = dataMap
steps.input(cmdMap)
return steps
}
// WhiteBalanceSRGB histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) WhiteBalanceSRGB(threshold float32) *Steps {
steps.input(doubleMap("white_balance_histogram_area_threshold_srgb", "threshold", threshold))
return steps
}
// GrayscaleNTSC histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) GrayscaleNTSC() *Steps {
return steps.colorFilterSRGB("grayscale_ntsc")
}
// GrayscaleFlat histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) GrayscaleFlat() *Steps {
return steps.colorFilterSRGB("grayscale_flat")
}
// GrayscaleBT709 histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) GrayscaleBT709() *Steps {
return steps.colorFilterSRGB("grayscale_bt709")
}
// GrayscaleRY histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) GrayscaleRY() *Steps {
return steps.colorFilterSRGB("grayscale_ry")
}
// Sepia histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Sepia() *Steps {
return steps.colorFilterSRGB("sepia")
}
// Invert histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Invert() *Steps {
return steps.colorFilterSRGB("invert")
}
func (steps *Steps) colorFilterSRGB(value string) *Steps {
steps.input(singleMap("color_filter_srgb", value))
return steps
}
// Alpha histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Alpha(value float32) *Steps {
return steps.colorFilterSRGBValue("alpha", value)
}
// Contrast histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Contrast(value float32) *Steps {
return steps.colorFilterSRGBValue("contrast", value)
}
// Brightness histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Brightness(value float32) *Steps {
return steps.colorFilterSRGBValue("brightness", value)
}
// Saturation histogram area
// This command is not recommended as it operates in the sRGB space and does not produce perfect results.
func (steps *Steps) Saturation(value float32) *Steps {
return steps.colorFilterSRGBValue("saturation", value)
}
// PNG encodes to a png
func (steps *Steps) PNG(operation ioOperation) *Steps {
return steps.Encode(operation, LosslessPNG{})
}
// JPEG encodes to a jpeg
func (steps *Steps) JPEG(operation ioOperation) *Steps {
return steps.Encode(operation, MozJPEG{})
}
// WebP encodes to a webp
func (steps *Steps) WebP(operation ioOperation) *Steps {
return steps.Encode(operation, WebPLossless{})
}
// GIF encodes to a gif
func (steps *Steps) GIF(operation ioOperation) *Steps {
return steps.Encode(operation, GIF{})
}
func (steps *Steps) colorFilterSRGBValue(name string, value float32) *Steps {
steps.input(doubleMap("color_filter_srgb", name, value))
return steps
}
// Step specify different nodes
type stepInterface interface {
toStep() interface{}
}
type edge struct {
Kind string `json:"kind"`
To uint `json:"to"`
From uint `json:"from"`
}
type graph struct {
edges []edge
}
func (gr *graph) AddEdge(from uint, to uint, kind string) {
gr.edges = append(gr.edges, edge{To: to, Kind: kind, From: from})
}
// NewStep creates a step that can be used to specify how graph should be processed
func NewStep() Steps {
return Steps{
vertex: []interface{}{},
last: 0,
ioID: 0,
innerGraph: graph{
edges: []edge{},
},
}
}