Browse Source

Adjustments to formatting to match project.

Former-commit-id: e5e64f7fd6484c572115f19ce78101889fbf3393
Former-commit-id: a37a55910678ebfa60b275ec6846b8ab07ce35a8
Former-commit-id: 84965228feebd8aa9cbc08849c2ad654c5d1cc1f
af/merge-core
Michael Weber 10 years ago
parent
commit
a51d68becb
  1. 12
      src/ImageProcessorCore/Formats/Jpg/Colors.cs
  2. 360
      src/ImageProcessorCore/Formats/Jpg/Decoder.cs
  3. 80
      src/ImageProcessorCore/Formats/Jpg/Encoder.cs
  4. 4
      src/ImageProcessorCore/Formats/Jpg/FDCT.cs
  5. 6
      src/ImageProcessorCore/Formats/Jpg/IDCT.cs
  6. 4
      src/ImageProcessorCore/Formats/Jpg/JpegDecoder.cs

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

@ -17,9 +17,9 @@ namespace ImageProcessorCore.Formats.Jpg
int icb = (-11056*r - 21712*g + 32768*b + (257<<15)) >> 16;
int icr = (32768*r - 27440*g - 5328*b + (257<<15)) >> 16;
if(iyy < 0) yy = 0; else if(iyy > 255) yy = 255; else yy = (byte)iyy;
if(icb < 0) cb = 0; else if(icb > 255) cb = 255; else cb = (byte)icb;
if(icr < 0) cr = 0; else if(icr > 255) cr = 255; else cr = (byte)icr;
if (iyy < 0) yy = 0; else if (iyy > 255) yy = 255; else yy = (byte)iyy;
if (icb < 0) cb = 0; else if (icb > 255) cb = 255; else cb = (byte)icb;
if (icr < 0) cr = 0; else if (icr > 255) cr = 255; else cr = (byte)icr;
}
public static void YCbCrToRGB(byte yy, byte cb, byte cr, out byte r, out byte g, out byte b)
@ -37,9 +37,9 @@ namespace ImageProcessorCore.Formats.Jpg
int ig = (yy1 - 22554*cb1 - 46802*cr1) >> 16;
int ib = (yy1 + 116130*cb1) >> 16;
if(ir < 0) r = 0; else if(ir > 255) r = 255; else r = (byte)ir;
if(ig < 0) g = 0; else if(ig > 255) g = 255; else g = (byte)ig;
if(ib < 0) b = 0; else if(ib > 255) b = 255; else b = (byte)ib;
if (ir < 0) r = 0; else if (ir > 255) r = 255; else r = (byte)ir;
if (ig < 0) g = 0; else if (ig > 255) g = 255; else g = (byte)ig;
if (ib < 0) b = 0; else if (ib > 255) b = 255; else b = (byte)ib;
}
}
}

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

File diff suppressed because it is too large

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

