Beispiel #1
0
        public static WriteableBitmap Decode(byte[] data, int startOffset, int length)
        {
            int endIndex = startOffset + length;
            int finalw = BitConverter.ToInt16(data, startOffset);
            int finalh = BitConverter.ToInt16(data, startOffset + 2);

            int sourcew = finalw;
            int sourceh = finalh;
            PalEntry[] pixels = null;

            int curIdx = 0x80 + startOffset;
            GIFTag gifTag = new GIFTag();
            gifTag.parse(data, curIdx);

            // This is basically heuristics. Writing a full GIF parser is complex and as the texture files are written by a tool,
            // we can safely make some assumptions about their structure.
            if (gifTag.nloop == 4) {

                int palw = DataUtil.getLEShort(data, curIdx + 0x30);
                int palh = DataUtil.getLEShort(data, curIdx + 0x34);

                curIdx += 0x50;
                GIFTag gifTag2 = new GIFTag();
                gifTag2.parse(data, curIdx);

                // 8 bit palletised
                PalEntry[] palette = PalEntry.readPalette(data, curIdx + 0x10, palw, palh);

                palette = PalEntry.unswizzlePalette(palette);

                int palLen = palw * palh * 4;
                curIdx += (palLen + 0x10);

                GIFTag gifTag50 = new GIFTag();
                gifTag50.parse(data, curIdx);
                curIdx += 0x20;

                int dbw = (sourcew / 2 + 0x07) & ~0x07;
                int dbh = (sourceh / 2 + 0x07) & ~0x07;

                // The following should be a loop, there are repeating sections
                while (curIdx < endIndex - 0x10) {
                    GIFTag gifTag3 = new GIFTag();
                    gifTag3.parse(data, curIdx);

                    int dimOffset = 0x10;

                    int thisRrw = DataUtil.getLEShort(data, curIdx + dimOffset);
                    int thisRrh = DataUtil.getLEShort(data, curIdx + dimOffset + 4);

                    int startx = DataUtil.getLEShort(data, curIdx + dimOffset + 20);
                    int starty = DataUtil.getLEShort(data, curIdx + dimOffset + 22);

                    curIdx += gifTag.nloop * 0x10 + 0x10;
                    pixels = readPixels32(pixels, data, palette, curIdx, startx, starty, thisRrw, thisRrh, dbw, dbh);
                    curIdx += thisRrw * thisRrh * 4;
                }
                if (palLen != 64) {
                    pixels = unswizzle8bpp(pixels, dbw * 2, dbh * 2);
                    sourcew = dbw * 2;
                    sourceh = dbh * 2;
                } else {
                    sourcew = dbw;
                    sourceh = dbh;
                }

            } else if (gifTag.nloop == 3) {
                GIFTag gifTag2 = new GIFTag();
                gifTag2.parse(data, startOffset + 0xC0);

                if (gifTag2.flg == 2) {
                    // image mode
                    pixels = readPixels32(data, startOffset + 0xD0, finalw, finalh);
                }
            }
            WriteableBitmap image = null;
            if (finalw != 0 && pixels != null) {
                image = new WriteableBitmap(
                    finalw, finalh,
                    96, 96,
                    PixelFormats.Bgr32,
                    null);
                image.Lock();
                unsafe {
                    IntPtr pBackBuffer = image.BackBuffer;
                    for (int y = 0; y < sourceh; ++y) {
                        for (int x = 0; x < sourcew; ++x) {
                            PalEntry pixel = pixels[y * sourcew + x];
                            if (pixel != null) {
                                if (x < finalw && y < finalh) {
                                    var p = pBackBuffer + y * image.BackBufferStride + x * 4;
                                    *((int*)p) = pixel.argb();
                                }
                            }
                        }
                    }
                }
                // Specify the area of the bitmap that changed.
                image.AddDirtyRect(new Int32Rect(0, 0, finalw, finalh));

                // Release the back buffer and make it available for display.
                image.Unlock();
            }
            return image;
        }
