Exemplo n.º 1
0
        static public bool Save(ImageBuffer image, Stream streamToSaveImageDataTo)
        {
            STargaHeader TargaHeader;

            BinaryWriter writerToSaveTo = new BinaryWriter(streamToSaveImageDataTo);

            int SourceDepth = image.BitDepth;

            // make sure there is something to save before opening the file
            if (image.Width <= 0 || image.Height <= 0)
            {
                return(false);
            }

            // set up the header
            TargaHeader.PostHeaderSkip = 0;             // no skip after the header
            if (SourceDepth == 8)
            {
                TargaHeader.ColorMapType   = 1;                         // Color type is Palette
                TargaHeader.ImageType      = 9;                         // 1 = Palette, 9 = RLE Palette
                TargaHeader.ColorMapStart  = 0;
                TargaHeader.ColorMapLength = 256;
                TargaHeader.ColorMapBits   = 24;
            }
            else
            {
                TargaHeader.ColorMapType = 0;                   // Color type is RGB
#if WRITE_RLE_COMPRESSED
                TargaHeader.ImageType = 10;                     // RLE RGB
#else
                TargaHeader.ImageType = 2;                      // RGB
#endif
                TargaHeader.ColorMapStart  = 0;
                TargaHeader.ColorMapLength = 0;
                TargaHeader.ColorMapBits   = 0;
            }
            TargaHeader.XStart     = 0;
            TargaHeader.YStart     = 0;
            TargaHeader.Width      = (ushort)image.Width;
            TargaHeader.Height     = (ushort)image.Height;
            TargaHeader.BPP        = (byte)SourceDepth;
            TargaHeader.Descriptor = 0;                 // all 8 bits are used for alpha

            TargaHeader.BinaryWrite(writerToSaveTo);

            byte[] pLineBuffer = new byte[image.StrideInBytesAbs() * 2];

            //int BytesToSave;
            switch (SourceDepth)
            {
            case 8:
                /*
                 * if (image.HasPalette())
                 * {
                 * for(int i=0; i<256; i++)
                 * {
                 *      TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_BLUE]);
                 *      TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_GREEN]);
                 *      TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_RED]);
                 * }
                 * }
                 * else
                 */
            {                                   // there is no palette for this DIB but we should write something
                for (int i = 0; i < 256; i++)
                {
                    writerToSaveTo.Write((byte)i);
                    writerToSaveTo.Write((byte)i);
                    writerToSaveTo.Write((byte)i);
                }
            }
                for (int i = 0; i < image.Height; i++)
                {
                    int    bufferOffset;
                    byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine8(pLineBuffer, buffer, bufferOffset, image.Width());
                    writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
                    writerToSaveTo.Write(buffer, bufferOffset, image.Width);
#endif
                }
                break;

            case 24:
                for (int i = 0; i < image.Height; i++)
                {
                    int    bufferOffset;
                    byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine24(pLineBuffer, buffer, bufferOffset, image.Width());
                    writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
                    writerToSaveTo.Write(buffer, bufferOffset, image.Width * 3);
#endif
                }
                break;

            case 32:
                for (int i = 0; i < image.Height; i++)
                {
                    int    bufferOffset;
                    byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine32(pLineBuffer, buffer, bufferOffset, image.Width);
                    writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
                    writerToSaveTo.Write(buffer, bufferOffset, image.Width * 4);
#endif
                }
                break;

            default:
                throw new NotSupportedException();
            }

            writerToSaveTo.Close();
            return(true);
        }
Exemplo n.º 2
0
		static public bool Save(ImageBuffer image, Stream streamToSaveImageDataTo)
		{
			STargaHeader TargaHeader;

			BinaryWriter writerToSaveTo = new BinaryWriter(streamToSaveImageDataTo);

			int SourceDepth = image.BitDepth;

			// make sure there is something to save before opening the file
			if (image.Width <= 0 || image.Height <= 0)
			{
				return false;
			}

			// set up the header
			TargaHeader.PostHeaderSkip = 0;	// no skip after the header
			if (SourceDepth == 8)
			{
				TargaHeader.ColorMapType = 1;		// Color type is Palette
				TargaHeader.ImageType = 9;		// 1 = Palette, 9 = RLE Palette
				TargaHeader.ColorMapStart = 0;
				TargaHeader.ColorMapLength = 256;
				TargaHeader.ColorMapBits = 24;
			}
			else
			{
				TargaHeader.ColorMapType = 0;		// Color type is RGB
#if WRITE_RLE_COMPRESSED
		        TargaHeader.ImageType = 10;		// RLE RGB
#else
				TargaHeader.ImageType = 2;		// RGB
#endif
				TargaHeader.ColorMapStart = 0;
				TargaHeader.ColorMapLength = 0;
				TargaHeader.ColorMapBits = 0;
			}
			TargaHeader.XStart = 0;
			TargaHeader.YStart = 0;
			TargaHeader.Width = (ushort)image.Width;
			TargaHeader.Height = (ushort)image.Height;
			TargaHeader.BPP = (byte)SourceDepth;
			TargaHeader.Descriptor = 0;	// all 8 bits are used for alpha

			TargaHeader.BinaryWrite(writerToSaveTo);

			byte[] pLineBuffer = new byte[image.StrideInBytesAbs() * 2];

			//int BytesToSave;
			switch (SourceDepth)
			{
				case 8:
					/*
				if (image.HasPalette())
				{
					for(int i=0; i<256; i++)
					{
						TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_BLUE]);
						TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_GREEN]);
						TGAFile.Write(image.GetPaletteIfAllocated()->pPalette[i * RGB_SIZE + RGB_RED]);
					}
				}
				else
					 */
					{	// there is no palette for this DIB but we should write something
						for (int i = 0; i < 256; i++)
						{
							writerToSaveTo.Write((byte)i);
							writerToSaveTo.Write((byte)i);
							writerToSaveTo.Write((byte)i);
						}
					}
					for (int i = 0; i < image.Height; i++)
					{
						int bufferOffset;
						byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine8(pLineBuffer, buffer, bufferOffset, image.Width());
			        writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
						writerToSaveTo.Write(buffer, bufferOffset, image.Width);
#endif
					}
					break;

				case 24:
					for (int i = 0; i < image.Height; i++)
					{
						int bufferOffset;
						byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine24(pLineBuffer, buffer, bufferOffset, image.Width());
                    writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
						writerToSaveTo.Write(buffer, bufferOffset, image.Width * 3);
#endif
					}
					break;

				case 32:
					for (int i = 0; i < image.Height; i++)
					{
						int bufferOffset;
						byte[] buffer = image.GetPixelPointerY(i, out bufferOffset);
#if WRITE_RLE_COMPRESSED
                    BytesToSave = CompressLine32(pLineBuffer, buffer, bufferOffset, image.Width);
                    writerToSaveTo.Write(pLineBuffer, 0, BytesToSave);
#else
						writerToSaveTo.Write(buffer, bufferOffset, image.Width * 4);
#endif
					}
					break;

				default:
					throw new NotSupportedException();
			}

			writerToSaveTo.Close();
			return true;
		}