예제 #1
0
        /// <summary>
        /// </summary>
        /// <param name="header">
        /// </param>
        /// <param name="palettes">
        /// </param>
        /// <param name="pixel_data">
        /// </param>
        /// <param name="y_start">
        /// </param>
        /// <param name="y_step">
        /// </param>
        /// <param name="y_end">
        /// </param>
        /// <param name="x_start">
        /// </param>
        /// <param name="x_step">
        /// </param>
        /// <param name="x_end">
        /// </param>
        /// <returns>
        /// </returns>
        private static byte[] _getImageDataGrey8bits(
            TGAHeader header, byte[] palettes, byte[] pixel_data, int y_start, int y_step, int y_end, int x_start, int x_step, int x_end)
        {
            var  image  = pixel_data;
            var  width  = header.width;
            var  height = header.height;
            byte color;
            var  i = 0;
            int  x;
            int  y;
            var  imageData = new byte[width * height * 4];

            for (y = y_start; y != y_end; y += y_step)
            {
                for (x = x_start; x != x_end; x += x_step, i++)
                {
                    color = image[i];
                    imageData[(x + width * y) * 4 + 0] = color;
                    imageData[(x + width * y) * 4 + 1] = color;
                    imageData[(x + width * y) * 4 + 2] = color;
                    imageData[(x + width * y) * 4 + 3] = 255;
                }
            }

            return(imageData);
        }
예제 #2
0
파일: tga.cs 프로젝트: tsu-kunn/TGA
            /// <summary>
            /// 指定データから作成
            /// </summary>
            /// <param name="header">TGAヘッダー</param>
            /// <param name="pImage">イメージデータ</param>
            /// <param name="pPalette">パレットデータ</param>
            /// <returns>RESULTタイプ</returns>
            public RESULT Create(TGAHeader header, byte[] pImage, byte[] pPalette)
            {
                // 引数チェック
                if (pImage == null || header.getImageSize() == 0)
                {
                    return(RESULT.ERROR_IMAGE);
                }
                if (pPalette != null && header.getPaletteSize() == 0)
                {
                    return(RESULT.ERROR_PALETTE);
                }
                if (pPalette == null && header.getPaletteSize() != 0)
                {
                    return(RESULT.ERROR_PALETTE);
                }

                // ヘッダーチェック
                if (!CheckSupport(header))
                {
                    return(RESULT.ERROR_HEADER);
                }

                // 情報の設定
                this.CalcSize(false);

                return(RESULT.ERROR_NONE);
            }
예제 #3
0
        /// <summary>
        /// Function to write out the DDS header to the stream.
        /// </summary>
        /// <param name="settings">Meta data for the image header.</param>
        /// <param name="writer">Writer interface for the stream.</param>
        /// <param name="conversionFlags">Flags for image conversion.</param>
        private void WriteHeader(IImageSettings settings, GorgonBinaryWriter writer, out TGAConversionFlags conversionFlags)
        {
            TGAHeader header = default(TGAHeader);

            conversionFlags = TGAConversionFlags.None;

            if ((settings.Width > 0xFFFF) ||
                (settings.Height > 0xFFFF))
            {
                throw new IOException(string.Format(Resources.GORGFX_IMAGE_FILE_INCORRECT_ENCODER, Codec));
            }

            header.Width  = (ushort)(settings.Width);
            header.Height = (ushort)(settings.Height);

            switch (settings.Format)
            {
            case BufferFormat.R8G8B8A8_UIntNormal:
            case BufferFormat.R8G8B8A8_UIntNormal_sRGB:
                header.ImageType  = TGAImageType.TrueColor;
                header.BPP        = 32;
                header.Descriptor = TGADescriptor.InvertY | TGADescriptor.RGB888A8;
                conversionFlags  |= TGAConversionFlags.Swizzle;
                break;

            case BufferFormat.B8G8R8A8_UIntNormal:
            case BufferFormat.B8G8R8A8_UIntNormal_sRGB:
                header.ImageType  = TGAImageType.TrueColor;
                header.BPP        = 32;
                header.Descriptor = TGADescriptor.InvertY | TGADescriptor.RGB888A8;
                break;

            case BufferFormat.B8G8R8X8_UIntNormal:
            case BufferFormat.B8G8R8X8_UIntNormal_sRGB:
                header.ImageType  = TGAImageType.TrueColor;
                header.BPP        = 24;
                header.Descriptor = TGADescriptor.InvertY;
                conversionFlags  |= TGAConversionFlags.RGB888;
                break;

            case BufferFormat.R8_UIntNormal:
            case BufferFormat.A8_UIntNormal:
                header.ImageType  = TGAImageType.BlackAndWhite;
                header.BPP        = 8;
                header.Descriptor = TGADescriptor.InvertY;
                break;

            case BufferFormat.B5G5R5A1_UIntNormal:
                header.ImageType  = TGAImageType.TrueColor;
                header.BPP        = 16;
                header.Descriptor = TGADescriptor.InvertY | TGADescriptor.RGB555A1;
                break;

            default:
                throw new IOException(string.Format(Resources.GORGFX_FORMAT_NOT_SUPPORTED, settings.Format));
            }

            // Persist to stream.
            writer.WriteValue(header);
        }
