rcairoでjpegを使う

cairoはjpegを使えないみたい。
linuxのconvertを使ってやってみた。
ついでに文字も書いてみた。
いい感じ。

convertは
sudo apt-get install imagemagick
で、手に入れた。

#!usr/bin/env ruby

require 'rubygems'
require 'sdl'
require 'cairo'

class Weather
def initialize(screen)
url = "http://weather.yahoo.co.jp/weather/"
io = IO.popen("wget -q -O - %s 2" % url)
html = ""
while line = io.gets
html << line
end
io.close
if /satellite.html(.*?)width/ =~ html
if /(http.*?)"/ =~ $1
url = $1
end
end
IO.popen("wget -q -O /tmp/input.jpg %s" % url).close
IO.popen("convert /tmp/input.jpg /tmp/input.png").close
@image = Cairo::ImageSurface.from_png("/tmp/input.png")
context = Cairo::Context.new(@image)
context.set_source_rgb(0, 0, 0)
context.font_size = 25
context.move_to(30, 50)
context.show_text('日本の上空')
format = Cairo::FORMAT_ARGB32
stride = Cairo::Format.stride_for_width(format,@image.width)
@screen = screen
@surface = SDL::Surface.new_from(@image.data,@image.width,@image.height,32,stride,0x00FF0000,0x0000FF00,0x000000FF,0xFF000000)
@x,@y = 10,70
end

def update
@x += 3
@x = 0 - @image.width if @x > @screen.w
@screen.put(@surface,@x,@y)
end
end

class Phase
def initialize(screen)
@screen = screen
@item = Weather.new(@screen)
end

def update
@screen.fill_rect(0,0,@screen.w,@screen.h,[20,20,20])
@item.update
end

def handling
while e=SDL::Event.poll
exit if e.kind_of?(SDL::Event::Quit) || (e.kind_of?(SDL::Event::KeyDown) && e.sym == SDL::Key::ESCAPE)
end
end

def run
loop do
handling
update
@screen.flip
SDL.delay(50)
end
end
end

SDL.init(SDL::INIT_EVERYTHING)
SDL::WM.set_caption("Weather","")
screen = SDL::setVideoMode(500,350,16,SDL::SWSURFACE)
Phase.new(screen).run
[PR]
by gaziya | 2011-01-28 20:29