Example #1
0
        public static System.Drawing.Size GetTGASize(string filename)
        {
            System.IO.FileStream f = System.IO.File.OpenRead(filename);

            System.IO.BinaryReader br = new System.IO.BinaryReader(f);

            tgaHeader header = new tgaHeader();
            header.Read(br);
            br.Close();

            return new System.Drawing.Size(header.ImageSpec.Width, header.ImageSpec.Height);
        }
Example #2
0
        public static unsafe ManagedImage LoadTGAImage(System.IO.Stream source, bool mask)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

            System.IO.BinaryReader br = new System.IO.BinaryReader(ms);

            tgaHeader header = new tgaHeader();
            header.Read(br);

            if (header.ImageSpec.PixelDepth != 8 &&
                header.ImageSpec.PixelDepth != 16 &&
                header.ImageSpec.PixelDepth != 24 &&
                header.ImageSpec.PixelDepth != 32)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.AlphaBits > 8)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.Width > 4096 ||
                header.ImageSpec.Height > 4096)
                throw new ArgumentException("Image too large.");

            byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4];
            System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();

            fixed (byte* pdecoded = &decoded[0])
            {
                bd.Width = header.ImageSpec.Width;
                bd.Height = header.ImageSpec.Height;
                bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
                bd.Stride = header.ImageSpec.Width * 4;
                bd.Scan0 = (IntPtr)pdecoded;

                switch (header.ImageSpec.PixelDepth)
                {
                    case 8:
                        decodeStandard8(bd, header, br);
                        break;
                    case 16:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial16(bd, header, br);
                        else
                            decodeStandard16(bd, header, br);
                        break;
                    case 24:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial24(bd, header, br);
                        else
                            decodeStandard24(bd, header, br);
                        break;
                    case 32:
                        decodeStandard32(bd, header, br);
                        break;
                    default:
                        return null;
                }
            }

            int n = header.ImageSpec.Width * header.ImageSpec.Height;
            ManagedImage image;
            
            if (mask && header.ImageSpec.AlphaBits == 0 && header.ImageSpec.PixelDepth == 8)
            {
                image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height,
                    ManagedImage.ImageChannels.Alpha);
                int p = 3;

                for (int i = 0; i < n; i++)
                {
                    image.Alpha[i] = decoded[p];
                    p += 4;
                }
            }
            else
            {
                image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height,
                    ManagedImage.ImageChannels.Color | ManagedImage.ImageChannels.Alpha);
                int p = 0;

                for (int i = 0; i < n; i++)
                {
                    image.Blue[i] = decoded[p++];
                    image.Green[i] = decoded[p++];
                    image.Red[i] = decoded[p++];
                    image.Alpha[i] = decoded[p++];
                }
            }

            br.Close();
            return image;
        }
Example #3
0
        public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

            System.IO.BinaryReader br = new System.IO.BinaryReader(ms);

            tgaHeader header = new tgaHeader();
            header.Read(br);

            if (header.ImageSpec.PixelDepth != 8 &&
                header.ImageSpec.PixelDepth != 16 &&
                header.ImageSpec.PixelDepth != 24 &&
                header.ImageSpec.PixelDepth != 32)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.AlphaBits > 8)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.Width > 4096 ||
                header.ImageSpec.Height > 4096)
                throw new ArgumentException("Image too large.");

            System.Drawing.Bitmap b = new System.Drawing.Bitmap(
                header.ImageSpec.Width, header.ImageSpec.Height);

            System.Drawing.Imaging.BitmapData bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
                System.Drawing.Imaging.ImageLockMode.WriteOnly,
                System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            switch (header.ImageSpec.PixelDepth)
            {
                case 8:
                    decodeStandard8(bd, header, br);
                    break;
                case 16:
                    if (header.ImageSpec.AlphaBits > 0)
                        decodeSpecial16(bd, header, br);
                    else
                        decodeStandard16(bd, header, br);
                    break;
                case 24:
                    if (header.ImageSpec.AlphaBits > 0)
                        decodeSpecial24(bd, header, br);
                    else
                        decodeStandard24(bd, header, br);
                    break;
                case 32:
                    decodeStandard32(bd, header, br);
                    break;
                default:
                    b.UnlockBits(bd);
                    b.Dispose();
                    return null;
            }
            b.UnlockBits(bd);
            br.Close();
            return b;
        }