예제 #4
0
        private void ExportUVTGA(string path = "")
        {
            bool           ok          = !(path == "");
            SaveFileDialog FileDialog1 = new SaveFileDialog();

            FileDialog1.Filter = "TGA Files(*.tga)|*.tga";
            if (ok || FileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (!ok)
                {
                    path = FileDialog1.FileName;
                }
                FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
                TGA            = CreateHeaderTGA();
                TGA.width      = (short)ImageSizeX;
                TGA.height     = (short)ImageSizeY;
                TGA.bits       = 32;
                TGA.descriptor = 32;
                byte[] buff = StructureToByteArray(TGA);
                fs.Write(buff, 0, buff.Length);
                for (int i = 0; i < memsize / 2; i++)
                {
                    fs.WriteByte(255);
                    fs.WriteByte(memory[i * 2]);
                    fs.WriteByte(memory[i * 2 + 1]);
                    fs.WriteByte(255);
                }
                fs.Close();
                if (!ok)
                {
                    MessageBox.Show("Done");
                }
            }
        }
예제 #5
0
    /// <summary>
    /// Writes the image as a text file
    /// </summary>
    /// <param name="path">The place the file is going to go</param>
    /// <param name="rle">Whether or not is being compressed</param>
    /// <returns></returns>
    public bool WriteToFile(string path, bool rle = true)
    {
        var bpp = (int)Format;

        using (var writer = new BinaryWriter(File.Create(path))) {
            var header = new TGAHeader {
                IdLength        = 0,          // The IDLength set to 0 indicates that there is no image identification field in the TGA file
                ColorMapType    = 0,          // a value of 0 indicates that no palette is included
                BitsPerPixel    = (byte)(bpp * 8),
                Width           = (short)Width,
                Height          = (short)Height,
                DataTypeCode    = DataTypeFor(bpp, rle),
                ImageDescriptor = (byte)(0x20 | (Format == Format.BGRA ? 8 : 0))                 // top-left origin
            };
            WriteTo(writer, header);
            if (!rle)
            {
                writer.Write(buffer);
            }
            else
            {
                UnloadRleData(writer);
            }
        }
        return(true);
    }
