Class: Rixmap::Format::PCX::PCXImageIO

Inherits:
ImageIO::BaseImageIO show all
Defined in:
lib/rixmap/format/pcx.rb

Overview

PCX画像入出力処理実装.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ImageIO::BaseImageIO

#open, #read, #readable?, #save, #writable?, #write

Constructor Details

#initialize(options = {}) ⇒ PCXImageIO

PCX入出力オブジェクトを初期化します.

Parameters:

  • options (Hash) (defaults to: {})

    オプションパラメータ

Options Hash (options):

  • :compression (Boolean)

    圧縮するかどうか (Default: true)



215
216
217
218
219
# File 'lib/rixmap/format/pcx.rb', line 215

def initialize(options={})
  super(options)

  @compression = true unless defined?(@compression)
end

Class Method Details

.readable?(magic) ⇒ Boolean

指定データがPCXとして読み込めるデータかどうかを調べます.

Parameters:

  • magic (String)

    画像の先頭データ

Returns:

  • (Boolean)

    PCXとして読み込める場合はtrue



191
192
193
# File 'lib/rixmap/format/pcx.rb', line 191

def self.readable?(magic)
  return magic[0].ord == FILE_SIGNATURE && magic[1].ord == FORMAT_VERSION_30
end

.writable?(image) ⇒ Boolean

指定画像がPCXとして書き込めるかどうかを判定します.

実は透明度付のPCX画像はサポートしてなかったりします. 書き込み処理は実行できますが、その場合は透明度を無視して 書き込むようになってます.

Parameters:

Returns:

  • (Boolean)

    PCXとして書き込める場合はtrue



203
204
205
206
207
208
209
# File 'lib/rixmap/format/pcx.rb', line 203

def self.writable?(image)
  if image.has_alpha?
    return false
  else
    return true
  end
end

Instance Method Details

#decode(data, options = {}) ⇒ Rixmap::Image

バイト列をPCXデータとして復元します.

Parameters:

  • data (String)

    バイト列

  • options (Hash) (defaults to: {})

    オプションパラメータ (現在未実装)

Returns:



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
# File 'lib/rixmap/format/pcx.rb', line 329

def decode(data, options={})
  header_data = data.byteslice(0, 128)
  image_data  = data.byteslice(128, (data.bytesize - 128)).unpack('C*')

  # ヘッダを再構築
  header = PCXFileHeader.new
  header.unpack(header_data)

  # ヘッダを確認
  unless header.magic == FILE_SIGNATURE
    raise ArgumentError.new("Input data is not PCX image data")
  end
  unless header.version == FORMAT_VERSION_30
    raise NotImplementedError.new("Currently supported only PCX Version 3.0, yet")
  end
  unless header.encoding == ENCODINGTYPE_NONE || header.encoding == ENCODINGTYPE_RLE
    raise ArgumentError.new("Unknown encoding-type: #{header.encoding}")
  end
  unless header.colortype == COLORTYPE_NORMAL || header.colortype == COLORTYPE_GRAYSCALE
    raise ArgumentError.new("Unknown color-type: #{header.colortype}")
  end
  unless header.bit_width == 8
    raise NotImplementedError.new("Currently supported only 8-bit image")
  end
  if header.bit_planes < 1
    raise ArgumentError.new("Input image has no pixel data")
  end

  # 画像を復元
  image = nil
  imwidth  = header.right - header.left + 1
  imheight = header.bottom - header.top + 1
  total_bytes = header.line_width * header.bit_planes * imheight

  # ピクセルデータを復元
  pixels = nil
  pixel_end_offset = nil
  if header.encoding == ENCODINGTYPE_RLE
    rle = RLE.new
    pixels, pixel_end_offset = rle.decode(image_data, total_bytes)
  else
    pixels = image_data.slice(0, total_bytes)
    pixel_end_offset = total_bytes
  end

  # まずはカラータイプで分岐
  if header.colortype == COLORTYPE_GRAYSCALE  # グレースケール
    image = Rixmap::Image.new(Rixmap::GRAYSCALE, imwidth, imheight)
    if header.bit_planes > 1    # 輝度値 + 謎プレーン
      pixels.each_slice(header.line_width * header.bit_planes).each_with_index do |line, i|
        break if i >= imheight
        image[h].luminance = line.slice(0, imwidth)
      end
    else                        # 輝度値
      pixels.each_slice(header.line_width).each_with_index do |line, i|
        break if i >= imheight
        image[i].luminance = line
      end
    end
  else                                        # カラー形式
    if header.bit_planes > 3      # RGB + 謎プレーン
      image = Rixmap::Image.new(Rixmap::RGB, imwidth, imheight)
      roffset = 0
      goffset = header.line_width
      boffset = header.line_width * 2
      pixels.each_slice(header.line_width * header.bit_planes).each_with_index do |line, i|
        break if i >= imheight
        image[i].red   = line.slice(roffset, imwidth)
        image[i].green = line.slice(goffset, imwidth)
        image[i].blue  = line.slice(boffset, imwidth)
      end
    elsif header.bit_planes == 3  # RGB
      image = Rixmap::Image.new(Rixmap::RGB, imwidth, imheight)
      roffset = 0
      goffset = header.line_width
      boffset = header.line_width * 2
      pixels.each_slice(header.line_width * 3).each_with_index do |line, i|
        break if i >= imheight
        image[i].red   = line.slice(roffset, imwidth)
        image[i].green = line.slice(goffset, imwidth)
        image[i].blue  = line.slice(boffset, imwidth)
      end
    elsif header.bit_planes == 1  # INDEXED
      image = Rixmap::Image.new(Rixmap::INDEXED, imwidth, imheight)
      pixels.each_slice(header.line_width).each_with_index do |line, i|
        break if i >= image.height
        image[i].palette = line
      end

      if pixel_end_offset < image_data.length && image_data[pixel_end_offset] == PALETTE_SEPARATOR && !image.palette.nil?
        # パレットある
        palette_data = image_data.slice(pixel_end_offset + 1, (image_data.length - (pixel_end_offset + 1)))
        palette = image.palette
        palette_data.each_slice(3).each_with_index do |color, i|
          break if i >= palette.size
          palette[i] = color
        end
      end
    else                          # !?
      raise RuntimeError.new("Illegal bit-planes #{header.bit_planes}")
    end
  end

  return image
