Skip to content
scriptum edited this page Dec 8, 2012 · 5 revisions

New Cheetah's image loader integrated with SOIL now supports images with mask. This is an image with alpha channel in separate file. You can use either PNG format or two JPG files: one for color channels, other for alpha channel. You can combine PNG+JPG or any other supported formats.

Look at these examples:

Checkers, png

Checkers, png, image size: 229k.

+

Checkers JPG (color) + PNG (mask), image size: only 30k!

As you see, very good compression ratio could be achieved without noticeable quality reduction. Cheetah engine automatically tile mask if image is bigger than mask, so you can even more compress repeating patterns.

Let's see how does it appears in engine:

require 'lib.cheetah'
require 'lib.lquery.init'
local C = cheetah
C.init('Masked Image', 800, 600, 32, 'v')
--note option 'm': means "mask" - engine tries to load file checkers_mask.jpg
local img = C.newImage('checkers.jpg', 'm')
Entity:new(screen):image(img):draggable()
C.mainLoop()

Full example: https://github.com/scriptum/Cheetah/tree/vertex-buffer/bin/Release/Demos/Images/MaskImage

You may ask: how can I generate mask image? It's easy and could be done in two ways:

  1. Using GIMP: create a mask for layer from alpha-channel, show it and save. Save color image without alpha.
  2. Use this ImageMagick bash script (copy and save it as get_mask.sh):
#!/bin/sh
QUALITY=""
while [ $# -gt 0 ]
do
	if [ "$1" = "-q" ]
	then
		shift
		QUALITY="-quality $1"
	else
		echo Processing "$1"
		img="${1%.png}.jpg"
		img_mask="${1%.png}_mask.jpg"
		img_mask2="${1%.png}_mask.png"
		convert "$1" $QUALITY "$img"
		convert "$1" $QUALITY -channel matte -separate "$img_mask"
		convert "$1" -channel matte -separate "$img_mask2"
		img_size=`stat -c%s "$img"`
		size_jpg=`stat -c%s "$img_mask"`
		size_png=`stat -c%s "$img_mask2"`
		if [ $size_jpg -gt $size_png ]
		then
			rm "$img_mask"
			sum_size=`expr $size_png + $img_size`
		else
			rm "$img_mask2"
			sum_size=`expr $size_jpg + $img_size`
		fi
		if [ $sum_size -gt `stat -c%s "$1"` ]
		then
			rm "$img" "$img_mask2" "$img_mask" 2> /dev/null
		fi
	fi
	shift
done

Example:

bash gen_mask.sh my_file.png

bash gen_mask.sh -quality 85 *.png

bash gen_mask.sh -quality 85 1.png -quality 90 2.png


Tutorials | Documentation
Clone this wiki locally