예제 #6
0
    public static void Main(string[] args)
    {
        TGAHeader hdr;

        Pixel[] pix;


        //Read in tga file
        using (var ifile = new FileStream("in.tga", FileMode.Open)) {
            using (var bfile = new BinaryReader(ifile)) {
                hdr = new TGAHeader(bfile);
                pix = new Pixel[hdr.width * hdr.height];
                byte[] tmp = new byte[pix.Length * 3];
                bfile.Read(tmp, 0, tmp.Length);
                for (int i = 0, j = 0; i < pix.Length; ++i)
                {
                    pix[i].b = tmp[j++];
                    pix[i].g = tmp[j++];
                    pix[i].r = tmp[j++];
                }
            }
        }

        Console.WriteLine("Read TGA file");

        //check tga header for bad data
        if (hdr.colorType != 0)
        {
            throw new Exception("Bad colorType");
        }
        if (hdr.compression != 2)
        {
            throw new Exception("Can only use uncompressed input");
        }
        if (hdr.bitsPerPixel != 24)
        {
            throw new Exception("Can only use BGR (24 bit)");
        }
        if (hdr.descriptor != 0)
        {
            throw new Exception("Can only use BGR (descriptor 0)");
        }

        //create empty byte list and compress to it
        var opix = new List <byte>();

        compressIt(pix, opix, hdr);
        hdr.compression = 10;           //File is compressed

        //Write out compressed tga file
        using (var ofile = new FileStream("out.tga", FileMode.Create)) {
            using (var bfile = new BinaryWriter(ofile)) {
                hdr.Write(bfile);
                byte[] tmp = new byte[opix.Count];
                opix.CopyTo(tmp);
                bfile.Write(tmp);
            }
        }
        Console.WriteLine("Done");
    }
예제 #7
0
        /// <summary>
        /// </summary>
        /// <param name="header">
        /// </param>
        /// <param name="palettes">
        /// </param>
        /// <param name="pixel_data">
        /// </param>
        /// <param name="y_start">
        /// </param>
        /// <param name="y_step">
        /// </param>
        /// <param name="y_end">
        /// </param>
        /// <param name="x_start">
        /// </param>
        /// <param name="x_step">
        /// </param>
        /// <param name="x_end">
        /// </param>
        /// <returns>
        /// </returns>
        private static byte[] _getImageData16bits(
            TGAHeader header, byte[] palettes, byte[] pixel_data, int y_start, int y_step, int y_end, int x_start, int x_step, int x_end)
        {
            var image  = pixel_data;
            var width  = header.width;
            var height = header.height;
            int color;
            var i = 0;
            int x;
            int y;
            var imageData = new byte[width * height * 4];

            for (y = y_start; y != y_end; y += y_step)
            {
                for (x = x_start; x != x_end; x += x_step, i += 2)
                {
                    color = image[i + 0] + (image[i + 1] >> 8);
                    imageData[(x + width * y) * 4 + 0] = (byte)((color & 0x7C00) << 7);
                    imageData[(x + width * y) * 4 + 1] = (byte)((color & 0x03E0) << 2);
                    imageData[(x + width * y) * 4 + 2] = (byte)((color & 0x001F) << 3);
                    imageData[(x + width * y) * 4 + 3] = (byte)(((color & 0x8000) > 0) ? 0 : 255);
                }
            }

            return(imageData);
        }
예제 #8
0
        public TGAHeader CreateHeaderTGA()
        {
            TGAHeader temp = new TGAHeader();

            temp.imagetype = 0x2;
            temp.bits      = 0x18;
            return(temp);
        }
예제 #9
0
        private MemoryStream ExportTGA()
        {
            MemoryStream m = new MemoryStream();

            TGA            = CreateHeaderTGA();
            TGA.width      = (short)ImageSizeX;
            TGA.height     = (short)ImageSizeY;
            TGA.bits       = 32;
            TGA.descriptor = 32;
            byte[] buff = StructureToByteArray(TGA);
            m.Write(buff, 0, buff.Length);
            m.Write(memory, 0, memsize);
            return(m);
        }
예제 #10
0
 public static Color32[] ReadImage(TGAHeader header, byte[] data)
 {
     TGAImage image = new TGAImage();
     TGAImageType imageType = header.imageType;
     if (imageType == TGAImageType.Uncompressed_TrueColor)
     {
         return ReadTrueColorImage(header, data);
     }
     if (imageType != TGAImageType.RTE_TrueColor)
     {
         Debug.Log("Image type of " + header.imageType.ToString() + " is not supported.");
         return null;
     }
     return ReadRTETrueColorImage(header, data);
 }
