Class: Rixmap::Format::XPM::XPM1ImageIO

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

Overview

XPM1形式用入出力実装クラス.

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ImageIO::BaseImageIO

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

Constructor Details

This class inherits a constructor from Rixmap::ImageIO::BaseImageIO

Class Method Details

.readable?(magic) ⇒ Boolean

XPM1形式で読み込めるかどうかを調べます.

Parameters:

  • magic (String)

    先頭バイトデータ

Returns:

  • (Boolean)

    XPM1形式で読み込める場合はtrue



90
91
92
93
94
95
96
# File 'lib/rixmap/format/xpm.rb', line 90

def self.readable?(magic)
  if MAGIC_XPM1_PATTERN.match(magic)
    return true
  else
    return false
  end
end

.writable?(image) ⇒ Boolean

XPM1形式で書き込めるかどうかを調べます.

Parameters:

Returns:

  • (Boolean)

    XPM1形式で書き込める場合はtrue



102
103
104
105
106
107
108
# File 'lib/rixmap/format/xpm.rb', line 102

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

Instance Method Details

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

バイト列データからXPM1形式として画像を復元します.

Parameters:

  • data (String)

    XPM1形式画像データ

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

    オプションパラメータ (未使用)

Returns:



152
153
154
155
156
157
158
159
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
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
215
216
217
218
# File 'lib/rixmap/format/xpm.rb', line 152

def decode(data, options={})
  magic, imdata = data.split($/, 2)
  unless MAGIC_XPM1_PATTERN.match(magic)
    raise ArgumentError.new("input image is not XPM image")
  end

  # 画像データからコメント部分を除去
  imdata.gsub!(%r{/\*.*\*/}, "")    # Remove C Style Comments
  imdata.gsub!(%r{//.*$}, "")       # Remove C++ Style Comments

  # XPM画像かどうかをチェック
  xpmmatch = %r{static\s+char\s*\*\s*([[:graph:]]+)\s*\[\]\s*=\s*\{(.*)\};}m.match(imdata)
  unless xpmmatch
    raise ArgumentError.new("input image data is broken as XPM")
  end

  # 行分割
  #xpmlines = Enumerator.new(xpmmatch[2].strip.split($/))
  xpmlines = xpmmatch[2].strip.split($/).to_enum

  begin
    # 画像情報を解析
    xpminfo = %r{"\s*([[:digit:]]+)\s+([[:digit:]]+)\s+([[:digit:]]+)\s+([[:digit:]]+)\s*"}.match(xpmlines.next)
    unless xpminfo
      raise ArgumentError.new("input image data is broken as XPM")
    end
    width   = xpminfo[1].to_i
    height  = xpminfo[2].to_i
    ncolors = xpminfo[3].to_i
    nchars  = xpminfo[4].to_i   # 一色あたりの文字数

    # 画像オブジェクト
    palette = Rixmap::Palette.new(ncolors)
    image   = Rixmap::Image.new(Rixmap::INDEXED, width, height, :palette => palette)

    # 色表を構築
    ctbl = {}
    cptn = %r{"\s*(.{#{nchars}})\s+(c)\s+#([0-9A-Fa-f]{6})\s*.*"}
    ncolors.times do |i|
      cline  = xpmlines.next
      cmatch = cptn.match(cline)
      if cmatch
        ckey   = cmatch[1]
        cvalue = cmatch[3]
        ctbl[ckey] = i
        palette[i] = Rixmap::Color.new(*(cvalue.unpack('a2a2a2').collect {|e| e.to_i(16) }))
      end
    end

    # ピクセルを解析
    pixptn  = %r{"\s*(.{#{nchars * width}})\s*"}
    packfmt = "a#{nchars}" * width
    height.times do |h|
      pixline  = xpmlines.next
      pixmatch = pixptn.match(pixline)
      if pixmatch
        pixmatch[1].unpack(packfmt).each_with_index do |e, w|
          image[w, h] = ctbl[e]
        end
      end
    end

    return image
  rescue StopIteration
    raise RuntimeError.new("illegal content")
  end
end

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

画像をXPM1形式でバイト列にエンコードします.

Parameters:

  • image (Rixmap::Image)

    対象画像

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

    オプションパラメータ (未使用)

Returns:

  • (String)

    バイト列

Raises:

  • (ArgumentError)


115
116
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
# File 'lib/rixmap/format/xpm.rb', line 115

def encode(image, options={})
  raise ArgumentError.new("input image is not supported") unless image.indexed?

  # 画像データ格納変数名
  varname = "XFACE"

  # 画像データ
  tokens  = []
  tokens << MAGIC_XPM1
  tokens << "static char* #{varname}[] = {"

  # パレットデータ
  ctbl = XPMColorTable.new(image.palette)
  tokens << %Q{"#{image.width} #{image.height} #{image.palette.size} #{ctbl.code_size}",}
  ctbl.each do |item|
    tokens << %Q{"#{item[0]}\tc\t#{item[1]}",}
  end

  # ピクセルデータ
  image.height.times do |h|
    line = image[h].collect {|v| ctbl.codes[v] }.join("")
    tokens << %Q{"#{line}",}
  end
  tokens[-1] = tokens[-1].slice(0..-2)    # 最終行の末尾を調整

  # データ終端
  tokens << "};"

  # 連結
  return tokens.join("\n")
end