Browse Source

jpeg now preserves dpi

Former-commit-id: fcf404c9fede3784fd312aaba739185e12aba2d8
Former-commit-id: 08d5865cda85d173d37e895373cb110d6fa4b053
Former-commit-id: 1350e5c663eca447189095abfe5f8a316c5a970c
af/merge-core
James Jackson-South 10 years ago
parent
commit
ad2249bbec
  1. 2
      src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id
  2. 74
      src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs
  3. 4
      tests/ImageProcessorCore.Tests/Formats/Jpg/GeneralFormatTests.cs

2
src/ImageProcessorCore/Formats/Jpg/JpegDecoderCore.cs.REMOVED.git-id

@ -1 +1 @@
3ef7ce74c01efdb8145d6b3d03c937c862025a00
64d830f6c3b8d21e7c1d250c9ab513a24cd54923

74
src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs

@ -488,15 +488,10 @@ namespace ImageProcessorCore.Formats
int componentCount = 3;
// Write the Start Of Image marker.
double densityX = ((Image<T,TP>)image).HorizontalResolution;
double densityX = ((Image<T, TP>)image).HorizontalResolution;
double densityY = ((Image<T, TP>)image).VerticalResolution;
//WriteApplicationHeader(densityX, densityY);
// TODO: JFIF header etc.
this.buffer[0] = 0xff;
this.buffer[1] = 0xd8;
stream.Write(this.buffer, 0, 2);
WriteApplicationHeader((short)densityX, (short)densityY);
// Write the quantization tables.
this.WriteDQT();
@ -539,50 +534,41 @@ namespace ImageProcessorCore.Formats
/// <summary>
/// Writes the application header containing the Jfif identifier plus extra data.
/// </summary>
/// <param name="image">The image to encode from.</param>
/// <param name="writer">The writer to write to the stream.</param>
private void WriteApplicationHeader(double horizontalResolution, double verticalResolution)
/// <param name="horizontalResolution">The resolution of the image in the x- direction.</param>
/// <param name="verticalResolution">The resolution of the image in the y- direction.</param>
private void WriteApplicationHeader(short horizontalResolution, short verticalResolution)
{
// Write the start of image marker. Markers are always prefixed with with 0xff.
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[1] = JpegConstants.Markers.SOI;
this.outputStream.Write(this.buffer, 0, 2);
// Write the jfif headers
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[0] = JpegConstants.Markers.APP0; // Application Marker
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[0] = JpegConstants.Markers.XFF;
this.buffer[0] = JpegConstants.Markers.XFF;
byte[] jfif = {
JpegConstants.Markers.XFF,
JpegConstants.Markers.APP0, // Application Marker
0x00,
0x10,
0x4a, // J
0x46, // F
0x49, // I
0x46, // F
0x00, // = "JFIF",'\0'
0x01, // versionhi
0x01, // versionlo
0x01, // xyunits as dpi
};
// Write the JFIF headers
this.buffer[2] = JpegConstants.Markers.XFF;
this.buffer[3] = JpegConstants.Markers.APP0; // Application Marker
this.buffer[4] = 0x00;
this.buffer[5] = 0x10;
this.buffer[6] = 0x4a; // J
this.buffer[7] = 0x46; // F
this.buffer[8] = 0x49; // I
this.buffer[9] = 0x46; // F
this.buffer[10] = 0x00; // = "JFIF",'\0'
this.buffer[11] = 0x01; // versionhi
this.buffer[12] = 0x01; // versionlo
this.buffer[13] = 0x01; // xyunits as dpi
// No thumbnail
byte[] thumbnail = {
0x00, // Thumbnail width
0x00 // Thumbnail height
};
// http://stackoverflow.com/questions/2188660/convert-short-to-byte-in-java
//writer.Write(jfif);
//writer.Write((short)densityX);
//writer.Write((short)densityY);
//writer.Write(thumbnail);
this.buffer[14] = 0x00; // Thumbnail width
this.buffer[15] = 0x00; // Thumbnail height
this.outputStream.Write(this.buffer, 0, 16);
// Resolution. Big Endian
this.buffer[0] = (byte)(horizontalResolution >> 8);
this.buffer[1] = (byte)horizontalResolution;
this.buffer[2] = (byte)(verticalResolution >> 8);
this.buffer[3] = (byte)verticalResolution;
this.outputStream.Write(this.buffer, 0, 4);
}
/// <summary>

4
tests/ImageProcessorCore.Tests/Formats/Jpg/JpegFileTests.cs → tests/ImageProcessorCore.Tests/Formats/Jpg/GeneralFormatTests.cs

@ -1,4 +1,4 @@
// <copyright file="SamplerTests.cs" company="James Jackson-South">
// <copyright file="GeneralFormatTests.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -9,7 +9,7 @@ namespace ImageProcessorCore.Tests
using Xunit;
public class JpegFileTests : FileTestBase
public class GeneralFormatTests : FileTestBase
{
[Fact]
public void ResolutionShouldChange()
Loading…
Cancel
Save