Exemplo n.º 1
0
        //作者:Wetor
        //时间:2019.1.18
        public void PngToCZ1(string outfile)
        {
            Bitmap       Picture = new Bitmap(File.Open(outfile, FileMode.Open));
            StructWriter Writer  = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create));
            CZ1Header    header;

            header.Signature    = "CZ1";
            header.HeaderLength = 0x10;
            header.Width        = (ushort)Picture.Width;
            header.Heigth       = (ushort)Picture.Height;
            header.Colorbits    = 8;
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);


            Pixel32_BGRA Pixel = new Pixel32_BGRA();

            Pixel.R = 255;
            Pixel.G = 255;
            Pixel.B = 255;
            for (int k = 0; k < 256; k++)
            {
                Pixel.A = (byte)k;
                Writer.WriteStruct(ref Pixel);
            }

            byte[] bytes = new byte[Picture.Height * Picture.Width];
            int    i     = 0;

            for (int y = 0; y < Picture.Height; y++)
            {
                for (int x = 0; x < Picture.Width; x++)
                {
                    bytes[i] = Picture.GetPixel(x, y).A;
                    i++;
                }
            }


            int file_num = bytes.Length / 130554 + 1;
            //System.Diagnostics.Debug.WriteLine("{0} {1} {2}", file_num, listBytes.Count, 130554);
            List <int[]> out_list = new List <int[]>();

            Writer.Write(file_num);
            for (int k = 0; k < file_num; k++)
            {
                List <int>    listBytes    = new List <int>();
                StringBuilder decompressed = new StringBuilder();
                byte[]        tmp_bytes    = new byte[130554];
                if (k == file_num - 1)
                {
                    Array.Copy(bytes, k * 130554, tmp_bytes, 0, bytes.Length - k * 130554);
                }
                else
                {
                    Array.Copy(bytes, k * 130554, tmp_bytes, 0, 130554);
                }


                foreach (char kk in tmp_bytes)
                {
                    decompressed.Append(kk);
                }
                listBytes = LzwUtil.Compress(decompressed.ToString());
                out_list.Add(listBytes.ToArray());
                Writer.Write(listBytes.Count);
                //string tmp_str;
                System.Diagnostics.Debug.WriteLine("{0}", k);

                /*if (k== file_num - 1)
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, listBytes.Count - 130554 / 2 * k));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }
                 * else
                 * {
                 *  tmp_list.AddRange(listBytes.GetRange(130554 / 2 * k, 130554 / 2));
                 *  tmp_list.Insert(0, 0);
                 *  tmp_str = Decompress(tmp_list);
                 * }*/
                if (k == file_num - 1)
                {
                    Writer.Write(bytes.Length - k * 130554);
                }
                else
                {
                    Writer.Write(130554);
                }
                //Writer.Write(0xFFFD);
            }

            /*List<byte> output = new List<byte>();
             * string str = Decompress(listBytes);
             * foreach (var c in str)
             * {
             *  output.Add((byte)c);
             * }
             * // Writer.Write(output.ToArray());
             * System.Diagnostics.Debug.WriteLine(output.Count);*/

            for (int k = 0; k < out_list.Count; k++)
            {
                for (int kk = 0; kk < out_list[k].Length; kk++)
                {
                    Writer.Write((UInt16)out_list[k][kk]);
                }
            }
            Writer.Close();
        }
Exemplo n.º 2
0
        public Bitmap Export(byte[] Texture, string name = "")
        {
            StructReader Reader = new StructReader(new MemoryStream(Texture));
            CZ1Header    Header = new CZ1Header();

            Reader.ReadStruct(ref Header);

            if (Header.Signature != "CZ1\x0")
            {
                throw new BadImageFormatException();
            }

            Reader.Seek(Header.HeaderLength, SeekOrigin.Begin);
            Bitmap Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format32bppArgb);

            if (Header.Colorbits == 4)//4bit
            {
                //字库格式
                //System.Diagnostics.Debug.WriteLine(4);
                //表
                Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[16];
                Pixel32_BGRA   Pixel      = new Pixel32_BGRA();
                for (int i = 0; i < ColorPanel.Length; i++)
                {
                    Reader.ReadStruct(ref Pixel);
                    ColorPanel[i] = Pixel;
                }

                //lmz解压
                var bytes = Decompress(Reader, name);

                //解压后的像素
                Queue <int> queue = new Queue <int>();
                foreach (var b in bytes)
                {
                    int low4bit  = b & 0x0F;
                    int high4bit = (b & 0xF0) >> 4;
                    queue.Enqueue(low4bit);
                    queue.Enqueue(high4bit);
                }

                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        int index = queue.Dequeue();
                        Picture.SetPixel(x, y, Color.FromArgb(ColorPanel[index].A, ColorPanel[index].R, ColorPanel[index].G, ColorPanel[index].B));
                    }
                }
            }
            else if (Header.Colorbits == 8)//8bit
            {
                System.Diagnostics.Debug.WriteLine(8);
                Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[256];
                Pixel32_BGRA   Pixel      = new Pixel32_BGRA();
                for (int i = 0; i < ColorPanel.Length; i++)
                {
                    Reader.ReadStruct(ref Pixel);
                    ColorPanel[i] = Pixel;
                }

                var         bytes = Decompress(Reader);
                Queue <int> queue = new Queue <int>();
                foreach (var b in bytes)
                {
                    queue.Enqueue(b);
                }
                // var ie = bytes.GetEnumerator();
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        int index = queue.Dequeue();
                        //int index = BitConverter.ToInt16(new byte[] { ie.Current, 0x00 }, 0);
                        Picture.SetPixel(x, y, Color.FromArgb(ColorPanel[index].A, ColorPanel[index].R, ColorPanel[index].G, ColorPanel[index].B));
                    }
                }
            }
            else if (Header.Colorbits == 24)
            {
                System.Diagnostics.Debug.WriteLine(24);
                List <byte> bytes = (List <byte>)Decompress(Reader);
                Reader = new StructReader(new MemoryStream(bytes.ToArray()));

                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        Pixel24_RGB Pixel = new Pixel24_RGB();
                        Reader.ReadStruct(ref Pixel);
                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }
            else if (Header.Colorbits == 32)//32
            {
                System.Diagnostics.Debug.WriteLine(32);
                List <byte> bytes = (List <byte>)Decompress(Reader);
                Reader = new StructReader(new MemoryStream(bytes.ToArray()));


                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        Pixel32_RGBA Pixel = new Pixel32_RGBA();
                        Reader.ReadStruct(ref Pixel);
                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.A, Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }
            Reader.Close();
            return(Picture);
        }
