Class: Bitmap

Inherits:
Object
  • Object
show all
Defined in:
lib/rgss3/bitmap.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Bitmap

Returns a new instance of Bitmap.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rgss3/bitmap.rb', line 7

def initialize(*args)
  case args.size
  when 1
    basename, = args
    filename = RGSS3::RTP.find!(basename, ['', '.png', 'jpg'])
    initialize_with_gosu_image Gosu::Image.new(filename)
  when 2
    initialize_with_rmagick_image Magick::Image.new(*args) { self.background_color = 'none' }
  else
    raise ArgumentError
  end
end

Instance Attribute Details

#fontObject

Returns the value of attribute font.



5
6
7
# File 'lib/rgss3/bitmap.rb', line 5

def font
  @font
end

#gosu_imageObject (readonly)

Returns the value of attribute gosu_image.



4
5
6
# File 'lib/rgss3/bitmap.rb', line 4

def gosu_image
  @gosu_image
end

#rectObject (readonly)

Returns the value of attribute rect.



4
5
6
# File 'lib/rgss3/bitmap.rb', line 4

def rect
  @rect
end

Class Method Details

.from_gosu(img) ⇒ Object

NEW



223
224
225
226
227
# File 'lib/rgss3/bitmap.rb', line 223

def self.from_gosu(img)
  bitmap = allocate
  bitmap.initialize_with_gosu_image(img)
  bitmap
end

.from_rmagick(img) ⇒ Object



229
230
231
232
233
# File 'lib/rgss3/bitmap.rb', line 229

def self.from_rmagick(img)
  bitmap = allocate
  bitmap.rmagick_image = img
  bitmap
end

.gosu_to_rmagick(image) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/rgss3/bitmap.rb', line 235

def self.gosu_to_rmagick(image)
  Magick::Image.from_blob(image.to_blob) {
    self.format = "RGBA"
    self.size = "#{image.width}x#{image.height}"
    self.depth = 8
  }.first
end

.pixel_map!(rmagick_image, &block) ⇒ Object



243
244
245
246
247
248
249
250
# File 'lib/rgss3/bitmap.rb', line 243

def self.pixel_map!(rmagick_image, &block)
  return to_enum(__method__, rmagick_image) unless block
  width = rmagick_image.columns
  height = rmagick_image.rows
  pixels = rmagick_image.get_pixels(0, 0, width, height)
  pixels.map!(&block)
  rmagick_image.store_pixels(0, 0, width, height, pixels)
end

Instance Method Details

#blt(x, y, src_bitmap, src_rect, opacity = 255) ⇒ Object

untested



53
54
55
56
57
58
# File 'lib/rgss3/bitmap.rb', line 53

def blt(x, y, src_bitmap, src_rect, opacity = 255)
  # opacity is not supported
  im2 = src_bitmap.gosu_image.subimage(*src_rect)
  @gosu_image.insert(im2, x, y)
  set_dirty
end

#blurObject



153
154
155
# File 'lib/rgss3/bitmap.rb', line 153

def blur
  # self.rmagick_image = rmagick_image.blur_image
end

#change_imageObject



264
265
266
# File 'lib/rgss3/bitmap.rb', line 264

def change_image
  @dirty = true
end

#check_disposedObject

Raises:



268
269
270
# File 'lib/rgss3/bitmap.rb', line 268

def check_disposed
  raise RGSSError, "Disposed Bitmap" if @disposed
end

#clearObject



114
115
116
# File 'lib/rgss3/bitmap.rb', line 114

def clear
  self.rmagick_image = Magick::Image.new(width, height)
end

#clear_rect(*args) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rgss3/bitmap.rb', line 118

def clear_rect(*args)
  case args.size
  when 1, 4
    if args[0].is_a?(Rect)
      x, y, width, height = *args[0].to_a
    else
      x, y, width, height = *args
    end
  else
    raise ArgumentError
  end
  f = Magick::Image.new(width, height) { self.background_color = 'none' }
  @gosu_image.insert(f, x, y)
  set_dirty
end

#disposeObject



36
37
38
# File 'lib/rgss3/bitmap.rb', line 36

def dispose
  @disposed = true
end

#dispose!Object



272
273
274
275
276
# File 'lib/rgss3/bitmap.rb', line 272

def dispose!
  @gosu_image = nil
  @rmagick_image = nil
  @disposed = true
end

#disposed?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/rgss3/bitmap.rb', line 40

def disposed?
  @disposed
end

#draw_text(*args) ⇒ Object



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
# File 'lib/rgss3/bitmap.rb', line 161

def draw_text(*args)
  case args.size
  when 2, 3
    rect, string, align = args
    x, y, width, height = *rect
  when 5, 6
    x, y, width, height, string, align = args
  else
    raise ArgumentError
  end

  string = string.to_s
  string.gsub!('<', '&lt;')
  string.gsub!('>', '&gt;')
  if @font.bold
    string.prepend("<b>") << "</b>"
  end
  if @font.italic
    string.prepend("<i>") << "</i>"
  end
  text_image = Gosu::Image.from_text(string, @font.size, font: @font.first_existant_name)
  x += (width - text_image.width) * (align || 0) / 2
  y += (height - text_image.height)  / 2
  text_image = Bitmap.gosu_to_rmagick(text_image)
  image = text_image.dup
  font_pixel = @font.color.to_pixel
  Bitmap.pixel_map!(image) do |pixel|
    result = font_pixel.dup
    result.opacity = pixel.opacity
    result
  end
  if @font.outline
    font_pixel = @font.out_color.to_pixel
    Bitmap.pixel_map!(text_image) do |pixel|
      result = font_pixel.dup
      result.opacity = pixel.opacity
      result
    end
    image.composite!(text_image, 1, 1, Magick::DstOverCompositeOp)
  end
  # no shadow support for now
  # if @font.shadow
  #   shadow = bigger_image
  #   font_pixel = Magick::Pixel.from_color('rgba(0,0,0,128)')
  #   Bitmap.pixel_map!(shadow) do |pixel|
  #     result = font_pixel.dup
  #     result.opacity = pixel.opacity
  #     result
  #   end
  #   image.composite!(shadow, 0, 0, Magick::DstOverCompositeOp)
  # end
  # @gosu_image.insert(image, x, y)
  self.rmagick_image = rmagick_image.composite!(image, x, y, Magick::OverCompositeOp)
