Exemplo n.º 1
0
        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        /// <returns>Returns a string that represents the current object.</returns>
        public override string ToString()
        {
            var sb = new StringBuilder();

            sb.AppendFormat("IDLength       : {0}\r\n", IDLength);
            sb.AppendFormat("ColorMapType   : {0}({1})\r\n", ColorMapType, ColorMapTypes.ToFormattedText(ColorMapType));
            sb.AppendFormat("ImageType      : {0}({1})\r\n", ImageType, ImageTypes.ToFormattedText(ImageType));
            sb.AppendFormat("ColorMapStart  : {0}\r\n", ColorMapStart);
            sb.AppendFormat("ColorMapLength : {0}\r\n", ColorMapLength);
            sb.AppendFormat("ColorMapDepth  : {0}\r\n", ColorMapDepth);
            sb.AppendFormat("XOffset        : {0}\r\n", XOffset);
            sb.AppendFormat("YOffset        : {0}\r\n", YOffset);
            sb.AppendFormat("Width          : {0}\r\n", Width);
            sb.AppendFormat("Height         : {0}\r\n", Height);
            sb.AppendFormat("PixelDepth     : {0}\r\n", PixelDepth);
            sb.AppendFormat("ImageDescriptor: 0x{0:X02}(attribute bits: {1}, image origin: {2})\r\n",
                            ImageDescriptor,
                            AttributeBits,
                            ImageOriginTypes.ToFormattedText(ImageOrigin));
            return(sb.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Read an image data.
        /// </summary>
        /// <param name="reader">A binary reader that contains TGA file. Caller must dipose the binary reader.</param>
        private void ReadImageBytes(BinaryReader reader)
        {
            switch (Header.ImageType)
            {
            case ImageTypes.ColorMapped:
            case ImageTypes.TrueColor:
            case ImageTypes.Monochrome:
                ReadUncompressedData(reader);
                break;

            case ImageTypes.CompressedColorMapped:
            case ImageTypes.CompressedTrueColor:
            case ImageTypes.CompressedMonochrome:
                DecodeRunLengthEncoding(reader);
                break;

            default:
                throw new NotSupportedException(
                          string.Format("Image type \"{0}({1})\" isn't supported.", Header.ImageType, ImageTypes.ToFormattedText(Header.ImageType)));
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Extracts a pixel data.
        /// </summary>
        /// <param name="rawPixelData">A raw pixel data in the TGA file.</param>
        /// <returns>
        /// Returns a pixel data in the palette, if an image type is color-mapped.
        /// Returns a raw pixel data, if an image type is RGB or grayscale.
        /// </returns>
        private byte[] ExtractPixelData(byte[] rawPixelData)
        {
            byte[] pixelData = null;

            switch (Header.ImageType)
            {
            case ImageTypes.ColorMapped:
            case ImageTypes.CompressedColorMapped:
            {
                // Extracts a pixel data in the palette.
                var paletteIndex  = GetPaletteIndex(rawPixelData);
                var bytesPerPixel = GetBytesPerPixel();
                var realPixelData = new byte[bytesPerPixel];
                Array.Copy(ColorMap,
                           (Header.ColorMapStart + paletteIndex) * bytesPerPixel,
                           realPixelData,
                           0,
                           realPixelData.Length);
                pixelData = realPixelData;
            }
            break;

            case ImageTypes.TrueColor:
            case ImageTypes.Monochrome:
            case ImageTypes.CompressedTrueColor:
            case ImageTypes.CompressedMonochrome:
                // Returns a raw pixel data as is.
                pixelData = rawPixelData;
                break;

            default:
                throw new NotSupportedException(
                          string.Format("Image type \"{0}({1})\" isn't supported.", Header.ImageType, ImageTypes.ToFormattedText(Header.ImageType)));
            }

            if (!HasAlpha() && !useAlphaChannelForcefully_ && (GetPixelFormat() == PixelFormats.Bgra32))
            {
                pixelData[ArgbOffset.Alpha] = 0xFF;
            }

            return(pixelData);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Gets a pixel format of TGA image.
        /// </summary>
        /// <returns>Returns a pixel format of TGA image.</returns>
        private PixelFormat GetPixelFormat()
        {
            switch (Header.ImageType)
            {
            case ImageTypes.ColorMapped:
            case ImageTypes.CompressedColorMapped:
            {
                // color depth of color-mapped image is defined in the palette
                switch (Header.ColorMapDepth)
                {
                case ColorDepth.Bpp15:
                case ColorDepth.Bpp16:
                    return(PixelFormats.Bgr555);

                case ColorDepth.Bpp24:
                    return(PixelFormats.Bgr24);

                case ColorDepth.Bpp32:
                    return(PixelFormats.Bgra32);

                default:
                    throw new NotSupportedException(string.Format("Color depth isn't supported({0}bpp).", Header.ColorMapDepth));
                }
            }

            case ImageTypes.TrueColor:
            case ImageTypes.CompressedTrueColor:
            {
                switch (Header.PixelDepth)
                {
                case ColorDepth.Bpp15:
                case ColorDepth.Bpp16:
                    return(PixelFormats.Bgr555);

                case ColorDepth.Bpp24:
                    return(PixelFormats.Bgr24);

                case ColorDepth.Bpp32:
                    return(PixelFormats.Bgra32);

                default:
                    throw new NotSupportedException(string.Format("Color depth isn't supported({0}bpp).", Header.PixelDepth));
                }
            }

            case ImageTypes.Monochrome:
            case ImageTypes.CompressedMonochrome:
            {
                switch (Header.PixelDepth)
                {
                case ColorDepth.Bpp8:
                    return(PixelFormats.Gray8);

                default:
                    throw new NotSupportedException(string.Format("Color depth isn't supported({0}bpp).", Header.PixelDepth));
                }
            }

            default:
                throw new NotSupportedException(
                          string.Format("Image type \"{0}({1})\" isn't supported.", Header.ImageType, ImageTypes.ToFormattedText(Header.ImageType)));
            }
        }