@ -189,9 +189,9 @@ namespace ImageProcessorCore.Formats.Jpg
{
int maxValue = 0;
foreach(var v in s.values)
foreach (var v in s.values)
{
if(v > maxValue)
if (v > maxValue)
maxValue = v;
}
@ -200,10 +200,10 @@ namespace ImageProcessorCore.Formats.Jpg
int code = 0;
int k = 0;
for(int i = 0; i < s.count.Length; i++)
for (int i = 0; i < s.count.Length; i++)
{
int nBits = (i+1) << 24;
for(int j = 0; j < s.count[i]; j++)
for (int j = 0; j < s.count[i]; j++)
{
values[s.values[k]] = (uint)(nBits | code);
code++;
@ -240,11 +240,11 @@ namespace ImageProcessorCore.Formats.Jpg
nBits += this.nBits;
bits <<= (int)(32 - nBits);
bits |= this.bits;
while(nBits >= 8)
while (nBits >= 8)
{
byte b = (byte)(bits >> 24);
writeByte(b);
if(b == 0xff)
if (b == 0xff)
writeByte(0x00);
bits <<= 8;
nBits -= 8;
@ -266,19 +266,19 @@ namespace ImageProcessorCore.Formats.Jpg
{
int a = v;
int b = v;
if(a < 0)
if (a < 0)
{
a = -v;
b = v-1;
}
uint nBits = 0;
if(a < 0x100)
if (a < 0x100)
nBits = bitCount[a];
else
nBits = 8 + (uint)bitCount[a>>8];
emitHuff(h, (int)((runLength<<4)|nBits));
if(nBits > 0)
if (nBits > 0)
emit((uint)b & (uint)((1 << ((int)nBits)) - 1), nBits);
}
@ -297,7 +297,7 @@ namespace ImageProcessorCore.Formats.Jpg
{
int markerlen = 2 + nQuantIndex*(1+Block.blockSize);
writeMarkerHeader(dqtMarker, markerlen);
for(int i = 0; i < nQuantIndex; i++)
for (int i = 0; i < nQuantIndex; i++)
{
writeByte((byte)i);
w.Write(quant[i], 0, quant[i].Length);
@ -318,7 +318,7 @@ namespace ImageProcessorCore.Formats.Jpg
buf[3] = (byte)(wid >> 8);
buf[4] = (byte)(wid & 0xff);
buf[5] = (byte)(nComponent);
if(nComponent == 1)
if (nComponent == 1)
{
buf[6] = 1;
// No subsampling for grayscale image.
@ -327,7 +327,7 @@ namespace ImageProcessorCore.Formats.Jpg
}
else
{
for(int i = 0; i < nComponent; i++)
for (int i = 0; i < nComponent; i++)
{
buf[3*i+6] = (byte)(i + 1);
// We use 4:2:0 chroma subsampling.
@ -345,17 +345,17 @@ namespace ImageProcessorCore.Formats.Jpg
int markerlen = 2;
huffmanSpec[] specs = theHuffmanSpec;
if(nComponent == 1)
if (nComponent == 1)
{
// Drop the Chrominance tables.
specs = new huffmanSpec[] { theHuffmanSpec[0], theHuffmanSpec[1] };
}
foreach(var s in specs)
foreach (var s in specs)
markerlen += 1 + 16 + s.values.Length;
writeMarkerHeader(dhtMarker, markerlen);
for(int i = 0; i < specs.Length; i++)
for (int i = 0; i < specs.Length; i++)
{
var s = specs[i];
@ -380,17 +380,17 @@ namespace ImageProcessorCore.Formats.Jpg
var h = (huffIndex)(2*(int)q+1);
int runLength = 0;
for(int zig = 1; zig < Block.blockSize; zig++)
for (int zig = 1; zig < Block.blockSize; zig++)
{
int ac = div(b[unzig[zig]], 8*quant[(int)q][zig]);
if(ac == 0)
if (ac == 0)
{
runLength++;
}
else
{
while(runLength > 15)
while (runLength > 15)
{
emitHuff(h, 0xf0);
runLength -= 16;
@ -400,7 +400,7 @@ namespace ImageProcessorCore.Formats.Jpg
runLength = 0;
}
}
if(runLength > 0)
if (runLength > 0)
emitHuff(h, 0x00);
return dc;
}
@ -411,9 +411,9 @@ namespace ImageProcessorCore.Formats.Jpg
{
int xmax = m.Width - 1;
int ymax = m.Height - 1;
for(int j = 0; j < 8; j++)
for (int j = 0; j < 8; j++)
{
for(int i = 0; i < 8; i++)
for (int i = 0; i < 8; i++)
{
byte yy, cb, cr;
@ -469,12 +469,12 @@ namespace ImageProcessorCore.Formats.Jpg
// dst block.
private void scale(Block dst, Block[] src)
{
for(int i = 0; i < 4; i++)
for (int i = 0; i < 4; i++)
{
int dstOff = ((i&2)<<4) | ((i&1)<<2);
for(int y = 0; y < 4; y++)
for (int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
for (int x = 0; x < 4; x++)
{
int j = 16*y + 2*x;
int sum = src[i][j] + src[i][j+1] + src[i][j+8] + src[i][j+9];
@ -519,14 +519,14 @@ namespace ImageProcessorCore.Formats.Jpg
Block[] cr = new Block[4];
int prevDCY = 0, prevDCCb = 0, prevDCCr = 0;
for(int i = 0; i < 4; i++) cb[i] = new Block();
for(int i = 0; i < 4; i++) cr[i] = new Block();
for (int i = 0; i < 4; i++) cb[i] = new Block();
for (int i = 0; i < 4; i++) cr[i] = new Block();
for(int y = 0; y < m.Height; y += 16)
for (int y = 0; y < m.Height; y += 16)
{
for(int x = 0; x < m.Width; x += 16)
for (int x = 0; x < m.Width; x += 16)
{
for(int i = 0; i < 4; i++)
for (int i = 0; i < 4; i++)
{
int xOff = (i & 1) * 8;
int yOff = (i & 2) * 4;
@ -551,34 +551,34 @@ namespace ImageProcessorCore.Formats.Jpg
{
this.w = w;
for(int i = 0; i < theHuffmanSpec.Length; i++)
for (int i = 0; i < theHuffmanSpec.Length; i++)
theHuffmanLUT[i] = new huffmanLUT(theHuffmanSpec[i]);
for(int i = 0; i < nQuantIndex; i++)
for (int i = 0; i < nQuantIndex; i++)
quant[i] = new byte[Block.blockSize];
if(m.Width >= (1<<16) || m.Height >= (1<<16))
if (m.Width >= (1<<16) || m.Height >= (1<<16))
throw new Exception("jpeg: image is too large to encode");
if(quality < 1) quality = 1;
if(quality > 100) quality = 100;
if (quality < 1) quality = 1;
if (quality > 100) quality = 100;
// Convert from a quality rating to a scaling factor.
int scale;
if(quality < 50)
if (quality < 50)
scale = 5000 / quality;
else
scale = 200 - quality*2;
// Initialize the quantization tables.
for(int i = 0; i < nQuantIndex; i++)
for (int i = 0; i < nQuantIndex; i++)
{
for(int j = 0; j < Block.blockSize; j++)
for (int j = 0; j < Block.blockSize; j++)
{
int x = unscaledQuant[i,j];
x = (x*scale + 50) / 100;
if(x < 1) x = 1;
if(x > 255) x = 255;
if (x < 1) x = 1;
if (x > 255) x = 255;
quant[i][j] = (byte)x;
}
}
@ -613,7 +613,7 @@ namespace ImageProcessorCore.Formats.Jpg
// div returns a/b rounded to the nearest integer, instead of rounded to zero.
private static int div(int a, int b)
{
if(a >= 0)
if (a >= 0)
return (a + (b >> 1)) / b;
else
return -((-a + (b >> 1)) / b);

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

@ -27,7 +27,7 @@ namespace ImageProcessorCore.Formats.Jpg
private static void FDCT(Block b)
{
// Pass 1: process rows.
for(int y = 0; y < 8; y++)
for (int y = 0; y < 8; y++)
{
int x0 = b[y*8+0];
int x1 = b[y*8+1];
@ -85,7 +85,7 @@ namespace ImageProcessorCore.Formats.Jpg
// Pass 2: process columns.
// We remove pass1Bits scaling, but leave results scaled up by an overall factor of 8.
for(int x = 0; x < 8; x++)
for (int x = 0; x < 8; x++)
{
int tmp0 = b[0*8+x] + b[7*8+x];
int tmp1 = b[1*8+x] + b[6*8+x];

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

@ -34,12 +34,12 @@ namespace ImageProcessorCore.Formats.Jpg
private static void IDCT(Block src)
{
// Horizontal 1-D IDCT.
for(int y = 0; y < 8; y++)
for (int y = 0; y < 8; y++)
{
int y8 = y * 8;
// If all the AC components are zero, then the IDCT is trivial.
if(src[y8+1] == 0 && src[y8+2] == 0 && src[y8+3] == 0 &&
if (src[y8+1] == 0 && src[y8+2] == 0 && src[y8+3] == 0 &&
src[y8+4] == 0 && src[y8+5] == 0 && src[y8+6] == 0 && src[y8+7] == 0)
{
int dc = src[y8+0] << 3;
@ -103,7 +103,7 @@ namespace ImageProcessorCore.Formats.Jpg
}
// Vertical 1-D IDCT.
for(int x = 0; x < 8; x++)
for (int x = 0; x < 8; x++)
{
// Similar to the horizontal 1-D IDCT case, if all the AC components are zero, then the IDCT is trivial.
// However, after performing the horizontal 1-D IDCT, there are typically non-zero AC components, so

4
src/ImageProcessorCore/Formats/Jpg/JpegDecoder.cs

@ -103,7 +103,7 @@ namespace ImageProcessorCore.Formats
float[] pixels = new float[pixelWidth * pixelHeight * 4];
if(decoder.nComp == 1)
if (decoder.nComp == 1)
{
Parallel.For(
0,
@ -122,7 +122,7 @@ namespace ImageProcessorCore.Formats
}
});
}
else if(decoder.nComp == 3)
else if (decoder.nComp == 3)
{
Parallel.For(
0,

Loading…
Cancel
Save