Class: Rszr::Image

Inherits:
Object
  • Object
show all
Extended by:
Identification
Includes:
Buffered, Transformations, Orientation
Defined in:
lib/rszr/image.rb,
ext/rszr/image.c

Defined Under Namespace

Modules: Transformations

Constant Summary collapse

GRAVITIES =
[true, :center, :n, :nw, :w, :sw, :s, :se, :e, :ne].freeze
BLENDING_MODES =
i[copy add subtract reshade].freeze

Constants included from Orientation

Orientation::ROTATIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Transformations

#blend, #blend!, #blur, #blur!, #brighten, #brighten!, #contrast, #contrast!, #crop, #crop!, #fill, #fill!, #filter, #flip, #flop, #gamma, #gamma!, #rectangle, #rectangle!, #resize, #resize!, #rotate, #rotate!, #sharpen, #sharpen!, #turn, #turn!

Methods included from Orientation

included

Methods included from Buffered

included

Constructor Details

#initialize(width, height, alpha: false, background: nil) ⇒ Image

Returns a new instance of Image.

Raises:

  • (ArgumentError)


181
182
183
184
185
186
187
188
# File 'lib/rszr/image.rb', line 181

def initialize(width, height, alpha: false, background: nil)
  raise ArgumentError, 'illegal image dimensions' if width < 1 || width > 32766 || height < 1 || height > 32766
  raise ArgumentError, 'background must respond to to_fill' if background && !(background.respond_to?(:to_fill))
  _initialize(width, height).tap do |image|
    image.alpha = alpha
    image.fill!(background) if background
  end
end

Class Method Details

.load(path, autorotate: Rszr.autorotate, **opts) ⇒ Object Also known as: open

Raises:



12
13
14
15
16
17
18
# File 'lib/rszr/image.rb', line 12

def load(path, autorotate: Rszr.autorotate, **opts)
  path = path.to_s
  raise FileNotFound unless File.exist?(path)
  image = _load(path)
  autorotate(image, path) if autorotate
  image
end

.load_data(data, autorotate: Rszr.autorotate, **opts) ⇒ Object

Raises:



21
22
23
24
25
26
# File 'lib/rszr/image.rb', line 21

def load_data(data, autorotate: Rszr.autorotate, **opts)
  raise LoadError, 'Unknown format' unless format = identify(data)
  with_tempfile(format, data) do |file|
    load(file.path, autorotate: autorotate, **opts)
  end
end

Instance Method Details

#[](x, y) ⇒ Object



46
47
48
49
50
# File 'lib/rszr/image.rb', line 46

def [](x, y)
  if x >= 0 && x <= width - 1 && y >= 0 && y <= height - 1
    Color::RGBA.new(*_pixel(x, y))
  end
end

#alphaObject Also known as: alpha?



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'ext/rszr/image.c', line 137

static VALUE rszr_image_alpha_get(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);

  imlib_context_set_image(handle->image);
  if (imlib_image_has_alpha()) {
    return Qtrue;
  }

  return Qfalse;
}

#alpha=(rb_alpha) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
# File 'ext/rszr/image.c', line 151

static VALUE rszr_image_alpha_set(VALUE self, VALUE rb_alpha)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);

  imlib_context_set_image(handle->image);
  imlib_image_set_has_alpha(RTEST(rb_alpha) ? 1 : 0);

  return Qnil;
}

#dimensionsObject



30
31
32
# File 'lib/rszr/image.rb', line 30

def dimensions
  [width, height]
end

#dupObject

static VALUE rszr_image_get_quality(VALUE self) {

rszr_image_handle * handle;
int quality;

Data_Get_Struct(self, rszr_image_handle, handle);

imlib_context_set_image(handle->image);
quality = imlib_image_get_attached_value("quality");

if (quality) {
  return INT2NUM(quality);
} else {
  return Qnil;
}

}

