コード例 #1
0
ファイル: TIMFile.cs プロジェクト: derplayer/Conan-Explorer
        /// <summary>
        /// Converts the TIM file into a bitmap.
        /// </summary>
        /// <returns></returns>
        private Bitmap TIM2BMP()
        {
            uint y, x;
            uint tim_row_off;

            MemoryStream memoryStream = new MemoryStream();

            using (BinaryReader reader = new BinaryReader(new FileStream(FilePath, FileMode.Open)))
            {
                TIMHeader.Load(reader);

                PS_BITMAP bitmapHeader = new PS_BITMAP(TIMHeader.ImageWidthPixels, TIMHeader.ImageHeight, TIMHeader.BPP);
                bitmapHeader.Write(memoryStream);

                if (TIMHeader.HasClut)
                {
                    if (TIMHeader.BPP == 4)
                    {
                        for (x = 0; x < 16; x++)
                        {
                            PS_RGB rgb = new PS_RGB(TIMHeader.ClutEntries[x].Data);
                            memoryStream.WriteByte(rgb.B);
                            memoryStream.WriteByte(rgb.G);
                            memoryStream.WriteByte(rgb.R);
                            memoryStream.WriteByte(0);
                        }
                    }
                    else if (TIMHeader.BPP == 8)
                    {
                        for (x = 0; x < 256; x++)
                        {
                            PS_RGB rgb = new PS_RGB(TIMHeader.ClutEntries[x].Data);
                            memoryStream.WriteByte(rgb.B);
                            memoryStream.WriteByte(rgb.G);
                            memoryStream.WriteByte(rgb.R);
                            memoryStream.WriteByte(0);
                        }
                    }
                }
                else
                {
                    if (TIMHeader.BPP == 4)
                    {
                        memoryStream.Position += 64;
                    }
                    else if (TIMHeader.BPP == 8)
                    {
                        memoryStream.Position += 1024;
                    }
                }

                if (TIMHeader.BPP == 16)
                {
                    y = (uint)(TIMHeader.ImageWidthPixels * 24) / 8;
                }
                else
                {
                    y = (uint)(TIMHeader.ImageWidthPixels * TIMHeader.BPP) / 8;
                }

                uint row_round = y;

                if ((row_round & 3) != 0)
                {
                    row_round |= 3;
                    row_round++;
                }

                row_round -= y;

                for (y = 0; y < TIMHeader.ImageHeight; y++)
                {
                    tim_row_off = (uint)(TIMHeader.ImageWidth * 2 * (TIMHeader.ImageHeight - 1 - y));

                    reader.BaseStream.Position = TIMHeader.DataOffset + tim_row_off;

                    for (x = 0; x < TIMHeader.ImageWidth; x++)
                    {
                        ushort c = reader.ReadUInt16();

                        switch (TIMHeader.BPP)
                        {
                        case 4:
                            memoryStream.WriteByte((byte)(((c >> 4) & 0xf) | ((c & 0xf) << 4)));
                            memoryStream.WriteByte((byte)(((c >> 12) & 0xf) | (((c >> 8) & 0xf) << 4)));
                            break;

                        case 8:
                            memoryStream.Write(BitConverter.GetBytes(c), 0, 2);
                            break;

                        case 16:
                            PS_RGB rgb = new PS_RGB(c);
                            memoryStream.WriteByte(rgb.B);
                            memoryStream.WriteByte(rgb.G);
                            memoryStream.WriteByte(rgb.R);
                            break;
                        }
                    }

                    for (x = 0; x < row_round; x++)
                    {
                        memoryStream.WriteByte(0);
                    }
                }
            }
            return(new Bitmap(memoryStream));
        }
コード例 #2
0
ファイル: TIMFile.cs プロジェクト: derplayer/Conan-Explorer
        /// <summary>
        /// Converts a bitmap into a TIM file
        /// </summary>
        /// <param name="bitmap">Bitmap</param>
        /// <param name="settings">Encoding settings</param>
        private void BMP2TIM(Bitmap bitmap, TIMEncodingSettings settings)
        {
            CLUTEntry[] oldEntries = new CLUTEntry[TIMHeader.ClutEntries.Length];
            Array.Copy(TIMHeader.ClutEntries, oldEntries, oldEntries.Length);
            if (!settings.UseOriginalCLUT)
            {
                TIMHeader.GenerateClut(bitmap, settings);
            }
            if (settings.UseOriginalColor)
            {
                TIMHeader.SetOriginalColor(oldEntries);
            }
            if (settings.UseOriginalTransparency)
            {
                TIMHeader.SetSemiTransparentBits(oldEntries);
            }
            using (BinaryWriter writer = new BinaryWriter(new FileStream(FilePath, FileMode.OpenOrCreate)))
            {
                TIMHeader.Write(writer.BaseStream);
                switch (TIMHeader.BPP)
                {
                case 24:
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x += 2)
                        {
                            Color color1 = bitmap.GetPixel(x, y);
                            Color color2 = bitmap.GetPixel(x + 1, y);
                            writer.Write((ushort)((color1.G << 8) | color1.R));
                            writer.Write((ushort)((color2.R << 8) | color1.B));
                            writer.Write((ushort)((color2.B << 8) | color2.G));
                        }
                    }
                    break;

                case 16:
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x++)
                        {
                            Color  color = bitmap.GetPixel(x, y);
                            PS_RGB rgb   = new PS_RGB(color.R, color.G, color.B);
                            writer.Write(rgb.ToRGBPSX(settings));
                        }
                    }
                    break;

                case 8:
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x += 2)
                        {
                            Color  color1   = bitmap.GetPixel(x, y);
                            Color  color2   = bitmap.GetPixel(x + 1, y);
                            ushort shortbuf = (ushort)((GetColorIndex(bitmap, color2, settings.UseOriginalCLUT) << 8) | GetColorIndex(bitmap, color1, settings.UseOriginalCLUT));
                            writer.Write(shortbuf);
                        }
                    }
                    break;

                case 4:
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        for (int x = 0; x < bitmap.Width; x += 4)
                        {
                            Color  color1   = bitmap.GetPixel(x, y);
                            Color  color2   = bitmap.GetPixel(x + 1, y);
                            Color  color3   = bitmap.GetPixel(x + 2, y);
                            Color  color4   = bitmap.GetPixel(x + 3, y);
                            ushort shortbuf = (ushort)((GetColorIndex(bitmap, color4, settings.UseOriginalCLUT) << 12) |
                                                       (GetColorIndex(bitmap, color3, settings.UseOriginalCLUT) << 8) |
                                                       (GetColorIndex(bitmap, color2, settings.UseOriginalCLUT) << 4) |
                                                       GetColorIndex(bitmap, color1, settings.UseOriginalCLUT));
                            writer.Write(shortbuf);
                        }
                    }
                    break;
                }
            }
        }