Beispiel #2
0
        public static WriteableBitmap Decode(byte[] data, int startOffset, int length)
        {
            int endIndex = startOffset + length;
            int finalw   = BitConverter.ToInt16(data, startOffset);
            int finalh   = BitConverter.ToInt16(data, startOffset + 2);

            int sourcew = finalw;
            int sourceh = finalh;

            PalEntry[] pixels = null;

            int    curIdx = 0x80 + startOffset;
            GIFTag gifTag = new GIFTag();

            gifTag.parse(data, curIdx);

            // This is basically heuristics. Writing a full GIF parser is complex and as the texture files are written by a tool,
            // we can safely make some assumptions about their structure.
            if (gifTag.nloop == 4)
            {
                int palw = DataUtil.getLEShort(data, curIdx + 0x30);
                int palh = DataUtil.getLEShort(data, curIdx + 0x34);

                curIdx += 0x50;
                GIFTag gifTag2 = new GIFTag();
                gifTag2.parse(data, curIdx);

                // 8 bit palletised
                PalEntry[] palette = PalEntry.readPalette(data, curIdx + 0x10, palw, palh);

                palette = PalEntry.unswizzlePalette(palette);

                int palLen = palw * palh * 4;
                curIdx += (palLen + 0x10);

                GIFTag gifTag50 = new GIFTag();
                gifTag50.parse(data, curIdx);
                curIdx += 0x20;

                int dbw = (sourcew / 2 + 0x07) & ~0x07;
                int dbh = (sourceh / 2 + 0x07) & ~0x07;

                // The following should be a loop, there are repeating sections
                while (curIdx < endIndex - 0x10)
                {
                    GIFTag gifTag3 = new GIFTag();
                    gifTag3.parse(data, curIdx);

                    int dimOffset = 0x10;

                    int thisRrw = DataUtil.getLEShort(data, curIdx + dimOffset);
                    int thisRrh = DataUtil.getLEShort(data, curIdx + dimOffset + 4);

                    int startx = DataUtil.getLEShort(data, curIdx + dimOffset + 20);
                    int starty = DataUtil.getLEShort(data, curIdx + dimOffset + 22);

                    curIdx += gifTag.nloop * 0x10 + 0x10;
                    pixels  = readPixels32(pixels, data, palette, curIdx, startx, starty, thisRrw, thisRrh, dbw, dbh);
                    curIdx += thisRrw * thisRrh * 4;
                }
                if (palLen != 64)
                {
                    pixels  = unswizzle8bpp(pixels, dbw * 2, dbh * 2);
                    sourcew = dbw * 2;
                    sourceh = dbh * 2;
                }
                else
                {
                    sourcew = dbw;
                    sourceh = dbh;
                }
            }
            else if (gifTag.nloop == 3)
            {
                GIFTag gifTag2 = new GIFTag();
                gifTag2.parse(data, startOffset + 0xC0);

                if (gifTag2.flg == 2)
                {
                    // image mode
                    pixels = readPixels32(data, startOffset + 0xD0, finalw, finalh);
                }
            }
            WriteableBitmap image = null;

            if (finalw != 0 && pixels != null)
            {
                image = new WriteableBitmap(
                    finalw, finalh,
                    96, 96,
                    PixelFormats.Bgr32,
                    null);
                image.Lock();
                unsafe {
                    IntPtr pBackBuffer = image.BackBuffer;
                    for (int y = 0; y < sourceh; ++y)
                    {
                        for (int x = 0; x < sourcew; ++x)
                        {
                            PalEntry pixel = pixels[y * sourcew + x];
                            if (pixel != null)
                            {
                                if (x < finalw && y < finalh)
                                {
                                    var p = pBackBuffer + y * image.BackBufferStride + x * 4;
                                    *((int *)p) = pixel.argb();
                                }
                            }
                        }
                    }
                }
                // Specify the area of the bitmap that changed.
                image.AddDirtyRect(new Int32Rect(0, 0, finalw, finalh));

                // Release the back buffer and make it available for display.
                image.Unlock();
            }
            return(image);
        }
