Exemplo n.º 1
0
        private static void writeData(Bitmap png, BinaryWriterWrapper bw, bool premultiply)
        {
            bw.Write7BitEncodedInt(1);       // type-reader-count
            bw.WriteString(TEXTURE_2D_TYPE); // type-reader-name
            bw.WriteInt(0);                  // reader version number
            bw.Write7BitEncodedInt(0);       // shared-resource-count
            // writing the image pixel data
            bw.WriteByte(1);                 // type id + 1 (referencing the TEXTURE_2D_TYPE)
            bw.WriteInt(0);                  // surface format; 0=color
            bw.WriteInt(png.Width);
            bw.WriteInt(png.Height);
            bw.WriteInt(1);              // mip count
            bw.WriteInt(imageSize(png)); // number of bytes in the image pixel data
            BitmapData bitmapData = png.LockBits(new Rectangle(0, 0, png.Width, png.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            try {
                var    length = bitmapData.Stride * bitmapData.Height;
                byte[] bytes  = new byte[length];
                Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
                for (int i = 0; i < bytes.Length; i += 4)
                {
                    // always swap red and blue channels
                    // premultiply alpha if requested
                    int a = bytes[i + 3];
                    if (!premultiply || a == 255)
                    {
                        // No premultiply necessary
                        byte b = bytes[i];
                        bytes[i]     = bytes[i + 2];
                        bytes[i + 2] = b;
                    }
                    else if (a != 0)
                    {
                        byte b = bytes[i];
                        bytes[i]     = (byte)(bytes[i + 2] * a / 255);
                        bytes[i + 1] = (byte)(bytes[i + 1] * a / 255);
                        bytes[i + 2] = (byte)(b * a / 255);
                    }
                    else
                    {
                        // alpha is zero, so just zero everything
                        bytes[i]     = 0;
                        bytes[i + 1] = 0;
                        bytes[i + 2] = 0;
                    }
                }
                bw.WriteByteArray(bytes);
            } finally {
                png.UnlockBits(bitmapData);
            }
        }
Exemplo n.º 2
0
 private static void writeCompressedData(BinaryWriterWrapper bw, Bitmap png, bool premultiply)
 {
     using (MemoryStream stream = new MemoryStream()) {
         byte[] uncompressedData;
         using (BinaryWriterWrapper mw = new BinaryWriterWrapper(new BinaryWriter(stream))) {
             writeData(png, mw, premultiply);
             uncompressedData = stream.ToArray();
         }
         byte[] compressedData = XCompress.Compress(uncompressedData);
         bw.WriteInt(6 + 4 + 4 + compressedData.Length);       // compressed file size including headers
         bw.WriteInt(uncompressedData.Length);                 // uncompressed data size (exluding headers! only the data)
         bw.WriteByteArray(compressedData);
     }
 }
Exemplo n.º 3
0
		private static void writeData(Bitmap png, BinaryWriterWrapper bw) {
			bw.Write7BitEncodedInt(1);       // type-reader-count
			bw.WriteString(TEXTURE_2D_TYPE); // type-reader-name
			bw.WriteInt(0);                  // reader version number
			bw.Write7BitEncodedInt(0);       // shared-resource-count
			// writing the image pixel data
			bw.WriteByte(1); // type id + 1 (referencing the TEXTURE_2D_TYPE)
			bw.WriteInt(0);  // surface format; 0=color
			bw.WriteInt(png.Width);
			bw.WriteInt(png.Height);
			bw.WriteInt(1); // mip count
			bw.WriteInt(imageSize(png)); // number of bytes in the image pixel data
			BitmapData bitmapData = png.LockBits(new Rectangle(0, 0, png.Width, png.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
			try {
				var length = bitmapData.Stride * bitmapData.Height;
				byte[] bytes = new byte[length];
				Marshal.Copy(bitmapData.Scan0, bytes, 0, length);
				for (int i = 0; i < bytes.Length; i += 4) {
					byte b = bytes[i];
					bytes[i] = bytes[i + 2];
					bytes[i + 2] = b;
				}
				bw.WriteByteArray(bytes);
			} finally {
				png.UnlockBits(bitmapData);
			}
		}
Exemplo n.º 4
0
		private static void writeCompressedData(BinaryWriterWrapper bw, Bitmap png) {
			using (MemoryStream stream = new MemoryStream()) {
				byte[] uncompressedData;
				using (BinaryWriterWrapper mw = new BinaryWriterWrapper(new BinaryWriter(stream))) {
					writeData(png, mw);
					uncompressedData = stream.ToArray();
				}
				byte[] compressedData = XCompress.Compress(uncompressedData);
				bw.WriteInt(6 + 4 + 4 + compressedData.Length); // compressed file size including headers
				bw.WriteInt(uncompressedData.Length); // uncompressed data size (exluding headers! only the data)
				bw.WriteByteArray(compressedData);
			}
		}