Example #4
0
        static void decodeStandard32(System.Drawing.Imaging.BitmapData b,
            tgaHeader hdr, System.IO.BinaryReader br)
        {
            // i must convert the input stream to a sequence of uint values
            // which I then unpack.
            tgaCD cd = new tgaCD();
            cd.RMask = 0x00ff0000;
            cd.GMask = 0x0000ff00;
            cd.BMask = 0x000000ff;
            cd.AMask = 0xff000000;
            cd.RShift = 0;
            cd.GShift = 0;
            cd.BShift = 0;
            cd.AShift = 0;
            cd.FinalOr = 0x00000000;
            cd.NeedNoConvert = true;

            if (hdr.RleEncoded)
                decodeRle(b, 4, cd, br, hdr.ImageSpec.BottomUp);
            else
                decodePlain(b, 4, cd, br, hdr.ImageSpec.BottomUp);
        }
Example #5
0
        static void decodeStandard16(
            System.Drawing.Imaging.BitmapData b,
            tgaHeader hdr,
            System.IO.BinaryReader br)
        {
            // i must convert the input stream to a sequence of uint values
            // which I then unpack.
            tgaCD cd = new tgaCD();
            cd.RMask = 0x00f80000;	// from 0xF800
            cd.GMask = 0x0000fc00;	// from 0x07E0
            cd.BMask = 0x000000f8;  // from 0x001F
            cd.AMask = 0x00000000;
            cd.RShift = 8;
            cd.GShift = 5;
            cd.BShift = 3;
            cd.AShift = 0;
            cd.FinalOr = 0xff000000;

            if (hdr.RleEncoded)
                decodeRle(b, 2, cd, br, hdr.ImageSpec.BottomUp);
            else
                decodePlain(b, 2, cd, br, hdr.ImageSpec.BottomUp);
        }
Example #6
0
 static void decodeStandard8(
     System.Drawing.Imaging.BitmapData b,
     tgaHeader hdr,
     System.IO.BinaryReader br)
 {
     tgaCD cd = new tgaCD();
     cd.RMask = 0x000000ff;
     cd.GMask = 0x000000ff;
     cd.BMask = 0x000000ff;
     cd.AMask = 0x000000ff;
     cd.RShift = 0;
     cd.GShift = 0;
     cd.BShift = 0;
     cd.AShift = 0;
     cd.FinalOr = 0x00000000;
     if (hdr.RleEncoded)
         decodeRle(b, 1, cd, br, hdr.ImageSpec.BottomUp);
     else
         decodePlain(b, 1, cd, br, hdr.ImageSpec.BottomUp);
 }
Example #7
0
        public static unsafe byte[] LoadTGARaw(System.IO.Stream source)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

            System.IO.BinaryReader br = new System.IO.BinaryReader(ms);

            tgaHeader header = new tgaHeader();
            header.Read(br);

            if (header.ImageSpec.PixelDepth != 8 &&
                header.ImageSpec.PixelDepth != 16 &&
                header.ImageSpec.PixelDepth != 24 &&
                header.ImageSpec.PixelDepth != 32)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.AlphaBits > 8)
                throw new ArgumentException("Not a supported tga file.");

            if (header.ImageSpec.Width > 4096 ||
                header.ImageSpec.Height > 4096)
                throw new ArgumentException("Image too large.");

            byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4];
            System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();

            fixed (byte* pdecoded = &decoded[0])
            {
                bd.Width = header.ImageSpec.Width;
                bd.Height = header.ImageSpec.Height;
                bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
                bd.Stride = header.ImageSpec.Width * 4;
                bd.Scan0 = (IntPtr)pdecoded;

                switch (header.ImageSpec.PixelDepth)
                {
                    case 8:
                        decodeStandard8(bd, header, br);
                        break;
                    case 16:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial16(bd, header, br);
                        else
                            decodeStandard16(bd, header, br);
                        break;
                    case 24:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial24(bd, header, br);
                        else
                            decodeStandard24(bd, header, br);
                        break;
                    case 32:
                        decodeStandard32(bd, header, br);
                        break;
                    default:
                        return null;
                }
            }

            // swap red and blue channels (TGA is BGRA)
            byte tmp;
            for (int i = 0; i < decoded.Length; i += 4)
            {
                tmp = decoded[i];
                decoded[i] = decoded[i + 2];
                decoded[i + 2] = tmp;
            }

            br.Close();
            return decoded;
        }
