From 439509f3be7678f7221d6cb3d3b345683394c2ee Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 27 Jul 2016 18:00:36 +1000 Subject: [PATCH] WIP JFIF header Former-commit-id: dd8bcac8f7d1170abb4796ae621e2a3409a1f2e3 Former-commit-id: f8f08b02228d80090537579c1581959ae4d03a9a Former-commit-id: 7f62ae7e5dac7a5412233c7a229a905c13306351 --- .../Formats/Jpg/JpegEncoderCore.cs | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs b/src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs index c9a8427d59..99da0c5dfb 100644 --- a/src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Jpg/JpegEncoderCore.cs @@ -488,6 +488,11 @@ namespace ImageProcessorCore.Formats int componentCount = 3; // Write the Start Of Image marker. + double densityX = ((Image)image).HorizontalResolution; + double densityY = ((Image)image).VerticalResolution; + + //WriteApplicationHeader(densityX, densityY); + // TODO: JFIF header etc. this.buffer[0] = 0xff; this.buffer[1] = 0xd8; @@ -531,6 +536,55 @@ namespace ImageProcessorCore.Formats return -((-dividend + (divisor >> 1)) / divisor); } + /// + /// Writes the application header containing the Jfif identifier plus extra data. + /// + /// The image to encode from. + /// The writer to write to the stream. + private void WriteApplicationHeader(double horizontalResolution, double 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 + }; + + // 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); + } + /// /// Writes the Define Quantization Marker and tables. ///