end

#fill_rect(*args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/rgss3/bitmap.rb', line 69

def fill_rect(*args)
  case args.size
  when 2, 5
    if args[0].is_a?(Rect)
      rect, color = args
      x, y, width, height = *rect
    else
      x, y, width, height, color = *args
    end
  else
    raise ArgumentError
  end
  img = Magick::Image.new(width, height) { self.background_color = color.to_rmagick_color }
  @gosu_image.insert(img, x, y)
  set_dirty
end

#get_pixel(x, y) ⇒ Object



134
135
136
# File 'lib/rgss3/bitmap.rb', line 134

def get_pixel(x, y)
  Color.from_pixel(rmagick_image.pixel_color(x, y))
end

#gradient_fill_rect(*args) ⇒ Object



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
# File 'lib/rgss3/bitmap.rb', line 86

def gradient_fill_rect(*args)
  case args.size
  when 3, 4
    rect, start_color, end_color, vertical = args
    x, y, width, height = *rect
  when 6, 7
    x, y, width, height, start_color, end_color, vertical = args
  else
    raise ArgumentError
  end
  start_color = start_color.to_rmagick_color
  end_color = end_color.to_rmagick_color

  if vertical
    x2 = width
    y2 = 0
  else
    x2 = 0
    y2 = height
  end

  fill = Magick::GradientFill.new(0, 0, x2, y2, start_color, end_color)

  img = Magick::Image.new(width, height, fill)
  @gosu_image.insert(img, x, y)
  set_dirty
end

#heightObject



48
49
50
# File 'lib/rgss3/bitmap.rb', line 48

def height
  @gosu_image.height
end

#hue_change(hue) ⇒ Object

Untested



143
144
145
146
147
148
149
150
151
# File 'lib/rgss3/bitmap.rb', line 143

def hue_change(hue)
  image = rmagick_image
  Bitmap.pixel_map!(rmagick_image) do |pixel|
    h, *sla = pixel.to_hsla
    h = (h + hue) % 360
    Pixel.from_hsla(h, *sla)
  end
  self.rmagick_image = image
end

#init_other_attrObject



31
32
33
34
# File 'lib/rgss3/bitmap.rb', line 31

def init_other_attr
  @rect = Rect.new(0, 0, @gosu_image.width, @gosu_image.height)
  @font = Font.new
end

#initialize_with_gosu_image(gosu_image) ⇒ Object



20
21
22
23
24
# File 'lib/rgss3/bitmap.rb', line 20

def initialize_with_gosu_image(gosu_image)
  @gosu_image = gosu_image
  init_other_attr
  set_dirty
end

#initialize_with_rmagick_image(rmagick_image) ⇒ Object



26
27
28
29
# File 'lib/rgss3/bitmap.rb', line 26

def initialize_with_rmagick_image(rmagick_image)
  self.rmagick_image = rmagick_image
  init_other_attr
end

#radial_blur(angle, division) ⇒ Object



157
158
159
# File 'lib/rgss3/bitmap.rb', line 157

def radial_blur(angle, division)
  blur
end

#rmagick_imageObject

If bitmap.rmagick_image is changed, the behaviour is undefined. If you want to change it, call set_dirty or bitmap.rmagick_image = image after the change.



255
256
257
258
259
260
261
262
# File 'lib/rgss3/bitmap.rb', line 255

def rmagick_image
  if @dirty
    @dirty = false
    @rmagick_image = Bitmap.gosu_to_rmagick(@gosu_image)
  else
    @rmagick_image
  end
end

#rmagick_image=(image) ⇒ Object



278
279
280
281
# File 'lib/rgss3/bitmap.rb', line 278

def rmagick_image=(image)
  @rmagick_image = image
  @gosu_image = Gosu::Image.new(@rmagick_image)
end

#set_dirtyObject



283
284
285
# File 'lib/rgss3/bitmap.rb', line 283

def set_dirty
  @dirty = true
end

#set_pixel(x, y, color) ⇒ Object



138
139
140
# File 'lib/rgss3/bitmap.rb', line 138

def set_pixel(x, y, color)
  fill_rect(x, y, 1, 1, color)
end

#stretch_blt(dest_rect, src_bitmap, src_rect, opacity = 255) ⇒ Object

untested



61
62
63
64
65
66
67
# File 'lib/rgss3/bitmap.rb', line 61

def stretch_blt(dest_rect, src_bitmap, src_rect, opacity = 255)
  im2 = src_bitmap.gosu_image.subimage(*src_rect)
  im2 = Bitmap.gosu_to_rmagick(gosu_to_rmagick)
  im2.resize!(dest_rect.width, dest_rect.height)
  @gosu_image.insert(im2, dest_rect.x, dest_rect.y)
  set_dirty
end

#text_size(string) ⇒ Object



216
217
218
219
# File 'lib/rgss3/bitmap.rb', line 216

def text_size(string)
  f = Gosu::Font.new(@font.size, font: @font.first_existant_name)
  Rect.new(0, 0, f.text_width(string.to_s, f.height))
end

#widthObject



44
45
46
# File 'lib/rgss3/bitmap.rb', line 44

def width
  @gosu_image.width
end