コード例 #1
0
ファイル: PalEntry.cs プロジェクト: Stranho/bgda-explorer
        public static PalEntry[] readPalette(byte[] fileData, int startOffset, int palw, int palh)
        {
            int numEntries = palw * palh;
            PalEntry[] palette = new PalEntry[numEntries];
            for (int i = 0; i < numEntries; ++i) {
                PalEntry pe = new PalEntry();
                pe.r = fileData[startOffset + i * 4];
                pe.g = fileData[startOffset + i * 4 + 1];
                pe.b = fileData[startOffset + i * 4 + 2];
                pe.a = fileData[startOffset + i * 4 + 3];

                palette[i] = pe;
            }
            return palette;
        }
コード例 #2
0
ファイル: PalEntry.cs プロジェクト: Stranho/bgda-explorer
        public static PalEntry[] unswizzlePalette(PalEntry[] palette)
        {
            if (palette.Length == 256) {
                PalEntry[] unswizzled = new PalEntry[palette.Length];

                int j = 0;
                for (int i = 0; i < 256; i += 32, j += 32) {
                    copy(unswizzled, i, palette, j, 8);
                    copy(unswizzled, i + 16, palette, j + 8, 8);
                    copy(unswizzled, i + 8, palette, j + 16, 8);
                    copy(unswizzled, i + 24, palette, j + 24, 8);
                }
                return unswizzled;
            } else {
                return palette;
            }
        }
コード例 #3
0
ファイル: WorldTexFile.cs プロジェクト: Stranho/bgda-explorer
        private void decodeBlock(int xblock, int yblock, int blockDataStart, int table0Start, WriteableBitmap image, PalEntry[] palette, HuffVal[] huffVals)
        {
            int tableOffset = table0Start + 0x800;
            int table1Len = DataUtil.getLEInt(fileData, tableOffset) * 2;
            int table1Start = tableOffset + 4;
            int table2Start = table1Start + table1Len;
            int table3Start = table2Start + 0x48;

            int[] pix8s = new int[16*16];
            int curpix8=0;
            int startBit=0;
            int prevPixel=0;
            for (int y=0; y<16; ++y){
                for (int x=0; x < 16; ++x){
                    int startWordIdx = startBit / 16;
                    int word1 = DataUtil.getLEUShort(fileData, blockDataStart + startWordIdx * 2);
                    int word2 = DataUtil.getLEUShort(fileData, blockDataStart + startWordIdx * 2 + 2);
                    // if startBit is 0, word == word1
                    // if startBit is 1, word is 15 bits of word1 and 1 bit of word2
                    int word = ((word1 << 16 | word2) >> (16 - (startBit & 0x0f))) & 0xFFFF;

                    int byte1 = (word >> 8) & 0xff;
                    HuffVal hv = huffVals[byte1];
                    int pixCmd;
                    if (hv.numBits != 0){
                        pixCmd = hv.val;
                        startBit += hv.numBits;
                    } else {
                        // Must be more than an 8 bit code
                        int bit=9;
                        int a = word >> (16-bit);
                        int v = DataUtil.getLEInt(fileData, table3Start + bit*4);
                        while (v < a){
                            ++bit;
                            if (bit > 16){
                                throw new Exception("A decoding error occured");
                            }
                            a = word >> (16-bit);
                            v = DataUtil.getLEInt(fileData, table3Start + bit*4);
                        }
                        startBit += bit;
                        int val = DataUtil.getLEInt(fileData, table2Start + bit*4);
                        int table1Index = a + val;

                        pixCmd = DataUtil.getLEShort(fileData, table1Start + table1Index*2);
                    }
                    int pix8 = 0;
                    if (pixCmd < 0x100){
                        pix8 = pixCmd;
                    } else if (pixCmd < 0x105){
                        int backjump = backJumpTable[pixCmd - 0x100];
                        if ((curpix8 + backjump) >= 0){
                            pix8 = pix8s[curpix8 + backjump];
                        } else {
                            throw new Exception("Something went wrong");
                        }
                    } else {
                        int table0Index = (pixCmd - 0x105) + prevPixel * 8;
                        pix8 = fileData[table0Start + table0Index] & 0xFF;
                    }

                    pix8s[curpix8++] = pix8;

                    prevPixel = pix8 & 0xFF;
                    PalEntry pixel = palette[pix8  & 0xFF];
                    var pBackBuffer = image.BackBuffer;
                    int xpos = xblock * 16 + x;
                    int ypos = yblock * 16 + y;
                    var p = pBackBuffer + ypos * image.BackBufferStride + xpos * 4;
                    unsafe
                    {
                        *((int*)p) = pixel.argb();
                    }
                }
            }
        }