예제 #11
0
 /// <summary>
 /// Writes the header information
 /// </summary>
 /// <param name="writer">Writer to use to write</param>
 /// <param name="header">The header</param>
 static void WriteTo(BinaryWriter writer, TGAHeader header)
 {
     writer.Write(header.IdLength);
     writer.Write(header.ColorMapType);
     writer.Write((byte)header.DataTypeCode);
     writer.Write(header.ColorMapOrigin);
     writer.Write(header.ColorMapLength);
     writer.Write(header.ColorMapDepth);
     writer.Write(header.OriginX);
     writer.Write(header.OriginY);
     writer.Write(header.Width);
     writer.Write(header.Height);
     writer.Write(header.BitsPerPixel);
     writer.Write(header.ImageDescriptor);
 }
예제 #12
0
    /// <summary>
    /// Reads header from a TGA file
    /// </summary>
    /// <param name="reader">Type of reader</param>
    /// <returns>Header</returns>
    static TGAHeader ReadHeader(BinaryReader reader)
    {
        var header = new TGAHeader {
            IdLength        = reader.ReadByte(),
            ColorMapType    = reader.ReadByte(),
            DataTypeCode    = (DataType)reader.ReadByte(),
            ColorMapOrigin  = reader.ReadInt16(),
            ColorMapLength  = reader.ReadInt16(),
            ColorMapDepth   = reader.ReadByte(),
            OriginX         = reader.ReadInt16(),
            OriginY         = reader.ReadInt16(),
            Width           = reader.ReadInt16(),
            Height          = reader.ReadInt16(),
            BitsPerPixel    = reader.ReadByte(),
            ImageDescriptor = reader.ReadByte()
        };

        return(header);
    }
예제 #13
0
        private MemoryStream ExportUVTGA()
        {
            MemoryStream m = new MemoryStream();

            TGA            = CreateHeaderTGA();
            TGA.width      = (short)ImageSizeX;
            TGA.height     = (short)ImageSizeY;
            TGA.bits       = 32;
            TGA.descriptor = 32;
            byte[] buff = StructureToByteArray(TGA);
            m.Write(buff, 0, buff.Length);
            for (int i = 0; i < memsize / 2; i++)
            {
                m.WriteByte(255);
                m.WriteByte(memory[i * 2]);
                m.WriteByte(memory[i * 2 + 1]);
                m.WriteByte(255);
            }
            return(m);
        }
예제 #14
0
        /// <summary>
        /// </summary>
        /// <param name="data">
        /// </param>
        /// <returns>
        /// </returns>
        public static TGAHeader GetTGAHeader(byte[] data)
        {
            var offset = 0;

            var header = new TGAHeader
            {
                id_length           = data[offset++],
                colormap_type       = data[offset++],
                image_type          = data[offset++],
                colormap_index      = data[offset++] | data[offset++] << 8,
                    colormap_length = data[offset++] | data[offset++] << 8,
                    colormap_size   = data[offset++],
                    origin          = new Array <int>(data[offset++] | data[offset++] << 8, data[offset++] | data[offset++] << 8),
                    width           = data[offset++] | data[offset++] << 8,
                    height          = data[offset++] | data[offset++] << 8,
                    pixel_size      = data[offset++],
                    flags           = data[offset++]
            };

            return(header);
        }
예제 #15
0
파일: tga.cs 프로젝트: tsu-kunn/TGA
            /**--------------------------------------------------------------------------
            * 静的メソッド
            *--------------------------------------------------------------------------*/
            /// <summary>
            /// 対応チェック
            /// </summary>
            /// <param name="header">TGAヘッダー構造体</param>
            /// <returns>true:サポートしている形式</returns>
            public static bool CheckSupport(TGAHeader header)
            {
                // 原点が0ではない?
                if (header.imageX != 0 && header.imageY != 0)
                {
                    return(false);
                }

                // 対応していないイメージタイプ
                TYPE type = (TYPE)header.imageType;

                if (!((TYPE.IMAGE_TYPE_NONE < type && type < TYPE.IMAGE_TYPE_MAX) ||
                      (TYPE.IMAGE_TYPE_INDEX_RLE <= type && type <= TYPE.IMAGE_TYPE_RLE_MAX)))
                {
                    return(false);
                }

                // 対応ビット?
                if (header.imageBit != 8 && header.imageBit != 16 &&
                    header.imageBit != 24 && header.imageBit != 32)
                {
                    return(false);
                }

                // 対応パレット?
                if (header.usePalette == 1)
                {
                    if (header.paletteIndex != 0)
                    {
                        return(false);
                    }
                    if (header.paletteBit != 24 && header.paletteBit != 32)
                    {
                        return(false);
                    }
                }

                return(true);
            }
