adventuregamestudio / ags

AGS editor and engine source code
Other
708 stars 159 forks source link

Introduce BitmapData class: a plain buffer of pixels, may be used without full Bitmap dependency #2435

Closed ivan-mogilko closed 6 months ago

ivan-mogilko commented 6 months ago

This introduces BitmapData class, which is a simple non-owning wrapper over a plain buffer of pixels, combined with its description (width, height, pixel format etc). The purpose of this class is to be used instead of a Bitmap class where we do not need full set of drawing operations, but only read, write and storage of pixel data.

The PixelBuffer is a derived class, which contains owned buffer.

Changed Bitmap class to contain a pixel buffer compatible with PixelBuffer directly, instead of letting Allegro's BITMAP to allocate one. Added couple of functions to Allegro sources, which let create BITMAP struct without allocating actual data, and insert our custom buffer pointer instead. This means that Allegro's BITMAP won't own the data, but only reference it. This lets to construct Bitmap by moving (swapping) PixelBuffer contents, instead of copying.

Moved bitmap loading and saving into a separate files called "image_file.h/cpp". The loading functions now create and return PixelBuffer, saving function work with BitmapData. Bitmap object is only created after loading was successful, from a valid PixelBuffer object. The functions themselves mostly did not change, only have Bitmap getters replaced with respective getters of BitmapData object.

ericoporto commented 6 months ago

This appears to be working correctly upon testing saving a "bmp" bitmap from script and loading it.

But I am getting an assert error when trying to load a PCX file in bitmapdata.h in line 124:

    inline uint8_t *GetLine(size_t i)
    {
        assert(_stride * i < _dataSize);
        return &_buf[_stride * i];
    }

The problem is _dataSize is zero. The issue is the bpp of this pcx is 24-bits (it just has no alpha channel!), so it fails ColorDepthToPixelFormat in bitmapdata.h. I think pcx files are always either 8-bit or 24-bit. Edit: according to wikipedia on PCX I am wrong, there are quite a few pcx bpp.

Edit: save game screenshots, I think are 24-bit too.

ericoporto commented 6 months ago

Hey, this looks like it's working with the games I tested with now. The only thing I noticed is that BitmapData::GetPixel(int x, int y) in bitmapdata.cpp is implemented for the case of 1 and 4 bits but not for 15 or 24 bits.

ericoporto commented 6 months ago

I can't test the 15-bit as I don't know how to generate image for it but this seems like it's working alright with 24-bit and the other things.