示例#1
0
        private void addFrame_Click(object sender, EventArgs e)
        {
            sprite.RDTSpriteNumFrames++;
            curFrame++;

            rdtSubfileData newSubFileData = new rdtSubfileData(images[curFrame - 1]);

            images.Insert(curFrame, newSubFileData);           //create new dummy image for this to fill
            palettes.Insert(curFrame, palettes[curFrame - 1]); //create new dummy image for this to fill
            sprite.RDTSpriteFrameDurations.Insert(curFrame, sprite.RDTSpriteFrameDurations[curFrame - 1]);

            GetNewFrameImageFromFile(true);
        }
示例#2
0
        public rdtSubfileData(rdtSubfileData basis)
        {
            parentfile = basis.parentfile;
            filebytes  = new byte[basis.filebytes.Length];
            Array.Copy(basis.filebytes, 0, filebytes, 0, basis.filebytes.Length);
            IndexInList    = basis.IndexInList;
            subfileType    = basis.subfileType;
            spriteSettings = basis.spriteSettings;

            graphicsType = basis.graphicsType;

            width  = basis.width;
            height = basis.height;

            offsetX = basis.offsetX;
            offsetY = basis.offsetY;

            image = basis.image;

            writeAddress = basis.writeAddress;
        }
示例#3
0
        public void ReadFile()         //actually reads the file into filebytes
        {
            if (parentarcfile != null) //if it's an arc subfile
            {
                if (offset != 0)
                {
                    filebytes = new byte[size];
                    Array.Copy(parentarcfile.filebytes, offset, filebytes, 0, size);
                }

                DecompressFile();   //check for compression

                switch (Path.GetExtension(filename).ToLower())
                {
                case ".st":
                    ReadTextFileEPF();
                    break;

                case ".st2":
                    ReadTextFileHR();
                    break;
                }
            }
            else if (parentrdtfile != null)                         //if it's an rdt subfile
            {
                rdtSubfileData subfileTable = new rdtSubfileData(); //read this subfile's table
                int            overallIndex = 0;
                subfileTable.IndexInList = overallIndex;

                subfileTable.parentfile = this;
                subfileTable.ReadRawData(parentrdtfile.filebytes, offset);

                rdtSubfileDataList.Add(subfileTable);
                overallIndex++;

                if (subfileTable.filebytes == null || subfileTable.filebytes.Length == 0)
                {
                    return;
                }

                //now read the subfile table
                int numlists = BitConverter.ToInt32(subfileTable.filebytes, 0);  //number of lists e.g. 2: the list of centre bounds, the list of compressed files
                //Console.WriteLine(numlists);

                int curOffset = 4;  //offset in subfile table filebytes

                //From what I can tell, the file offsets in an RDT initially lead to a table (beginning with ID 0x03),
                //which contains lists of file types. The first one tends to be a centre bounds etc file, describing the
                //sprite. The next list tends to contain LZ10 compressed stuff, where the first file is the offset of
                //animation metadata. Then, frame durations. Then after that, it's the offsets of alternating
                //palettes and sprites. All these offsets are relative to the start of the RDT, which is a pain...

                for (int i = 0; i < numlists; i++)
                {
                    int countInList = BitConverter.ToUInt16(subfileTable.filebytes, curOffset);
                    curOffset += 2;
                    for (int f = 0; f < countInList; f++)
                    {
                        rdtSubfileData newSubfile = new rdtSubfileData();
                        newSubfile.IndexInList = overallIndex;
                        newSubfile.parentfile  = this;
                        newSubfile.ReadRawData(parentrdtfile.filebytes, BitConverter.ToInt32(subfileTable.filebytes, curOffset));
                        overallIndex++;

                        if (f == 0)
                        {
                            newSubfile.graphicsType = "GraphicsMetadata";
                        }
                        else if (f == 1)
                        {
                            newSubfile.graphicsType = "GraphicsFrameDurations";
                        }

                        if (f > 1)
                        {
                            if (f % 2 == 0)
                            {
                                newSubfile.graphicsType = "palette";
                            }
                            else
                            {
                                newSubfile.graphicsType = "image";
                            }
                        }

                        newSubfile.Parse();
                        rdtSubfileDataList.Add(newSubfile);
                        curOffset += 4;
                    }
                }

                filebytes = new Byte[1];    //set dummy filebytes so that this file registers as read by EPFExplorer
            }
        }
