public void MakeSureTheBlockChecksArgumentsBeforeWritingAnything()
        {
            var virtualDisk = VirtualDiskTestFactory.ConstructDefaultTestDisk();

            DiskBlock block = new DiskBlock(virtualDisk, 10, 0, 0);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentNullException>(
                delegate
            {
                block.WriteBytes(null, 10, 11);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentException>(
                delegate
            {
                block.WriteBytes(new byte[2], 10, 11);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                block.WriteBytes(new byte[2], -10, 11);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                block.WriteBytes(new byte[2], 10, -1);
            });
        }
        public void TryWriteReadCycleForABuffer()
        {
            var virtualDisk = VirtualDiskTestFactory.ConstructDefaultTestDisk();

            DiskBlock block = new DiskBlock(virtualDisk, 10, 0, 0);

            for (int i = 0; i < 2048 / 4; i++)
            {
                byte[]       fourByteBlock = new byte[4];
                MemoryStream writerStream  = new MemoryStream(fourByteBlock);
                BinaryWriter writer        = new BinaryWriter(writerStream);

                writer.Write(i);

                block.WriteBytes(fourByteBlock, 0, fourByteBlock.Length);
            }

            byte[] allBytes = block.ReadAll();

            MemoryStream readerStream = new MemoryStream(allBytes);
            BinaryReader reader       = new BinaryReader(readerStream);

            for (int i = 0; i < 2048 / 4; i++)
            {
                Assert.AreEqual(i, reader.ReadInt32());
            }

            Assert.IsFalse(block.CanAcceptBytesAtCurrentPosition);
        }
        public void MakeSureThePositionChangesAsYouAreWritingToTheBlock()
        {
            var virtualDisk = VirtualDiskTestFactory.ConstructDefaultTestDisk();

            DiskBlock block = new DiskBlock(virtualDisk, 10, 0, 0);

            Assert.AreEqual(0, block.Position);

            block.WriteBytes(new byte[15], 0, 15);

            Assert.AreEqual(15, block.Position); // после последнего записанного байта.

            block.WriteBytes(new byte[15], 9, 6);

            Assert.AreEqual(21, block.Position); // после последнего записанного байта.
        }
        public void MakeSureBlockConstructionIsPossibleOnlyWithSaneArguments()
        {
            var virtualDisk = VirtualDiskTestFactory.ConstructDefaultTestDisk();

            DiskBlock block = new DiskBlock(virtualDisk, 10, 0, 0);

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                new DiskBlock(virtualDisk, 100000000, 0, 0);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentNullException>(
                delegate
            {
                new DiskBlock(null, 10, 0, 0);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                new DiskBlock(virtualDisk, -2, 0, 0);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                new DiskBlock(virtualDisk, 15, virtualDisk.BlockSizeInBytes * 5, 0);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                new DiskBlock(virtualDisk, 15, virtualDisk.BlockSizeInBytes, 56789);
            });

            ExceptionAssert.MakeSureExceptionIsRaisedBy <ArgumentOutOfRangeException>(
                delegate
            {
                new DiskBlock(virtualDisk, 15, virtualDisk.BlockSizeInBytes, -56789);
            });
        }
Beispiel #5
0
        private DiskBlock[] OrderDiskBlocks()
        {
            var result = new DiskBlock[IndexFile.DROO.Rooms.Count];

            //a númeração de discos é chata, e pode pular alguns rooms.
            //o que essa rotina faz é percorrer todos os rooms encontrados
            //e adiciona-los em um array com o mesmo tamanho do Indice de rooms,
            //deixando nulo os indices dos rooms que não estão em uso (number 0 no arquivo de indice).
            //Dessa forma fica muito mais facil atualizar a tabela de offsets de indices.
            List <DiskBlock> diskBlocks = DataFile.GetLFLFs();

            int nextRoomIndex = 0;

            for (int i = 0; i < result.Length; i++)
            {
                if (IndexFile.DROO.Rooms[i].Number == 1)
                {
                    result[i] = diskBlocks[nextRoomIndex];
                    nextRoomIndex++;
                }
            }
            return(result);
        }
Beispiel #6
0
        public override void SetAndRefreshData(Structures.BlockBase blockBase)
        {
            base.SetAndRefreshData(blockBase);

            if (ReferenceEquals(_diskBlock, blockBase))
            {
                return;
            }

            _loading = true;

            _diskBlock = (DiskBlock)blockBase;

            var roomBlock = (RoomBlock)_diskBlock.Childrens.Single(r => r.GetType() == typeof(RoomBlock));

            //Configurando as palettas Padrão
            PalettesData       pals         = roomBlock.GetPALS();
            List <PaletteData> paletteDatas = null;

            if (pals != null)
            {
                paletteDatas = pals.GetWRAP().GetAPALs();
            }
            Palettes.Items.Clear();
            if (paletteDatas != null)
            {
                for (int i = 0; i < paletteDatas.Count; i++)
                {
                    Palettes.Items.Add("Palette #" + i);
                }
            }
            else
            {
                Palettes.Items.Add("Palette #0");
            }
            if (Palettes.SelectedIndex < 0)
            {
                Palettes.SelectedIndex = 0;
            }
            Palettes.Visible = Palettes.Items.Count != 1;
            //Termino de configuração das palettas padrão

            TreeImages.Nodes.Clear();
            _roomImages = new Dictionary <string, RoomBlockImageControl>();
            CreateInfos = new Dictionary <string, ClassCreateInfo>();

            TreeNode backgroundNode = null;

            RoomImage RMIM = roomBlock.GetRMIM();

            if (RMIM.GetIM00().GetSMAP().Strips.Count > 0)
            {
                var createInfo = new ClassCreateInfo();
                createInfo.ImageType = ImageType.Background;
                createInfo.ControlId = "Background";
                CreateInfos.Add("Background", createInfo);

                backgroundNode = TreeImages.Nodes.Add("Background", "Room Background");
            }

            List <ZPlane> zPlanes = RMIM.GetIM00().GetZPlanes();

            for (int i = 0; i < zPlanes.Count; i++)
            {
                string planeKey = "Background ZPlane " + (i + 1);

                var createInfo = new ClassCreateInfo();
                createInfo.ImageType   = ImageType.ZPlane;
                createInfo.ControlId   = planeKey;
                createInfo.ZPlaneIndex = i;
                CreateInfos.Add(planeKey, createInfo);

                if (backgroundNode == null)
                {
                    TreeImages.Nodes.Add(planeKey, "Background Z-Plane " + (i + 1));
                }
                else
                {
                    backgroundNode.Nodes.Add(planeKey, "Z-Plane " + (i + 1));
                }
            }

            //Objetos
            List <ObjectImage> OBIMs = roomBlock.GetOBIMs();

            for (int i = 0; i < OBIMs.Count; i++)
            {
                TreeNode nodeObject = TreeImages.Nodes.Add("_object" + i, "Object " + i);

                ObjectImage      item = OBIMs[i];
                List <ImageData> IMXX = item.GetIMxx();

                for (int j = 0; j < IMXX.Count; j++)
                {
                    ImageData image = IMXX[j];

                    string   objectImageKey = string.Format("Object #{0}-{1}", i, j);
                    TreeNode nodeImage      = nodeObject.Nodes.Add(objectImageKey, "Image " + j);

                    var createInfo = new ClassCreateInfo();
                    createInfo.ImageType   = ImageType.Object;
                    createInfo.ControlId   = objectImageKey;
                    createInfo.ObjectIndex = i;
                    createInfo.ImageIndex  = j;
                    CreateInfos.Add(objectImageKey, createInfo);

                    List <ZPlane> objectZPlanes = image.GetZPlanes();
                    for (int k = 0; k < objectZPlanes.Count; k++)
                    {
                        string objectZPlaneKey = string.Format("Object #{0}-{1} ZPlane {2}", i, j, (k + 1));

                        nodeImage.Nodes.Add(objectZPlaneKey, "Z-Plane " + k);

                        createInfo             = new ClassCreateInfo();
                        createInfo.ImageType   = ImageType.ObjectsZPlane;
                        createInfo.ControlId   = objectZPlaneKey;
                        createInfo.ObjectIndex = i;
                        createInfo.ImageIndex  = j;
                        createInfo.ZPlaneIndex = k;
                        CreateInfos.Add(objectZPlaneKey, createInfo);
                    }
                }

                //Remove os itens se não tiver nenhuma imagem neles, só serve para poluir a tela.
                if (nodeObject.Nodes.Count == 0)
                {
                    TreeImages.Nodes.Remove(nodeObject);
                }
            }

            //Costumes
            List <Costume> costumesList = _diskBlock.Childrens.OfType <Costume>().ToList();

            for (int i = 0; i < costumesList.Count; i++)
            {
                TreeNode costume = TreeImages.Nodes.Add("_costume" + i, string.Format("Costume {0}", i.ToString().PadLeft(3, '0')));

                Costume currentCostume = costumesList[i];
                for (int j = 0; j < currentCostume.Pictures.Count; j++)
                {
                    //Vamos filtras apenas os frames que tem imagem para decodificar.
                    if (currentCostume.Pictures[j].ImageData.Length == 0 ||
                        currentCostume.Pictures[j].ImageData.Length == 1 && currentCostume.Pictures[j].ImageData[0] == 0)
                    {
                        continue;
                    }

                    string costumeKey = string.Format("Costume #{0}-{1}", i, j);

                    var createInfo = new ClassCreateInfo();
                    createInfo.ImageType  = ImageType.Costume;
                    createInfo.Costume    = currentCostume;
                    createInfo.ControlId  = costumeKey;
                    createInfo.ImageIndex = j;
                    CreateInfos.Add(costumeKey, createInfo);

                    costume.Nodes.Add(costumeKey, string.Format("Frame {0}", j.ToString().PadLeft(2, '0')));
                }
            }

            TreeImages.ExpandAll();
            TreeImages.SelectedNode = TreeImages.Nodes[0];

            _loading = false;
        }
Beispiel #7
0
        private void OK_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ExportLocation.Text))
            {
                return;
            }
            if (!Directory.Exists(ExportLocation.Text))
            {
                return;
            }

            string location  = ExportLocation.Text;
            int    fileCount = 0;

            Cursor        = Cursors.WaitCursor;
            Cancel.Cursor = Cursors.Default;

            _cancelExport = false;
            _exporting    = true;
            foreach (Control control in Controls)
            {
                if (control.Name != "Cancel" && control.GetType() != typeof(Label) && control.GetType() != typeof(ProgressBar))
                {
                    control.Enabled = false;
                }
            }

            Application.DoEvents();

            var diskBlocks = _scummFile.DataFile.GetLFLFs();

            Progress.Maximum = diskBlocks.Count - 1;
            Progress.Value   = 0;
            Progress.Visible = true;

            FilesExported.Visible      = true;
            FilesExportedLabel.Visible = true;

            var decoder = new ImageDecoder();

            decoder.UseTransparentColor = ExportWithTransparency.Checked;

            var bompDecoder = new BompImageDecoder();

            bompDecoder.UseTransparentColor = ExportWithTransparency.Checked;

            var costumeDecoder = new CostumeImageDecoder();

            costumeDecoder.UseTransparentColor = ExportWithTransparency.Checked;

            var zplaneDecoder = new ZPlaneDecoder();

            var convert = new ImageDepthConversor();


            for (int i = 0; i < diskBlocks.Count; i++)
            {
                if (_cancelExport)
                {
                    break;
                }

                var currentRoom = (RoomBlock)diskBlocks[i].Childrens.Single(r => r.GetType() == typeof(RoomBlock));

                if (ExportBackgrounds.Checked)
                {
                    Bitmap background = decoder.Decode(currentRoom);
                    if (background != null)
                    {
                        if (Export8Bits.Checked)
                        {
                            background = convert.CopyToBpp(background, 8, currentRoom.GetDefaultPalette().Colors);
                        }
                        string backgroundName = Path.Combine(location, string.Format("Room#{0}.png", i));
                        background.Save(backgroundName, System.Drawing.Imaging.ImageFormat.Png);
                        File.WriteAllText(backgroundName + ".idx", string.Join(";", decoder.UsedIndexes));
                    }
                    FilesExported.Text = (++fileCount).ToString();
                }

                if (ExportBackgroundZPlanes.Checked)
                {
                    List <ZPlane> zPlanes = currentRoom.GetRMIM().GetIM00().GetZPlanes();

                    for (int j = 0; j < zPlanes.Count; j++)
                    {
                        if (_cancelExport)
                        {
                            break;
                        }

                        Bitmap zplane = zplaneDecoder.Decode(currentRoom, j);
                        if (zplane != null)
                        {
                            if (Export8Bits.Checked)
                            {
                                zplane = convert.CopyToBpp(zplane, 1, new Color[2] {
                                    Color.Black, Color.White
                                });
                            }
                            zplane.Save(Path.Combine(location, string.Format("Room#{0} ZP#{1}.png", i, j)), System.Drawing.Imaging.ImageFormat.Png);
                            FilesExported.Text = (++fileCount).ToString();
                        }
                    }
                }


                if (ExportObjects.Checked)
                {
                    var OBIMs = currentRoom.GetOBIMs();
                    for (int j = 0; j < OBIMs.Count; j++)
                    {
                        ObjectImage      objectImage = OBIMs[j];
                        List <ImageData> IMxx        = objectImage.GetIMxx();

                        for (int k = 0; k < IMxx.Count; k++)
                        {
                            if (_cancelExport)
                            {
                                break;
                            }

                            Bitmap image;
                            int[]  usedIndexes;

                            if (IMxx[k].GetSMAP() == null)
                            {
                                image       = bompDecoder.Decode(currentRoom, j, k);
                                usedIndexes = bompDecoder.UsedIndexes.ToArray();
                            }
                            else
                            {
                                image       = decoder.Decode(currentRoom, j, k);
                                usedIndexes = decoder.UsedIndexes.ToArray();
                            }

                            if (Export8Bits.Checked)
                            {
                                image = convert.CopyToBpp(image, 8, currentRoom.GetDefaultPalette().Colors);
                            }
                            string objectFilename = Path.Combine(location, string.Format("Room#{0} Obj#{1} Img#{2}.png", i, j, k));
                            image.Save(objectFilename, System.Drawing.Imaging.ImageFormat.Png);
                            File.WriteAllText(objectFilename + ".idx", string.Join(";", usedIndexes));

                            FilesExported.Text = (++fileCount).ToString();
                        }
                    }
                }

                if (ExportObjectsZPlanes.Checked)
                {
                    var OBIMs = currentRoom.GetOBIMs();
                    for (int j = 0; j < OBIMs.Count; j++)
                    {
                        ObjectImage      objectImage = OBIMs[j];
                        List <ImageData> IMxx        = objectImage.GetIMxx();

                        for (int k = 0; k < IMxx.Count; k++)
                        {
                            List <ZPlane> zplanes = IMxx[k].GetZPlanes();
                            for (int l = 0; l < zplanes.Count; l++)
                            {
                                if (_cancelExport)
                                {
                                    break;
                                }

                                Bitmap zplane = zplaneDecoder.Decode(currentRoom, j, k, l);

                                if (Export8Bits.Checked)
                                {
                                    zplane = convert.CopyToBpp(zplane, 1, new Color[2] {
                                        Color.Black, Color.White
                                    });
                                }
                                zplane.Save(Path.Combine(location, string.Format("Room#{0} Obj#{1} Img#{2} ZP#{3}.png", i, j, k, l)), System.Drawing.Imaging.ImageFormat.Png);

                                FilesExported.Text = (++fileCount).ToString();
                            }
                        }
                    }
                }

                if (ExportCostumes.Checked)
                {
                    DiskBlock currentDisk = diskBlocks[i];

                    List <Costume> costumesList = currentDisk.Childrens.OfType <Costume>().ToList();
                    for (int j = 0; j < costumesList.Count; j++)
                    {
                        Costume costume = costumesList[j];
                        for (int k = 0; k < costume.Pictures.Count; k++)
                        {
                            if (_cancelExport)
                            {
                                break;
                            }

                            if (costume.Pictures[k].ImageData.Length == 0 ||
                                costume.Pictures[k].ImageData.Length == 1 && costume.Pictures[k].ImageData[0] == 0)
                            {
                                continue;
                            }

                            Bitmap image = costumeDecoder.Decode(currentRoom, costume, k);

                            if (Export8Bits.Checked)
                            {
                                var c = new List <Color>();
                                for (int z = 0; z < 256; z++)
                                {
                                    c.Add(Color.Black);
                                }

                                PaletteData defaultPallete = currentRoom.GetDefaultPalette();
                                for (int z = 0; z < costume.Palette.Count; z++)
                                {
                                    c[z] = defaultPallete.Colors[costume.Palette[z]];
                                }
                                image = convert.CopyToBpp(image, 8, c.ToArray());
                            }

                            image.Save(Path.Combine(location, string.Format("Room#{0} Costume#{1} FrameIndex#{2}.png", i, j, k)), System.Drawing.Imaging.ImageFormat.Png);
                            FilesExported.Text = (++fileCount).ToString();
                        }
                    }
                }

                Progress.Value = i;
                Application.DoEvents();
            }

            Progress.Visible = false;
            _exporting       = false;
            Cursor           = Cursors.Default;

            if (_cancelExport)
            {
                MessageBox.Show("Export cancelled.");
            }
            else
            {
                MessageBox.Show("All images sucefully exported");
            }
            Close();
        }