Example #8
0
        public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

            using (System.IO.BinaryReader br = new System.IO.BinaryReader(ms))
            {
                tgaHeader header = new tgaHeader();
                header.Read(br);

                if (header.ImageSpec.PixelDepth != 8 &&
                    header.ImageSpec.PixelDepth != 16 &&
                    header.ImageSpec.PixelDepth != 24 &&
                    header.ImageSpec.PixelDepth != 32)
                    throw new ArgumentException("Not a supported tga file.");

                if (header.ImageSpec.AlphaBits > 8)
                    throw new ArgumentException("Not a supported tga file.");

                if (header.ImageSpec.Width > 4096 ||
                    header.ImageSpec.Height > 4096)
                    throw new ArgumentException("Image too large.");

				System.Drawing.Bitmap b;
				System.Drawing.Imaging.BitmapData bd;

				// Create a bitmap for the image.
				// Only include an alpha layer when the image requires one.
				if (header.ImageSpec.AlphaBits > 0 ||
					header.ImageSpec.PixelDepth == 8 ||	// Assume  8 bit images are alpha only
					header.ImageSpec.PixelDepth == 32)	// Assume 32 bit images are ARGB
				{	// Image needs an alpha layer
					b = new System.Drawing.Bitmap(
						header.ImageSpec.Width,
						header.ImageSpec.Height,
						System.Drawing.Imaging.PixelFormat.Format32bppArgb);

					bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
						System.Drawing.Imaging.ImageLockMode.WriteOnly,
						System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
				}
				else
				{	// Image does not need an alpha layer, so do not include one.
					b = new System.Drawing.Bitmap(
						header.ImageSpec.Width,
						header.ImageSpec.Height,
						System.Drawing.Imaging.PixelFormat.Format32bppRgb);

					bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
						System.Drawing.Imaging.ImageLockMode.WriteOnly,
						System.Drawing.Imaging.PixelFormat.Format32bppRgb);
				}

                switch (header.ImageSpec.PixelDepth)
                {
                    case 8:
                        decodeStandard8(bd, header, br);
                        break;
                    case 16:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial16(bd, header, br);
                        else
                            decodeStandard16(bd, header, br);
                        break;
                    case 24:
                        if (header.ImageSpec.AlphaBits > 0)
                            decodeSpecial24(bd, header, br);
                        else
                            decodeStandard24(bd, header, br);
                        break;
                    case 32:
                        decodeStandard32(bd, header, br);
                        break;
                    default:
                        b.UnlockBits(bd);
                        b.Dispose();
                        return null;
                }

                b.UnlockBits(bd);
                return b;
            }
        }
        public static unsafe ManagedImage LoadTGAImage(System.IO.Stream source, bool mask)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            using (MemoryStream ms = new System.IO.MemoryStream(buffer))
                using (BinaryReader br = new BinaryReader(ms))
                {
                    tgaHeader header = new tgaHeader();
                    header.Read(br);

                    if (header.ImageSpec.PixelDepth != 8 &&
                        header.ImageSpec.PixelDepth != 16 &&
                        header.ImageSpec.PixelDepth != 24 &&
                        header.ImageSpec.PixelDepth != 32)
                    {
                        throw new ArgumentException("Not a supported tga file.");
                    }

                    if (header.ImageSpec.AlphaBits > 8)
                    {
                        throw new ArgumentException("Not a supported tga file.");
                    }

                    if (header.ImageSpec.Width > 4096 ||
                        header.ImageSpec.Height > 4096)
                    {
                        throw new ArgumentException("Image too large.");
                    }

                    byte[] decoded = new byte[header.ImageSpec.Width * header.ImageSpec.Height * 4];
                    System.Drawing.Imaging.BitmapData bd = new System.Drawing.Imaging.BitmapData();

                    fixed(byte *pdecoded = &decoded[0])
                    {
                        bd.Width       = header.ImageSpec.Width;
                        bd.Height      = header.ImageSpec.Height;
                        bd.PixelFormat = System.Drawing.Imaging.PixelFormat.Format32bppPArgb;
                        bd.Stride      = header.ImageSpec.Width * 4;
                        bd.Scan0       = (IntPtr)pdecoded;

                        switch (header.ImageSpec.PixelDepth)
                        {
                        case 8:
                            decodeStandard8(bd, header, br);
                            break;

                        case 16:
                            if (header.ImageSpec.AlphaBits > 0)
                            {
                                decodeSpecial16(bd, header, br);
                            }
                            else
                            {
                                decodeStandard16(bd, header, br);
                            }
                            break;

                        case 24:
                            if (header.ImageSpec.AlphaBits > 0)
                            {
                                decodeSpecial24(bd, header, br);
                            }
                            else
                            {
                                decodeStandard24(bd, header, br);
                            }
                            break;

                        case 32:
                            decodeStandard32(bd, header, br);
                            break;

                        default:
                            return(null);
                        }
                    }

                    int          n = header.ImageSpec.Width * header.ImageSpec.Height;
                    ManagedImage image;

                    if (mask && header.ImageSpec.AlphaBits == 0 && header.ImageSpec.PixelDepth == 8)
                    {
                        image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height,
                                                 ManagedImage.ImageChannels.Alpha);
                        int p = 3;

                        for (int i = 0; i < n; i++)
                        {
                            image.Alpha[i] = decoded[p];
                            p += 4;
                        }
                    }
                    else
                    {
                        image = new ManagedImage(header.ImageSpec.Width, header.ImageSpec.Height,
                                                 ManagedImage.ImageChannels.Color | ManagedImage.ImageChannels.Alpha);
                        int p = 0;

                        for (int i = 0; i < n; i++)
                        {
                            image.Blue[i]  = decoded[p++];
                            image.Green[i] = decoded[p++];
                            image.Red[i]   = decoded[p++];
                            image.Alpha[i] = decoded[p++];
                        }
                    }

                    br.Close();
                    return(image);
                }
        }
        public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            using (MemoryStream ms = new MemoryStream(buffer))
                using (System.IO.BinaryReader br = new BinaryReader(ms))
                {
                    tgaHeader header = new tgaHeader();
                    header.Read(br);

                    if (header.ImageSpec.PixelDepth != 8 &&
                        header.ImageSpec.PixelDepth != 16 &&
                        header.ImageSpec.PixelDepth != 24 &&
                        header.ImageSpec.PixelDepth != 32)
                    {
                        throw new ArgumentException("Not a supported tga file.");
                    }

                    if (header.ImageSpec.AlphaBits > 8)
                    {
                        throw new ArgumentException("Not a supported tga file.");
                    }

                    if (header.ImageSpec.Width > 4096 ||
                        header.ImageSpec.Height > 4096)
                    {
                        throw new ArgumentException("Image too large.");
                    }

                    System.Drawing.Bitmap             b;
                    System.Drawing.Imaging.BitmapData bd;

                    // Create a bitmap for the image.
                    // Only include an alpha layer when the image requires one.
                    if (header.ImageSpec.AlphaBits > 0 ||
                        header.ImageSpec.PixelDepth == 8 || // Assume  8 bit images are alpha only
                        header.ImageSpec.PixelDepth == 32)  // Assume 32 bit images are ARGB
                    {                                       // Image needs an alpha layer
                        b = new System.Drawing.Bitmap(
                            header.ImageSpec.Width,
                            header.ImageSpec.Height,
                            System.Drawing.Imaging.PixelFormat.Format32bppArgb);

                        bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
                    }
                    else
                    {                   // Image does not need an alpha layer, so do not include one.
                        b = new System.Drawing.Bitmap(
                            header.ImageSpec.Width,
                            header.ImageSpec.Height,
                            System.Drawing.Imaging.PixelFormat.Format32bppRgb);

                        bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
                                        System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                        System.Drawing.Imaging.PixelFormat.Format32bppRgb);
                    }

                    switch (header.ImageSpec.PixelDepth)
                    {
                    case 8:
                        decodeStandard8(bd, header, br);
                        break;

                    case 16:
                        if (header.ImageSpec.AlphaBits > 0)
                        {
                            decodeSpecial16(bd, header, br);
                        }
                        else
                        {
                            decodeStandard16(bd, header, br);
                        }
                        break;

                    case 24:
                        if (header.ImageSpec.AlphaBits > 0)
                        {
                            decodeSpecial24(bd, header, br);
                        }
                        else
                        {
                            decodeStandard24(bd, header, br);
                        }
                        break;

                    case 32:
                        decodeStandard32(bd, header, br);
                        break;

                    default:
                        b.UnlockBits(bd);
                        b.Dispose();
                        return(null);
                    }

                    b.UnlockBits(bd);
                    return(b);
                }
        }
