Browse Source

Merge pull request #396 from Greenliff/feature/donotclosestream

#395 fix

Former-commit-id: 8fd3e2b25ba11bc10a8a96b9a16345c317603619
Former-commit-id: d74a772fb16b947caa6635d59f3bf6d5919e49f0
Former-commit-id: d713e673fbfdcee2aa43cc98cd2fef36fb4546a2
pull/1/head
James Jackson-South 10 years ago
parent
commit
252a413767
  1. 53
      src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs
  2. 63
      src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs

53
src/ImageProcessorCore/Formats/Bmp/BmpEncoderCore.cs

@ -42,35 +42,34 @@ namespace ImageProcessorCore.Formats
rowWidth += 4 - amount;
}
using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream))
{
int bpp = (int)this.bmpBitsPerPixel;
EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream);
BmpFileHeader fileHeader = new BmpFileHeader
{
Type = 19778, // BM
Offset = 54,
FileSize = 54 + (image.Height * rowWidth * bpp)
};
int bpp = (int)this.bmpBitsPerPixel;
BmpInfoHeader infoHeader = new BmpInfoHeader
{
HeaderSize = 40,
Height = image.Height,
Width = image.Width,
BitsPerPixel = (short)(8 * bpp),
Planes = 1,
ImageSize = image.Height * rowWidth * bpp,
ClrUsed = 0,
ClrImportant = 0
};
WriteHeader(writer, fileHeader);
this.WriteInfo(writer, infoHeader);
this.WriteImage(writer, image);
writer.Flush();
}
BmpFileHeader fileHeader = new BmpFileHeader
{
Type = 19778, // BM
Offset = 54,
FileSize = 54 + (image.Height * rowWidth * bpp)
};
BmpInfoHeader infoHeader = new BmpInfoHeader
{
HeaderSize = 40,
Height = image.Height,
Width = image.Width,
BitsPerPixel = (short)(8 * bpp),
Planes = 1,
ImageSize = image.Height * rowWidth * bpp,
ClrUsed = 0,
ClrImportant = 0
};
WriteHeader(writer, fileHeader);
this.WriteInfo(writer, infoHeader);
this.WriteImage(writer, image);
writer.Flush();
}
/// <summary>

63
src/ImageProcessorCore/Formats/Gif/GifEncoderCore.cs

@ -56,47 +56,46 @@ namespace ImageProcessorCore.Formats
this.Quantizer = new OctreeQuantizer { Threshold = this.Threshold };
}
using (EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream))
{
// Ensure that quality can be set but has a fallback.
int quality = this.Quality > 0 ? this.Quality : imageBase.Quality;
this.Quality = quality > 0 ? quality.Clamp(1, 256) : 256;
EndianBinaryWriter writer = new EndianBinaryWriter(EndianBitConverter.Little, stream);
// Ensure that quality can be set but has a fallback.
int quality = this.Quality > 0 ? this.Quality : imageBase.Quality;
this.Quality = quality > 0 ? quality.Clamp(1, 256) : 256;
// Get the number of bits.
this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(this.Quality);
// Get the number of bits.
this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(this.Quality);
// Quantize the image returning a palette.
QuantizedImage quantized = this.Quantizer.Quantize(image, this.Quality);
// Quantize the image returning a palette.
QuantizedImage quantized = this.Quantizer.Quantize(image, this.Quality);
// Write the header.
this.WriteHeader(writer);
// Write the header.
this.WriteHeader(writer);
// Write the LSD. We'll use local color tables for now.
this.WriteLogicalScreenDescriptor(image, writer, quantized.TransparentIndex);
// Write the LSD. We'll use local color tables for now.
this.WriteLogicalScreenDescriptor(image, writer, quantized.TransparentIndex);
// Write the first frame.
this.WriteGraphicalControlExtension(imageBase, writer, quantized.TransparentIndex);
this.WriteImageDescriptor(image, writer);
this.WriteColorTable(quantized, writer);
this.WriteImageData(quantized, writer);
// Write the first frame.
this.WriteGraphicalControlExtension(imageBase, writer, quantized.TransparentIndex);
this.WriteImageDescriptor(image, writer);
this.WriteColorTable(quantized, writer);
this.WriteImageData(quantized, writer);
// Write additional frames.
if (image.Frames.Any())
// Write additional frames.
if (image.Frames.Any())
{
this.WriteApplicationExtension(writer, image.RepeatCount, image.Frames.Count);
foreach (ImageFrame frame in image.Frames)
{
this.WriteApplicationExtension(writer, image.RepeatCount, image.Frames.Count);
foreach (ImageFrame frame in image.Frames)
{
QuantizedImage quantizedFrame = this.Quantizer.Quantize(frame, this.Quality);
this.WriteGraphicalControlExtension(frame, writer, quantizedFrame.TransparentIndex);
this.WriteImageDescriptor(frame, writer);
this.WriteColorTable(quantizedFrame, writer);
this.WriteImageData(quantizedFrame, writer);
}
QuantizedImage quantizedFrame = this.Quantizer.Quantize(frame, this.Quality);
this.WriteGraphicalControlExtension(frame, writer, quantizedFrame.TransparentIndex);
this.WriteImageDescriptor(frame, writer);
this.WriteColorTable(quantizedFrame, writer);
this.WriteImageData(quantizedFrame, writer);
}
// TODO: Write Comments extension etc
writer.Write(GifConstants.EndIntroducer);
}
// TODO: Write Comments extension etc
writer.Write(GifConstants.EndIntroducer);
}
/// <summary>

Loading…
Cancel
Save