cntsaifloyd / maplelib2

Automatically exported from code.google.com/p/maplelib2
0 stars 0 forks source link

Loading png property with format type 517 incorrectly #3

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Load the midforest backgrounds and try to view the basic sky background (no = 0)
All you see is a bunch of weird black and white stripes.
I've taken the liberty of fixing this issue in my own wzlib so here is the C++ 
code that I use.
Feel free to port it back over and fix your own code.
Buf1 and Buf2 are just pre-allocated arrays of uint8_t.

    case 517:
        {
            uint32_t len = sprite.data->width*sprite.data->height/128;
            Decompress(length, len);
            for (uint32_t i = 0; i < len*2; i++) {
                uint8_t b4 = (Buf1[i*2]&0x0F)|((Buf1[i*2]&0x0F)<<4);
                uint8_t b3 = (Buf1[i*2]&0xF0)|((Buf1[i*2]&0xF0)>>4);
                uint8_t b2 = (Buf1[i*2+1]&0x0F)|((Buf1[i*2+1]&0x0F)<<4);
                uint8_t b1 = (Buf1[i*2+1]&0xF0)|((Buf1[i*2+1]&0xF0)>>4);
                for (uint32_t j = 0; j < 256; j++) {
                    Buf2[i*1024+j*4] = b1;
                    Buf2[i*1024+j*4+1] = b2;
                    Buf2[i*1024+j*4+2] = b3;
                    Buf2[i*1024+j*4+3] = b4;
                }
            }
            glTexImage2D(GL_TEXTURE_2D, 0, 4, sprite.data->width, sprite.data->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, Buf2);
            break;
        }

Original issue reported on code.google.com by retep998 on 15 Sep 2011 at 6:58

GoogleCodeExporter commented 9 years ago
Actually, that code above is still wrong.
Here is the completely correct code.

uint32_t len = sprite.data->width*sprite.data->height/128;
Decompress(length, len);
for (uint32_t i = 0; i*2 < len; i++) {
    for (uint32_t j = 0; j < 512; j++) {
        Buf2[i*512+j*2] = Buf1[2*i];
        Buf2[i*512+j*2+1] = Buf1[2*i+1];
    }
}
glTexImage2D(GL_TEXTURE_2D, 0, 4, sprite.data->width, sprite.data->height, 0, 
GL_BGR, GL_UNSIGNED_SHORT_5_6_5_REV, Buf2);

Original comment by retep998 on 20 Sep 2011 at 4:48

GoogleCodeExporter commented 9 years ago
517 format actually is R5G6B5 style, similar with 512. and its easy to load, 
just:
Width >>= 4;
Height >>= 4;
And now use 512's way to load.

And the map.wz only two 517 image:
Map.wz\Back\midForest\back\0: 
http://www.huosoft.com/bbs/UpFile/UpAttachment/2011-6/20116231816251.bmp
Map.wz\Back\dryRock\back\0: 
http://www.huosoft.com/bbs/UpFile/UpAttachment/2011-6/20116231816250.bmp

I know map 000050000 use that image.

Original comment by thesh...@live.cn on 22 Sep 2011 at 5:36

GoogleCodeExporter commented 9 years ago
Well, this seems still have a little problem.
Because the origin position is (64, 64).
So only tile copy it.

This is c# code i used.
In WzPngProperty::ParsePng:
                case 517:
                    {
                        int SourceWidth = width >> 4;
                        int SourceHeight = height >> 4;

                        uncompressedSize = SourceWidth * SourceHeight * 2;
                        decBuf = new byte[uncompressedSize];
                        zlib.Read(decBuf, 0, uncompressedSize);

                        bmp = new Bitmap(width, height, PixelFormat.Format16bppRgb565);
                        bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.WriteOnly, PixelFormat.Format16bppRgb565);

                        int CopyCountH = width / SourceWidth;
                        int X = 0, Y = 0;
                        for (Y = 0; Y < height; ++Y)
                        {
                            for (X = 0; X < CopyCountH; ++X)
                            {
                                Marshal.Copy(decBuf, (Y % SourceHeight) * 2,
                                    (IntPtr)(bmpData.Scan0.ToInt32() + (Y * width + X * SourceWidth) * 2),
                                    SourceWidth * 2);
                            }
                        }

                        bmp.UnlockBits(bmpData);
                    }
                    break;

Original comment by thesh...@live.cn on 5 Oct 2011 at 5:04

Attachments: