This class holds all of the header properties of a Targa image. This includes the TGA File Header section the ImageID and the Color Map.
Пример #1
0
        /// <summary>
        /// Loads the Targa Header information from the file.
        /// </summary>
        /// <param name="binReader">A BinaryReader that points the loaded file byte stream.</param>
        public static void LoadTGAHeaderInfo(BinaryReader binReader, TargaHeader objTargaHeader)
        {

            if (binReader != null && binReader.BaseStream != null && binReader.BaseStream.Length > 0 && binReader.BaseStream.CanSeek == true)
            {
                try
                {
                    // set the cursor at the beginning of the file.
                    binReader.BaseStream.Seek(0, SeekOrigin.Begin);

                    // read the header properties from the file
                    objTargaHeader.SetImageIDLength(binReader.ReadByte());
                    objTargaHeader.SetColorMapType((ColorMapTypes)binReader.ReadByte());
                    objTargaHeader.SetImageType((ImageType)binReader.ReadByte());

                    objTargaHeader.SetColorMapFirstEntryIndex(binReader.ReadInt16());
                    objTargaHeader.SetColorMapLength(binReader.ReadInt16());
                    objTargaHeader.SetColorMapEntrySize(binReader.ReadByte());

                    objTargaHeader.SetXOrigin(binReader.ReadInt16());
                    objTargaHeader.SetYOrigin(binReader.ReadInt16());
                    objTargaHeader.SetWidth(binReader.ReadInt16());
                    objTargaHeader.SetHeight(binReader.ReadInt16());

                    byte pixeldepth = binReader.ReadByte();
                    switch (pixeldepth)
                    {
                        case 8:
                        case 16:
                        case 24:
                        case 32:
                            objTargaHeader.SetPixelDepth(pixeldepth);
                            break;

                        default:
                            throw new Exception("Targa Image only supports 8, 16, 24, or 32 bit pixel depths.");
                    }


                    byte ImageDescriptor = binReader.ReadByte();
                    objTargaHeader.SetAttributeBits((byte)Utilities.GetBits(ImageDescriptor, 0, 4));

                    objTargaHeader.SetVerticalTransferOrder((VerticalTransferOrder)Utilities.GetBits(ImageDescriptor, 5, 1));
                    objTargaHeader.SetHorizontalTransferOrder((HorizontalTransferOrder)Utilities.GetBits(ImageDescriptor, 4, 1));

                    // load ImageID value if any
                    if (objTargaHeader.ImageIDLength > 0)
                    {
                        byte[] ImageIDValueBytes = binReader.ReadBytes(objTargaHeader.ImageIDLength);
                        objTargaHeader.SetImageIDValue(System.Text.Encoding.ASCII.GetString(ImageIDValueBytes).TrimEnd('\0'));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }


                // load color map if it's included and/or needed
                // Only needed for UNCOMPRESSED_COLOR_MAPPED and RUN_LENGTH_ENCODED_COLOR_MAPPED
                // image types. If color map is included for other file types we can ignore it.
                if (objTargaHeader.ColorMapType == ColorMapTypes.COLOR_MAP_INCLUDED)
                {
                    if (objTargaHeader.ImageType == ImageType.UNCOMPRESSED_COLOR_MAPPED ||
                        objTargaHeader.ImageType == ImageType.RUN_LENGTH_ENCODED_COLOR_MAPPED)
                    {
                        if (objTargaHeader.ColorMapLength > 0)
                        {
                            try
                            {
                                for (int i = 0; i < objTargaHeader.ColorMapLength; i++)
                                {
                                    int a = 0;
                                    int r = 0;
                                    int g = 0;
                                    int b = 0;

                                    // load each color map entry based on the ColorMapEntrySize value
                                    switch (objTargaHeader.ColorMapEntrySize)
                                    {
                                        case 15:
                                            byte[] color15 = binReader.ReadBytes(2);
                                            // remember that the bytes are stored in reverse oreder
                                            objTargaHeader.ColorMap.Add(Utilities.GetColorFrom2Bytes(color15[1], color15[0]));
                                            break;
                                        case 16:
                                            byte[] color16 = binReader.ReadBytes(2);
                                            // remember that the bytes are stored in reverse oreder
                                            objTargaHeader.ColorMap.Add(Utilities.GetColorFrom2Bytes(color16[1], color16[0]));
                                            break;
                                        case 24:
                                            b = Convert.ToInt32(binReader.ReadByte());
                                            g = Convert.ToInt32(binReader.ReadByte());
                                            r = Convert.ToInt32(binReader.ReadByte());
                                            objTargaHeader.ColorMap.Add(System.Drawing.Color.FromArgb(r, g, b));
                                            break;
                                        case 32:
                                            a = Convert.ToInt32(binReader.ReadByte());
                                            b = Convert.ToInt32(binReader.ReadByte());
                                            g = Convert.ToInt32(binReader.ReadByte());
                                            r = Convert.ToInt32(binReader.ReadByte());
                                            objTargaHeader.ColorMap.Add(System.Drawing.Color.FromArgb(a, r, g, b));
                                            break;
                                        default:
                                            throw new Exception("TargaImage only supports ColorMap Entry Sizes of 15, 16, 24 or 32 bits.");

                                    }


                                }
                            }
                            catch (Exception ex)
                            {
                                throw ex;
                            }



                        }
                        else
                        {
                            throw new Exception("Image Type requires a Color Map and Color Map Length is zero.");
                        }
                    }


                }
                else
                {
                    if (objTargaHeader.ImageType == ImageType.UNCOMPRESSED_COLOR_MAPPED ||
                        objTargaHeader.ImageType == ImageType.RUN_LENGTH_ENCODED_COLOR_MAPPED)
                    {
                        throw new Exception("Image Type requires a Color Map and there was not a Color Map included in the file.");
                    }
                }


            }
            else
            {
                throw new Exception(@"Error loading file, could not read file from disk.");
            }
        }
Пример #2
0
 /// <summary>
 /// Creates TGA image from stream.
 /// </summary>
 /// <param name="stream">Stream containing image.</param>
 /// <param name="prevHeader">TargaHeader if previously loaded.</param>
 public TargaImage(Stream stream, TargaHeader prevHeader = null) : this(prevHeader) 
 {
     byte[] filebytes = stream.ReadBytes((int)stream.Length);
     LoadFromStream(filebytes);
 }
Пример #3
0
 /// <summary>
 /// Creates a new instance of the TargaImage object.
 /// </summary>
 public TargaImage(TargaHeader prevHeader = null)
 {
     this.objTargaFooter = new TargaFooter();
     this.objTargaHeader = prevHeader ?? new TargaHeader();
     this.objTargaExtensionArea = new TargaExtensionArea();
     this.bmpTargaImage = null;
     this.bmpImageThumbnail = null;
 }
Пример #4
0
 public void Save(MemoryStream ms, WriteableBitmap img)
 {
     TargaHeader header = new TargaHeader();
 }
Пример #5
0
        /// <summary>
        /// Clears out all objects and resources.
        /// </summary>
        private void ClearAll()
        {
            if (this.bmpTargaImage != null)
            {
                this.bmpTargaImage.Dispose();
                this.bmpTargaImage = null;
            }
            if (this.ImageByteHandle.IsAllocated)
                this.ImageByteHandle.Free();

            if (this.ThumbnailByteHandle.IsAllocated)
                this.ThumbnailByteHandle.Free();

            this.objTargaHeader = new TargaHeader();
            this.objTargaExtensionArea = new TargaExtensionArea();
            this.objTargaFooter = new TargaFooter();
            this.eTGAFormat = TGAFormat.UNKNOWN;
            this.intStride = 0;
            this.intPadding = 0;
            this.rows.Clear();
            this.row.Clear();
            this.strFileName = string.Empty;

        }