Exemplo n.º 1
0
        /// <summary>
        /// 取得Tpl图片的大小信息
        /// </summary>
        /// <param name="byData"></param>
        /// <param name="startPos"></param>
        /// <returns></returns>
        public int GetTplSize(byte[] byData, int startPos)
        {
            int tplFileSize = 0;

            // File Header
            TplFileHeader tplFileHeader = new TplFileHeader();

            tplFileHeader.NumberOfImages     = Util.GetOffset(byData, startPos + 0x4, startPos + 0x7);
            tplFileHeader.OffsetOfImageTable = Util.GetOffset(byData, startPos + 0x8, startPos + 0xB);

            // 循环处理
            List <Image> imageList = new List <Image>();
            int          intImageOffsetTablePos = startPos + 0xC;

            for (int i = 0; i < tplFileHeader.NumberOfImages; i++)
            {
                // 取得Image Header信息
                int intOffsetOfImageHeader   = startPos + Util.GetOffset(byData, intImageOffsetTablePos, intImageOffsetTablePos + 3);
                int intOffsetOfPaletteHeader = startPos + Util.GetOffset(byData, intImageOffsetTablePos + 4, intImageOffsetTablePos + 7);
                intImageOffsetTablePos += 8;

                int imageHeight      = Util.GetOffset(byData, intOffsetOfImageHeader, intOffsetOfImageHeader + 1);
                int imageWidth       = Util.GetOffset(byData, intOffsetOfImageHeader + 2, intOffsetOfImageHeader + 3);
                int imageFormat      = Util.GetOffset(byData, intOffsetOfImageHeader + 4, intOffsetOfImageHeader + 7);
                int imageDataAddress = Util.GetOffset(byData, intOffsetOfImageHeader + 8, intOffsetOfImageHeader + 11);

                if (i == tplFileHeader.NumberOfImages - 1)
                {
                    // 取得图像
                    tplFileSize = imageDataAddress + Util.GetImageByteCount(imageHeight, imageWidth, Util.GetImageFormat(imageFormat));
                }
            }

            return(tplFileSize);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 导入图片到Tpl文件
        /// </summary>
        /// <param name="tplFile"></param>
        /// <param name="rowIndex"></param>
        /// <param name="byImage"></param>
        public byte[] TplImgImport(byte[] byData, int rowIndex, byte[] byImage, byte[] byPalette)
        {
            try
            {
                // File Header
                TplFileHeader tplFileHeader = new TplFileHeader();
                tplFileHeader.FileMagic          = Util.GetHeaderString(byData, 0x0, 0x3);
                tplFileHeader.NumberOfImages     = Util.GetOffset(byData, 0x4, 0x7);
                tplFileHeader.OffsetOfImageTable = Util.GetOffset(byData, 0x8, 0xB);

                // 循环处理
                int intImageOffsetTablePos = 0xC;
                for (int i = 0; i < tplFileHeader.NumberOfImages; i++)
                {
                    // 取得Image Header信息
                    int intOffsetOfImageHeader   = Util.GetOffset(byData, intImageOffsetTablePos, intImageOffsetTablePos + 3);
                    int intOffsetOfPaletteHeader = Util.GetOffset(byData, intImageOffsetTablePos + 4, intImageOffsetTablePos + 7);
                    intImageOffsetTablePos += 8;

                    int imageHeight      = Util.GetOffset(byData, intOffsetOfImageHeader, intOffsetOfImageHeader + 1);
                    int imageWidth       = Util.GetOffset(byData, intOffsetOfImageHeader + 2, intOffsetOfImageHeader + 3);
                    int imageFormat      = Util.GetOffset(byData, intOffsetOfImageHeader + 4, intOffsetOfImageHeader + 7);
                    int imageDataAddress = Util.GetOffset(byData, intOffsetOfImageHeader + 8, intOffsetOfImageHeader + 11);

                    // 取得图像
                    byte[] imageData = new byte[Util.GetImageByteCount(imageHeight, imageWidth, Util.GetImageFormat(imageFormat))];
                    Array.Copy(byData, imageDataAddress, imageData, 0, imageData.Length);

                    if (rowIndex == i)
                    {
                        //// 由于C4_CI4目前无法正确编码,暂时将C4_CI4各式转成CMPR各式
                        //if (imageFormat == 0x08)
                        //{
                        //    imageFormat = 0x0e;
                        //    byData[intOffsetOfImageHeader + 7] = 0x0e;
                        //}

                        if (imageFormat == 0x08 ||
                            imageFormat == 0x09 ||
                            imageFormat == 0x0A)
                        {
                            // C4_CI4、C8_CI8、C14X2_CI14x2
                            // 取得模板信息
                            int paletteDataAddress = Util.GetOffset(byData, intOffsetOfPaletteHeader + 8, intOffsetOfPaletteHeader + 11);
                            Array.Copy(byImage, 0, byData, imageDataAddress, byImage.Length);
                            Array.Copy(byPalette, 0, byData, paletteDataAddress, byPalette.Length);
                        }
                        else
                        {
                            // 通常类型、CMPR
                            Array.Copy(byImage, 0, byData, imageDataAddress, byImage.Length);
                            return(byData);
                        }
                    }
                }

                return(byData);
            }
            catch (Exception me)
            {
                MessageBox.Show(me.Message + "\n" + me.StackTrace);
                return(byData);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 取得Tpl信息
        /// </summary>
        /// <param name="byData"></param>
        /// <param name="tplImageInfo"></param>
        /// <returns></returns>
        public Image[] TplDecode(byte[] byData, List <ImageHeader> tplImageInfo)
        {
            this.TplFileSize = 0;

            // File Header
            TplFileHeader tplFileHeader = new TplFileHeader();

            tplFileHeader.FileMagic          = Util.GetHeaderString(byData, 0x0, 0x3);
            tplFileHeader.NumberOfImages     = Util.GetOffset(byData, 0x4, 0x7);
            tplFileHeader.OffsetOfImageTable = Util.GetOffset(byData, 0x8, 0xB);

            // 循环处理
            List <Image> imageList = new List <Image>();
            int          intImageOffsetTablePos = 0xC;

            for (int i = 0; i < tplFileHeader.NumberOfImages; i++)
            {
                // 取得Image Header信息
                int intOffsetOfImageHeader   = Util.GetOffset(byData, intImageOffsetTablePos, intImageOffsetTablePos + 3);
                int intOffsetOfPaletteHeader = Util.GetOffset(byData, intImageOffsetTablePos + 4, intImageOffsetTablePos + 7);
                intImageOffsetTablePos += 8;

                int imageHeight      = Util.GetOffset(byData, intOffsetOfImageHeader, intOffsetOfImageHeader + 1);
                int imageWidth       = Util.GetOffset(byData, intOffsetOfImageHeader + 2, intOffsetOfImageHeader + 3);
                int imageFormat      = Util.GetOffset(byData, intOffsetOfImageHeader + 4, intOffsetOfImageHeader + 7);
                int imageDataAddress = Util.GetOffset(byData, intOffsetOfImageHeader + 8, intOffsetOfImageHeader + 11);

                ImageHeader headerItem = new ImageHeader();
                tplImageInfo.Add(headerItem);
                headerItem.Width  = imageWidth;
                headerItem.Height = imageHeight;
                headerItem.Format = Util.GetImageFormat(imageFormat);

                // 取得图像
                Image  image;
                byte[] imageData = new byte[Util.GetImageByteCount(imageHeight, imageWidth, headerItem.Format)];
                Array.Copy(byData, imageDataAddress, imageData, 0, imageData.Length);

                // 设置Tpl大小
                if (i == tplFileHeader.NumberOfImages - 1)
                {
                    // 取得图像
                    this.TplFileSize = imageDataAddress + imageData.Length;
                }

                if (imageFormat == 0x08 ||
                    imageFormat == 0x09 ||
                    imageFormat == 0x0A)
                {
                    // C4_CI4、C8_CI8、C14X2_CI14x2
                    // 取得模板信息
                    int    paletteEntryCount  = Util.GetOffset(byData, intOffsetOfPaletteHeader, intOffsetOfPaletteHeader + 1);
                    int    paletteUnknown     = Util.GetOffset(byData, intOffsetOfPaletteHeader + 2, intOffsetOfPaletteHeader + 3);
                    int    paletteFormat      = Util.GetOffset(byData, intOffsetOfPaletteHeader + 4, intOffsetOfPaletteHeader + 7);
                    int    paletteDataAddress = Util.GetOffset(byData, intOffsetOfPaletteHeader + 8, intOffsetOfPaletteHeader + 11);
                    byte[] byPalette          = new byte[paletteEntryCount * 2];
                    Array.Copy(byData, paletteDataAddress, byPalette, 0, byPalette.Length);
                    headerItem.PaletteData = byPalette;

                    image = Util.PaletteImageDecode(
                        new Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb),
                        imageData,
                        Util.GetImageFormat(imageFormat),
                        byPalette,
                        paletteFormat);

                    headerItem.PaletteFormat = paletteFormat;
                }
                else if (imageFormat == 0x0e)
                {
                    // CMPR
                    image = Util.CmprImageDecode(
                        new Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb),
                        imageData);
                }
                else
                {
                    // 通常类型
                    image = Util.ImageDecode(
                        new Bitmap(imageWidth, imageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb),
                        imageData,
                        Util.GetImageFormat(imageFormat));
                }

                imageList.Add(image);
            }

            return(imageList.ToArray());
        }