• Skip to site navigation
  • Skip to content
  • Skip to sidebar
  • Skip to footer
  • Go to archive page
Shadowmaster’s Lair
  • Home
  • Projects
  • Articles
  • About
  • Contact
  • Blog
‹ Previous | Blog index | Next ›

Wesnoth-TC meets gd

Sunday, March 7, 2010

Since I'm fed up with libpng's complexity (which borders on obscurity), I asked for recommendations on libraries for reading, editing and writing PNG files in #defocus, freenode's social channel. Someone mentioned gd, which I mostly knew for being a dependency of PHP-based software that deals with graphics, so I decided to give it a try.

It turns out to be incredibly easy to learn and use — I quickly started the gd_writer branch for the Wesnoth Team Colorizer tool and nuked hundreds of convoluted lines of libpng calls to replace them with short, concise and clean code.

However, I then noticed something in the (thorough and easy to read) documentation that spelled trouble for my usage:

gd retains only 8 bits of resolution for each of the red, green and blue channels, and only 7 bits of resolution for the alpha channel. The former restriction affects only a handful of very rare 48-bit color and 16-bit grayscale PNG images. The second restriction affects all semitransparent PNG images, but the difference is essentially invisible to the eye. 7 bits of alpha channel resolution is, in practice, quite a lot.

No justification is provided in the documentation as far as I could find. However, the main header file (gd.h, which is perfectly readable unlike libpng's png.h) has this precious missing bit explaining the reasoning here:

If 'truecolor' [in a gdImage struct] is set true, the image is truecolor; pixels are represented by integers, which must be 32 bits wide or more.

True colors are repsented [sic] as follows:

ARGB

Where 'A' (alpha channel) occupies only the LOWER 7 BITS of the MSB. This very small loss of alpha channel resolution allows gd 2.x to keep backwards compatibility by allowing signed integers to be used to represent colors, and negative numbers to represent special cases, just as in gd 1.x.

Ah, good old backwards compatibility, always biting the programmer in the ass after a while — Windows 9x being a major commercial example of backwards compatibility gone mad.

While I agree that the difference might not be noticeable for the human eye, this still means that wesnoth-tc/gd produces output that differs from the original images in more than just team colors. It'd be often silly to use team-colored semi-transparent in Wesnoth unit sprites, but team color and palette switches can, thanks to the flexibility of the image path functions mechanism, be applied on virtually any kind of image that SDL_image can read into a surface pixmap, including things such as transparent haloes for visual effects.

Experimenting with wesnoth-tc/gd, a linear sequence of pixels with these alpha channel values:

255, 254, 253, 252, 251, 250, 249, 258 [...]

...turns into this in the application's output:

255, 255, 253, 253, 251, 251, 249, 249 [...]

What happens here is that values below 0x7F — which is 127 in base 10 and 1111111 in base 2, and as you can see, the maximum positive integer that can be represented with 7 bits — can only be even, and values above are odd. There's no 0x7F.

I could just ignore this issue and merge the gd_writer branch, but no, I think I'll just try yet another library. It's a pity because this experiment had created the possibility of making a web interface for recoloring/team-coloring Wesnoth artwork, but I believe this kind of loss of information isn't any good for our purposes, even if it should be virtually unnoticeable at the standard 72x72 sprite scale. At least unit shadows shouldn't be affected by this limitation since their alpha value is supposed to be 153, which would remain unchanged under this scheme.

And so, Wesnoth-TC 1.5 stays in Development Hell for now, and the search for a simple enough libpng wrapper continues.

Posted in Software, Wesnoth at 22:39 UTC | No comments
Trackbacks
Trackback URI
No trackbacks
Comments
Linear | Threaded
No comments
Add Comment
All fields are optional. Your email address won't be publicly displayed.
Standard emoticons like :-) and ;-) are converted to images.
 
 
 
‹ February ’12  
Mo Tu We Th Fr Sa Su
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29        
  • Recent posts
  • Archives
  • RSS/XML RSS 1.0
  • RSS/XML RSS 2.0
  • Atom/XML Atom 1.0
  • RSS/XML Comments
Twitter: @shikadilord
  • Okay, that failed spectacularly. Go back to your regular schedule, #Wesnoth forum users.4 days ago
  • Not to alarm you, #Wesnoth people, but I'm going to break your forums for a few seconds!4 days ago
  • Who cares about #Wesnoth 1.10. I'm a developer, I use trunk! http://t.co/upxww27M6 days ago
  • Goodbye, #Wesnoth 1.8!6 days ago
  • I knew I missed something during #Wesnoth 1.9.x. http://t.co/s33x5BUr1 week ago
  • Shadowmaster’s Blog: Wesnoth add-on tests and sanity checking http://t.co/CbUGlI711 week ago
Categories
  • XML Frogatto
  • XML Hardware
  • XML IRC
  • XML freenode
  • XML Miscellaneous
  • XML Personal
  • XML Projects
  • XML Rei 2 IRC Bot
  • XML Wesnoth-TC
  • XML Site updates
  • XML Software
  • XML Web browsers
  • XML Web design
  • XML phpBB
  • XML Wesnoth
  • XML Wesnoth Evolution
  • XML Wesnoth-UMC-Dev
Projects
  • Wesnoth Add-ons
  • Wesnoth-TC/RCX
  • Frogatto levels
  • Rei 2 IRC Bot
  • Wesnoth-UMC-Dev Registry
  • Shikadibot 0314
  • phpBB 3.0 Mods/Hacks
Articles
  • Wesnoth Evolution
Links
  • Battle for Wesnoth
  • Wesnoth-UMC-Dev
  • Frogatto & Friends
Contact • Site Information & Disclaimer

Copyright © 2006-2012 by Ignacio R. Morelle. All rights reserved.
Powered by Poison Ivy/Dorset6 D9 and Serendipity.
Hosting provided by rewound.net and NearlyFreeSpeech.NET.

Serendipity PHP Weblog Valid XHTML 1.0 Transitional