end

#encode(image, options = {}) ⇒ String

PCX形式として画像をエンコードします.

Parameters:

  • image (Rixmap::Image)

    対象画像

  • options (Hash) (defaults to: {})

    オプションパラメータ

Options Hash (options):

  • :compression (Boolean)

    圧縮するかどうか (Defalt: true)

Returns:

  • (String)

    画像データバイト列



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
# File 'lib/rixmap/format/pcx.rb', line 227

def encode(image, options={})
  # 圧縮フラグを取得
  compression = if options[:compression].nil?
                  @compression
                else
                  options[:compression]
                end

  # ヘッダを構築 (共通部分)
  header = PCXFileHeader.new
  header.left   = 0
  header.top    = 0
  header.right  = image.width - 1
  header.bottom = image.height - 1
  header.encoding = if compression
                      ENCODINGTYPE_RLE
                    else
                      ENCODINGTYPE_NONE
                    end
  header.line_width = image.width
  header.bit_width  = 8

  # ピクセルデータ置場
  pixels = Array.new

  # エンコーダ
  rle = RLE.new

  # 各形式での処理
  if image.indexed?       # インデックスカラー形式
    header.colortype = COLORTYPE_NORMAL
    header.bit_planes = 1

    # ピクセルの結合
    if compression
      # RLE
      image.each_line do |line|
        pixels.concat(rle.encode(line.palette))
      end
    else
      # 無圧縮ピクセルデータ
      image.each_line do |line|
        pixels.concat(line.palette)
      end
    end
  elsif image.grayscale?  # グレースケール形式
    header.colortype = COLORTYPE_GRAYSCALE

    # 透明度は無視
    header.bit_planes = 1

    # ピクセルの結合
    if compression
      # RLE
      image.each_line do |line|
        pixels.concat(rle.encode(line.luminance))
      end
    else
      # 無圧縮
      image.each_line do |line|
        pixels.concat(line.luminance)
      end
    end
  elsif image.rgb?        # RGBカラー形式
    header.colortype = COLORTYPE_NORMAL

    # 透明度は無視
    header.bit_planes = 3

    # ピクセルの結合
    if compression
      # RLE
      image.each_line do |line|
        pixels.concat(rle.encode(line.red))
        pixels.concat(rle.encode(line.green))
        pixels.concat(rle.encode(line.blue))
      end
    else
      # 無圧縮
      image.each_line do |line|
        pixels.concat(line.red)
        pixels.concat(line.green)
        pixels.concat(line.blue)
      end
    end
  end

  # 結合
  data = header.pack() + pixels.pack('C*')
  if image.indexed? && !image.palette.nil?
    data.concat("\x0C")
    data.concat(image.palette.to_s('RGB'))
  end

  return data
end