コード例 #4
0
ファイル: TexDecoder.cs プロジェクト: Stranho/bgda-explorer
        private static PalEntry[] unswizzle8bpp(PalEntry[] pixels, int w, int h)
        {
            PalEntry[] unswizzled = new PalEntry[pixels.Length];

            for (int y = 0; y < h; ++y) {
                for (int x = 0; x < w; ++x) {

                    int block_location = (y & (~0xf)) * w + (x & (~0xf)) * 2;
                    int swap_selector = (((y + 2) >> 2) & 0x1) * 4;
                    int posY = (((y & (~3)) >> 1) + (y & 1)) & 0x7;
                    int column_location = posY * w * 2 + ((x + swap_selector) & 0x7) * 4;

                    int byte_num = ((y >> 1) & 1) + ((x >> 2) & 2);     // 0,1,2,3

                    int idx = block_location + column_location + byte_num;
                    if (idx < pixels.Length) {
                        unswizzled[(y * w) + x] = pixels[idx];
                    }
                }
            }

            return unswizzled;
        }
コード例 #5
0
ファイル: TexDecoder.cs プロジェクト: Stranho/bgda-explorer
        private static PalEntry[] readPixels32(byte[] fileData, int startOffset, int w, int h)
        {
            int numPixels = w * h;
            PalEntry[] pixels = new PalEntry[numPixels];
            int destIdx = 0;
            int endOffset = startOffset + numPixels * 4;
            for (int idx = startOffset; idx < endOffset; ) {
                PalEntry pe = new PalEntry();
                pe.r = fileData[idx++];
                pe.g = fileData[idx++];
                pe.b = fileData[idx++];
                pe.a = fileData[idx++];

                pixels[destIdx++] = pe;
            }

            return pixels;
        }
コード例 #6
0
ファイル: TexDecoder.cs プロジェクト: Stranho/bgda-explorer
 private static PalEntry[] readPixels32(PalEntry[] pixels, byte[] fileData, PalEntry[] palette, int startOffset, int startx, int starty, int rrw, int rrh, int dbw, int dbh)
 {
     if (palette.Length == 256) {
         int numDestBytes = dbh * dbw * 4;
         int widthBytes = dbw * 4;
         if (pixels == null) {
             pixels = new PalEntry[numDestBytes];
         }
         int idx = startOffset;
         for (int y = 0; y < rrh && (y+starty) < dbh; ++y) {
             for (int x = 0; x < rrw; ++x) {
                 int destIdx = (y+starty) * widthBytes + (x + startx) * 4;
                 pixels[destIdx++] = palette[fileData[idx++] & 0xFF];
                 pixels[destIdx++] = palette[fileData[idx++] & 0xFF];
                 pixels[destIdx++] = palette[fileData[idx++] & 0xFF];
                 pixels[destIdx] = palette[fileData[idx++] & 0xFF];
             }
         }
         return pixels;
     } else {
         int numDestBytes = rrh * dbw;
         if (pixels == null) {
             pixels = new PalEntry[numDestBytes];
         }
         int idx = startOffset;
         bool lowbit = false;
         for (int y = 0; y < rrh; ++y) {
             for (int x = 0; x < rrw; ++x) {
                 int destIdx = (y + starty) * dbw + x + startx;
                 if (!lowbit) {
                     pixels[destIdx] = palette[fileData[idx] >> 4 & 0x0F];
                 } else {
                     pixels[destIdx] = palette[fileData[idx++] & 0x0F];
                 }
                 lowbit = !lowbit;
             }
         }
         return pixels;
     }
 }
コード例 #7
0
ファイル: PalEntry.cs プロジェクト: Stranho/bgda-explorer
 private static void copy(PalEntry[] unswizzled, int i, PalEntry[] swizzled, int j, int num)
 {
     for (int x = 0; x < num; ++x) {
         unswizzled[i + x] = swizzled[j + x];
     }
 }