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



28
29
30
31
32
# File 'ext/image/image.c', line 28

VALUE image_initialize(VALUE self, VALUE rb_image_blob)
{
  rb_funcall(self, rb_intern("blob="), 1, rb_image_blob);
  return Qtrue;
}

Instance Attribute Details

#blobObject

Define our instance methods

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
22
23
24
25
26
# 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);

  DestroyMagickWand(magick_wand);
  DestroyMagick();

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

Instance Method Details

#alpha_unpackObject



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

VALUE image_alpha_unpack(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  unsigned long height = MagickGetImageHeight(magick_wand);
  unsigned long width = MagickGetImageWidth(magick_wand);
  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);

  VALUE rb_pixels = rb_str_new((const char *)pixels, pixel_count);

  free(pixels);
  DestroyMagickWand(magick_wand);
  DestroyMagick();

  return rb_pixels;
}

#colorspaceObject



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'ext/image/image.c', line 92

VALUE image_colorspace(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  ColorspaceType color_space = MagickGetImageColorspace(magick_wand);

  DestroyMagickWand(magick_wand);
  DestroyMagick();

  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



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'ext/image/image.c', line 34

VALUE image_depth(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  unsigned long depth = MagickGetImageDepth(magick_wand);

  DestroyMagickWand(magick_wand);
  DestroyMagick();

  return INT2NUM(depth);
}

#heightObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'ext/image/image.c', line 73

VALUE image_height(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  unsigned long height = MagickGetImageHeight(magick_wand);

  DestroyMagickWand(magick_wand);
  DestroyMagick();

  return INT2NUM(height);
}

#unpackObject



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

VALUE image_unpack(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  ColorspaceType color_space = MagickGetImageColorspace(magick_wand);
  unsigned long height = MagickGetImageHeight(magick_wand);
  unsigned long width = MagickGetImageWidth(magick_wand);
  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);

  VALUE rb_pixels = rb_str_new((const char *)pixels, buffer_size);

  free(pixels);
  DestroyMagickWand(magick_wand);
  DestroyMagick();

  return rb_pixels;
}

#widthObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'ext/image/image.c', line 53

VALUE image_width(VALUE self)
{
  InitializeMagick(".");
  MagickWand *magick_wand = NewMagickWand();

  VALUE rb_blob = rb_funcall(self, rb_intern("blob"), 0);
  long double image_blob_length = RSTRING_LEN(rb_blob);
  char *image_blob = RSTRING_PTR(rb_blob);

  MagickReadImageBlob(magick_wand, image_blob, image_blob_length);

  unsigned long width = MagickGetImageWidth(magick_wand);

  DestroyMagickWand(magick_wand);
  DestroyMagick();


  return INT2NUM(width);
}