예제 #16
0
    /// <summary>
    /// This function returns a value of type TGAHeader, it is assigned to the variable header in the function Image
    /// </summary>
    /// <param name="reader">passed into the function when called it is used to add data to the variable header</param>
    /// <returns name="header">variable declared in the function it is returned, contains data from reader variable that was added in </returns>
    static TGAHeader ReadHeader(BinaryReader reader)
    {
        /// <summary>
        /// header is set based on the data passed into the function from reader
        /// </summary>
        /// <value></value>
        var header = new TGAHeader {
            IdLength        = reader.ReadByte(),
            ColorMapType    = reader.ReadByte(),
            DataTypeCode    = (DataType)reader.ReadByte(),
            ColorMapOrigin  = reader.ReadInt16(),
            ColorMapLength  = reader.ReadInt16(),
            ColorMapDepth   = reader.ReadByte(),
            OriginX         = reader.ReadInt16(),
            OriginY         = reader.ReadInt16(),
            Width           = reader.ReadInt16(),
            Height          = reader.ReadInt16(),
            BitsPerPixel    = reader.ReadByte(),
            ImageDescriptor = reader.ReadByte()
        };

        return(header);
    }
예제 #17
0
    //ipix = uncompressed data, opix = new data list to compress to
    static void compressIt(Pixel[] ipix, List <byte> opix, TGAHeader hdr)
    {
        //int ipixSize = ipix.Length;
        int maxSize = 0;
        List <List <byte> > rows = new List <List <byte> >(hdr.height);

        while (maxSize++ < hdr.height)
        {
            rows.Add(new List <byte>());
        }

        Parallel.For(0, hdr.height, k => {              //break up tasks by height of image
            List <byte> row = new List <byte>();        //this rows byte List
            int start       = k * hdr.width;            //get start index of ipix array
            int ipixSize    = (k + 1) * hdr.width;      //get ending index of ipix array

            int i, n;
            int[] repeats = new int[ipixSize];
            for (i = ipixSize - 2; i >= 0; i--)         //accumulate repeats of each pixel in ipix
            {
                if (ipix[i] == ipix[i + 1])
                {
                    repeats[i] = repeats[i + 1] + 1;
                }
                else
                {
                    repeats[i] = 1;
                }
            }

            i = start;
            while (i < ipixSize)                         //Compress Image
            {
                int j = i;
                while (j < ipixSize && repeats[j] < 2)  //find two identical pixels
                {
                    j++;
                }
                while (i < j)
                {
                    n = j - i;
                    if (n > 128)
                    {
                        n = 128;
                    }

                    outputByte(n - 1, ref row);
                    while (n-- > 0)
                    {
                        outputPixel(ipix[i++], ref row);
                    }
                }

                if (i == ipixSize)  //check at end, else guaranteed run of identical pixels
                {
                    break;
                }

                n = repeats[i];
                if (n > 128)
                {
                    n = 128;
                }
                outputByte(0x80 + (n - 1), ref row);
                outputPixel(ipix[i], ref row);
                i += n;
            }
            rows[k] = row;
        });
        foreach (List <byte> row in rows)
        {
            foreach (byte pix in row)
            {
                opix.Add(pix);
            }
        }
    }
 protected void RGBA(TGAHeader tex, byte[] data)
 {
     Color32[] colorArray = TGAUtils.ReadImage(tex, data);
     int num = colorArray.Length * 4;
     this._data = new byte[num];
     int index = 0;
     int num3 = 0;
     while (index < colorArray.Length)
     {
         this._data[num3++] = colorArray[index].r;
         this._data[num3++] = colorArray[index].g;
         this._data[num3++] = colorArray[index].b;
         this._data[num3++] = colorArray[index].a;
         index++;
     }
 }
