示例#1
0
        public static byte[] LZ77_Compress(byte[] data, bool header = false)
        {
            ByteArrayOutputStream res = new ByteArrayOutputStream();

            if (header)
            {
                res.writeUInt(0x37375A4C); //LZ77
            }

            res.writeInt((data.Length << 8) | 0x10);

            byte[] tempBuffer = new byte[16];

            //Current byte to compress.
            int current = 0;

            while (current < data.Length)
            {
                int  tempBufferCursor = 0;
                byte blockFlags       = 0;
                for (int i = 0; i < 8; i++)

                {
                    //Not sure if this is needed. The DS probably ignores this data.
                    if (current >= data.Length)
                    {
                        tempBuffer[tempBufferCursor++] = 0;
                        continue;
                    }

                    int searchPos = 0;
                    int searchLen = 0;
                    LZ77_Compress_Search(data, current, out searchPos, out searchLen);
                    int searchDisp = current - searchPos - 1;
                    if (searchLen > 2) //We found a big match, let's write a compressed block.
                    {
                        blockFlags |= (byte)(1 << (7 - i));
                        tempBuffer[tempBufferCursor++] = (byte)((((searchLen - 3) & 0xF) << 4) + ((searchDisp >> 8) & 0xF));
                        tempBuffer[tempBufferCursor++] = (byte)(searchDisp & 0xFF);
                        current += searchLen;
                    }
                    else
                    {
                        tempBuffer[tempBufferCursor++] = data[current++];
                    }
                }

                res.writeByte(blockFlags);
                for (int i = 0; i < tempBufferCursor; i++)
                {
                    res.writeByte(tempBuffer[i]);
                }
            }

            return(res.getArray());
        }
示例#2
0
 public void writeZone(ByteArrayOutputStream outp)
 {
     outp.writeUShort((ushort)X);
     outp.writeUShort((ushort)Y);
     outp.writeUShort((ushort)Width);
     outp.writeUShort((ushort)Height);
     outp.writeByte((byte)Number);
     outp.writeByte(0);
     outp.writeByte(0);
     outp.writeByte(0);
 }
示例#3
0
 public void save(ByteArrayOutputStream outp)
 {
     foreach (List <ObjectDefTile> row in tiles)
     {
         foreach (ObjectDefTile t in row)
         {
             t.write(outp);
         }
         outp.writeByte(0xFE); //new line
     }
     outp.writeByte(0xFF);     //end object
 }
示例#4
0
        public void saveObjects()
        {
            ByteArrayOutputStream eObjIndexFile = new ByteArrayOutputStream();
            ByteArrayOutputStream eObjFile      = new ByteArrayOutputStream();

            for (int i = 0; i < Objects.Length; i++)
            {
                if (Objects[i] == null)
                {
                    break;
                }

                eObjIndexFile.writeUShort((ushort)eObjFile.getPos());
                eObjIndexFile.writeByte((byte)Objects[i].width);
                eObjIndexFile.writeByte((byte)Objects[i].height);
                Objects[i].save(eObjFile);
            }

            ObjFile.replace(eObjFile.getArray(), this);
            ObjIndexFile.replace(eObjIndexFile.getArray(), this);
        }
示例#5
0
 public void write(ByteArrayOutputStream outp)
 {
     if (controlTile)
     {
         outp.writeByte(controlByte);
     }
     else if (emptyTile)
     {
         outp.writeByte(controlByte);
         outp.writeByte(0);
         outp.writeByte(0);
     }
     else
     {
         outp.writeByte(controlByte);
         outp.writeByte((byte)(tileID % 256));
         outp.writeByte((byte)(tileID / 256 + t.ObjectDefTileOffset));
     }
 }