示例#4
0
        public void AddSubNodesToByteArray(TreeNode node, Byte[] nodeTree)
        {
            nodeTree[posInNodeTree] = (byte)node.Nodes.Count;
            posInNodeTree++;
            int basePos = posInNodeTree;

            posInNodeTree = basePos + (node.Nodes.Count * 5);


            for (int i = 0; i < node.Nodes.Count; i++)
            {
                if (node.Nodes[i].Text == "nullNode")
                {
                    nodeTree[basePos + (i * 5)] = 0x00;
                    archivedfile file = archivedfiles[NullNodesInArchivedFileOrder.IndexOf(node.Nodes[i])];


                    List <rdtSubfileData> backuplist = new List <rdtSubfileData>(file.rdtSubfileDataList);

                    //prepare data for import

                    List <Image>  images   = new List <Image>();
                    List <Byte[]> palettes = new List <Byte[]>();

                    List <rdtSubfileData> savedImages   = new List <rdtSubfileData>();
                    List <rdtSubfileData> savedPalettes = new List <rdtSubfileData>();



                    foreach (rdtSubfileData subfiledata in file.rdtSubfileDataList) //get palettes
                    {
                        if (subfiledata.graphicsType == "palette")
                        {
                            subfiledata.DecompressLZ10IfCompressed();
                            palettes.Add(subfiledata.filebytes);
                            savedPalettes.Add(subfiledata);
                        }
                    }

                    int        imageIndex  = 0;
                    List <int> skipIndices = new List <int>();

                    bool is_modified = false;

                    List <ushort> offsetsX = new List <ushort>();
                    List <ushort> offsetsY = new List <ushort>();

                    foreach (rdtSubfileData subfiledata in file.rdtSubfileDataList) //get images
                    {
                        if (subfiledata.graphicsType == "image")
                        {
                            if (subfiledata.image != null) //if it was modified or viewed by the user, use the modified one
                            {
                                images.Add(subfiledata.image);
                                savedImages.Add(subfiledata);
                                is_modified = true;
                            }
                            else          //otherwise, just read the existing image for the first time and apply the existing palette
                            {
                                subfiledata.LoadImage(file.GetPalette(palettes[imageIndex], 1, file.RDTSpriteBPP));
                                images.Add(subfiledata.image);
                                skipIndices.Add(images.Count - 1); //so that we know not to bother rereading it and its palette
                                savedImages.Add(subfiledata);
                            }
                            offsetsX.Add(subfiledata.offsetX);
                            offsetsY.Add(subfiledata.offsetY);

                            imageIndex++;
                        }
                    }

                    //remove the existing images and palette subfiledata in the archivedfile
                    int indexOfFirstImageOrPalette = 0;

                    foreach (rdtSubfileData f in file.rdtSubfileDataList)
                    {
                        if (f.graphicsType != "image" && f.graphicsType != "palette")
                        {
                            indexOfFirstImageOrPalette++;
                            continue;
                        }
                        break;
                    }

                    file.rdtSubfileDataList.RemoveRange(indexOfFirstImageOrPalette, file.rdtSubfileDataList.Count - indexOfFirstImageOrPalette);


                    //now we add the updated images back to the list


                    //make global palette for sprite

                    rdtSubfileData newPalette = new rdtSubfileData();



                    // read individual frames

                    for (int j = 0; j < images.Count; j++)
                    {
                        //make palette

                        newPalette = new rdtSubfileData();

                        if (skipIndices.Contains(j))       //no need to reread image data, as the user didn't edit or view it
                        {
                            newPalette = savedPalettes[0];
                            newPalette.graphicsType = "palette";
                            newPalette.DecompressLZ10IfCompressed();
                            file.rdtSubfileDataList.Add(newPalette);
                            file.rdtSubfileDataList.Add(savedImages[j]);
                        }
                        else                       //the user edited or viewed this file, so rebuild the image and palette
                        {
                            //make palette
                            newPalette = new rdtSubfileData();
                            Color[] palette = new Color[0];

                            if (file.RDTSpriteBPP == 4)
                            {
                                palette = new Color[16];
                            }
                            else if (file.RDTSpriteBPP == 8)
                            {
                                palette = new Color[256];
                            }

                            //put image colours in palette

                            if (file.RDTSpriteAlphaColour.A == 0 && file.RDTSpriteAlphaColour.R == 0 && file.RDTSpriteAlphaColour.G == 0 && file.RDTSpriteAlphaColour.B == 0)
                            {
                                file.RDTSpriteAlphaColour = file.GetPalette(palettes[j], 1, file.RDTSpriteBPP)[0];
                            }

                            Color[] coloursToAdd = Get_Unique_Colours(images, palette.Length);
                            Array.Copy(coloursToAdd, 0, palette, 0, coloursToAdd.Length);
                            //Console.WriteLine("number of unique colours: " + coloursToAdd.Length);

                            file.RDTSpriteAlphaColour = Color.FromArgb(0x00, file.RDTSpriteAlphaColour.R, file.RDTSpriteAlphaColour.G, file.RDTSpriteAlphaColour.B); //make sure the dummy A value is 0x00

                            //now make sure the alpha colour is at index 0
                            if (palette[0] != file.RDTSpriteAlphaColour)
                            {
                                int checkIndex = 0;

                                while (checkIndex < palette.Length)     //go through the palette looking for the alpha colour's current position
                                {
                                    if (palette[checkIndex] == file.RDTSpriteAlphaColour)
                                    {
                                        break;
                                    }
                                    checkIndex++;
                                }

                                //swap the alpha colour into index 0, and the index 0 colour to where the alpha colour used to be
                                palette[checkIndex] = palette[0];
                                palette[0]          = file.RDTSpriteAlphaColour;
                            }

                            //CREATE BINARY NBFC IMAGE AND PALETTE, THEN MAKE SUBFILEDATAS FOR THEM AND ADD THEM TO LIST

                            //create binary palette


                            newPalette.subfileType  = 0x04;
                            newPalette.graphicsType = "palette";

                            newPalette.filebytes = new byte[1 + (palette.Length * 2)];

                            int colorindex = 0;

                            foreach (Color c in palette)
                            {
                                ushort ABGR1555Color = form1.ColorToABGR1555(c);
                                newPalette.filebytes[1 + (colorindex * 2)] = (byte)(ABGR1555Color & 0x00FF);
                                newPalette.filebytes[2 + (colorindex * 2)] = (byte)((ABGR1555Color & 0xFF00) >> 8);

                                colorindex++;
                            }

                            file.rdtSubfileDataList.Add(newPalette);

                            //create binary image here

                            rdtSubfileData newImage = new rdtSubfileData();
                            newImage.subfileType = 0x04;

                            int fakeWidth = images[j].Width;       //fakewidth should only be used when setting the size of the byte array, and nowhere else!

                            if (file.RDTSpriteBPP == 4)
                            {
                                while (fakeWidth % 2 != 0)
                                {
                                    fakeWidth++;
                                }
                                newImage.filebytes = new byte[8 + ((fakeWidth / 2) * images[j].Height)];
                            }
                            else
                            {
                                newImage.filebytes = new byte[8 + (fakeWidth * images[j].Height)];
                            }

                            form1.WriteU16ToArray(newImage.filebytes, 0, (ushort)images[j].Width);
                            form1.WriteU16ToArray(newImage.filebytes, 2, (ushort)images[j].Height);
                            form1.WriteU16ToArray(newImage.filebytes, 4, offsetsX[j]);
                            form1.WriteU16ToArray(newImage.filebytes, 6, offsetsY[j]);

                            int curOffset = 8;

                            Bitmap imageTemp = (Bitmap)images[j];

                            Color newPixel;

                            if (file.RDTSpriteBPP == 4)
                            {
                                for (int y = 0; y < imageTemp.Height; y++)
                                {
                                    for (int x = 0; x < imageTemp.Width; x++)
                                    {
                                        newPixel = imageTemp.GetPixel(x, y);
                                        newImage.filebytes[curOffset] = (byte)(newImage.filebytes[curOffset] | (byte)form1.FindIndexOfColorInPalette(palette, Color.FromArgb(newPixel.A, newPixel.R & 0xF8, newPixel.G & 0xF8, newPixel.B & 0xF8)));
                                        if (x < imageTemp.Width - 1)
                                        {
                                            x++;
                                            newPixel = imageTemp.GetPixel(x, y);
                                            newImage.filebytes[curOffset] = (byte)(newImage.filebytes[curOffset] | (byte)(form1.FindIndexOfColorInPalette(palette, Color.FromArgb(newPixel.A, newPixel.R & 0xF8, newPixel.G & 0xF8, newPixel.B & 0xF8)) << 4));
                                        }

                                        curOffset++;
                                    }
                                }
                            }
                            else
                            {
                                for (int y = 0; y < imageTemp.Height; y++)
                                {
                                    for (int x = 0; x < imageTemp.Width; x++)
                                    {
                                        newPixel = imageTemp.GetPixel(x, y);
                                        newImage.filebytes[curOffset] = (byte)form1.FindIndexOfColorInPalette(palette, Color.FromArgb(newPixel.A, newPixel.R & 0xF8, newPixel.G & 0xF8, newPixel.B & 0xF8));
                                        curOffset++;
                                    }
                                }
                            }

                            file.rdtSubfileDataList.Add(newImage);
                        }
                    }

                    form1.WriteU16ToArray(file.rdtSubfileDataList[2].filebytes, 0, file.RDTSpriteNumFrames);
                    form1.WriteU16ToArray(file.rdtSubfileDataList[2].filebytes, 2, file.RDTSpriteWidth);
                    form1.WriteU16ToArray(file.rdtSubfileDataList[2].filebytes, 4, file.RDTSpriteHeight);
                    file.rdtSubfileDataList[2].filebytes[6] = (byte)file.RDTSpriteBPP;

                    file.rdtSubfileDataList[3].filebytes = new byte[file.RDTSpriteNumFrames * 2];

                    for (int j = 0; j < file.RDTSpriteFrameDurations.Count; j++)
                    {
                        form1.WriteU16ToArray(file.rdtSubfileDataList[3].filebytes, j * 2, file.RDTSpriteFrameDurations[j]);
                    }



                    file.rdtSubfileDataList[1].RebuildFilebytesFromSettings();  //rebuild centre bounds etc config


                    int offsetOfSubfileTable = 0x11 + nodeTree.Length + data.Count;

                    file.rdtSubfileDataList[0].filebytes = new Byte[8 + ((file.rdtSubfileDataList.Count - 1) * 4)];  //make space in the subfile table

                    file.rdtSubfileDataList[0].filebytes[0] = 2;
                    file.rdtSubfileDataList[0].filebytes[4] = 1;
                    form1.WriteU16ToArray(file.rdtSubfileDataList[0].filebytes, 0x0A, (ushort)(file.rdtSubfileDataList.Count - 2));

                    int pos_in_subfiletable = 0x0C;


                    //a lot of things need to be updated before the following happens! I don't know if they all read into their filebytes in the first place, so you need to rebuild them first!

                    int spritePaletteOffset = 0; //global palette for the sprite so that it can just be pasted around


                    foreach (rdtSubfileData subfiledata in file.rdtSubfileDataList)
                    {
                        subfiledata.writeAddress = 0;

                        int  offsetOfThisSubfile = 0;
                        bool dontWrite           = false;

                        int offset_in_subfiletable_where_address_should_be_written = (offsetOfSubfileTable - (0x11 + nodeTree.Length)) + pos_in_subfiletable;

                        if (file.rdtSubfileDataList.IndexOf(subfiledata) > 0) //if it's not the subfile table, add an entry to the subfile table
                        {
                            if (offsetOfThisSubfile == 0)                     //if it wasn't written to by the previous check
                            {
                                offsetOfThisSubfile      = 0x11 + nodeTree.Length + data.Count;
                                subfiledata.writeAddress = offsetOfThisSubfile;
                            }

                            if (subfiledata.graphicsType == "palette" && spritePaletteOffset != 0)  //if it's not the first palette to be processed, just store a reference to the earlier palette
                            {
                                dontWrite           = true;
                                offsetOfThisSubfile = spritePaletteOffset;
                            }
                            else if (subfiledata.graphicsType == "palette") //but if it is the first one, store its offset so we can reference it later
                            {
                                spritePaletteOffset = offsetOfThisSubfile;
                            }


                            subfiledata.writeAddress = offsetOfThisSubfile;


                            data[offset_in_subfiletable_where_address_should_be_written]     = (byte)(offsetOfThisSubfile & 0x000000FF);
                            data[offset_in_subfiletable_where_address_should_be_written + 1] = (byte)((offsetOfThisSubfile & 0x0000FF00) >> 8);
                            data[offset_in_subfiletable_where_address_should_be_written + 2] = (byte)((offsetOfThisSubfile & 0x00FF0000) >> 16);
                            data[offset_in_subfiletable_where_address_should_be_written + 3] = (byte)((offsetOfThisSubfile & 0xFF000000) >> 24);

                            if (pos_in_subfiletable == 0x0C)
                            {
                                pos_in_subfiletable += 2;
                            }

                            pos_in_subfiletable += 4;
                        }

                        //now write the subfile file into data

                        if (dontWrite)  //if it's just an instance of an existing subfile, don't write a new one
                        {
                            continue;
                        }


                        //compress if needed

                        if (subfiledata.subfileType == 0x04)
                        {
                            subfiledata.filebytes = DSDecmp.NewestProgram.Compress(subfiledata.filebytes, new DSDecmp.Formats.Nitro.LZ10());
                        }

                        //check for identical files that were already processed

                        if (file.rdtSubfileDataList.IndexOf(subfiledata) > 0 && subfiledata.graphicsType != "palette")
                        {
                            foreach (Byte[] alreadyProcessedFile in AlreadyProcessedFilesAndOffsetsInData.Keys) //if this file is equal to one that was already processed
                            {
                                if (ByteArraysAreEqual(alreadyProcessedFile, subfiledata.filebytes))
                                {
                                    //this file is identical to one that was already processed, so just store a reference to the existing one
                                    dontWrite = true;

                                    data[offset_in_subfiletable_where_address_should_be_written]     = (byte)AlreadyProcessedFilesAndOffsetsInData[alreadyProcessedFile];
                                    data[offset_in_subfiletable_where_address_should_be_written + 1] = (byte)(AlreadyProcessedFilesAndOffsetsInData[alreadyProcessedFile] >> 8);
                                    data[offset_in_subfiletable_where_address_should_be_written + 2] = (byte)(AlreadyProcessedFilesAndOffsetsInData[alreadyProcessedFile] >> 16);
                                    data[offset_in_subfiletable_where_address_should_be_written + 3] = (byte)(AlreadyProcessedFilesAndOffsetsInData[alreadyProcessedFile] >> 24);
                                    break;
                                }
                            }
                        }


                        if (dontWrite)  //check again for dontwrite
                        {
                            continue;
                        }

                        //otherwise, write subfiletype and size, then write filebytes

                        AlreadyProcessedFilesAndOffsetsInData.Add(subfiledata.filebytes, offsetOfThisSubfile);
                        //Console.WriteLine(AlreadyProcessedFilesAndOffsetsInData.Count);

                        data.Add((byte)subfiledata.subfileType);
                        data.Add((byte)0);

                        data.Add((byte)(subfiledata.filebytes.Length & 0x000000FF));
                        data.Add((byte)((subfiledata.filebytes.Length & 0x0000FF00) >> 8));
                        data.Add((byte)((subfiledata.filebytes.Length & 0x00FF0000) >> 16));
                        data.Add((byte)((subfiledata.filebytes.Length & 0xFF000000) >> 24));

                        foreach (Byte b in subfiledata.filebytes)
                        {
                            data.Add(b);
                        }
                    }

                    form1.WriteU32ToArray(nodeTree, basePos + (i * 5) + 1, (uint)offsetOfSubfileTable); //write offset of the subfile table to the last node in the string

                    file.rdtSubfileDataList = backuplist;
                }
                else
                {
                    nodeTree[basePos + (i * 5)] = (byte)node.Nodes[i].Text[0];
                    form1.WriteU32ToArray(nodeTree, basePos + (i * 5) + 1, 0x11 + (uint)posInNodeTree);
                    AddSubNodesToByteArray(node.Nodes[i], nodeTree);
                }
            }
        }