Exemplo n.º 3
0
        private Bitmap Export(byte[] Texture, string infile)
        {
            StructReader Reader = new StructReader(new MemoryStream(Texture));
            DatHeader    Header = new DatHeader();

            Reader.ReadStruct(ref Header);
            //Header.Blockh = (ushort)Math.Ceiling((float)Header.Heigth / (float)Header.Colorblock);
            Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[0];

            uint Signature  = Header.Signature;
            int  PixivBytes = 0;
            int  DatType    = 1;

            if (Signature == 0x00200102)
            {
                Console.WriteLine("DatType 1");//no colorpanel //32 or 24bit
            }
            else if (Signature == 0x04280102)
            {
                Console.WriteLine("DatType 2");//has colorpanel 256色 8bit
                DatType = 2;
            }
            else
            {
                Console.WriteLine("Unsupport Format Signature:" + Header.Signature);
                File.AppendAllText("ErrOutput.txt", "Unsupport Format Signature:" + Header.Signature + "\r\n" + infile + "\r\n");
            }

            if (Header.ColorbitsType == 0x80)
            {
                PixivBytes = 4;
            }
            else if (Header.ColorbitsType == 0xA8)
            {
                PixivBytes = 3;
            }
            else if (Header.ColorbitsType == 0x81)
            {
                PixivBytes = 1;
            }
            else
            {
                Console.WriteLine("Unsupport Format ColorbitsType:" + Header.ColorbitsType);
                File.AppendAllText("ErrOutput.txt", "Unsupport Format ColorbitsType:" + Header.ColorbitsType + "\r\n" + infile + "\r\n");
                return(null);
            }

            if (DatType == 2)
            {
                Pixel32_BGRA tmpPixel = new Pixel32_BGRA();
                ColorPanel = new Pixel32_BGRA[256];
                for (int j = 0; j < ColorPanel.Length; j++)
                {
                    //TODO 颜色表的 颜色还有错误
                    tmpPixel.R = (byte)(Reader.ReadByte());
                    tmpPixel.G = (byte)(Reader.ReadByte());
                    tmpPixel.B = (byte)(Reader.ReadByte());
                    tmpPixel.A = (byte)(Reader.ReadByte());
                    //tmpPixel.R = (byte)(tmpPixel.R == 0 ? 0xFF : tmpPixel.R);
                    //tmpPixel.G = (byte)(tmpPixel.G == 0 ? 0xFF : tmpPixel.G);
                    //tmpPixel.B = (byte)(tmpPixel.B == 0 ? 0xFF : tmpPixel.B);
                    //tmpPixel.A = (byte)(tmpPixel.A == 0 ? 0xFF : tmpPixel.A);
                    ColorPanel[j] = tmpPixel;
                }

                //256*4
                Reader.ReadUInt32();              //10 00 00 00  //16 Unknow
                Reader.ReadUInt32();              //38 04 00 00  //1080 Unknow
            }
            Reader.ReadUInt32();                  //totallennextbytes=blocksize_byteslen+compressedLen
            Reader.ReadUInt32();                  //w h
            Reader.ReadUInt16();                  //blocksize_byteslen=2+2+4+4+blockcount*8
            int BlockCount = Reader.ReadUInt16(); //block count

            uint decompressedLen = Reader.ReadUInt32();

            Console.WriteLine("decompressedTotalLen :" + decompressedLen);
            uint compressedLen = Reader.ReadUInt32();

            Console.WriteLine("compressedTotalLen :" + compressedLen);


            List <byte>            output             = new List <byte>();
            Dictionary <int, uint> rawSizeList        = new Dictionary <int, uint>();
            Dictionary <int, uint> compressedSizeList = new Dictionary <int, uint>();
            int i;

            for (i = 0; i < BlockCount; i++)
            {
                uint fileCompressedSize = Reader.ReadUInt32();
                uint fileRawSize        = Reader.ReadUInt32();
                rawSizeList.Add(i, fileRawSize);
                compressedSizeList.Add(i, fileCompressedSize);
            }

            for (i = 0; i < BlockCount; i++)
            {
                List <int>  lmzBytes   = new List <int>();
                List <byte> unBytes    = new List <byte>();
                int         totalcount = (int)compressedSizeList[i];
                for (int j = 0; j < totalcount / 2; j++)
                {
                    //st.Read(bytArray2Count, 0, 2);//

                    lmzBytes.Add(Reader.ReadUInt16());
                }
                //解压lzw
                string str = LzwUtil.Decompress2(lmzBytes);
                foreach (var c in str)
                {
                    unBytes.Add((byte)c);
                }

                //颜色填充方式
                if (Header.ColorbitsAlgorithm == 0x00)
                {
                    //raw
                }
                else if (Header.ColorbitsAlgorithm == 0x02)
                {
                    //diff
                    int pixivNum = unBytes.Count / PixivBytes;

                    int    lineBytesNum = Header.Width * PixivBytes;
                    byte[] pre_line     = new byte[lineBytesNum];
                    byte[] curr_line    = new byte[lineBytesNum];
                    for (int idx = 0; idx < Math.Ceiling((double)pixivNum / (double)Header.Width); idx++)
                    {
                        unBytes.CopyTo(idx * lineBytesNum, curr_line, 0, (idx + 1) * lineBytesNum < unBytes.Count ? lineBytesNum : unBytes.Count % lineBytesNum);
                        if (idx != 0)
                        {
                            for (int x = 0; x < lineBytesNum; x++)
                            {
                                curr_line[x] += pre_line[x];
                                curr_line[x]--;
                            }
                        }
                        for (int x = 0; x < lineBytesNum && idx * lineBytesNum + x < unBytes.Count; x++)
                        {
                            unBytes[idx * lineBytesNum + x] = curr_line[x];
                        }
                        curr_line.CopyTo(pre_line, 0);
                    }
                }
                output.AddRange(unBytes);
            }
            Bitmap       Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format32bppArgb);
            Pixel32_BGRA Pixel   = new Pixel32_BGRA();
            MemoryStream ms      = new MemoryStream(output.ToArray());

            byte[] bytArray4Count = new byte[4];
            ms.Seek(0, SeekOrigin.Begin);
            if (PixivBytes == 4)//32 argb
            {
                Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format32bppArgb);
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        ms.Read(bytArray4Count, 0, 4); //

                        Pixel.R = --bytArray4Count[0]; // == 0 ? (byte)0xFF : bytArray4Count[0];
                        Pixel.G = --bytArray4Count[1]; // == 0 ? (byte)0xFF : bytArray4Count[1];
                        Pixel.B = --bytArray4Count[2]; // == 0 ? (byte)0xFF : bytArray4Count[2];
                        Pixel.A = --bytArray4Count[3]; // == 0 ? (byte)0xFF : bytArray4Count[3];

                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.A, Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }

            else if (PixivBytes == 3)//24 rgb
            {
                Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format24bppRgb);
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        ms.Read(bytArray4Count, 0, 3); //

                        Pixel.R = --bytArray4Count[0]; // == 0 ? (byte)0xFF : bytArray4Count[0];
                        Pixel.G = --bytArray4Count[1]; // == 0 ? (byte)0xFF : bytArray4Count[1];
                        Pixel.B = --bytArray4Count[2]; // == 0 ? (byte)0xFF : bytArray4Count[2];
                        //Pixel.A = bytArray4Count[3] == 0 ? (byte)0xFF : bytArray4Count[3];

                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }

            else if (PixivBytes == 1)//8 argb
            {
                Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format32bppArgb);
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        byte b = (byte)(ms.ReadByte() - 1); //
                        Pixel.R = ColorPanel[b].R;
                        Pixel.G = ColorPanel[b].G;
                        Pixel.B = ColorPanel[b].B;
                        Pixel.A = ColorPanel[b].A;

                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.A, Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }


            Reader.Close();
            return(Picture);
        }
