• Skip to site navigation
  • Skip to content
  • Skip to sidebar
  • Skip to footer
  • Go to archive page
Shadowmaster’s Lair
A light in the darkness, where everything is possible...
  • Home
  • Projects
  • About
  • Contact
  • Blog [ frontpage ]
« Previous | 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 specific URI for this entry
No Trackbacks
Comments
Display comments as (Linear | Threaded)
No comments
Add Comment
Standard emoticons like :-) and ;-) are converted to images.
 
 
 
« September '10 »
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 30      
  • Recent posts
  • Archives
Twitter
  • @Grickit Ajax? http://is.gd/f3j4A1 hour ago
  • It's a sad thing that fontconfig's existence isn't helping me with keeping consistent font rendering rules here.21 hours ago
  • Apparently, the connectivity issues with my website have been solved...1 day ago
  • To #Wesnoth users who are looking at the Mainline Campaign Feedback forum right now: no, you aren't hallucinating.1 day ago
  • Connections to my website are still not working correctly here, even from the university's network. I guess some routing problem...1 day ago
  • My website, and nearlyfreespeech.net's blog, are still taking forever to load here. I guess I'll have my 1:30 minutes of sleep.1 day ago

Follow me

Categories
  • Frogatto
  • Hardware
  • Miscellaneous
  • Personal
  • Site updates
  • Software
  • Web browsers
  • Web design
  • phpBB
  • Wesnoth
  • Wesnoth-UMC-Dev
  • freenode
All categories
Syndication
  • XML RSS 1.0 feed
  • XML RSS 2.0 feed
  • ATOM/XML ATOM 1.0 feed
  • XML RSS 2.0 comments
Projects
  • Frogatto levels
  • Wesnoth add-ons
  • Wesnoth Team Colorizer
  • Wesnoth-UMC-Dev Registry Model
  • Shikadibot 0314
  • Miscellaneous
    • Irssi scripts
    • phpBB 3.0.x hacks
Links
  • Frogatto & Friends
  • Battle for Wesnoth
  • Wesnoth-UMC-Dev
  • Grickit’s blog
  • Turuk’s blog
  • The Monochrome
Frogatto & Friends
Serendipity PHP Weblog
Valid XHTML 1.0 Transitional
Copyright © 2006-2010 by Ignacio R. Morelle. All rights reserved. Powered by Poison Ivy/Dorset3 r5 and Serendipity.
Hosting by rewound.net and NearlyFreeSpeech.NET.
The opinions expressed herein do not represent those of any of the projects and organizations the author might be affiliated with, unless otherwise stated. The author is not responsible for the content hosted by external websites linked from this site. All software, documentation or instructions of any fashion are provided as is, and under its own license terms. Use/read/eat/drink at your own risk.