Class: GMagick::Image

Inherits:
Object
  • Object
show all
Defined in:
ext/image/image.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rb_image_blob) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'ext/image/image.c', line 23

VALUE image_initialize(VALUE self, VALUE rb_image_blob)
{
  MagickWand *magick_wand;

  InitializeMagick(".");

  magick_wand = NewMagickWand();

  long double image_blob_length = RSTRING_LEN(rb_image_blob);
  char *image_blob = RSTRING_PTR(rb_image_blob);
  MagickReadImageBlob(magick_wand, (const unsigned char *)image_blob, image_blob_length);

  VALUE rb_wand = Data_Wrap_Struct(rb_cObject, NULL, NULL, magick_wand);

  rb_funcall(self, rb_intern("wand="), 1, rb_wand);
  rb_funcall(self, rb_intern("blob="), 1, rb_image_blob);

  return Qtrue;
}

Instance Attribute Details

#blobObject

Define our instance methods

#wandObject

Class Method Details

.format(rb_image_blob) ⇒ Object

Define our class methods



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'ext/image/image.c', line 5

VALUE format(VALUE self, VALUE rb_image_blob)
{
  const char *format = NULL;
  long double image_blob_length = RSTRING_LEN(rb_image_blob);
  char *image_blob = RSTRING_PTR(rb_image_blob);

  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();
  MagickReadImageBlob(magick_wand, (const unsigned char *)image_blob, image_blob_length);
  format = MagickGetImageFormat(magick_wand);

  if (format) {
    return rb_str_new2(format);
  } else {
    return Qnil;
  }
}

Instance Method Details

#alpha_unpackObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'ext/image/image.c', line 116

VALUE image_alpha_unpack(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);
  int height = NUM2INT( rb_funcall(self, rb_intern("height"), 0) );
  int width = NUM2INT( rb_funcall(self, rb_intern("width"), 0) );
  int pixel_count = height * width;
  unsigned char *pixels;

  pixels = (unsigned char *)malloc(pixel_count * sizeof(char));
  MagickGetImagePixels(magick_wand, 0, 0, width, height, "A", CharPixel, pixels);
  return rb_str_new((const char *)pixels, pixel_count);
}

#colorspaceObject



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'ext/image/image.c', line 71

VALUE image_colorspace(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);
  ColorspaceType color_space = MagickGetImageColorspace(magick_wand);
  if (color_space == RGBColorspace) {
    return ID2SYM(rb_intern("DeviceRGB"));
  } else if (color_space == CMYKColorspace) {
    return ID2SYM(rb_intern("DeviceCMYK"));
  } else {
    rb_raise(rb_eTypeError, "Unknown colorspace");
  }
}

#depthObject



43
44
45
46
47
48
49
50
51
# File 'ext/image/image.c', line 43

VALUE image_depth(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);

  unsigned long depth = MagickGetImageChannelDepth(magick_wand, RedChannel);
  return INT2NUM(depth);
}

#heightObject



62
63
64
65
66
67
68
69
# File 'ext/image/image.c', line 62

VALUE image_height(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);
  unsigned long height = MagickGetImageHeight(magick_wand);
  return INT2NUM(height);
}

#unpackObject



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
# File 'ext/image/image.c', line 86

VALUE image_unpack(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);
  ColorspaceType color_space = MagickGetImageColorspace(magick_wand);

  int height = NUM2INT( rb_funcall(self, rb_intern("height"), 0) );
  int width = NUM2INT( rb_funcall(self, rb_intern("width"), 0) );
  int pixel_count = height * width;
  int buffer_size;
  char color_format[8];
  unsigned char *pixels;

  if (color_space == RGBColorspace) {
     buffer_size = 3 * pixel_count;
     strcpy(color_format, "RGB");
  } else if (color_space == CMYKColorspace) {
     buffer_size = 4 * pixel_count;
     strcpy(color_format, "CMYK");
  } else {
    rb_raise(rb_eTypeError, "Unknown colorspace");
  }

  pixels = (unsigned char *)malloc(buffer_size * sizeof(char));
  MagickGetImagePixels(magick_wand, 0, 0, width, height, color_format, CharPixel, pixels);

  return rb_str_new((const char *)pixels, buffer_size);
}

#widthObject



53
54
55
56
57
58
59
60
# File 'ext/image/image.c', line 53

VALUE image_width(VALUE self)
{
  VALUE rb_wand = rb_funcall(self, rb_intern("wand"), 0);
  MagickWand *magick_wand;
  Data_Get_Struct(rb_wand, MagickWand, magick_wand);
  unsigned long width = MagickGetImageWidth(magick_wand);
  return INT2NUM(width);
}