Exemplo n.º 4
0
        public override void FileImport(string path, string outpath = null)
        {
            CZ0HeaderInfo cz0HeaderInfo = null;
            string        infopath      = path.Replace(".png", ".json");

            if (File.Exists(infopath))
            {
                cz0HeaderInfo = JsonConvert.DeserializeObject <CZ0HeaderInfo>(File.ReadAllText(infopath));
            }
            CZOutputInfo czOutput = new CZOutputInfo();

            Bitmap       Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(path)));
            StructWriter Writer  = new StructWriter(File.Open(path + ".cz0", FileMode.Create));
            CZ0Header    header;

            if (cz0HeaderInfo == null)
            {
                header.Signature    = "CZ0";
                header.HeaderLength = 0x40;
                header.Width        = (ushort)Picture.Width;
                header.Heigth       = (ushort)Picture.Height;
                header.Colorbits    = 32;
            }
            else
            {
                header = cz0HeaderInfo.cz0Header;
            }
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);

            if (header.Colorbits == 32)
            {
                System.Diagnostics.Debug.WriteLine(32);
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        Writer.Write(Picture.GetPixel(x, y).R);
                        Writer.Write(Picture.GetPixel(x, y).G);
                        Writer.Write(Picture.GetPixel(x, y).B);
                        Writer.Write(Picture.GetPixel(x, y).A);
                    }
                }
            }
            else if (header.Colorbits == 24)
            {
                System.Diagnostics.Debug.WriteLine(24);
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        Writer.Write(Picture.GetPixel(x, y).R);
                        Writer.Write(Picture.GetPixel(x, y).G);
                        Writer.Write(Picture.GetPixel(x, y).B);
                    }
                }
            }
            else if (header.Colorbits == 8)
            {
                System.Diagnostics.Debug.WriteLine(8);
                List <Pixel32_BGRA> list = new List <Pixel32_BGRA>();
                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        var color  = Picture.GetPixel(x, y);
                        var color2 = new Pixel32_BGRA();
                        color2.R = color.R;
                        color2.G = color.G;
                        color2.B = color.B;
                        color2.A = color.A;
                        if (!list.Contains(color2))
                        {
                            list.Add(color2);
                        }
                    }
                }

                //颜色大于256
                if (list.Count > 256)
                {
                    Console.WriteLine("Over 256 Color!!");
                    //调用pngquant png 8bit缩减
                    if (File.Exists("pngquant.exe") || File.Exists("pngquant"))
                    {
                        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                        {
                            var psi = new ProcessStartInfo("pngquant.exe")
                            {
                                RedirectStandardOutput = true
                            };
                            psi.Arguments = " 256 " + path + " --ext tmp ";
                            var proc = Process.Start(psi);
                            if (proc == null)
                            {
                                Console.WriteLine("Can not exec.");
                            }
                            else
                            {
                                using (var sr = proc.StandardOutput)
                                {
                                    while (!sr.EndOfStream)
                                    {
                                        Console.WriteLine(sr.ReadLine());
                                    }

                                    if (!proc.HasExited)
                                    {
                                        proc.Kill();
                                    }
                                }
                            }
                            string pathtmp = path.Replace(".png", "tmp");
                            Picture = new Bitmap(new MemoryStream(File.ReadAllBytes(pathtmp)));
                            File.Delete(pathtmp);
                        }
                        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                        {
                            //TODO
                        }
                        else
                        {
                            //TODO
                        }

                        list.Clear();
                        for (int y = 0; y < Picture.Height; y++)
                        {
                            for (int x = 0; x < Picture.Width; x++)
                            {
                                var color  = Picture.GetPixel(x, y);
                                var color2 = new Pixel32_BGRA();
                                color2.R = color.R;
                                color2.G = color.G;
                                color2.B = color.B;
                                color2.A = color.A;
                                if (!list.Contains(color2))
                                {
                                    list.Add(color2);
                                }
                            }
                        }
                    }
                    else
                    {
                        throw new Exception("Need pngquant !! Download From https://pngquant.org/");
                    }
                }

                var pixel = new Pixel32_BGRA();
                for (int i = 0; i < list.Count; i++)
                {
                    pixel = list[i];
                    Writer.WriteStruct(ref pixel);
                }
                for (int i = 0; i < 256 - list.Count; i++)
                {
                    var pixelempty = new Pixel32_BGRA()
                    {
                        R = 0, G = 0, B = 0, A = 255
                    };
                    Writer.WriteStruct(ref pixelempty);
                }

                for (int y = 0; y < Picture.Height; y++)
                {
                    for (int x = 0; x < Picture.Width; x++)
                    {
                        var color  = Picture.GetPixel(x, y);
                        var color2 = new Pixel32_BGRA();
                        color2.R = color.R;
                        color2.G = color.G;
                        color2.B = color.B;
                        color2.A = color.A;
                        uint index = (uint)list.IndexOf(color2);
                        Writer.Write((byte)index);
                    }
                }
            }
            Writer.Close();
        }
