private static PageImage CreateFaxTiffFastTrack(PageImage page, PaperSize paperSize, FaxQuality faxQuality, ImageOperationResult result) { PageInfo inf = null; PageImage ret = new PageImage(); Bitmap src = null; Bitmap destroy = null; src = BitmapHelper.CreateCopy1BppIndexed(page._sourceBmp); inf = new PageInfo(src); //If the size is not right copy to other size (padding or reducing) if (inf.GetStandardPaperSize != paperSize) { if (inf.GetStandardPaperSize == PaperSize.Legal && paperSize == PaperSize.Letter) { destroy = src; src = BitmapHelper.CreateCopyFaxGeometry(src, faxQuality, paperSize, ImageUtility.InterpolationMode); if (destroy != null) { destroy.Dispose(); destroy = null; } inf = new PageInfo(src); } if (inf.GetStandardPaperSize == PaperSize.Letter && paperSize == PaperSize.Legal) { destroy = src; src = BitmapHelper.CreateCopyFaxGeometryPadding(src, faxQuality, paperSize); if (destroy != null) { destroy.Dispose(); destroy = null; } inf = new PageInfo(src); } } //Make sure its 1bpp if (inf.PixelFormat != PixelFormat.Format1bppIndexed) { destroy = src; src = BitmapHelper.CreateCopy1BppIndexed(src); if (destroy != null) { destroy.Dispose(); destroy = null; } inf = new PageInfo(src); } //Reduce or increase quality as needed if (inf.GetStandardFaxQuality != faxQuality) { if (inf.GetStandardFaxQuality == FaxQuality.Fine && faxQuality == FaxQuality.Normal) { destroy = src; src = BitmapHelper.ConvertTiffHiToTiffLow(src, ImageUtility.HighToLowScaleMethod); if (destroy != null) { destroy.Dispose(); destroy = null; } inf = new PageInfo(src); } if (inf.GetStandardFaxQuality == FaxQuality.Normal && faxQuality == FaxQuality.Fine) { destroy = src; src = BitmapHelper.ConvertTiffLowToTiffHi(src); if (destroy != null) { destroy.Dispose(); destroy = null; } inf = new PageInfo(src); } } ret._pageInfo = null; ret._sourceBmp = src; return(ret); }
private static void ConvertPixelFormatTo1bppIndexed(PageImage source, PageImage destination) { // Lock source bitmap in memory BitmapData sourceData = source.Bitmap.LockBits(new Rectangle(0, 0, source.Bitmap.Width, source.Bitmap.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); // Copy image data to binary array int imageSize = sourceData.Stride * sourceData.Height; byte[] sourceBuffer = new byte[imageSize]; Marshal.Copy(sourceData.Scan0, sourceBuffer, 0, imageSize); // Unlock source bitmap source.Bitmap.UnlockBits(sourceData); // Lock destination bitmap in memory BitmapData destinationData = destination.Bitmap.LockBits(new Rectangle(0, 0, destination.Bitmap.Width, destination.Bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format1bppIndexed); // Create destination buffer imageSize = destinationData.Stride * destinationData.Height; byte[] destinationBuffer = new byte[imageSize]; int sourceIndex = 0; int destinationIndex = 0; int pixelTotal = 0; byte destinationValue = 0; int pixelValue = 128; int height = source.Bitmap.Height; int width = source.Bitmap.Width; int threshold = 500; // Iterate lines for (int y = 0; y < height; y++) { sourceIndex = y * sourceData.Stride; destinationIndex = y * destinationData.Stride; destinationValue = 0; pixelValue = 128; // Iterate pixels for (int x = 0; x < width; x++) { // Compute pixel brightness (i.e. total of Red, Green, and Blue values) pixelTotal = sourceBuffer[sourceIndex + 1] + sourceBuffer[sourceIndex + 2] + sourceBuffer[sourceIndex + 3]; if (pixelTotal > threshold) { destinationValue += (byte)pixelValue; } if (pixelValue == 1) { destinationBuffer[destinationIndex] = destinationValue; destinationIndex++; destinationValue = 0; pixelValue = 128; } else { pixelValue >>= 1; } sourceIndex += 4; } if (pixelValue != 128) { destinationBuffer[destinationIndex] = destinationValue; } } // Copy binary image data to destination bitmap Marshal.Copy(destinationBuffer, 0, destinationData.Scan0, imageSize); // Unlock destination bitmap destination.Bitmap.UnlockBits(destinationData); }