예제 #19
0
 private Color32[] ReadImage(TGAHeader header, byte[] data)
 {
     switch (header.imageType)
     {
         case TGAImageType.RTE_TrueColor:
             return ReadRTETrueColorImage(header, data);
         case TGAImageType.Uncompressed_TrueColor:
             return ReadTrueColorImage(header, data);
         default:
             Debug.Log("Image type of " + header.imageType.ToString() + " is not supported.");
             return null;
     }
 }
예제 #20
0
    public bool ReadImage(FileInfo file)
    {
        byte[] data = File.ReadAllBytes(file.FullName);
        if (data == null)
        {
            Debug.LogError("TGA: data error");
            return false;
        }

        if (data.Length < 18)
        {
            Debug.LogError("TGA invalid length of only " + data.Length + "bytes");
            return false;
        }

        header = new TGAHeader(data);

        //Debug.Log(header.width + " " + header.height + " " + header.bpp + " " + header.imageType.ToString());

        colorData = ReadImage(header, data);

        if (colorData == null)
            return false;

        return true;
    }
예제 #21
0
    private Color32[] ReadTrueColorImage(TGAHeader header, byte[] data)
    {
        int bpp = header.pixelDepth / 8;
        bool hasAlpha = (bpp == 4);
        Color32[] colors = new Color32[header.width * header.height];
        int index = 18; // initial offset
        int colorIndex = 0;
        Color32 pixelColor = new Color32(255, 255, 255, 255);

        for (int y = 0; y < header.height; y++)
        {
            for (int x = 0; x < header.width; x++)
            {
                if (hasAlpha)
                {
                    pixelColor.b = data[index++];
                    pixelColor.g = data[index++];
                    pixelColor.r = data[index++];
                    pixelColor.a = data[index++];
                }
                else
                {
                    pixelColor.b = data[index++];
                    pixelColor.g = data[index++];
                    pixelColor.r = data[index++];
                }

                colors[colorIndex++] = pixelColor;
            }
        }

        return colors;
    }
예제 #22
0
    private Color32[] ReadRTETrueColorImage(TGAHeader header, byte[] data)
    {
        int bpp = header.pixelDepth / 8;
        bool hasAlpha = (bpp == 4);

        Color32[] colors = new Color32[header.width * header.height];
        int index = 18; // initial offset
        int colorIndex = 0;
        Color32 pixelColor = new Color32(255, 255, 255, 255);

        byte pixelRun;
        byte i;

        while (colorIndex < header.nPixels)
        {
            pixelRun = data[index++];

            if ((pixelRun & 128) != 0)
            {
                // this is a RLE packet so we need to remove the last bit (-128) and add one to the count to get the number of pixels
                pixelRun = (byte)((pixelRun - 128) + 1);

                if (hasAlpha)
                {
                    pixelColor.b = data[index++];
                    pixelColor.g = data[index++];
                    pixelColor.r = data[index++];
                    pixelColor.a = data[index++];
                }
                else
                {
                    pixelColor.b = data[index++];
                    pixelColor.g = data[index++];
                    pixelColor.r = data[index++];
                }

                for (i = 0; i < pixelRun; i++)
                {
                    colors[colorIndex++] = pixelColor;
                }
            }
            else
            {
                // this is not an RLE packet since last bit is not set. just add one to get count of pixels
                pixelRun = (byte)(pixelRun + 1);

                for (i = 0; i < pixelRun; i++)
                {
                    if (hasAlpha)
                    {
                        pixelColor.b = data[index++];
                        pixelColor.g = data[index++];
                        pixelColor.r = data[index++];
                        pixelColor.a = data[index++];
                    }
                    else
                    {
                        pixelColor.b = data[index++];
                        pixelColor.g = data[index++];
                        pixelColor.r = data[index++];
                    }

                    colors[colorIndex++] = pixelColor;
                }
            }
        }

        return colors;
    }