Exemplo n.º 5
0
        public Bitmap Export(byte[] Texture, string name = "")
        {
            StructReader Reader = new StructReader(new MemoryStream(Texture));
            CZ0Header    Header = new CZ0Header();

            Reader.ReadStruct(ref Header);

            if (Header.Signature != "CZ0\x0")
            {
                throw new BadImageFormatException();
            }

            Reader.Seek(Header.HeaderLength, SeekOrigin.Begin);
            CZ0HeaderInfo cz0HeaderInfo = new CZ0HeaderInfo();

            cz0HeaderInfo.cz0Header = Header;
            Bitmap Picture = new Bitmap(Header.Width, Header.Heigth, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            if (Header.Colorbits == 8)
            {
                System.Diagnostics.Debug.WriteLine(8);
                Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[256];
                Pixel32_BGRA   Pixel      = new Pixel32_BGRA();
                for (int i = 0; i < ColorPanel.Length; i++)
                {
                    Reader.ReadStruct(ref Pixel);
                    ColorPanel[i] = Pixel;
                }
                cz0HeaderInfo.ColorPanel = ColorPanel;
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        byte tmp = 0;
                        Reader.ReadStruct(ref tmp);
                        uint index = tmp;
                        Picture.SetPixel(x, y, Color.FromArgb(ColorPanel[index].A, ColorPanel[index].R, ColorPanel[index].G, ColorPanel[index].B));
                    }
                }
            }
            else if (Header.Colorbits == 24)
            {
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        Pixel24_RGB Pixel = new Pixel24_RGB();
                        Reader.ReadStruct(ref Pixel);
                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }
            else if (Header.Colorbits == 32)
            {
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        Pixel32_RGBA Pixel = new Pixel32_RGBA();
                        Reader.ReadStruct(ref Pixel);
                        Picture.SetPixel(x, y, Color.FromArgb(Pixel.A, Pixel.R, Pixel.G, Pixel.B));
                    }
                }
            }
            Reader.Close();
            string json = JsonConvert.SerializeObject(cz0HeaderInfo, Formatting.Indented);

            File.WriteAllText(name + ".json", json);
            return(Picture);
        }
