-
Notifications
You must be signed in to change notification settings - Fork 0
/
fi.lua
470 lines (409 loc) · 11.4 KB
/
fi.lua
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
-- The file is in public domain
-- nyfair ([email protected])
local ffi = require 'ffi'
local filua = ffi.load('freeimage')
ffi.cdef[[
typedef struct { uint8_t b, g, r, a; } RGBA;
void* FreeImage_Load(int, const char*, int);
void* FreeImage_LoadU(int, const char*, int);
int FreeImage_Save(int, void*, const char*, int);
int FreeImage_SaveU(int, void*, const char*, int);
void* FreeImage_Clone(void*);
void FreeImage_Unload(void*);
void* FreeImage_EnlargeCanvas(void*, int, int, int, int, RGBA*, int);
void* FreeImage_Allocate(int, int, int, unsigned, unsigned, unsigned);
unsigned FreeImage_GetBPP(void*);
unsigned FreeImage_GetWidth(void*);
unsigned FreeImage_GetHeight(void*);
unsigned FreeImage_GetDotsPerMeterX(void*);
unsigned FreeImage_GetDotsPerMeterY(void*);
void FreeImage_SetDotsPerMeterX(void*, unsigned);
void FreeImage_SetDotsPerMeterY(void*, unsigned);
int FreeImage_GetFIFFromFilename(const char*);
int FreeImage_GetFIFFromFilenameU(const char*);
int FreeImage_GetFileType(const char*, int);
int FreeImage_GetFileTypeU(const char*, int);
RGBA* FreeImage_GetPalette(void*);
void* FreeImage_SetTransparentIndex(void*, int);
void* FreeImage_ConvertToGreyscale(void*);
void* FreeImage_ConvertTo24Bits(void*);
void* FreeImage_ConvertTo32Bits(void*);
void* FreeImage_ConvertFromRawBitsEx(int, const char*, int, int, int, int, unsigned, unsigned, unsigned, unsigned, int);
void FreeImage_ConvertToRawBits(char*, void*, int, unsigned, unsigned, unsigned, unsigned, int);
void* FreeImage_Rotate(void*, double, RGBA*);
int FreeImage_FlipHorizontal(void*);
int FreeImage_FlipVertical(void*);
void* FreeImage_Rescale(void*, int, int, int);
int FreeImage_JPEGTransform(const char*, const char*, int, int);
int FreeImage_JPEGTransformU(const char*, const char*, int, int);
int FreeImage_JPEGCrop(const char *, const char*, int, int, int, int);
int FreeImage_JPEGCropU(const char *, const char*, int, int, int, int);
void* FreeImage_Copy(void*, int, int, int, int);
int FreeImage_Paste(void*, void*, int, int, int);
void* FreeImage_CreateView(void*, int, int, int, int);
void* FreeImage_Composite(void*, int, RGBA*, void*);
int FreeImage_GetPixelColor(void*, unsigned, unsigned, RGBA*);
int FreeImage_SetPixelColor(void*, unsigned, unsigned, RGBA*);
void* FreeImage_GetChannel(void*, int);
int FreeImage_SetChannel(void*, void*, int);
int FreeImage_SwapColors(void*, RGBA*, RGBA*, int);
int FreeImage_Invert(void*);
]]
-- helper
if ffi.os == 'Windows' then
require 'fswin'
function getfmt(name)
local fmt = filua.FreeImage_GetFIFFromFilenameU(u2w(name))
if fmt > -1 then
return fmt
else
return filua.FreeImage_GetFileTypeU(u2w(name), 0)
end
end
function open(name, flag)
local fmt = getfmt(name)
return filua.FreeImage_LoadU(fmt, u2w(name), flag or 0)
end
function save(img, name, flag)
local fmt = getfmt(name)
return filua.FreeImage_SaveU(fmt, img, u2w(name), flag or 0)
end
function jpgcrop(src, left, top, right, bottom, dst)
if dst == nil then
dst = stripext(src)..'_crop.jpg'
end
filua.FreeImage_JPEGCropU(u2w(src), u2w(dst), left, top, right, bottom)
end
function jpgtran(src, func, dst, perfect)
if dst == nil then
dst = stripext(src)..'_tran.jpg'
end
filua.FreeImage_JPEGTransformU(u2w(src), u2w(dst), func, perfect or 0)
end
else
require 'fsposix'
function getfmt(name)
local fmt = filua.FreeImage_GetFIFFromFilename(name)
if fmt > -1 then
return fmt
else
return filua.FreeImage_GetFileType(name, 0)
end
end
function open(name, flag)
local fmt = getfmt(name)
return filua.FreeImage_Load(fmt, name, flag or 0)
end
function save(img, name, flag)
local fmt = getfmt(name)
return filua.FreeImage_Save(fmt, img, name, flag or 0)
end
function jpgcrop(src, left, top, right, bottom, dst)
if dst == nil then
dst = stripext(src)..'_crop.jpg'
end
filua.FreeImage_JPEGCrop(src, dst, left, top, right, bottom)
end
function jpgtran(src, func, dst, perfect)
if dst == nil then
dst = stripext(src)..'_tran.jpg'
end
filua.FreeImage_JPEGTransform(src, dst, func, perfect or 0)
end
end
function stripext(fn)
local idx = fn:match('.+()%..+$')
if idx then
return fn:sub(1, idx - 1)
else
return fn
end
end
function clone(img)
return filua.FreeImage_Clone(img)
end
function free(img)
filua.FreeImage_Unload(v)
end
function freeAll(imgs)
for k, v in ipairs(imgs) do
free(v)
end
end
function color(r, g, b, a)
local color = ffi.new('RGBA[?]', 1)
color[0].b= b or 0
color[0].g= g or 0
color[0].r= r or 0
color[0].a= a or 0
return color
end
function enlarge(img, left, top, right, bottom, rgba)
return filua.FreeImage_EnlargeCanvas(img, left, top, right, bottom, rgba or color(), 0)
end
function newimg(width, height, bpp, r, g, b)
return filua.FreeImage_Allocate(width, height, bpp or 24, r or 0, g or 0, b or 0)
end
-- Common Info
function getbpp(img)
return filua.FreeImage_GetBPP(img)
end
function getw(img)
return filua.FreeImage_GetWidth(img)
end
function geth(img)
return filua.FreeImage_GetHeight(img)
end
function getdpi(img)
local x = filua.FreeImage_GetDotsPerMeterX(img)
local y = filua.FreeImage_GetDotsPerMeterY(img)
return math.floor(x*0.0254+0.5), math.floor(y*0.0254+0.5)
end
function setdpi(img, x, y)
filua.FreeImage_SetDotsPerMeterX(img, x/0.0254)
filua.FreeImage_SetDotsPerMeterY(img, y/0.0254)
end
function greyalpha(back, front, channel)
local b
if getbpp(back) == 32 then
b = clone(back)
else
b = to32(back)
end
if getbpp(front) == 8 then
setchannel(b, front, 4)
else
local f = getchannel(front, channel or 2)
invert(f)
setchannel(b, f, 4)
free(f)
end
return b
end
function selfgreyalpha(img)
local l = copy(img, 0, 0, getw(img)/2, geth(img))
local r = copy(img, getw(img)/2, 0, getw(img), geth(img))
local b = greyalpha(l, r)
freeAll({l, r})
return b
end
function coloralpha(img, r, g, b)
local a
local bpp = getbpp(img)
if bpp == 32 or bpp == 8 then
a = clone(img)
else
a = to32(img)
end
bpp = getbpp(a)
if bpp == 32 then
swapcolor(a, color(r,g,b,0), color(r,g,b,255))
else
local c = filua.FreeImage_GetPalette(img)
for i = 0, 255 do
if c[i].r == r and c[i].g == g and c[i].b == b then
filua.FreeImage_SetTransparentIndex(a, i)
break
end
end
end
return a
end
-- Utilities
function copy(img, left, top, right, bottom)
return filua.FreeImage_Copy(img, left, top, right, bottom)
end
function paste(back, front, left, top, alpha)
filua.FreeImage_Paste(back, front, left, top, alpha or 255)
end
function ref(img, left, top, right, bottom)
return filua.FreeImage_CreateView(img, left, top, right, bottom)
end
function composite(back, front)
return filua.FreeImage_Composite(front, 0, nil, back)
end
function to8(src)
return filua.FreeImage_ConvertToGreyscale(src)
end
function to24(src)
return filua.FreeImage_ConvertTo24Bits(src)
end
function to32(src)
return filua.FreeImage_ConvertTo32Bits(src)
end
function rotate(src, degree, rgba)
return filua.FreeImage_Rotate(src, degree, rgba or color())
end
function scale(src, width, height, filter)
return filua.FreeImage_Rescale(src, width, height, filter or 5)
end
function fliph(src)
filua.FreeImage_FlipHorizontal(src)
end
function flipv(src)
filua.FreeImage_FlipVertical(src)
end
function getpixel(img, x, y, rgba)
filua.FreeImage_GetPixelColor(img, x, y, rgba)
end
function setpixel(img, x, y, rgba)
filua.FreeImage_SetPixelColor(img, x, y, rgba)
end
function getchannel(img, channel)
return filua.FreeImage_GetChannel(img, channel)
end
function setchannel(dst, src, channel)
return filua.FreeImage_SetChannel(dst, src, channel)
end
function swapcolor(img, fromcolor, tocolor)
filua.FreeImage_SwapColors(img, fromcolor or color(0,0,0,0), tocolor or color(0,0,0,255), 0)
end
function invert(img)
filua.FreeImage_Invert(img)
end
function fromraw(buf, width, height, pitch, bpp, copy, fliph, rbit, gbit, bbit)
-- copy=0 will clone raw buf, rgb bit will only use in 16bpp image
return filua.FreeImage_ConvertFromRawBitsEx(1, buf, 1, width, height, pitch, bpp, rbit or 5, gbit or 6, bbit or 5, fliph or 1)
end
function toraw(buf, img, pitch, bpp, fliph, rbit, gbit, bbit)
filua.FreeImage_ConvertToRawBits(buf, img, pitch, bpp, rbit or 5, gbit or 6, bbit or 5, fliph or 1)
end
-- File-based process function
function convert(src, dst, flag)
if src:find('*') then
for k,v in ipairs(ls(src)) do
local img = open(v)
save(img, stripext(v)..'.'..dst, flag)
print(v)
free(img)
end
else
local img = open(src)
save(img, dst, flag)
free(img)
end
end
function convbpp(src, bpp, dst, flag)
if bpp==24 or bpp==32 or bpp==8 then
if src:find('*') then
if dst == nil then
dst = 'bmp'
end
for k,v in ipairs(ls(src)) do
local img = open(v)
local out
if bpp == 24 then out = to24(img)
elseif bpp == 32 then out = to32(img)
else out = to8(img)
end
save(out, stripext(v)..'.'..dst, flag)
print(v)
freeAll({img, out})
end
else
if dst == nil then
dst = src
end
local img = open(src)
local out
if bpp == 24 then out = to24(img)
elseif bpp == 32 then out = to32(img)
else out = to8(img)
end
save(out, dst, flag)
freeAll({img, out})
end
end
end
function combine(back, front, dst, left, top, flag)
local img1 = open(back)
local img2 = open(front)
paste(img1, img2, left, top)
save(img1, dst, flag)
freeAll({img1, img2})
end
function combinealpha(back, front, dst, flag)
local img1 = open(back)
local img2 = open(front)
local b
local f
local bpp = getbpp(img1)
if bpp == 32 or bpp == 8 then
b = img1
else
b = to32(img1)
free(img1)
end
bpp = getbpp(img2)
if bpp == 32 or bpp == 8 then
f = img2
else
f = to8(img2)
free(img2)
end
local img3 = composite(b, f)
save(img3, dst, flag)
freeAll({b, f, img3})
end
function splitw(src, num)
local img = open(src)
local width = getw(img) / num
local height = geth(img)
for x = 1, num do
local imgx = newimg(width, height, getbpp(img))
local tmp = ref(img, width*(x-1), 0, width*x, height)
paste(imgx, tmp, 0, 0)
save(imgx, stripext(src)..'_'..tostring(x)..'.bmp')
freeAll({tmp, imgx})
end
free(img)
end
function splith(src, num)
local img = open(src)
local width = getw(img)
local height = geth(img) / num
for y = 1, num do
local imgy = newimg(width, height, getbpp(img))
local tmp = ref(img, 0, height*(y-1), width, height*y)
paste(imgy, tmp, 0, 0)
save(imgy, stripext(src)..'_'..tostring(y)..'.bmp')
freeAll({tmp, imgy})
end
free(img)
end
function mergew(x1, x2, offset)
local img1 = open(x1)
local img2 = open(x2)
local height = geth(img1)
local width = getw(img1) + getw(img2) - offset or 0
local img = newimg(width, height, getbpp(img1))
paste(img, img1, 0, 0)
paste(img, img2, getw(img1) - offset or 0, 0)
save(img, stripext(x1)..'_'..stripext(x2)..'.bmp')
freeAll({img, img1, img2})
end
function mergews(x1, x2, offset)
for k1,v1 in ipairs(ls(x1)) do
for k2,v2 in ipairs(ls(x2)) do
mergew(v1, v2, offset)
end
end
end
function mergeh(y1, y2, offset)
local img1 = open(y1)
local img2 = open(y2)
local height = geth(img1) + geth(img2) - offset or 0
local width = getw(img1)
local img = newimg(width, height, getbpp(img1))
paste(img, img1, 0, 0)
paste(img, img2, 0, geth(img1) - offset or 0)
save(img, stripext(y1)..'_'..stripext(y2)..'.bmp')
freeAll({img, img1, img2})
end
function mergehs(y1, y2, offset)
for k1,v1 in ipairs(ls(y1)) do
for k2,v2 in ipairs(ls(y2)) do
mergeh(v1, v2, offset)
end
end
end