예제 #23
0
파일: tga.cs 프로젝트: tsu-kunn/TGA
 /**--------------------------------------------------------------------------
 * 公開
 *--------------------------------------------------------------------------*/
 /// <summary>
 /// コンストラクタ
 /// </summary>
 public TGA()
 {
     m_Header = new TGAHeader();
     m_Footer = new TGAFooter();
 }
예제 #24
0
 private static Color32[] ReadRTETrueColorImage(TGAHeader header, byte[] data)
 {
     MethodInfo read = typeof(TGAImage).GetMethod("ReadRTETrueColorImage", (BindingFlags.Instance | BindingFlags.NonPublic));
     Color32[] colors = read.Invoke(new TGAImage(), new object[] { header, data }) as Color32[];
     return colors;
 }
            public void CreateMap(MapDepth depth, string path)
            {
                string fullPath = Path.Combine(Directory.GetCurrentDirectory(), Path.Combine("GameData", path));
                byte[] bytes = File.ReadAllBytes(fullPath);
                TGAHeader tex = new TGAHeader(bytes);

                this._name = Path.GetFileNameWithoutExtension(fullPath); ;
                this._width = tex.width;
                this._height = tex.height;
                this._bpp = (int)depth;
                this._rowWidth = this._width * this._bpp;

                switch (depth)
                {
                    case MapDepth.Greyscale:
                        GreyscaleFromRGB(tex, bytes);
                        break;

                    case MapDepth.HeightAlpha:
                        HeightAlpha(tex, bytes);
                        break;

                    case MapDepth.RGB:
                        RGB(tex, bytes);
                        break;

                    case MapDepth.RGBA:
                        RGBA(tex, bytes);
                        break;
                }

                this._isCompiled = true;
            }
 protected void GreyscaleFromRGB(TGAHeader tex, byte[] data)
 {
     Color32[] colorArray = TGAUtils.ReadImage(tex, data);
     int length = colorArray.Length;
     this._data = new byte[length];
     for (int i = 0; i < length; i++)
     {
         this._data[i] = colorArray[i].r;
     }
 }
예제 #27
0
        private byte[] CreateTGA()
        {
            bool      tgaSaveMipMaps = false;
            TGAHeader header         = new TGAHeader();

            switch (Format)
            {
            case TextureFormat.Alpha8:
                header.imageType  = 3;
                header.pixelDepth = 8;
                break;

            case TextureFormat.RGBA32:
            case TextureFormat.ARGB32:
            case TextureFormat.BGRA32:
            case TextureFormat.RGBA4444:
            case TextureFormat.ARGB4444:
                header.imageType  = 2;
                header.pixelDepth = 32;
                break;

            case TextureFormat.RGB24:
            case TextureFormat.RGB565:
                header.imageType  = 2;
                header.pixelDepth = 24;
                break;

            default:
                throw new TextureException("Invalid format for TGA: " + Format + "(" + Name + ")");
            }

            // Convert image to RGBA32
            ConvertToRGBA32();

            int mipMapCount = 1;

            if (MipMap)
            {
                mipMapCount = GetMipMapCount(Width, Height);
            }

            for (int i = 0; i < ImageCount; i++)
            {
                header.imageWidth  = Width;
                header.imageHeight = Height;

                // only want first (largest) mipmap
                for (int j = 0; j < 1; j++)
                {
                    int imageSize = header.imageWidth * header.imageHeight * header.pixelDepth / 8;
                    if (tgaSaveMipMaps || j == 0)
                    {
                        //ByteBuffer bbTga = ByteBuffer.allocateDirect(TGAHeader.SIZE + imageSize);
                        //bbTga.order(ByteOrder.LITTLE_ENDIAN);

                        _data.ResetPos();
                        using (var ms = new MemoryStream())
                            using (BinaryWriter bw = new BinaryWriter(ms))
                            {
                                byte[] bytes = new byte[imageSize];
                                _data.Read(bytes, 0, imageSize);

                                header.write(bw);
                                bw.Write(bytes);

                                return(ms.ToArray());
                            }
                    }
                }
            }
            // something went wrong, if get here
            Logger.Log(LogLevel.ERROR, "TGA creation failed for {0}", Name);
            return(new byte[0]);
        }