Пример #1
0
        public GhFileReader(BinaryReader reader, bool isGh5 = true)
        {
            ReadResource(reader);

            reader.BaseStream.Seek(0, SeekOrigin.Begin);
            byte[] buffer = reader.ReadBytes((int)reader.BaseStream.Length);

            reader.BaseStream.Seek(HeaderSize, SeekOrigin.Begin);


            int filePos = HeaderSize;
            int size    = -1;
            int index   = 0;

            while (size != 0)
            {
                reader.BaseStream.Seek(filePos, SeekOrigin.Begin);

                byte imageType = reader.ReadByte();
                byte flag1     = reader.ReadByte();
                byte flag2     = reader.ReadByte();
                byte rowCount  = reader.ReadByte();
                size = reader.ReadInt32();

                IGfxImage img = null;

                switch (imageType)
                {
                case 0:
                    img = new GfxImage16Bit(buffer, 128, rowCount, isGh5);
                    break;

                case 1:
                    img = new GfxImage16Bit(buffer, 256, rowCount, isGh5);
                    break;

                case 2:
                    img = new GfxImageWithPalette(buffer, 128, rowCount);
                    break;

                case 3:
                    img = new GfxImageWithPalette(buffer, 256, rowCount);
                    break;
                }

                img.Flag1      = flag1;
                img.Flag2      = flag2;
                img.DataOffset = filePos + 8;

                img.Index      = index;
                img.GroupIndex = index;

                images.Add(img);

                filePos += size + 8;
                index++;
            }
        }
Пример #2
0
        private static ImageData[] LoadFromBitmap(string path, int i, ICollectionFileReader file)
        {
            IGfxImage image = file.GetImage(i);

            bool saveByIndex = false;            //file.HasDIL;
            int  jobIndex    = saveByIndex ? (image as GfxImage).jobIndex : 0;

            string basePath = $"export/{path}/{(saveByIndex ? $"{jobIndex}/" : "")}";

            List <ImageData> imgs = new List <ImageData>();
            var files             = Directory.GetFiles(basePath, "*.*");
            int maxlen            = files.Max(x => x.Length);
            var result            = files.OrderBy(x => x.PadLeft(maxlen, '0')).ToList();

            int ind = 0;

            foreach (string filePath in result)
            {
                Bitmap map = new Bitmap(filePath);

                ImageData data = new ImageData(map.Height, map.Width);

                //image.Width = map.Width;
                //image.Height = map.Height;

                BitmapData d = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);

                int size = map.Height * map.Width * 4;

                data.data = new Byte[size];

                byte[] argb = new byte[size];
                Marshal.Copy(d.Scan0, argb, 0, size);

                map.UnlockBits(d);

                int index = 0;
                for (int y = 0; y < map.Height; y++)
                {
                    for (int x = 0; x < map.Width; x++)
                    {
                        //so LockBits stores the data as bgra, because f**k you, thats why...
                        data.data[index + 0] = argb[index + 2];                        //g1
                        data.data[index + 1] = argb[index + 1];                        //r2
                        data.data[index + 2] = argb[index + 0];                        //a3
                        data.data[index + 3] = argb[index + 3];                        //b0

                        index += 4;
                    }
                }

                Console.WriteLine($"{ind++}/{result.Count}");

                imgs.Add(data);
            }
            return(imgs.ToArray());
        }
Пример #3
0
        private static void SaveToBitmap(string path, int i, ICollectionFileReader file)
        {
            bool saveByIndex = file.HasDIL;

            IGfxImage image  = file.GetImage(i);
            int       width  = image.Width;
            int       height = image.Height;

            using (DirectBitmap b = new DirectBitmap(image.Width, image.Height)) {
                ImageData data = image.GetImageData();

                int index = 0;
                for (int y = 0; y < height; y++)
                {
                    for (int x = 0; x < width; x++)
                    {
                        int alpha = 255;

                        if (index >= data.data.Length)
                        {
                            break;
                        }

                        byte red   = data.data[index + 0];
                        byte green = data.data[index + 1];
                        byte blue  = data.data[index + 2];

                        if (red == 255 && green + blue == 0)
                        {
                            alpha = removeAlpha ? 0 : alpha;
                        }
                        if (green == 255 && red + blue == 0)
                        {
                            alpha = removeShadows ? 0 : alpha;
                        }
                        else if (onlyShadows)
                        {
                            red   = 0;
                            green = 0;
                            blue  = 0;
                            alpha = 0;
                        }

                        b.SetPixel(x, y, Color.FromArgb(alpha, red, green, blue));

                        index += 4;
                    }
                }

                int jobIndex = saveByIndex ? (image as GfxImage).jobIndex : 0;

                string basePath = $"export/{path}/{(saveByIndex ? $"{jobIndex}/" : "")}";
                Directory.CreateDirectory(basePath);
                b.Bitmap.Save(basePath + $"{i}.png", System.Drawing.Imaging.ImageFormat.Png);
            }
        }