Example #11
0
        public static System.Drawing.Bitmap LoadTGA(System.IO.Stream source)
        {
            byte[] buffer = new byte[source.Length];
            source.Read(buffer, 0, buffer.Length);

            System.IO.MemoryStream ms = new System.IO.MemoryStream(buffer);

            System.IO.BinaryReader br = new System.IO.BinaryReader(ms);

            tgaHeader header = new tgaHeader();

            header.Read(br);

            if (header.ImageSpec.PixelDepth != 8 &&
                header.ImageSpec.PixelDepth != 16 &&
                header.ImageSpec.PixelDepth != 24 &&
                header.ImageSpec.PixelDepth != 32)
            {
                throw new ArgumentException("Not a supported tga file.");
            }

            if (header.ImageSpec.AlphaBits > 8)
            {
                throw new ArgumentException("Not a supported tga file.");
            }

            if (header.ImageSpec.Width > 4096 ||
                header.ImageSpec.Height > 4096)
            {
                throw new ArgumentException("Image too large.");
            }

            System.Drawing.Bitmap b = new System.Drawing.Bitmap(
                header.ImageSpec.Width, header.ImageSpec.Height);

            System.Drawing.Imaging.BitmapData bd = b.LockBits(new System.Drawing.Rectangle(0, 0, b.Width, b.Height),
                                                              System.Drawing.Imaging.ImageLockMode.WriteOnly,
                                                              System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
            switch (header.ImageSpec.PixelDepth)
            {
            case 8:
                decodeStandard8(bd, header, br);
                break;

            case 16:
                if (header.ImageSpec.AlphaBits > 0)
                {
                    decodeSpecial16(bd, header, br);
                }
                else
                {
                    decodeStandard16(bd, header, br);
                }
                break;

            case 24:
                if (header.ImageSpec.AlphaBits > 0)
                {
                    decodeSpecial24(bd, header, br);
                }
                else
                {
                    decodeStandard24(bd, header, br);
                }
                break;

            case 32:
                decodeStandard32(bd, header, br);
                break;

            default:
                b.UnlockBits(bd);
                b.Dispose();
                return(null);
            }
            b.UnlockBits(bd);
            br.Close();
            return(b);
        }