示例#6
0
        public void write(ByteArrayOutputStream outp, ByteArrayOutputStream cam, int camID)
        {
            outp.writeUShort((ushort)X);
            outp.writeUShort((ushort)Y);
            outp.writeUShort((ushort)Width);
            outp.writeUShort((ushort)Height);
            outp.writeByte((byte)Number);
            outp.writeByte((byte)camID);
            outp.writeByte((byte)Music);
            outp.writeByte((byte)Unknown1);
            outp.writeByte((byte)Unknown2);
            outp.writeByte((byte)Unknown3);
            outp.writeByte((byte)Lighting);
            outp.writeByte((byte)FlagpoleID);

            cam.writeInt(CameraTop);
            cam.writeInt(CameraBottom);
            cam.writeInt(CameraTopSpin);
            cam.writeInt(CameraBottomSpin);
            cam.writeUShort((ushort)camID);
            cam.writeUShort((ushort)CameraBottomStick);
            cam.writeUInt(0); //This seems just padding.
        }
示例#7
0
        private void importPNGButton_Click(object sender, EventArgs e)
        {
            getFiles();

            if (GFXFile == null)
            {
                return;
            }
            if (PalFile == null)
            {
                return;
            }
            if (LayoutFile == null)
            {
                return;
            }

            // Create a local copy because the global variables could be overwritten while the background is importing
            File myGFXFile    = GFXFile;
            File myPalFile    = PalFile;
            File myLayoutFile = LayoutFile;

            int offs    = bg.topLayer ? 256 : 576;
            int palOffs = bg.topLayer ? 8 : 10;

            if (bg.mappedTileset)
            {
                offs    = 192;
                palOffs = 2;
            }

            OpenFileDialog ofd = new OpenFileDialog();

            ofd.Filter          = LanguageManager.Get("Filters", "png");
            ofd.CheckFileExists = true;

            if (ofd.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            string filename = ofd.FileName;

            Bitmap b = new Bitmap(filename);

            if (b.Size != new Size(512, 512))
            {
                throw new Exception("Wrong image size");
            }

            BgPNGImportPrompt bgPrompt = new BgPNGImportPrompt(bg.topLayer, bg.mappedTileset);

            if (!bgPrompt.finished)
            {
                return;
            }

            ImageTiler ti;

            if (bg.topLayer)
            {
                bgPrompt.bgFirstTileOffset = 0;
                ti = new ImageTiler(b, new Size(256, bgPrompt.fgHeightInPixels), new Size(512, 512), 50);
            }
            else
            {
                ti = new ImageTiler(b, new Size(256, bgPrompt.bgHeightInPixels), new Size(512, 512), 50);
            }

            Color[] palette = ImageIndexer.createPaletteForImage(b);

            ByteArrayOutputStream oo = new ByteArrayOutputStream();

            for (int i = 0; i < palette.Length; i++)
            {
                oo.writeUShort(NSMBTileset.toRGB15(palette[i]));
            }

            ByteArrayOutputStream offsetBitmapData = new ByteArrayOutputStream();

            for (int i = 0; i < 256 * bgPrompt.bgFirstTileOffset; i++)
            {
                offsetBitmapData.writeByte(0);
            }

            //byte[] newBitmapData = ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette);
            byte[] newBitmapData = new byte[offsetBitmapData.getArray().Length + ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette).Length];
            Buffer.BlockCopy(offsetBitmapData.getArray(), 0, newBitmapData, 0, offsetBitmapData.getArray().Length);
            Buffer.BlockCopy(ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette), 0, newBitmapData, offsetBitmapData.getArray().Length, ImageIndexer.indexImageWithPalette(ti.tileBuffer, palette).Length);

            myPalFile.beginEdit(this);
            myPalFile.replace(ROM.LZ77_Compress(oo.getArray()), this);
            myPalFile.endEdit(this);
            myGFXFile.beginEdit(this);
            myGFXFile.replace(ROM.LZ77_Compress(newBitmapData), this);
            myGFXFile.endEdit(this);
            b.Dispose();

            ByteArrayOutputStream layout = new ByteArrayOutputStream();

            for (int y = 0; y < ti.heightTileCount; y++)
            {
                for (int x = 0; x < ti.widthTileCount; x++)
                {
                    layout.writeUShort((ushort)((ti.tileMap[x, y] + offs + (bgPrompt.bgFirstTileOffset * 4)) | (palOffs << 12)));
                }
            }

            myLayoutFile.beginEdit(this);
            myLayoutFile.replace(ROM.LZ77_Compress(layout.getArray()), this);
            myLayoutFile.endEdit(this);
        }