示例#5
0
        public void GetNewFrameImageFromFile(bool addingFrame)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            openFileDialog1.Title           = "Select image file";
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            openFileDialog1.Filter          = "png (*.png)|*.png";

            bool failed = false;

            if (tempPalette == null)
            {
                tempPalette = new List <Color>();
            }

            rdtSubfileData oldImage   = new rdtSubfileData(images[curFrame]);
            List <Color>   oldPalette = new List <Color>(tempPalette);

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                images[curFrame].image = (Bitmap)Image.FromFile(openFileDialog1.FileName);

                //make palette
                tempPalette = new List <Color>();

                Color potentialNewColour;

                for (int y = 0; y < images[curFrame].image.Height; y++)
                {
                    for (int x = 0; x < images[curFrame].image.Width; x++)
                    {
                        potentialNewColour = ((Bitmap)images[curFrame].image).GetPixel(x, y);
                        potentialNewColour = Color.FromArgb(0x00, potentialNewColour.R & 0xF8, potentialNewColour.G & 0xF8, potentialNewColour.B & 0xF8);

                        if (!tempPalette.Contains(potentialNewColour))
                        {
                            tempPalette.Add(potentialNewColour);
                        }

                        if (tempPalette.Count > 256)
                        {
                            break;
                        }
                    }
                }

                if (sprite.RDTSpriteBPP == 4 && tempPalette.Count > 16)
                {
                    MessageBox.Show("For a 4BPP sprite, you should have a maximum of 16 colours.\nYou had: " + tempPalette.Count + " colours.", "Too many colours!", MessageBoxButtons.OK);
                    failed = true;
                }
                else if (sprite.RDTSpriteBPP == 8 && tempPalette.Count > 256)
                {
                    MessageBox.Show("For an 8BPP sprite, you should have a maximum of 256 colours.\nYou had: " + tempPalette.Count + " colours.", "Too many colours!", MessageBoxButtons.OK);
                    failed = true;
                }

                if (!failed)
                {
                    //guess alpha colour
                    sprite.RDTSpriteAlphaColour = ((Bitmap)images[curFrame].image).GetPixel(0, 0);   //assume the top left corner is the alpha colour. The user can change this later if they wish
                    sprite.RDTSpriteAlphaColour = Color.FromArgb(0x00, sprite.RDTSpriteAlphaColour.R & 0xF8, sprite.RDTSpriteAlphaColour.G & 0xF8, sprite.RDTSpriteAlphaColour.B & 0xF8);
                    SetAlphaColourDisplay();
                }
            }
            else
            {
                failed = true;
            }

            if (failed)
            {
                if (addingFrame)    //undo the addition of the new frame
                {
                    images.RemoveAt(curFrame);
                    palettes.RemoveAt(curFrame);
                    sprite.RDTSpriteFrameDurations.RemoveAt(curFrame);
                    sprite.RDTSpriteNumFrames--;
                    curFrame--;
                }
                else                //revert to the old frame
                {
                    images[curFrame] = oldImage;
                    tempPalette      = oldPalette;
                }
            }


            SendUpdateToRDT();
        }