public override void WriteTo(IO.BinaryWriter writer) { base.WriteTo(writer); writer.Write(_dataString); if (_dataBytes == null || _dataBytes.Length == 0) { writer.Write((Int32)0); } else { writer.Write((Int32)_dataBytes.Length); writer.Write(_dataBytes); } }
internal void WriteHeaders() { int i, j, index, offset; int[] tempArray; // Start of Image byte[] SOI = { (byte)0xFF, (byte)0xD8 }; WriteMarker(SOI); //System.Windows.MessageBox.Show(string.Format("{0},{1}", _input.Image.DensityX, _input.Image.DensityY)); if (!_input.HasJFIF) // Supplement JFIF if missing { byte[] JFIF = new byte[18] { (byte)0xff, (byte)0xe0, (byte)0x00, (byte)0x10, (byte)0x4a, (byte)0x46, (byte)0x49, (byte)0x46, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x01, (byte)0x00, (byte)0x00 }; //DPI if (_input.Image.DensityX > 0 && _input.Image.DensityY > 0) { JFIF[11] = 1; ushort dpiX = (ushort)_input.Image.DensityX; ushort dpiY = (ushort)_input.Image.DensityY; JFIF[12] = BitConverter.GetBytes(dpiX)[1]; JFIF[13] = BitConverter.GetBytes(dpiY)[0]; JFIF[14] = BitConverter.GetBytes(dpiX)[1]; JFIF[15] = BitConverter.GetBytes(dpiY)[0]; } WriteArray(JFIF); } IO.BinaryWriter writer = new IO.BinaryWriter(_outStream); /* APP headers and COM headers follow the same format * which has a 16-bit integer length followed by a block * of binary data. */ foreach (JpegHeader header in _input.MetaHeaders) { writer.Write(JPEGMarker.XFF); writer.Write(header.Marker); // Header's length writer.Write((short)(header.Data.Length + 2)); writer.Write(header.Data); } // The DQT header // 0 is the luminance index and 1 is the chrominance index byte[] DQT = new byte[134]; DQT[0] = JPEGMarker.XFF; DQT[1] = JPEGMarker.DQT; DQT[2] = (byte)0x00; DQT[3] = (byte)0x84; offset = 4; for (i = 0; i < 2; i++) { DQT[offset++] = (byte)((0 << 4) + i); tempArray = (int[])_dct.quantum[i]; for (j = 0; j < 64; j++) { DQT[offset++] = (byte)tempArray[ZigZag.ZigZagMap[j]]; } } WriteArray(DQT); // Start of Frame Header ( Baseline JPEG ) byte[] SOF = new byte[19]; SOF[0] = JPEGMarker.XFF; SOF[1] = JPEGMarker.SOF0; SOF[2] = (byte)0x00; SOF[3] = (byte)17; SOF[4] = (byte)_input.Precision; SOF[5] = (byte)((_input.Image.Height >> 8) & 0xFF); SOF[6] = (byte)((_input.Image.Height) & 0xFF); SOF[7] = (byte)((_input.Image.Width >> 8) & 0xFF); SOF[8] = (byte)((_input.Image.Width) & 0xFF); SOF[9] = (byte)_input.Image.ComponentCount; index = 10; for (i = 0; i < SOF[9]; i++) { SOF[index++] = (byte)JpegEncoder.CompID[i]; SOF[index++] = (byte)((_input.HsampFactor[i] << 4) + _input.VsampFactor[i]); SOF[index++] = (byte)JpegEncoder.QtableNumber[i]; } WriteArray(SOF); // The DHT Header byte[] DHT1, DHT2, DHT3, DHT4; int bytes, temp, oldindex, intermediateindex; index = 4; oldindex = 4; DHT1 = new byte[17]; DHT4 = new byte[4]; DHT4[0] = JPEGMarker.XFF; DHT4[1] = JPEGMarker.DHT; for (i = 0; i < 4; i++) { bytes = 0; // top 4 bits: table class (0=DC, 1=AC) // bottom 4: index (0=luminance, 1=chrominance) byte huffmanInfo = (i == 0) ? (byte)0x00 : (i == 1) ? (byte)0x10 : (i == 2) ? (byte)0x01 : (byte)0x11; DHT1[index++ - oldindex] = huffmanInfo; for (j = 0; j < 16; j++) { temp = _huf.bitsList[i][j]; DHT1[index++ - oldindex] = (byte)temp; bytes += temp; } intermediateindex = index; DHT2 = new byte[bytes]; for (j = 0; j < bytes; j++) { DHT2[index++ - intermediateindex] = (byte)_huf.val[i][j]; } DHT3 = new byte[index]; Array.Copy(DHT4, 0, DHT3, 0, oldindex); Array.Copy(DHT1, 0, DHT3, oldindex, 17); Array.Copy(DHT2, 0, DHT3, oldindex + 17, bytes); DHT4 = DHT3; oldindex = index; } DHT4[2] = (byte)(((index - 2) >> 8) & 0xFF); DHT4[3] = (byte)((index - 2) & 0xFF); WriteArray(DHT4); // Start of Scan Header byte[] SOS = new byte[14]; SOS[0] = JPEGMarker.XFF; SOS[1] = JPEGMarker.SOS; SOS[2] = (byte)0x00; SOS[3] = (byte)12; SOS[4] = (byte)_input.Image.ComponentCount; index = 5; for (i = 0; i < SOS[4]; i++) { SOS[index++] = (byte)JpegEncoder.CompID[i]; SOS[index++] = (byte)((JpegEncoder.DCtableNumber[i] << 4) + JpegEncoder.ACtableNumber[i]); } SOS[index++] = (byte)JpegEncoder.Ss; SOS[index++] = (byte)JpegEncoder.Se; SOS[index++] = (byte)((JpegEncoder.Ah << 4) + JpegEncoder.Al); WriteArray(SOS); }