Exemplo n.º 6
0
        private Bitmap Export(byte[] Texture)
        {
            StructReader Reader = new StructReader(new MemoryStream(Texture));
            CZ2Header    Header = new CZ2Header();

            Reader.ReadStruct(ref Header);

            if (Header.Signature != "CZ2\x0")
            {
                throw new BadImageFormatException();
            }

            Reader.Seek(Header.HeaderLength, SeekOrigin.Begin);
            Bitmap Picture = new Bitmap(Header.Width, Header.Heigth, PixelFormat.Format32bppArgb);

            if (Header.Colorbits == 4)//4bit
            {
                //字库格式
                //System.Diagnostics.Debug.WriteLine(4);
                //表
                Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[16];
                Pixel32_BGRA   Pixel      = new Pixel32_BGRA();
                for (int i = 0; i < ColorPanel.Length; i++)
                {
                    Reader.ReadStruct(ref Pixel);
                    ColorPanel[i] = Pixel;
                }

                //lmz解压
                var bytes = Decompress(Reader);

                //解压后的像素
                Queue <int> queue = new Queue <int>();
                foreach (var b in bytes)
                {
                    int low4bit  = b & 0x0F;
                    int high4bit = (b & 0xF0) >> 4;
                    queue.Enqueue(low4bit);
                    queue.Enqueue(high4bit);
                }

                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        int index = queue.Dequeue();
                        Picture.SetPixel(x, y, Color.FromArgb(ColorPanel[index].A, ColorPanel[index].R, ColorPanel[index].G, ColorPanel[index].B));
                    }
                }
            }
            else if (Header.Colorbits == 8)//8bit
            {
                System.Diagnostics.Debug.WriteLine(8);
                Pixel32_BGRA[] ColorPanel = new Pixel32_BGRA[256];
                Pixel32_BGRA   Pixel      = new Pixel32_BGRA();
                for (int i = 0; i < ColorPanel.Length; i++)
                {
                    Reader.ReadStruct(ref Pixel);
                    ColorPanel[i] = Pixel;
                }

                var         bytes = Decompress(Reader);
                Queue <int> queue = new Queue <int>();
                foreach (var b in bytes)
                {
                    queue.Enqueue(b);
                }
                bool isError = false;
                // var ie = bytes.GetEnumerator();
                for (int y = 0; y < Header.Heigth; y++)
                {
                    for (int x = 0; x < Header.Width; x++)
                    {
                        try
                        {
                            int index = queue.Dequeue();
                            //int index = BitConverter.ToInt16(new byte[] { ie.Current, 0x00 }, 0);
                            Picture.SetPixel(x, y, Color.FromArgb(ColorPanel[index].A, ColorPanel[index].R, ColorPanel[index].G, ColorPanel[index].B));
                        }
                        catch
                        {
                            Console.WriteLine(isError);
                            isError = true;
                            break;
                        }
                    }
                    if (isError)
                    {
                        Console.WriteLine(isError);
                        break;
                    }
                }
            }
            //else if (Header.Colorbits == 24)
            //{

            //    for (int y = 0; y < Header.Heigth; y++)
            //    for (int x = 0; x < Header.Width; x++)
            //    {
            //        Pixel24 Pixel = new Pixel24();
            //        Reader.ReadStruct(ref Pixel);
            //        Picture.SetPixel(x, y, Color.FromArgb(Pixel.R, Pixel.G, Pixel.B));
            //    }
            //}
            //else if (Header.Colorbits == 32)//32
            //{

            //    for (int y = 0; y < Header.Heigth; y++)
            //    for (int x = 0; x < Header.Width; x++)
            //    {
            //        Pixel32 Pixel = new Pixel32();
            //        Reader.ReadStruct(ref Pixel);
            //        Picture.SetPixel(x, y, Color.FromArgb(Pixel.A, Pixel.R, Pixel.G, Pixel.B));
            //    }
            //}
            Reader.Close();
            return(Picture);
        }
