Browse Source

Fix som encapsulation for the jpeg decoder lib.

Former-commit-id: c9c17e9855cc463958e7ecd09d86c257a4ae86a4
Former-commit-id: a40f80591f0a7c92256d19435e04944931279de4
Former-commit-id: 572894aa8d92fd9cb8f9658ff9d97300b1ad89df
pull/1/head
Michael Weber 10 years ago
parent
commit
878cb6b4ea
  1. 2
      src/ImageProcessorCore/Formats/Jpg/Block.cs
  2. 2
      src/ImageProcessorCore/Formats/Jpg/Colors.cs
  3. 13
      src/ImageProcessorCore/Formats/Jpg/Component.cs
  4. 66
      src/ImageProcessorCore/Formats/Jpg/Decoder.cs
  5. 2
      src/ImageProcessorCore/Formats/Jpg/Encoder.cs
  6. 2
      src/ImageProcessorCore/Formats/Jpg/FDCT.cs
  7. 2
      src/ImageProcessorCore/Formats/Jpg/IDCT.cs

2
src/ImageProcessorCore/Formats/Jpg/Block.cs

@ -3,7 +3,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System; using System;
using System.IO; using System.IO;
public class Block internal class Block
{ {
public const int blockSize = 64; public const int blockSize = 64;
private int[] _data; private int[] _data;

2
src/ImageProcessorCore/Formats/Jpg/Colors.cs

@ -3,7 +3,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System; using System;
using System.IO; using System.IO;
public static class Colors internal static class Colors
{ {
public static void RGBToYCbCr(byte r, byte g, byte b, out byte yy, out byte cb, out byte cr) public static void RGBToYCbCr(byte r, byte g, byte b, out byte yy, out byte cb, out byte cr)
{ {

13
src/ImageProcessorCore/Formats/Jpg/Component.cs

@ -1,13 +0,0 @@
namespace ImageProcessorCore.Formats.Jpg
{
using System;
using System.IO;
public class Component
{
public int h; // Horizontal sampling factor.
public int v; // Vertical sampling factor.
public byte c; // Component identifier.
public byte tq; // Quantization table destination selector.
}
}

66
src/ImageProcessorCore/Formats/Jpg/Decoder.cs

@ -4,7 +4,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System.IO; using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
public partial class Decoder internal partial class Decoder
{ {
private class errMissingFF00 : Exception { } private class errMissingFF00 : Exception { }
private class errShortHuffmanData : Exception { } private class errShortHuffmanData : Exception { }
@ -61,12 +61,20 @@ namespace ImageProcessorCore.Formats.Jpg
53, 60, 61, 54, 47, 55, 62, 63, 53, 60, 61, 54, 47, 55, 62, 63,
}; };
private class component
{
public int h; // Horizontal sampling factor.
public int v; // Vertical sampling factor.
public byte c; // Component identifier.
public byte tq; // Quantization table destination selector.
}
public Decoder() public Decoder()
{ {
huff = new huffman_class[maxTc + 1, maxTh + 1]; huff = new huffman_class[maxTc + 1, maxTh + 1];
quant = new Block[maxTq + 1]; quant = new Block[maxTq + 1];
tmp = new byte[2 * Block.blockSize]; tmp = new byte[2 * Block.blockSize];
comp = new Component[maxComponents]; comp = new component[maxComponents];
progCoeffs = new Block[maxComponents][]; progCoeffs = new Block[maxComponents][];
bits = new bits_class(); bits = new bits_class();
bytes = new bytes_class(); bytes = new bytes_class();
@ -79,10 +87,10 @@ namespace ImageProcessorCore.Formats.Jpg
quant[i] = new Block(); quant[i] = new Block();
for(int i = 0; i < comp.Length; i++) for(int i = 0; i < comp.Length; i++)
comp[i] = new Component(); comp[i] = new component();
} }
public class img_ycbcr private class img_ycbcr
{ {
public enum YCbCrSubsampleRatio public enum YCbCrSubsampleRatio
{ {
@ -320,9 +328,9 @@ namespace ImageProcessorCore.Formats.Jpg
public int width, height; public int width, height;
public int nComp; public int nComp;
public img_gray img1; //grayscale public img_gray img1; //grayscale
public img_ycbcr img3; //YCrCb public img_rgb imgrgb; //RGB
public img_rgb imgrgb; //YCrCb
private img_ycbcr img3; //YCrCb
private Stream r; private Stream r;
private bits_class bits; private bits_class bits;
private bytes_class bytes; private bytes_class bytes;
@ -335,7 +343,7 @@ namespace ImageProcessorCore.Formats.Jpg
private bool adobeTransformValid; private bool adobeTransformValid;
private byte adobeTransform; private byte adobeTransform;
private ushort eobRun; // End-of-Band run, specified in section G.1.2.2. private ushort eobRun; // End-of-Band run, specified in section G.1.2.2.
private Component[] comp; private component[] comp;
private Block[][] progCoeffs; // Saved state between progressive-mode scans. private Block[][] progCoeffs; // Saved state between progressive-mode scans.
private huffman_class[,] huff; private huffman_class[,] huff;
private Block[] quant; // Quantization tables, in zig-zag order. private Block[] quant; // Quantization tables, in zig-zag order.
@ -362,7 +370,7 @@ namespace ImageProcessorCore.Formats.Jpg
// receiveExtend is the composition of RECEIVE and EXTEND, specified in section // receiveExtend is the composition of RECEIVE and EXTEND, specified in section
// F.2.2.1. // F.2.2.1.
public int receiveExtend(byte t) private int receiveExtend(byte t)
{ {
if(bits.n < t) if(bits.n < t)
ensureNBits(t); ensureNBits(t);
@ -378,7 +386,7 @@ namespace ImageProcessorCore.Formats.Jpg
// processDHT processes a Define Huffman Table marker, and initializes a huffman // processDHT processes a Define Huffman Table marker, and initializes a huffman
// struct from its contents. Specified in section B.2.4.2. // struct from its contents. Specified in section B.2.4.2.
public void processDHT(int n) private void processDHT(int n)
{ {
while(n > 0) while(n > 0)
{ {
@ -524,7 +532,7 @@ namespace ImageProcessorCore.Formats.Jpg
throw new Exception("bad Huffman code"); throw new Exception("bad Huffman code");
} }
public bool decodeBit() private bool decodeBit()
{ {
if(bits.n == 0) if(bits.n == 0)
ensureNBits(1); ensureNBits(1);
@ -535,7 +543,7 @@ namespace ImageProcessorCore.Formats.Jpg
return ret; return ret;
} }
public uint decodeBits(int n) private uint decodeBits(int n)
{ {
if(bits.n < n) if(bits.n < n)
ensureNBits(n); ensureNBits(n);
@ -549,7 +557,7 @@ namespace ImageProcessorCore.Formats.Jpg
// fill fills up the bytes.buf buffer from the underlying io.Reader. It // fill fills up the bytes.buf buffer from the underlying io.Reader. It
// should only be called when there are no unread bytes in bytes. // should only be called when there are no unread bytes in bytes.
public void fill() private void fill()
{ {
if(bytes.i != bytes.j) if(bytes.i != bytes.j)
throw new Exception("jpeg: fill called when unread bytes exist"); throw new Exception("jpeg: fill called when unread bytes exist");
@ -576,7 +584,7 @@ namespace ImageProcessorCore.Formats.Jpg
// requires at least 8 bits for look-up, which means that Huffman decoding can // requires at least 8 bits for look-up, which means that Huffman decoding can
// sometimes overshoot and read one or two too many bytes. Two-byte overshoot // sometimes overshoot and read one or two too many bytes. Two-byte overshoot
// can happen when expecting to read a 0xff 0x00 byte-stuffed byte. // can happen when expecting to read a 0xff 0x00 byte-stuffed byte.
public void unreadByteStuffedByte() private void unreadByteStuffedByte()
{ {
bytes.i -= bytes.nUnreadable; bytes.i -= bytes.nUnreadable;
bytes.nUnreadable = 0; bytes.nUnreadable = 0;
@ -590,7 +598,7 @@ namespace ImageProcessorCore.Formats.Jpg
// readByte returns the next byte, whether buffered or not buffere It does // readByte returns the next byte, whether buffered or not buffere It does
// not care about byte stuffing. // not care about byte stuffing.
public byte readByte() private byte readByte()
{ {
while(bytes.i == bytes.j) while(bytes.i == bytes.j)
fill(); fill();
@ -601,7 +609,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// readByteStuffedByte is like readByte but is for byte-stuffed Huffman data. // readByteStuffedByte is like readByte but is for byte-stuffed Huffman data.
public byte readByteStuffedByte() private byte readByteStuffedByte()
{ {
byte x; byte x;
@ -666,7 +674,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// ignore ignores the next n bytes. // ignore ignores the next n bytes.
public void ignore(int n) private void ignore(int n)
{ {
// Unread the overshot bytes, if any. // Unread the overshot bytes, if any.
if(bytes.nUnreadable != 0) if(bytes.nUnreadable != 0)
@ -690,7 +698,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// Specified in section B.2.2. // Specified in section B.2.2.
public void processSOF(int n) private void processSOF(int n)
{ {
if(nComp != 0) if(nComp != 0)
throw new Exception("multiple SOF markers"); throw new Exception("multiple SOF markers");
@ -837,7 +845,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// Specified in section B.2.4.1. // Specified in section B.2.4.1.
public void processDQT(int n) private void processDQT(int n)
{ {
while(n > 0) while(n > 0)
{ {
@ -888,7 +896,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// Specified in section B.2.4.4. // Specified in section B.2.4.4.
public void processDRI(int n) private void processDRI(int n)
{ {
if(n != 2) if(n != 2)
throw new Exception("DRI has wrong length"); throw new Exception("DRI has wrong length");
@ -897,7 +905,7 @@ namespace ImageProcessorCore.Formats.Jpg
ri = ((int)tmp[0]<<8) + (int)tmp[1]; ri = ((int)tmp[0]<<8) + (int)tmp[1];
} }
public void processApp0Marker(int n) private void processApp0Marker(int n)
{ {
if(n < 5) if(n < 5)
{ {
@ -913,7 +921,7 @@ namespace ImageProcessorCore.Formats.Jpg
ignore(n); ignore(n);
} }
public void processApp14Marker(int n) private void processApp14Marker(int n)
{ {
if(n < 12) if(n < 12)
{ {
@ -935,7 +943,7 @@ namespace ImageProcessorCore.Formats.Jpg
} }
// decode reads a JPEG image from r and returns it as an image.Image. // decode reads a JPEG image from r and returns it as an image.Image.
public object decode(Stream r, bool configOnly) public void decode(Stream r, bool configOnly)
{ {
this.r = r; this.r = r;
@ -1015,7 +1023,7 @@ namespace ImageProcessorCore.Formats.Jpg
progressive = (marker == sof2Marker); progressive = (marker == sof2Marker);
processSOF(n); processSOF(n);
if(configOnly && jfif) if(configOnly && jfif)
return null; return;
break; break;
case dhtMarker: case dhtMarker:
if(configOnly) if(configOnly)
@ -1031,7 +1039,7 @@ namespace ImageProcessorCore.Formats.Jpg
break; break;
case sosMarker: case sosMarker:
if(configOnly) if(configOnly)
return null; return;
else else
processSOS(n); processSOS(n);
break; break;
@ -1059,19 +1067,13 @@ namespace ImageProcessorCore.Formats.Jpg
} }
if(img1 != null) if(img1 != null)
return img1; return;
if(img3 != null) else if(img3 != null)
{ {
if(comp[0].c == 'R' && comp[1].c == 'G' && comp[2].c == 'B') if(comp[0].c == 'R' && comp[1].c == 'G' && comp[2].c == 'B')
{
imgrgb = convert_direct_to_rgb(width, height); imgrgb = convert_direct_to_rgb(width, height);
return imgrgb;
}
else else
{
imgrgb = convert_to_rgb(width, height); imgrgb = convert_to_rgb(width, height);
return imgrgb;
}
} }
/*if img3 != nil { /*if img3 != nil {

2
src/ImageProcessorCore/Formats/Jpg/Encoder.cs

@ -3,7 +3,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System; using System;
using System.IO; using System.IO;
public partial class Encoder internal partial class Encoder
{ {
private const int sof0Marker = 0xc0; // Start Of Frame (Baseline). private const int sof0Marker = 0xc0; // Start Of Frame (Baseline).
private const int sof1Marker = 0xc1; // Start Of Frame (Extended Sequential). private const int sof1Marker = 0xc1; // Start Of Frame (Extended Sequential).

2
src/ImageProcessorCore/Formats/Jpg/FDCT.cs

@ -3,7 +3,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System; using System;
using System.IO; using System.IO;
public partial class Encoder internal partial class Encoder
{ {
// Trigonometric constants in 13-bit fixed point format. // Trigonometric constants in 13-bit fixed point format.
private const int fix_0_298631336 = 2446; private const int fix_0_298631336 = 2446;

2
src/ImageProcessorCore/Formats/Jpg/IDCT.cs

@ -3,7 +3,7 @@ namespace ImageProcessorCore.Formats.Jpg
using System; using System;
using System.IO; using System.IO;
public partial class Decoder internal partial class Decoder
{ {
private const int w1 = 2841; // 2048*sqrt(2)*cos(1*pi/16) private const int w1 = 2841; // 2048*sqrt(2)*cos(1*pi/16)
private const int w2 = 2676; // 2048*sqrt(2)*cos(2*pi/16) private const int w2 = 2676; // 2048*sqrt(2)*cos(2*pi/16)

Loading…
Cancel
Save