2009
12.28

I tried compiling Imaging in C++ Builder (it uses Delphi compiler to generate .obj file which C++ linker can link and also generates C++ header for Pascal unit) few years ago. It didn’t work – there was internal compiler error, I think right in ImagingTypes unit.

Few days ago I tried C++ Builder 2010 and was pleasantly surprised. It worked! I tried just the library core for now (ImagingTypes, Imaging, ImagingFormats, Pascal only file handlers, etc.) and it works without problems. I’m not sure which C++ Builder version is required for successful compile though. Versions 6 and 2006 stopped with internal error, 2010 worked, and there are 2 other versions between.

Anyway, I’ll try to check out most of the library until the 2010 trial expires for me. Hmm, I’m wondering how many people use C++ Builder for C++ development – I’ve never did something serious in it, basically just to get object files usable by Delphi – so I have no idea if it’s ok.

PS: Another C++ Builder related news – patch for OpenJpeg library to get it compiling in BCB is posted here.

2009
12.25

JPEG 2000 for Pascal project is based on OpenJpeg library. For a very long time there was a bug that caused alpha (fourth and subsequent image channels) channel to be saved with all the samples having the value of 0.5 (128 for 8bit channels). This buggy behavior also depended on compiler settings – optimization level in case of GCC. You could use at most O1 in Windows and Linux, and only O0 in Mac OS X. Bug was also present when compiling with C++ Builder (to get object files usable in Delphi) but only when irreversible DWT transformation was enabled in OpenJpeg during encoding (it wasn’t before, but working versions of both PasJpeg2000 and Imaging use it now when lossy compression is selected by user to get smaller files).
You can read more about it in this news group post. Basically it was all fixed by changing a condition in one if statement to prevent accessing the fourth element of a three element array.

So what can you expect in the next version of PasJpeg2000 library?
Higher GCC optimization levels should make it a lot faster when using Free Pascal (particularly in Mac OS X where O0 was used). Irreversible DWT transformation produces smaller lossy files than current PasJpeg2000 version and with optional MCT (multicomponent transform – basically RGB>YCbCr) you get even smaller ones. There’s now also a patch that enables OpenJpeg to get palettes from JP2 files so indexed JPEG 2000 images could be supported too. And finally, there are some bug fixes (wrong reconstruction of subsampled files, …).

2009
11.04

Shift Right: Delphi vs C

Few weeks ago I converted a little function from C language to Delphi. I kept getting completely wrong results all the time even though I was sure the C to Pascal conversion was right (it was really just few lines). After some desperate time, I just tried replacing SHR operator by normal DIV (as A SHR 1 = A DIV 2 and so on). To my surprise, I immediately got the right results. Can Delphi’s (I didn’t test it in Free Pascal) SHR operator behave differently than C’s >> operator?

It does in fact. SHR treats its first operand as unsigned value even though it is a variable of signed type whereas >> takes the sign bit into account. In the function I converted the operand for right shift was often negative and Delphi’s SHR just ignored the value of the sign bit.

A Bit of Code

int a, b1, b2;
a = -512;
b1 = a >> 1;
b2 = a / 2;

After running this C code both b1 and b2 have a value of -256.

var A, B1, B2: Integer;
A := -512;
B1 := A shr 1;
B2 := A div 2;

This Delphi code however yields different result: B2 is -256 as expected but B1 has a value of 2147483392.

A Bit of Assembler

Assembler output of C code:

Unit1.cpp.22: b1 = a >> 1;
mov eax,[ebp-$0c]
sar eax,1
mov [ebp-$10],eax
Unit1.cpp.23: b2 = a / 2;
mov edx,[ebp-$0c]
sar edx,1
jns $00401bb9
adc edx,$00
mov [ebp-$14],edx

Assembler output of Delphi code:

Unit1.pas.371: B1 := A shr 1;
mov eax,[ebp-$0c]
shr eax,1
mov [ebp-$1c],eax
Unit1.pas.373: B2 := A div 2;
mov eax,[ebp-$0c]
sar eax,1
jns $00565315
adc eax,$00
mov [ebp-$20],eax

As you can see, asm output of C and Delphi divisions is identical. What differs is asm for shift right operator. Delphi uses shr instruction whereas C uses sar instruction. The difference: shr does logical shift and sar does arithmetic one.

The SHR instruction clears the most significant bit (see Figure 6-7 in the Intel Architecture Software Developer’s Manual, Volume 1); the SAR instruction sets or clears the most significant bit to correspond to the sign (most significant bit) of the original value in the destination operand.

Quoted from: http://faydoc.tripod.com/cpu/shr.htm

2009
10.13

Vampyre Imaging Library project was updated to version 0.26.4 few hours ago. This was supposed to be fix/patch/update release only but new features got in nevertheless (most notably APNG support I wrote about earlier and arbitrary angle image rotations).

More info and downloads at Imaging’s homepage.

2009
09.20

Few days ago I was wondering how long it will take to see DirectX 11 headers translated to Delphi/Pascal. I knew that translations of new DX components Direct2D and DirectWrite are part of Delphi 2010, but what about Direct3D and DirectCompute?

I checked out Clootie graphics pages but no luck there. Unfortunately, it’s not so active anymore. After that I thought DX11 for Pascal wouldn’t appear in like several months at least. Surprise, I found it about 5 minutes later. John Bladen published his DX11 translation on DirectX for Delphi blog. Thanks John!

Now all that remains is buying a DX11 GPU. New Radeons should be released sometime during this week and I’m thinking of getting 5850 model in a few months. Rumours are that NVidia’s DX11 cards won’t be out before 2010, so probably no big price drops for 5800 Radeons are to be expected until then.

