forked from quirkey/magick
-
Notifications
You must be signed in to change notification settings - Fork 1
/
magick.go
563 lines (509 loc) · 19.4 KB
/
magick.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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Package magick implements image manipulation routines based on the
// ImageMagick MagickCore C library. It is an opinionated high level
// wrapper around the proven ImageMagick lib.
//
// magick's goal is to provide quick processing of images in ways most
// commonly used by photo and other image based applications. It is not
// the intention to implement the very large number of methods that
// MagickCore has to offer, rather just the most common needs for
// basic applications. It requires ImageMagick-devel libraries to
// be available in order to compile.
package magick
/*
#cgo pkg-config: MagickCore
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <magick/MagickCore.h>
void SetImageInfoFilename(ImageInfo *image_info, char *filename)
{
(void) CopyMagickString(image_info->filename,filename,MaxTextExtent);
}
MagickBooleanType CheckException(ExceptionInfo *exception)
{
register const ExceptionInfo
*p;
int haserr = 0;
assert(exception != (ExceptionInfo *) NULL);
assert(exception->signature == MagickSignature);
if (exception->exceptions == (void *) NULL)
return MagickFalse;
LockSemaphoreInfo(exception->semaphore);
ResetLinkedListIterator((LinkedListInfo *) exception->exceptions);
p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
exception->exceptions);
while (p != (const ExceptionInfo *) NULL)
{
if ((p->severity >= WarningException) && (p->severity < ErrorException))
haserr = 1;
if ((p->severity >= ErrorException) && (p->severity < FatalErrorException))
haserr = 1;
if (p->severity >= FatalErrorException)
haserr = 1;
p=(const ExceptionInfo *) GetNextValueInLinkedList((LinkedListInfo *)
exception->exceptions);
}
UnlockSemaphoreInfo(exception->semaphore);
return haserr == 0 ? MagickFalse : MagickTrue;
}
MagickBooleanType GetBlobSupport(ImageInfo *image_info)
{
ExceptionInfo *exception;
const MagickInfo *magick_info;
MagickBooleanType supported;
MagickBooleanType err;
exception = AcquireExceptionInfo();
magick_info = GetMagickInfo(image_info->magick,exception);
if (magick_info == (const MagickInfo *) NULL) {
return MagickFalse;
}
err = CheckException(exception);
DestroyExceptionInfo(exception);
if (err == MagickTrue) {
return MagickFalse;
}
supported = GetMagickBlobSupport(magick_info);
return supported;
}
Image *ReadImageFromBlob(ImageInfo *image_info, void *blob, size_t length)
{
Image *image;
ExceptionInfo *exception;
exception = AcquireExceptionInfo();
image_info->blob = blob;
image_info->length = length;
image = ReadImage(image_info, exception);
CatchException(exception);
DestroyExceptionInfo(exception);
return image;
}
Image *AddShadowToImage(Image *image, char *colorname, const double opacity,
const double sigma,const ssize_t x_offset,const ssize_t y_offset,
ExceptionInfo *exception)
{
Image *shadow_image;
if (QueryColorDatabase(colorname, &image->background_color, exception) == MagickFalse) {
return NULL;
}
shadow_image = ShadowImage(image, opacity, sigma, x_offset, y_offset, exception);
AppendImageToList(&shadow_image, image);
if (QueryColorDatabase("none", &shadow_image->background_color, exception) == MagickFalse) {
return NULL;
}
image = MergeImageLayers(shadow_image, MergeLayer, exception);
DestroyImage(shadow_image);
return image;
}
Image *FillBackgroundColor(Image *image, char *colorname, ExceptionInfo *exception)
{
Image *new_image;
new_image = CloneImage(image, 0, 0, MagickTrue, exception);
if (QueryColorDatabase(colorname, &image->background_color, exception) == MagickFalse) {
return NULL;
}
if (SetImageBackgroundColor(image) == MagickFalse) {
return NULL;
}
AppendImageToList(&image, new_image);
image = MergeImageLayers(image, MergeLayer, exception);
DestroyImage(new_image);
return image;
}
Image *SeparateAlphaChannel(Image *image, ExceptionInfo *exception){
Image *new_image;
new_image = CloneImage(image, 0, 0, MagickTrue, exception);
if (SeparateImageChannel(new_image, 0x0008) == MagickFalse){
return NULL;
}
return new_image;
}
Image *Negate(Image *image, ExceptionInfo *exception){
Image *new_image;
new_image = CloneImage(image, 0, 0, MagickTrue, exception);
if (NegateImage(new_image, MagickTrue) == MagickFalse){
return NULL;
}
return new_image;
}
*/
import "C"
import (
"io/ioutil"
"math"
"os"
"strings"
"unsafe"
)
func init() {
wd, err := os.Getwd()
if err != nil {
panic(err)
}
c_wd := C.CString(wd)
C.MagickCoreGenesis(c_wd, C.MagickFalse)
defer C.free(unsafe.Pointer(c_wd))
}
// A wrapper around an IM Image
type MagickImage struct {
Image (*C.Image)
ImageInfo (*C.ImageInfo)
}
// Geometry is usually defined as a string of WxH+X+Y
type MagickGeometry struct {
Width, Height, Xoffset, Yoffset int
}
type MagickError struct {
Severity string
Reason string
Description string
}
type Colorspace C.ColorspaceType
const (
RGB Colorspace = C.RGBColorspace // Red, Green, Blue colorspace.
GRAY Colorspace = C.GRAYColorspace // Similar to Luma (Y) according to ITU-R 601
TRANSPARENT Colorspace = C.TransparentColorspace // RGB which preserves the matte while quantizing colors.
XYZ Colorspace = C.XYZColorspace // CIE XYZ
YCC Colorspace = C.YCCColorspace // Kodak PhotoCD PhotoYCC
YUV Colorspace = C.YUVColorspace // YUV colorspace as used for computer video.
CMYK Colorspace = C.CMYKColorspace // Cyan, Magenta, Yellow, Black colorspace.
SRGB Colorspace = C.sRGBColorspace // Kodak PhotoCD sRGB
HSL Colorspace = C.HSLColorspace // Hue, saturation, luminosity
HWB Colorspace = C.HWBColorspace // Hue, whiteness, blackness
LAB Colorspace = C.LABColorspace // ITU LAB
REC_601_LUMA Colorspace = C.Rec601LumaColorspace // Luma (Y) according to ITU-R 601
REC_601_YCBCR Colorspace = C.Rec601YCbCrColorspace // YCbCr according to ITU-R 601
REC_709_LUMA Colorspace = C.Rec709LumaColorspace // Luma (Y) according to ITU-R 709
REC_709_YCBCR Colorspace = C.Rec709YCbCrColorspace // YCbCr according to ITU-R 709
OHTA Colorspace = C.OHTAColorspace
YIQ Colorspace = C.YIQColorspace
YPBPR Colorspace = C.YPbPrColorspace
)
type ImageType C.ImageType
const (
Bilevel ImageType = C.BilevelType
Grayscale ImageType = C.GrayscaleType
GrayscaleMatte ImageType = C.GrayscaleMatteType
Palette ImageType = C.PaletteType
PaletteMatte ImageType = C.PaletteMatteType
TrueColor ImageType = C.TrueColorType
TrueColorMatte ImageType = C.TrueColorMatteType
ColorSeparation ImageType = C.ColorSeparationType
ColorSeparationMatte ImageType = C.ColorSeparationMatteType
Optimize ImageType = C.OptimizeType
)
func (err *MagickError) Error() string {
return "MagickError " + err.Severity + ": " + err.Reason + "- " + err.Description
}
func ErrorFromExceptionInfo(exception *C.ExceptionInfo) (err error) {
return &MagickError{string(exception.severity), C.GoString(exception.reason), C.GoString(exception.description)}
}
// NewFromFile loads a file at filename into a MagickImage.
// Exceptions are returned as MagickErrors.
func NewFromFile(filename string) (im *MagickImage, err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
info := C.AcquireImageInfo()
c_filename := C.CString(filename)
defer C.free(unsafe.Pointer(c_filename))
C.SetImageInfoFilename(info, c_filename)
image := C.ReadImage(info, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
C.DestroyImageInfo(info)
return nil, ErrorFromExceptionInfo(exception)
}
return &MagickImage{Image: image, ImageInfo: info}, nil
}
// NewFromBlob takes a byte slice of image data and an extension that defines the
// image type (e.g. "png", "jpg", etc). It loads the image data and returns a MagickImage.
// The extension is required so that Magick knows what processor to use.
func NewFromBlob(blob []byte, extension string) (im *MagickImage, err error) {
if len(blob) < 1 {
return nil, &MagickError{"fatal", "", "zero length blob passed to NewFromBlob"}
}
if len(extension) < 1 {
return nil, &MagickError{"fatal", "", "zero length extension passed to NewFromBlob"}
}
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
info := C.AcquireImageInfo()
defer C.DestroyImageInfo(info)
c_filename := C.CString("image." + extension)
defer C.free(unsafe.Pointer(c_filename))
C.SetImageInfoFilename(info, c_filename)
var success (C.MagickBooleanType)
success = C.SetImageInfo(info, 1, exception)
if success != C.MagickTrue {
return nil, ErrorFromExceptionInfo(exception)
}
cloned_info := C.CloneImageInfo(info)
success = C.GetBlobSupport(info)
if success != C.MagickTrue {
// No blob support, lets try reading from a file
file, err := ioutil.TempFile("", "image."+extension)
if _, err = file.Write(blob); err != nil {
return nil, &MagickError{"fatal", "", "image format " + extension + " does not support blobs and could not write temp file"}
}
file.Close()
return NewFromFile(file.Name())
}
length := (C.size_t)(len(blob))
if length == 0 {
return nil, &MagickError{"fatal", "", "empty blob"}
}
blob_start := unsafe.Pointer(&blob[0])
image := C.ReadImageFromBlob(info, blob_start, length)
if image == nil {
return nil, &MagickError{"fatal", "", "corrupt image, not a " + extension}
}
if failed := C.CheckException(exception); failed == C.MagickTrue {
return nil, ErrorFromExceptionInfo(exception)
}
return &MagickImage{Image: image, ImageInfo: cloned_info}, nil
}
// Destroy frees the C memory for the image. Should be called after processing is done.
func (im *MagickImage) Destroy() (err error) {
if im.Image != nil {
C.DestroyImage(im.Image)
}
if im.ImageInfo != nil {
C.DestroyImageInfo(im.ImageInfo)
}
im.Image = nil
im.ImageInfo = nil
return
}
// ReplaceImage Replaces the underlying image, freeing the old one
func (im *MagickImage) ReplaceImage(new_image *C.Image) {
C.DestroyImage(im.Image)
im.Image = new_image
}
// Width returns the Width of the loaded image in pixels as an int
func (im *MagickImage) Width() int {
return (int)(im.Image.columns)
}
// Height returns the Height of the loaded image in pixels as an int
func (im *MagickImage) Height() int {
return (int)(im.Image.rows)
}
// Type returns the underlying encoding or "magick" of the image as a string
func (im *MagickImage) Type() (t string) {
return strings.Trim(string(C.GoBytes(unsafe.Pointer(&im.Image.magick), 4096)), "\x00")
}
// ResizeRatio() returns the ratio that the size you want to resize to
// defined by width/height is over the size of the underlying Image
func (im *MagickImage) ResizeRatio(width, height int) float64 {
return math.Abs((float64)(width*height) / (float64)(im.Width()*im.Height()))
}
// GetProperty() retreives the given attribute or freeform property
// string on the underlying Image
func (im *MagickImage) GetProperty(prop string) (value string) {
c_prop := C.CString(prop)
defer C.free(unsafe.Pointer(c_prop))
c_value := C.GetImageProperty(im.Image, c_prop)
defer C.free(unsafe.Pointer(c_value))
return C.GoString(c_value)
}
// SetProperty() saves the given string value either to specific known
// attribute or to a freeform property string on the underlying Image
func (im *MagickImage) SetProperty(prop, value string) (err error) {
c_prop := C.CString(prop)
defer C.free(unsafe.Pointer(c_prop))
c_value := C.CString(value)
defer C.free(unsafe.Pointer(c_value))
ok := C.SetImageProperty(im.Image, c_prop, c_value)
if ok == C.MagickFalse {
return &MagickError{"error", "", "could not set property"}
}
return
}
// Define() defines image format options
func (im *MagickImage) Define(prop string) (err error) {
c_prop := C.CString(prop)
defer C.free(unsafe.Pointer(c_prop))
ok := C.DefineImageProperty(im.Image, c_prop)
if ok == C.MagickFalse {
return &MagickError{"error", "", "could not define property"}
}
return
}
// Colorspace() allows to set colorspace to image
func (im *MagickImage) Colorspace(cs Colorspace) (err error) {
c_colorspace := C.ColorspaceType(cs)
ok := C.SetImageColorspace(im.Image, c_colorspace)
if ok == C.MagickFalse {
return &MagickError{"error", "", "could not set colorspace"}
}
return
}
func (im *MagickImage) SetImageType(tp ImageType) (err error) {
c_img_type := C.ImageType(tp)
ok := C.SetImageType(im.Image, c_img_type)
if ok == C.MagickFalse {
return &MagickError{"error", "", "could not set image type"}
}
return
}
// ParseGeometryToRectangleInfo converts from a geometry string (WxH+X+Y) into a Magick
// RectangleInfo that contains the individual properties
func (im *MagickImage) ParseGeometryToRectangleInfo(geometry string) (info C.RectangleInfo, err error) {
c_geometry := C.CString(geometry)
defer C.free(unsafe.Pointer(c_geometry))
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
C.ParseRegionGeometry(im.Image, c_geometry, &info, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
err = ErrorFromExceptionInfo(exception)
}
return
}
// ParseGeometry uses ParseGeometryToRectangleInfo to convert from a geometry string into a MagickGeometry
func (im *MagickImage) ParseGeometry(geometry string) (info *MagickGeometry, err error) {
rectangle, err := im.ParseGeometryToRectangleInfo(geometry)
if err != nil {
return nil, err
}
return &MagickGeometry{int(rectangle.width), int(rectangle.height), int(rectangle.x), int(rectangle.y)}, nil
}
// Progessive() is a shortcut for making the underlying image a
// Plane interlaced Progressive JPG
func (im *MagickImage) Progressive() {
im.ImageInfo.interlace = C.PlaneInterlace
}
func (im *MagickImage) Quality(quality int) {
im.Image.quality = (C.size_t)(quality)
}
// Resize resizes the image based on the geometry string passed and stores the resized image in place
// For more info about Geometry see http://www.imagemagick.org/script/command-line-processing.php#geometry
func (im *MagickImage) Resize(geometry string) (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
rect, err := im.ParseGeometryToRectangleInfo(geometry)
if err != nil {
return err
}
ratio := im.ResizeRatio(int(rect.width), int(rect.height))
var new_image *C.Image
if ratio > 0.4 {
new_image = C.AdaptiveResizeImage(im.Image, rect.width, rect.height, exception)
} else {
new_image = C.ThumbnailImage(im.Image, rect.width, rect.height, exception)
}
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// Crop crops the image based on the geometry string passed and stores the cropped image in place
// For more info about Geometry see http://www.imagemagick.org/script/command-line-processing.php#geometry
func (im *MagickImage) Crop(geometry string) (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
rect, err := im.ParseGeometryToRectangleInfo(geometry)
if err != nil {
return err
}
new_image := C.CropImage(im.Image, &rect, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// Shadow adds a dropshadow to the current (transparent) image and stores the shadowed image in place
// For more information about shadow options see: http://www.imagemagick.org/Usage/blur/#shadow
func (im *MagickImage) Shadow(color string, opacity, sigma float32, xoffset, yoffset int) (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
c_opacity := (C.double)(opacity)
c_sigma := (C.double)(sigma)
c_x := (C.ssize_t)(xoffset)
c_y := (C.ssize_t)(yoffset)
c_color := C.CString(color)
defer C.free(unsafe.Pointer(c_color))
new_image := C.AddShadowToImage(im.Image, c_color, c_opacity, c_sigma, c_x, c_y, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// FillBackgroundColor fills transparent areas of an image with a solid color and stores the filled image in place.
// color can be any color format that image magick understands, see: http://www.imagemagick.org/ImageMagick-7.0.0/script/color.php#models
func (im *MagickImage) FillBackgroundColor(color string) (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
c_color := C.CString(color)
defer C.free(unsafe.Pointer(c_color))
new_image := C.FillBackgroundColor(im.Image, c_color, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// SeparateAlphaChannel replaces the Image with grayscale data from the image's Alpha Channel values
func (im *MagickImage) SeparateAlphaChannel() (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
new_image := C.SeparateAlphaChannel(im.Image, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// Negate inverts the colors in the image
func (im *MagickImage) Negate() (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
new_image := C.Negate(im.Image, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
im.ReplaceImage(new_image)
return nil
}
// Strip strips the image of its extra meta (exif) data
func (im *MagickImage) Strip() (err error) {
ok := C.StripImage(im.Image)
if ok == C.MagickFalse {
return &MagickError{"error", "", "could not strip image"}
}
return
}
// ToBlob takes a (transformed) MagickImage and returns a byte slice in the format you specify with extension.
// Magick uses the extension to transform the image in to the proper encoding (e.g. "jpg", "png")
func (im *MagickImage) ToBlob(extension string) (blob []byte, err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
c_outpath := C.CString("image." + extension)
defer C.free(unsafe.Pointer(c_outpath))
C.SetImageInfoFilename(im.ImageInfo, c_outpath)
var outlength (C.size_t)
outblob := C.ImageToBlob(im.ImageInfo, im.Image, &outlength, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return nil, ErrorFromExceptionInfo(exception)
}
char_pointer := unsafe.Pointer(outblob)
defer C.free(char_pointer)
return C.GoBytes(char_pointer, (C.int)(outlength)), nil
}
// ToFile writes the (transformed) MagickImage to the regular file at filename. Magick determines
// the encoding of the output file by the extension given to the filename (e.g. "image.jpg", "image.png")
func (im *MagickImage) ToFile(filename string) (err error) {
exception := C.AcquireExceptionInfo()
defer C.DestroyExceptionInfo(exception)
c_outpath := C.CString(filename)
defer C.free(unsafe.Pointer(c_outpath))
C.SetImageInfoFilename(im.ImageInfo, c_outpath)
success := C.WriteImages(im.ImageInfo, im.Image, c_outpath, exception)
if failed := C.CheckException(exception); failed == C.MagickTrue {
return ErrorFromExceptionInfo(exception)
}
if success != C.MagickTrue {
return &MagickError{"fatal", "", "could not write to " + filename + " for unknown reason"}
}
return nil
}