Beispiel #3
0
        public static WriteableBitmap Decode(byte[] data, int startOffset)
        {
            var gsMem = new GSMemory();

            var length = DataUtil.getLEShort(data, startOffset + 6) * 16;

            int finalw      = BitConverter.ToInt16(data, startOffset);
            int finalh      = BitConverter.ToInt16(data, startOffset + 2);
            var offsetToGIF = DataUtil.getLEInt(data, startOffset + 16);

            var sourcew = finalw;
            var sourceh = finalh;

            PalEntry[] pixels = null;
            byte[]     bytes  = null;

            var curIdx   = offsetToGIF + startOffset;
            var endIndex = curIdx + length;

            var gifTag = new GIFTag();

            gifTag.parse(data, curIdx);

            // This is basically heuristics. Writing a full GIF parser is complex and as the texture files are written by a tool,
            // we can safely make some assumptions about their structure.
            if (gifTag.nloop == 4)
            {
                int palw = DataUtil.getLEShort(data, curIdx + 0x30);
                int palh = DataUtil.getLEShort(data, curIdx + 0x34);

                curIdx += gifTag.Length;
                var gifTag2 = new GIFTag();
                gifTag2.parse(data, curIdx);

                // 8 bit palletised
                var palette = PalEntry.readPalette(data, curIdx + GIFTag.Size, palw, palh);

                palette = PalEntry.unswizzlePalette(palette);

                curIdx += gifTag2.Length;
                var destWBytes = (finalw + 0x0f) & ~0x0f;
                var destHBytes = (finalh + 0x0f) & ~0x0f;

                var dpsm   = PSMCT32;
                var dbw    = 0;
                var dbp    = 0;
                var rrw    = 0;
                var rrh    = 0;
                var startx = 0;
                var starty = 0;

                while (curIdx < endIndex - GIFTag.Size)
                {
                    var gifTag3 = new GIFTag();
                    gifTag3.parse(data, curIdx);
                    while (!gifTag3.IsImage)
                    {
                        var trxregOffset = findADEntry(data, curIdx + GIFTag.Size, gifTag3.nloop, TRXREG);
                        if (trxregOffset != 0)
                        {
                            rrw = DataUtil.getLEShort(data, trxregOffset);
                            rrh = DataUtil.getLEShort(data, trxregOffset + 4);
                        }
                        var trxposOffset = findADEntry(data, curIdx + GIFTag.Size, gifTag3.nloop, TRXPOS);
                        if (trxposOffset != 0)
                        {
                            startx = DataUtil.getLEShort(data, trxposOffset + 0x04) & 0x07FF;
                            starty = DataUtil.getLEShort(data, trxposOffset + 0x06) & 0x07FF;
                        }
                        var bitbltOffset = findADEntry(data, curIdx + GIFTag.Size, gifTag3.nloop, BITBLTBUF);
                        if (bitbltOffset != 0)
                        {
                            //int sbw = fileData[bitbltOffset + 0x02] & 0x3F;
                            dbp  = data[bitbltOffset + 0x04] & 0x3FFF;
                            dbw  = data[bitbltOffset + 0x06] & 0x3F;
                            dpsm = data[bitbltOffset + 0x07] & 0x3F;
                        }

                        curIdx += gifTag3.Length;
                        if (curIdx + GIFTag.Size >= endIndex)
                        {
                            break;
                        }

                        gifTag3.parse(data, curIdx);
                    }
                    curIdx += GIFTag.Size;     // image gif tag
                    var bytesToTransfer = gifTag3.nloop * 16;

                    if (palette.Length == 16)
                    {
                        // source is PSMT4. Dest can be PSMT4 or PSMCT32
                        if (dpsm == PSMCT32)
                        {
                            var imageData    = data;
                            var imageDataIdx = curIdx;
                            // check for multiple IMAGE entries.
                            var nextTagInd = bytesToTransfer + curIdx;
                            if (nextTagInd < endIndex - GIFTag.Size)
                            {
                                var imageTag2 = new GIFTag();
                                imageTag2.parse(data, nextTagInd);
                                if (imageTag2.flg == 2)
                                {
                                    // IMAGE
                                    var bytesToTransfer2 = imageTag2.nloop * 16;
                                    imageDataIdx = 0;
                                    imageData    = new byte[bytesToTransfer + bytesToTransfer2];
                                    var j = curIdx;
                                    for (var i = 0; i < bytesToTransfer; ++i)
                                    {
                                        imageData[i] = data[j];
                                    }
                                    j = nextTagInd + GIFTag.Size;
                                    for (var i = bytesToTransfer; i < bytesToTransfer + bytesToTransfer2; ++i)
                                    {
                                        imageData[i] = data[j];
                                    }
                                    bytesToTransfer += imageTag2.Length;
                                }
                            }

                            gsMem.writeTexPSMCT32(dbp, dbw, startx, starty, rrw, rrh, imageData, imageDataIdx);

                            destWBytes = (finalw + 0x3f) & ~0x3f;
                            bytes      = gsMem.readTexPSMT4(dbp, destWBytes / 0x40, startx, starty, destWBytes, destHBytes);
                            bytes      = expand4bit(bytes);
                        }
                        else
                        {
                            // dest and source are the same and so image isn't swizzled
                            bytes = transferPSMT4(bytes, data, curIdx, startx, starty, rrw, rrh, destWBytes, destHBytes);
                        }
                    }
                    else
                    {
                        // source is PSMT8. Dest is always PSMCT32.
                        gsMem.writeTexPSMCT32(dbp, dbw, startx, starty, rrw, rrh, data, curIdx);
                    }
                    curIdx += bytesToTransfer;
                }
                if (palette.Length == 256)
                {
                    destWBytes = (finalw + 0x3f) & ~0x3f;
                    dbw        = destWBytes / 0x40;
                    bytes      = gsMem.readTexPSMT8(dbp, dbw, 0, 0, destWBytes, finalh);
                }
                // THIS IS A HACK
                if (palette.Length == 1024)
                {
                    destWBytes = (finalw + 0x3f) & ~0x3f;
                    dbw        = destWBytes / 0x40;
                    bytes      = gsMem.readTexPSMT8(dbp, dbw, 0, 0, destWBytes, finalh);
                }
                pixels  = applyPalette(palette, bytes);
                sourcew = destWBytes;
                sourceh = destHBytes;
            }
            else if (gifTag.nloop == 3)
            {
                var gifTag2 = new GIFTag();
                gifTag2.parse(data, startOffset + 0xC0);

                if (gifTag2.flg == 2)
                {
                    // image mode
                    pixels = readPixels32(data, startOffset + 0xD0, finalw, finalh);
                }
            }
            WriteableBitmap image = null;

            if (finalw != 0)
            {
                image = new WriteableBitmap(
                    finalw, finalh,
                    96, 96,
                    PixelFormats.Bgra32,
                    null);
                var imageBytes = new int[finalw * finalh];
                var stride     = 4 * finalw;
                var pixOffset  = 0;
                if (pixels != null)
                {
                    for (var y = 0; y < sourceh && y < finalh; ++y)
                    {
                        for (var x = 0; x < sourcew && x < finalw; ++x)
                        {
                            var pixel = pixels[y * sourcew + x];
                            if (pixel != null)
                            {
                                if (x < finalw && y < finalh)
                                {
                                    imageBytes[pixOffset++] = pixel.argb();
                                }
                            }
                        }
                    }
                }
                image.WritePixels(new Int32Rect(0, 0, finalw, finalh), imageBytes, stride, 0);
            }
            return(image);
        }