[Coco] Colormax MGE files too. was Re: Now you can convert CM3 files without screenshots !

Mathieu Bouchard matju at artengine.ca
Sun Oct 22 23:11:25 EDT 2017


Le 2017-10-22 à 15:28:00, Mathieu Bouchard a écrit :

> Here is a program I just wrote (in the Ruby language) for decoding CM3 files.

And now the same for ColorMax MGE files. (But neither decoder can handle 
animations)

-----------------8<--------découpez-ici--------8<-----------------
#!/usr/bin/env ruby
# encoding: utf-8
# mgetoppm - decoder for mge (ColorMax 3) format
# Copyright (c) 2017 by Mathieu Bouchard
# reads mge from stdin, outputs ppm that can be piped
# to cjpeg (.jpg) from jpeg-tools, or to pnmtopng (.png) from netpbm.

$px=$py=0
def debug(x) STDERR.puts x end
def dmp500(x)
   c=$palette[x]; STDOUT.write [(c[5]*2+c[2])*85,(c[4]*2+c[1])*85,(c[3]*2+c[0])*85].pack("ccc")
end
c2r=[0,21,2,20,6,49,35,4,33,5,14,1,12,10,3,28,
7,17,16,22,48,34,37,32,44,40,42,13,8,11,24,26,
56,19,18,50,54,52,38,36,46,45,41,15,9,25,27,30,
63,58,23,51,55,53,39,60,47,61,43,57,29,31,59,62]
f=STDIN; f.set_encoding("ASCII-8BIT")
subtyp=0; $cols=320<<subtyp; rows=200; colors=if subtyp==0 then 16 else 4 end
a=f.read(1).ord; if a!=0 then debug "invalid header"; exit 1 end
$palette=[]; 16.times {$palette<<f.read(1).ord}
is_rgb=f.read(1).ord==0; colorspace=if is_rgb; "RGB" else "CMP" end
if not is_rgb; 16.times {|i| $palette[i]=c2r[$palette[i]] } end
packed=f.read(1).ord!=0
titbuf=f.read(30); i=titbuf.index"\0"; titbuf[i..-1]="" if i; titbuf.rstrip!
if subtyp==0
   def dump a; dmp500 a>>4; dmp500 a&0x0F end
else
   def dump a; dmp500 a>>6; dmp500 (a>>4)&3; dmp500 (a>>2)&3; dmp500 a&3 end
end
cycles=f.read(1).ord
a=f.read(1).ord; botpal=a&0xF; toppal=(a>>4)&0xF
debug "#{$cols}x#{rows}, 16 couleurs #{colorspace}, titre «#{titbuf}»"
debug "palette=#{$palette.inspect}, cycles=#{cycles}, botpal=#{botpal}, 
toppal=#{toppal}"
puts "P6\n#{$cols} #{rows}\n255"
y=160*rows
if not packed
   loop {
     b=f.read(1).ord
     break if b==0
     a=f.read(1).ord
     b.times {dump a; y-=1; break if y<=0}
     break if y<=0
   }
else
   y.times {dump f.read(1).ord}
end
extra=0; extra+=1 while f.read(1); debug "#{extra} octets de trop" if extra>0
-----------------8<--------découpez-ici--------8<-----------------

  ______________________________________________________________________
| Mathieu BOUCHARD --- tél: 514.623.3801, 514.383.3801 --- Montréal, QC


More information about the Coco mailing list