Exemplo n.º 7
0
        //作者:Wetor
        //时间:2019.1.18
        public void PngToCZ1(string outfile)
        {
            CZOutputInfo czOutput = new CZOutputInfo();
            string       name     = outfile.Replace(".png", "").Replace(".Png", "").Replace(".PNG", "") + ".json";

            if (File.Exists(name))
            {
                czOutput = JsonConvert.DeserializeObject <CZOutputInfo>(File.ReadAllText(name));
            }

            Bitmap       Picture = new Bitmap(File.Open(outfile, FileMode.Open));
            StructWriter Writer  = new StructWriter(File.Open(outfile + ".cz1", FileMode.Create));
            CZ1Header    header;

            header.Signature    = "CZ1";
            header.HeaderLength = 0x10;
            header.Width        = (ushort)Picture.Width;
            header.Heigth       = (ushort)Picture.Height;
            header.Colorbits    = 4;
            Writer.WriteStruct(ref header);
            Writer.Seek(header.HeaderLength, SeekOrigin.Begin);


            Pixel32_BGRA Pixel = new Pixel32_BGRA();

            Pixel.R = 255;
            Pixel.G = 255;
            Pixel.B = 255;
            //FF FF FF 00
            //FF FF FF 18
            //FF FF FF 26
            //FF FF FF 35
            //FF FF FF 45
            //FF FF FF 55
            //FF FF FF 65
            //FF FF FF 75
            //FF FF FF 85
            //FF FF FF 96
            //FF FF FF A7
            //FF FF FF B8
            //FF FF FF C9
            //FF FF FF DB
            //FF FF FF EC
            //FF FF FF FF


            Pixel.A = (byte)0x00; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x18; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x26; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x35; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x45; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x55; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x65; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x75; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x85; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0x96; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xA7; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xB8; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xC9; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xDB; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xEC; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Pixel.A = (byte)0xFF; list.Add(Pixel.A);
            Writer.WriteStruct(ref Pixel);
            Queue <byte> queue = new Queue <byte>();
            int          i     = 0;

            for (int y = 0; y < Picture.Height; y++)
            {
                for (int x = 0; x < Picture.Width; x++)
                {
                    byte tmp = color256to16(Picture.GetPixel(x, y).A);
                    //bytes11[i] = (byte)list.IndexOf(tmp);
                    queue.Enqueue((byte)list.IndexOf(tmp));
                    i++;
                }
            }

            byte[] bytes2 = new byte[Picture.Height * Picture.Width];

            for (int j = 0; j < bytes2.Length / 2; j++)
            {
                var low4bit  = queue.Dequeue();
                var high4bit = queue.Dequeue();
                var b        = (uint)(high4bit << 4) + (uint)low4bit;
                bytes2[j] = (byte)b;
            }



            var ie = bytes2.ToList().GetEnumerator();
            List <List <int> > out_list = LzwUtil.Compress(ie, 0xFEFD);

            foreach (var item in out_list)
            {
                Console.WriteLine("add compressed count :" + item.Count);
                Console.WriteLine(item.Count);
                Console.WriteLine(LzwUtil.Decompress(item).Length);
            }



            //for (int k = 0; k < out_list.Count; k++)
            //{
            //    Writer.Write(out_list[k].Count);
            //    Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
            //    List<byte> bytes = new List<byte>();
            //    foreach (var item in out_list[k])
            //    {
            //        UInt16 bb = (UInt16)item;
            //        bytes.Add(BitConverter.GetBytes(bb)[0]);
            //        bytes.Add(BitConverter.GetBytes(bb)[1]);
            //    }
            //    //string tmp_str;
            //    System.Diagnostics.Debug.WriteLine("{0}", k);
            //}
            int file_num = out_list.Count;

            Writer.Write(out_list.Count);

            for (int k = 0; k < out_list.Count; k++)
            {
                //if (k >= out_list.Count - 1)
                //{
                //    Writer.Write((UInt32)0);
                //    Writer.Write((UInt32)0);
                //}
                //else
                //{
                Writer.Write(out_list[k].Count);
                Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
                //List<byte> bytes = new List<byte>();
                //foreach (var item in out_list[k])
                //{
                //    UInt16 bb = (UInt16)item;
                //    bytes.Add(BitConverter.GetBytes(bb)[0]);
                //    bytes.Add(BitConverter.GetBytes(bb)[1]);
                //}
                //string tmp_str;
                System.Diagnostics.Debug.WriteLine("{0}", k);
                //}
            }

            //Writer.Write(czOutput.filecount);
            //for (int k = 0; k < czOutput.filecount; k++)
            //{
            //    if (k >= out_list.Count - 1)
            //    {
            //        Writer.Write((UInt32)0);
            //        Writer.Write((UInt32)0);
            //    }
            //    else
            //    {
            //        Writer.Write(out_list[k].Count);
            //        Writer.Write(LzwUtil.Decompress(out_list[k]).Length);
            //        //List<byte> bytes = new List<byte>();
            //        //foreach (var item in out_list[k])
            //        //{
            //        //    UInt16 bb = (UInt16)item;
            //        //    bytes.Add(BitConverter.GetBytes(bb)[0]);
            //        //    bytes.Add(BitConverter.GetBytes(bb)[1]);
            //        //}
            //        //string tmp_str;
            //        System.Diagnostics.Debug.WriteLine("{0}", k);
            //    }
            //}
            uint totalsize_new = 0x10 + 4 * 16 + 4 + (uint)file_num * 4 * 2;
            uint totalsize_org = 0x10 + 4 * 16 + 4 + czOutput.filecount * 4 * 2;

            for (int k = 0; k < out_list.Count; k++)
            {
                for (int kk = 0; kk < out_list[k].Count; kk++)
                {
                    Writer.Write((UInt16)out_list[k][kk]);
                    totalsize_new += 2;
                }
            }

            totalsize_org += czOutput.TotalCompressedSize * 2;
            int diff = (int)(totalsize_org - totalsize_new);

            if (diff > 0)
            {
                diff = diff / 2;
                for (uint j = 0; j < diff; j++)
                {
                    Writer.Write((UInt16)0);
                }
            }
            else
            {
                Console.WriteLine("超长");
            }

            //int p = 0;
            //foreach (var blockinfo in czOutput.blockinfo)
            //{
            //    if (out_list.Count - 1 >= p)
            //    {
            //        for (int kk = 0; kk < out_list[p].Count; kk++)
            //        {
            //            Writer.Write((UInt16)out_list[p][kk]);
            //        }
            //        if (out_list[p].Count == blockinfo.CompressedSize)
            //        {
            //            Console.WriteLine("符合");
            //        }
            //        else
            //        {
            //            Int64 compressedcount = (Int64)out_list[p].Count - (Int64)blockinfo.CompressedSize;
            //            compressedcount = Math.Abs(compressedcount);
            //            Console.WriteLine("不符合补0-" + compressedcount);
            //            for (Int64 pp = 0; pp < compressedcount; pp++)
            //            {
            //                Writer.Write((UInt16)0);
            //            }
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine("不符合补0-" + blockinfo.CompressedSize);
            //        //整块补0
            //        for (Int64 pp = 0; pp < blockinfo.CompressedSize; pp++)
            //        {
            //            Writer.Write((UInt16)0);
            //        }
            //    }
            //    p++;
            //}
            Writer.Close();
        }