My pile of old graphic cards will get bigger once again. I don’t buy new CPU/motherboard/RAM too often (now it’s Core2 from 2007 and before that AthlonXP in 2002) and I usually manage to sell the old ones or put them on server duty somewhere. However, since GPU performance as well as requirements on it rise much more faster than that of CPU, I ended up with like 5 cards in last 8 years. I managed to sell Radeon 9000 and GeForce 6600 but I still have Riva TNT2 and Radeon X1950 in my closet somewhere (and S3 Trio!). Hopefully, I’ll find a buyer for my 1GB 4850 soon.

Lately, I’ve had bad luck buying some new stuff just before major price drop. Hopefully, this won’t happen with 5850. Not that I want it to remain expensive – just want to buy it after the drop, not  a little while before it.

2009
08.27

Imaging supported DXT image/texture compression since one of the earliest releases. Quality of compressed images isn’t very high though (at least the compression isn’t too slow). For future Imaging versions I plan to ditch the current compression code and add a new one. To be precise, two new ones – fast and lower quality (still probably better than current Imaging’s compression), and slow and higher quality mode. Fast one will be based on Real-Time DXT Compression by Id Soft. I’m not decided on high quality one yet, but probably something like cluster fit algorithm from Squish library.

Mainly for testing purposes during implementation of these new methods, I want to create extension for Imaging that compares two images (original and one reconstructed from compressed original) and measures PSNR and some other quality metrics.

I’m also thinking of implementing DXT5 based format using YCoCg colorspace and PVRTC (texture compression currently used in iPhone).

Few links if you’re interested:

Here’s some quick comparison of DXT compressors (click the image to see full size). Value in brackets is MSE (mean square error) – lesser number means compressed image is more similar to original.

DXT compressors comparison

2009
07.27

Daggerfall for Free!

One of my all time favorite games was released as free download few weeks ago as a celebration of 15th anniversary of Elder Scrolls series. It’s 1996 title The Elder Scrolls: Daggerfall by Bethesda. You can get 150MiB zip archive with install at BethSoft’s site. Getting it working on modern OS is not easy but DosBox should help here.

There were a lot of attempts at remakes of Daggerfall but all failed so far. However, a new promising one appeared recently and it’s progressing at warp speed since then (one month from wrongly textured entrance of Privateer’s Hold dungeon to rendering city blocks). Check out development blog and downloads at the homepage of DaggerXL. You need original Daggerfall data files to run the demo – no problem now when you can get it free.

Daggerfall Workshop was recently reopened as well. It’s a home of tools that let you explore Daggerfall’s game assets (textures, models, maps, music, etc.). There’s also a new exploring tool in development that looks really good, with very interesting blog posts about various Dagger’s file formats, rendering techniques, and so on.

I did my small share of dabbling in Daggerfall’s file formats too. Image and texture loaders in Object Pascal are part of ElderImagery extension of Imaging library and I’ve got BSA archive handler somewhere too. I’m now thinking of adding SKY format (backdrop images used in game for skies and far away mountains) loader, just for fun I guess.

When writing about Daggerfall, one cannot forget mentioning its music which was really excellent. Much more atmospheric and fitting than the music in recent Elder Scrolls games (there was also a lot more tracks). Check out Robert Hood’s arrangements of Eric Heberling’s original music tracks.

Castle WayrestWildreness at night

WildernessMy character

My other character

2009
07.07

I finally released first version of JPEG 2000 for Pascal library. It’s based on translated header and precompiled OpenJpeg library that was part of Imaging for a long time – now released separately.

There’s header translation working with both Delphi and FPC and original C library precompiled to object files (Delphi) and static libraries (FPC for Win32, Linux x86/x64, and Mac OS X).

I’ve written simple TBitmap descendant that loads and saves JPEG 2000 images. It’s only for Delphi now – can’t do LCL version with just few IFDEFS, there are lot more changes between VCL and LCL TBitmaps. I plan to write separate LCL version for one of upcoming releases (using TFPCustomImageWriter and TFPCustomImageReader classes).

You can get JPEG 2000 for Pascal library at it’s project page.

2009
06.22

APNG loading and animating implementation for Imaging wasn’t very hard work. However, there are not that many test images to be found on the Internet and most of the available ones are very simple. They’re usually not using disposal methods and are basically just collection of independent images. Big difference compared to GIF files – some of them were quite difficult to animate right. I even got most of nontrivial APNG test files by converting some more complex animated GIF files. Tools for creating APNG images are not yet as sophisticated as some GIF animation tools – there’s GIF to APNG converter, APNG Anime Maker, and some web based tools that assemble simple APNG file from bunch of uploaded single PNG frames.

Now I’m gonna extend PNG saver to allow saving multi images to simple APNG files, much like MNG saving works now. There’s really not a good unified way how to pass some more information to image file savers in current library design – that’s one of TODOs for new Imaging architecture.

2009
05.19

I started working on support for APNG format for Imaging library. APNG is unofficial extension of PNG image file format created by two guys from Mozilla Corporation. The point of APNG is to allow storing simple animations in PNG files (hence the “A” for “Animated”).

There is already PNG-like chunk based format for animations called MNG (already supported by Imaging – at least the basic features). However, MNG is quite complex format and its support among browsers and image viewers/editors is lacking. Code library supporting all MNG features is huge.

APNG on the other hand is just an extension of PNG and its implementation is not so complex. I’m going to load only the raw frames from files at first and see what will have to be done to support animating the frames next. Canvas class will have to be used here for alpha blending subsequent frames to previous ones. I’ll add option to turn the animating on/off just like it is available for animated GIF files.

More info about APNG: http://www.animatedpng.com and https://wiki.mozilla.org/APNG_Specification.