static VALUE rszr_image_set_quality(VALUE self, VALUE rb_quality) {

rszr_image_handle * handle;
int quality;

Check_Type(rb_quality, T_FIXNUM);
quality = FIX2INT(rb_quality);
if (quality <= 0) {
  rb_raise(rb_eArgError, "quality must be >= 0");
  return Qnil;
}

Data_Get_Struct(self, rszr_image_handle, handle);

imlib_context_set_image(handle->image);
imlib_image_attach_data_value("quality", NULL, quality, NULL);

return INT2NUM(quality);

}



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'ext/rszr/image.c', line 256

static VALUE rszr_image_dup(VALUE self)
{
  rszr_image_handle * handle;
  rszr_image_handle * cloned_handle;
  Imlib_Image cloned_image;
  VALUE oClonedImage;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  cloned_image = imlib_clone_image();
  
  if (!cloned_image) {
    rb_raise(eRszrTransformationError, "error cloning image");
    return Qnil;
  }
  
  oClonedImage = rszr_image_s_allocate(cImage);
  Data_Get_Struct(oClonedImage, rszr_image_handle, cloned_handle);
  cloned_handle->image = cloned_image;

  return oClonedImage;
}

#filter!(rb_filter_expr) ⇒ Object



356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'ext/rszr/image.c', line 356

static VALUE rszr_image_filter_bang(VALUE self, VALUE rb_filter_expr)
{
  rszr_image_handle * handle;
  char * filter_expr;
  
  filter_expr = StringValueCStr(rb_filter_expr);
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_apply_filter(filter_expr);
  
  return self;
}

#flip!Object



307
308
309
310
311
312
313
314
315
316
317
# File 'ext/rszr/image.c', line 307

static VALUE rszr_image_flip_bang(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_image_flip_vertical();

  return self;
}

#flop!Object



294
295
296
297
298
299
300
301
302
303
304
# File 'ext/rszr/image.c', line 294

static VALUE rszr_image_flop_bang(VALUE self)
{
  rszr_image_handle * handle;

  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  imlib_image_flip_horizontal();

  return self;
}

#formatObject



34
35
36
37
# File 'lib/rszr/image.rb', line 34

def format
  fmt = _format
  fmt == 'jpg' ? 'jpeg' : fmt
end

#format=(fmt) ⇒ Object



39
40
41
42
# File 'lib/rszr/image.rb', line 39

def format=(fmt)
  fmt = fmt.to_s if fmt.is_a?(Symbol)
  self._format = fmt
end

#heightObject



178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/rszr/image.c', line 178

static VALUE rszr_image_height(VALUE self)
{
  rszr_image_handle * handle;
  int height;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  height = imlib_image_get_height();
  
  return INT2NUM(height);
}

#inspectObject



52
53
54
55
56
# File 'lib/rszr/image.rb', line 52

def inspect
  fmt = format
  fmt = " #{fmt.upcase}" if fmt
  "#<#{self.class.name}:0x#{object_id.to_s(16)} #{width}x#{height}x#{alpha? ? 32 : 24}#{fmt}>"
end

#save(path, format: nil, quality: nil, interlace: false) ⇒ Object

Raises:

  • (ArgumentError)


190
191
192
193
194
195
# File 'lib/rszr/image.rb', line 190

def save(path, format: nil, quality: nil, interlace: false)
  format ||= format_from_filename(path) || self.format || 'jpg'
  raise ArgumentError, "invalid quality #{quality.inspect}" if quality && !(0..100).cover?(quality)
  ensure_path_is_writable(path)
  _save(path.to_s, format.to_s, quality, interlace)
end

#save_data(format: nil, quality: nil) ⇒ Object



197
198
199
200
201
202
203
204
# File 'lib/rszr/image.rb', line 197

def save_data(format: nil, quality: nil)
  format ||= self.format || 'jpg'
  with_tempfile(format) do |file|
    save(file.path, format: format, quality: quality)
    file.rewind
    file.read
  end
end

#widthObject

Instance methods



164
165
166
167
168
169
170
171
172
173
174
175
# File 'ext/rszr/image.c', line 164

static VALUE rszr_image_width(VALUE self)
{
  rszr_image_handle * handle;
  int width;
  
  Data_Get_Struct(self, rszr_image_handle, handle);
  
  imlib_context_set_image(handle->image);
  width = imlib_image_get_width();
  
  return INT2NUM(width);
}