Bitmap/Read an image through a pipe: Difference between revisions
(add Ruby) |
|
(No difference)
|
Revision as of 14:41, 6 February 2010
You are encouraged to solve this task according to the task description, using any language you may know.
This task is the opposite of the PPM conversion through a pipe. In this task, using a delegate tool (like cjpeg, one of the netpbm package, or convert of the ImageMagick package) we read an image file and load it into the data storage type defined here. We can also use the code from Read ppm file, so that we can use PPM format like a (natural) bridge between the foreign image format and our simple data storage.
C
Here I've used convert by ImageMagick. It is up to the program to understand the source file type; in this way, we can read theoretically any image format ImageMagick can handle. The get_ppm function defined in Read ppm file is used.
<lang c>image read_image(const char *name);</lang>
<lang c>#include "imglib.h"
- define MAXCMDBUF 100
- define MAXFILENAMELEN 256
- define MAXFULLCMDBUF (MAXCMDBUF + MAXFILENAMELEN)
image read_image(const char *name) {
FILE *pipe; char buf[MAXFULLCMDBUF]; image im; FILE *test = fopen(name, "r"); if ( test == NULL ) { fprintf(stderr, "cannot open file %s\n", name); return NULL; } fclose(test); snprintf(buf, MAXFULLCMDBUF, "convert \"%s\" ppm:-", name); pipe = popen(buf, "r"); if ( pipe != NULL ) { im = get_ppm(pipe); pclose(pipe); return im; } return NULL;
}</lang>
OCaml
The read_ppm
function of the page read ppm file and used by the code below would need to be changed to take as parameter an input channel instead of the filename.
<lang ocaml>let read_image ~filename =
if not(Sys.file_exists filename) then failwith(Printf.sprintf "the file %s does not exist" filename); let cmd = Printf.sprintf "convert \"%s\" ppm:-" filename in let ic, oc = Unix.open_process cmd in let img = read_ppm ~ic in (img)
- </lang>
Ruby
Extending Read ppm file#Ruby and PPM conversion through a pipe#Ruby. Uses the ImageMagick convert
tool.
<lang ruby>class Pixmap
def self.read_ppm(ios) format = ios.gets.chomp width, height = ios.gets.chomp.split.map {|n| n.to_i } max_colour = ios.gets.chomp
if (not PIXMAP_FORMATS.include?(format)) or width < 1 or height < 1 or max_colour != '255' then ios.close raise StandardError, "file '#{filename}' does not start with the expected header" end ios.binmode if PIXMAP_BINARY_FORMATS.include?(format)
bitmap = self.new(width, height) height.times do |y| width.times do |x| # read 3 bytes red, green, blue = case format when 'P3' then ios.gets.chomp.split when 'P6' then ios.read(3).unpack('C3') end bitmap[x,y] = RGBColour.new(red, green, blue) end end ios.close bitmap end
def self.open(filename) read_ppm(File.open(filename, 'r')) end
def self.open_from_jpeg(filename) read_ppm(IO.popen("convert jpg:#{filename} ppm:-", 'r')) end
end
bitmap = Pixmap.open_from_jpeg('file.jpg')</lang>
Tcl
<lang tcl>package require Tk
proc magickalReadImage {bufferImage fileName} {
set f [open |[list convert [file normalize $fileName] ppm:-] "rb"] try { $bufferImage put [read $f] -format ppm } finally { close $f }
}</lang>