private int WriteBlock(Block block, QuantIndex index, int prevDc) { FDCT.Transform(block); int dc = Round(block[0], 8*quant[(int) index][0]); EmitHuffRle((HuffIndex) (2*(int) index + 0), 0, dc - prevDc); var h = (HuffIndex) (2*(int) index + 1); int runLength = 0; for (int zig = 1; zig < Block.blockSize; zig++){ int ac = Round(block[unzig[zig]], 8*quant[(int) index][zig]); if (ac == 0){ runLength++; } else{ while (runLength > 15){ EmitHuff(h, 0xf0); runLength -= 16; } EmitHuffRle(h, runLength, ac); runLength = 0; } } if (runLength > 0){ EmitHuff(h, 0x00); } return dc; }
/// <summary> /// Writes data to "Define Quantization Tables" block for QuantIndex /// </summary> /// <param name="dqt">The "Define Quantization Tables" block</param> /// <param name="offset">Offset in "Define Quantization Tables" block</param> /// <param name="i">The quantization index</param> /// <param name="quant">The quantization table to copy data from</param> private static void WriteDataToDqt(byte[] dqt, ref int offset, QuantIndex i, ref Block8x8F quant) { dqt[offset++] = (byte)i; for (int j = 0; j < Block8x8F.Size; j++) { dqt[offset++] = (byte)quant[j]; } }
/// <summary> /// Writes a block of pixel data using the given quantization table, /// returning the post-quantized DC value of the DCT-transformed block. /// The block is in natural (not zig-zag) order. /// </summary> /// <param name="index">The quantization table index.</param> /// <param name="prevDC">The previous DC value.</param> /// <param name="src">Source block</param> /// <param name="tempDest1">Temporal block to be used as FDCT Destination</param> /// <param name="tempDest2">Temporal block 2</param> /// <param name="quant">Quantization table</param> /// <param name="unzigPtr">The 8x8 Unzig block pointer</param> /// <returns> /// The <see cref="int"/> /// </returns> private int WriteBlock( QuantIndex index, int prevDC, Block8x8F *src, Block8x8F *tempDest1, Block8x8F *tempDest2, Block8x8F *quant, byte *unzigPtr) { FastFloatingPointDCT.TransformFDCT(ref *src, ref *tempDest1, ref *tempDest2); Block8x8F.Quantize(tempDest1, tempDest2, quant, unzigPtr); float *unziggedDestPtr = (float *)tempDest2; int dc = (int)unziggedDestPtr[0]; // Emit the DC delta. this.EmitHuffRLE((HuffIndex)((2 * (int)index) + 0), 0, dc - prevDC); // Emit the AC components. var h = (HuffIndex)((2 * (int)index) + 1); int runLength = 0; for (int zig = 1; zig < Block8x8F.Size; zig++) { int ac = (int)unziggedDestPtr[zig]; if (ac == 0) { runLength++; } else { while (runLength > 15) { this.EmitHuff(h, 0xf0); runLength -= 16; } this.EmitHuffRLE(h, runLength, ac); runLength = 0; } } if (runLength > 0) { this.EmitHuff(h, 0x00); } return(dc); }
/// <summary> /// Writes a block of pixel data using the given quantization table, /// returning the post-quantized DC value of the DCT-transformed block. /// The block is in natural (not zig-zag) order. /// </summary> /// <param name="index">The quantization table index.</param> /// <param name="prevDC">The previous DC value.</param> /// <param name="src">Source block</param> /// <param name="tempDest1">Temporal block to be used as FDCT Destination</param> /// <param name="tempDest2">Temporal block 2</param> /// <param name="quant">Quantization table</param> /// <param name="unZig">The 8x8 Unzig block.</param> /// <param name="emitBufferBase">The reference to the emit buffer.</param> /// <returns>The <see cref="int"/>.</returns> private int WriteBlock( QuantIndex index, int prevDC, ref Block8x8F src, ref Block8x8F tempDest1, ref Block8x8F tempDest2, ref Block8x8F quant, ref ZigZag unZig, ref byte emitBufferBase) { FastFloatingPointDCT.TransformFDCT(ref src, ref tempDest1, ref tempDest2); Block8x8F.Quantize(ref tempDest1, ref tempDest2, ref quant, ref unZig); int dc = (int)tempDest2[0]; // Emit the DC delta. this.EmitHuffRLE((HuffIndex)((2 * (int)index) + 0), 0, dc - prevDC, ref emitBufferBase); // Emit the AC components. var h = (HuffIndex)((2 * (int)index) + 1); int runLength = 0; for (int zig = 1; zig < Block8x8F.Size; zig++) { int ac = (int)tempDest2[zig]; if (ac == 0) { runLength++; } else { while (runLength > 15) { this.EmitHuff(h, 0xf0, ref emitBufferBase); runLength -= 16; } this.EmitHuffRLE(h, runLength, ac, ref emitBufferBase); runLength = 0; } } if (runLength > 0) { this.EmitHuff(h, 0x00, ref emitBufferBase); } return(dc); }
/// <summary> /// Writes a block of pixel data using the given quantization table, /// returning the post-quantized DC value of the DCT-transformed block. /// The block is in natural (not zig-zag) order. /// </summary> /// <param name="block">The block to write.</param> /// <param name="index">The quantization table index.</param> /// <param name="prevDC">The previous DC value.</param> /// <returns>The <see cref="int"/></returns> private int WriteBlock(Block block, QuantIndex index, int prevDC) { FDCT.Transform(block); // Emit the DC delta. int dc = Round(block[0], 8 * this.quant[(int)index][0]); this.EmitHuffRLE((HuffIndex)((2 * (int)index) + 0), 0, dc - prevDC); // Emit the AC components. HuffIndex h = (HuffIndex)((2 * (int)index) + 1); int runLength = 0; for (int zig = 1; zig < Block.BlockSize; zig++) { int ac = Round(block[Unzig[zig]], 8 * this.quant[(int)index][zig]); if (ac == 0) { runLength++; } else { while (runLength > 15) { this.EmitHuff(h, 0xf0); runLength -= 16; } this.EmitHuffRLE(h, runLength, ac); runLength = 0; } } if (runLength > 0) { this.EmitHuff(h, 0x00); } return(dc); }
private int writeBlock(Block b, QuantIndex q, int prevDC, BinaryWriter writer) { ForwardDiscreteCosineTransform.Transform(b); // Emit the DC delta. var dc = div(b[0], 8 * DefineQuantizationTableSegment.quant[(int)q][0]); emitHuffRLE((HuffmanIndex)(2 * (int)q + 0), 0, dc - prevDC, writer); // Emit the AC components. var h = (HuffmanIndex)(2 * (int)q + 1); int runLength = 0; for (int zig = 1; zig < Block.BlockSize; zig++) { var ac = div(b[unzig[zig]], 8 * DefineQuantizationTableSegment.quant[(int)q][zig]); if (ac == 0) { runLength++; } else { while (runLength > 15) { emitHuff(h, 0xf0, writer); runLength -= 16; } emitHuffRLE(h, runLength, ac, writer); runLength = 0; } } if (runLength > 0) { emitHuff(h, 0x00, writer); } return(dc); }