Наследование: System.Web.UI.Page
Пример #1
0
        private static void testJpegOutput(JpegImage jpeg, CompressionParameters parameters, string jpegFileName)
        {
            using (FileStream output = new FileStream(jpegFileName, FileMode.Create))
                jpeg.WriteJpeg(output, parameters);

            FileAssert.AreEqual(jpegFileName, Tester.MapExpectedPath(jpegFileName));
        }
Пример #2
0
        private static void testBitmapOutput(JpegImage jpeg, string bitmapFileName)
        {
            using (FileStream output = new FileStream(bitmapFileName, FileMode.Create))
                jpeg.WriteBitmap(output);

            FileAssert.AreEqual(bitmapFileName, Tester.MapExpectedPath(bitmapFileName));
        }
Пример #3
0
 private static void testBitmapFromFile(string sourceFileName, string bitmapFileName)
 {
     using (JpegImage jpeg = new JpegImage(sourceFileName))
     {
         testBitmapOutput(jpeg, bitmapFileName);
     }
 }
Пример #4
0
 private static void testJpegFromFile(string fileName, string jpegFileName)
 {
     using (JpegImage jpeg = new JpegImage(fileName))
     {
         testJpegOutput(jpeg, jpegFileName);
     }
 }
Пример #5
0
        public Image Decode(Stream stream)
        {
            JpegImage jpg = new JpegImage(stream);

            int pixelWidth  = jpg.Width;
            int pixelHeight = jpg.Height;

            byte[] pixels = new byte[pixelWidth * pixelHeight * 4];

            if (!(jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8))
            {
                throw new NotSupportedException("JpegDecoder only support RGB color space.");
            }

            for (int y = 0; y < pixelHeight; y++)
            {
                SampleRow row = jpg.GetRow(y);

                for (int x = 0; x < pixelWidth; x++)
                {
                    Sample sample = row.GetAt(x);

                    int offset = (y * pixelWidth + x) * 4;

                    pixels[offset + 0] = (byte)sample[2];
                    pixels[offset + 1] = (byte)sample[1];
                    pixels[offset + 2] = (byte)sample[0];
                    pixels[offset + 3] = (byte)255;
                }
            }

            return(new Image(pixelWidth, pixelHeight, pixels));
        }
        public static void Run()
        {
            // ExStart:SupportForJPEGBITS
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_JPEG();
            int    bpp     = 2; // Set 2 bits per sample to see the difference in size and quality

            // The origin PNG with 8 bits per sample
            string originPngFileName = System.IO.Path.Combine(dataDir, "lena_16g_lin.png");

            // The output JPEG-LS with 2 bits per sample.
            string outputJpegFileName = "lena24b " + bpp + "-bit Gold.jls";

            using (PngImage pngImage = (PngImage)Image.Load(originPngFileName))
            {
                JpegOptions jpegOptions = new JpegOptions();
                jpegOptions.BitsPerChannel  = (byte)bpp;
                jpegOptions.CompressionType = JpegCompressionMode.JpegLs;
                pngImage.Save(outputJpegFileName, jpegOptions);
            }

            // The output PNG is produced from JPEG-LS to check image visually.
            string outputPngFileName = "lena24b " + bpp + "-bit Gold.png";

            using (JpegImage jpegImage = (JpegImage)Image.Load(outputJpegFileName))
            {
                jpegImage.Save(outputPngFileName, new PngOptions());
            }
        }
Пример #7
0
        public void Encode(Image image, Stream stream)
        {
            int pixelWidth  = image.Width;
            int pixelHeight = image.Height;

            byte[] sourcePixels = image.Pixels;

            SampleRow[] rows = new SampleRow[pixelHeight];

            for (int y = 0; y < pixelHeight; y++)
            {
                byte[] samples = new byte[pixelWidth * 3];

                for (int x = 0; x < pixelWidth; x++)
                {
                    int start  = x * 3;
                    int source = (y * pixelWidth + x) * 4;

                    samples[start]     = sourcePixels[source + 2];
                    samples[start + 1] = sourcePixels[source + 1];
                    samples[start + 2] = sourcePixels[source];
                }

                rows[y] = new SampleRow(samples, pixelWidth, 8, 3);
            }

            JpegImage jpg = new JpegImage(rows, Colorspace.RGB);

            jpg.WriteJpeg(stream, new CompressionParameters {
                Quality = Quality
            });
        }
Пример #8
0
        public static void Run()
        {
            // ExStart:SupportForJPEG-LSFormat
            // The path to the documents directory.
            string dataDir               = RunExamples.GetDataDir_JPEG();
            string sourceJpegFileName    = @"c:\aspose.work\lena24b.jls";
            string outputPngFileName     = @"c:\aspose.work\\lena24b.png";
            string outputPngRectFileName = @"c:\aspose.work\\lena24b_rect.png";

            // Decoding
            using (JpegImage jpegImage = (JpegImage)Image.Load(sourceJpegFileName))
            {
                JpegOptions jpegOptions = jpegImage.JpegOptions;

                // You can read new options:
                System.Console.WriteLine("Compression type:           {0}", jpegOptions.CompressionType);
                System.Console.WriteLine("Allowed lossy error (NEAR): {0}", jpegOptions.JpegLsAllowedLossyError);
                System.Console.WriteLine("Interleaved mode (ILV):     {0}", jpegOptions.JpegLsInterleaveMode);
                System.Console.WriteLine("Horizontal sampling:        {0}", ArrayToString(jpegOptions.HorizontalSampling));
                System.Console.WriteLine("Vertical sampling:          {0}", ArrayToString(jpegOptions.VerticalSampling));

                // Save the original JPEG-LS image to PNG.
                jpegImage.Save(outputPngFileName, new PngOptions());

                // Save the bottom-right quarter of the original JPEG-LS to PNG
                Rectangle quarter = new Rectangle(jpegImage.Width / 2, jpegImage.Height / 2, jpegImage.Width / 2, jpegImage.Height / 2);
                jpegImage.Save(outputPngRectFileName, new PngOptions(), quarter);
            }
        }
Пример #9
0
        private static void testJpegOutput(JpegImage jpeg, CompressionParameters parameters, string jpegFileName, string folderWithExpectedResults)
        {
            using (FileStream output = new FileStream(jpegFileName, FileMode.Create))
                jpeg.WriteJpeg(output, parameters);

            FileAssert.AreEqual(jpegFileName, Path.Combine(folderWithExpectedResults, jpegFileName));
        }
Пример #10
0
        private static void testBitmapOutput(JpegImage jpeg, string bitmapFileName, string folderWithExpectedResults)
        {
            using (FileStream output = new FileStream(bitmapFileName, FileMode.Create))
                jpeg.WriteBitmap(output);

            FileAssert.AreEqual(bitmapFileName, Path.Combine(folderWithExpectedResults, bitmapFileName));
        }
Пример #11
0
 private static void testJpegFromFile(string fileName, string jpegFileName, string folderWithExpectedResults)
 {
     using (JpegImage jpeg = new JpegImage(fileName))
     {
         testJpegOutput(jpeg, jpegFileName, folderWithExpectedResults);
     }
 }
        public static void Run()
        {
            // ExStart:SupportForCMYKAndYCCKColorModesInJPEGLossless
            // The path to the documents directory.
            string       dataDir    = RunExamples.GetDataDir_JPEG();
            MemoryStream jpegStream = new MemoryStream();

            try
            {
                // Save to JPEG Lossless CMYK
                using (JpegImage image = (JpegImage)Image.Load("056.jpg"))
                {
                    JpegOptions options = new JpegOptions();
                    options.ColorType       = JpegCompressionColorMode.Cmyk;
                    options.CompressionType = JpegCompressionMode.Lossless;

                    // The default profiles will be used.
                    options.RgbColorProfile  = null;
                    options.CmykColorProfile = null;

                    image.Save(jpegStream, options);
                }

                // Load from JPEG Lossless CMYK
                jpegStream.Position = 0;
                using (JpegImage image = (JpegImage)Image.Load(jpegStream))
                {
                    image.Save("056_cmyk.png", new PngOptions());
                }
            }
            finally
            {
                jpegStream.Dispose();
            }
        }
Пример #13
0
 public void TestGrayscaleJpegToBitmap()
 {
     using (JpegImage jpegImage = new JpegImage(Tester.MapOpenPath("turkey.jpg")))
     {
         testBitmapOutput(jpegImage, "turkey.png");
     }
 }
Пример #14
0
 public void TestGrayscaleJpegToBitmap()
 {
     using (JpegImage jpegImage = new JpegImage(m_testcase + "turkey.jpg"))
     {
         testBitmapOutput(jpegImage, "turkey.png", m_expectedResults);
     }
 }
Пример #15
0
 private static void testBitmapFromFile(string sourceFileName, string bitmapFileName, string folderWithExpectedResults)
 {
     using (JpegImage jpeg = new JpegImage(sourceFileName))
     {
         testBitmapOutput(jpeg, bitmapFileName, folderWithExpectedResults);
     }
 }
Пример #16
0
 public void TestCreateJpegImageFromPixels()
 {
     using (JpegImage jpegImage = createImageFromPixels())
     {
         testJpegOutput(jpegImage, "JpegImageFromPixels.jpg", m_expectedResults);
         testBitmapOutput(jpegImage, "JpegImageFromPixels.png", m_expectedResults);
     }
 }
Пример #17
0
        public void JpegToPdf_WhenCalledWithOneEmptyFile_ShouldThrow()
        {
            var image = new JpegImage
            {
                Bytes  = new byte[] { },
                Width  = 1,
                Height = 1
            };

            Assert.Throws <ArgumentNullException>(() => _fixture.Lib.JpegToPdf(new[] { image }));
        }
Пример #18
0
        public void JpegToPdf_WhenCalledWithOneFile_ShouldSucceed()
        {
            var image = new JpegImage
            {
                Bytes  = File.ReadAllBytes("Docs/image_0.jpeg"),
                Width  = 580,
                Height = 387
            };

            var pdfBytes = _fixture.Lib.JpegToPdf(new[] { image });

            Assert.True(pdfBytes.Length > Threshold);
        }
Пример #19
0
        public void JpegToPdf_WhenCalledWithOnePngFile_ShouldFail()
        {
            var image = new JpegImage
            {
                Bytes  = File.ReadAllBytes("Docs/image_2.png"),
                Width  = 1000,
                Height = 1000
            };

            var pdfBytes = _fixture.Lib.JpegToPdf(new[] { image });

            Assert.True(pdfBytes.Length < Threshold);
        }
Пример #20
0
        public void JpegToPdf_WhenCalledWithJpeg_ShouldSucceed()
        {
            var file = new JpegImage
            {
                Bytes  = File.ReadAllBytes("Assets/image_0.jpeg"),
                Width  = 1024,
                Height = 1024
            };

            var bytes = _fixture.DocNet.JpegToPdf(new[] { file });

            File.WriteAllBytes("../../../output_file.pdf", bytes);
        }
Пример #21
0
        public void TestDecompressionFromCMYKJpeg()
        {
            using (JpegImage jpeg = new JpegImage(Tester.MapOpenPath("ammerland.jpg")))
            {
                Assert.AreEqual(jpeg.BitsPerComponent, 8);
                Assert.AreEqual(jpeg.ComponentsPerSample, 4);
                Assert.AreEqual(jpeg.Colorspace, Colorspace.CMYK);
                Assert.AreEqual(jpeg.Width, 315);
                Assert.AreEqual(jpeg.Height, 349);

                testBitmapOutput(jpeg, "ammerland.bmp");
            }
        }
Пример #22
0
        public void TestDecompressionFromCMYKJpeg()
        {
            using (JpegImage jpeg = new JpegImage(m_testcase + "ammerland.jpg"))
            {
                Assert.AreEqual(jpeg.BitsPerComponent, 8);
                Assert.AreEqual(jpeg.ComponentsPerSample, 4);
                Assert.AreEqual(jpeg.Colorspace, Colorspace.CMYK);
                Assert.AreEqual(jpeg.Width, 315);
                Assert.AreEqual(jpeg.Height, 349);

                testBitmapOutput(jpeg, "ammerland.bmp", m_expectedResults);
            }
        }
Пример #23
0
        private static JpegImage createImageFromPixels()
        {
            byte[] rowData = new byte[96];
            for (int i = 0; i < rowData.Length; ++i)
            {
                if (i < 5)
                {
                    rowData[i] = 0xE4;
                }
                else if (i < 15)
                {
                    rowData[i] = 0xAB;
                }
                else if (i < 35)
                {
                    rowData[i] = 0x00;
                }
                else if (i < 55)
                {
                    rowData[i] = 0x65;
                }
                else
                {
                    rowData[i] = 0xF0;
                }
            }

            const int        width               = 24;
            const int        height              = 25;
            const byte       bitsPerComponent    = 8;
            const byte       componentsPerSample = 4;
            const Colorspace colorspace          = Colorspace.CMYK;

            SampleRow row = new SampleRow(rowData, width, bitsPerComponent, componentsPerSample);

            SampleRow[] rows = new SampleRow[height];
            for (int i = 0; i < rows.Length; ++i)
            {
                rows[i] = row;
            }

            JpegImage jpegImage = new JpegImage(rows, colorspace);

            Assert.AreEqual(jpegImage.Width, width);
            Assert.AreEqual(jpegImage.Height, rows.Length);
            Assert.AreEqual(jpegImage.BitsPerComponent, bitsPerComponent);
            Assert.AreEqual(jpegImage.ComponentsPerSample, componentsPerSample);
            Assert.AreEqual(jpegImage.Colorspace, colorspace);
            return(jpegImage);
        }
Пример #24
0
        public void GetValueShouldRetrieveImage()
        {
            byte[] imageBytes = Conversion.UtfToBytes("ImageValue");

            var yotiAttribute = new YotiAttribute <Image>("selfie", new JpegImage(imageBytes), null);

            var expectedImage = new JpegImage(imageBytes);

            string expectedBase64URI = string.Format("data:image/jpeg;base64,{0}", Conversion.BytesToBase64(imageBytes));
            Image  actualImage       = yotiAttribute.GetValue();

            Assert.IsTrue(new ImageComparer().Equals(expectedImage, actualImage));
            Assert.AreEqual(expectedBase64URI, actualImage.GetBase64URI());
        }
Пример #25
0
        private static void testJpegFromBitmap(Bitmap bmp, string jpegFileName)
        {
            using (JpegImage jpeg = new JpegImage(bmp))
            {
                Assert.AreEqual(jpeg.Width, bmp.Width);
                Assert.AreEqual(jpeg.Height, bmp.Height);
                Assert.AreEqual(jpeg.ComponentsPerSample, 3);                //Number of components in Bitmap

                using (FileStream output = new FileStream(jpegFileName, FileMode.Create))
                    jpeg.WriteJpeg(output);
            }

            FileAssert.AreEqual(jpegFileName, Path.Combine(m_expectedResults, jpegFileName));
        }
Пример #26
0
        public override byte[] Decode(Bytes.Buffer data, PdfDirectObject parameters, IDictionary <PdfName, PdfDirectObject> header)
        {
            var imageParams      = header;
            var dictHeight       = ((IPdfNumber)(header[PdfName.Height] ?? header[PdfName.H])).IntValue;
            var dictWidth        = ((IPdfNumber)(header[PdfName.Width] ?? header[PdfName.W])).IntValue;
            var bitsPerComponent = ((IPdfNumber)imageParams[PdfName.BitsPerComponent])?.IntValue ?? 8;
            var flag             = imageParams[PdfName.ImageMask] as PdfBoolean;
            var jpegOptions      = new JpegOptions(decodeTransform: null, colorTransform: null);

            // Checking if values need to be transformed before conversion.
            var decodeObj = imageParams[PdfName.Decode] ?? imageParams[PdfName.D];
            var decodeArr = decodeObj?.Resolve() as PdfArray;

            if (false && decodeArr != null)
            {
                var decode          = decodeArr.Select(p => ((IPdfNumber)p).IntValue).ToArray();
                var decodeArrLength = decodeArr.Count;
                var transform       = new int[decodeArr.Count];
                var transformNeeded = false;
                var maxValue        = (1 << bitsPerComponent) - 1;
                for (var i = 0; i < decodeArrLength; i += 2)
                {
                    transform[i]     = ((decode[i + 1] - decode[i]) * 256) | 0;
                    transform[i + 1] = (decode[i] * maxValue) | 0;
                    if (transform[i] != 256 || transform[i + 1] != 0)
                    {
                        transformNeeded = true;
                    }
                }
                if (transformNeeded)
                {
                    jpegOptions.DecodeTransform = transform;
                }
            }
            // Fetching the 'ColorTransform' entry, if it exists.
            if (parameters is PdfDictionary paramDict)
            {
                var colorTransform = paramDict[PdfName.ColorTransform];
                if (colorTransform is IPdfNumber number)
                {
                    jpegOptions.ColorTransform = number.IntValue;
                }
            }
            var jpegImage = new JpegImage(jpegOptions);

            jpegImage.Parse(data.GetBuffer());
            var buffer = jpegImage.GetData(width: dictWidth, height: dictHeight, forceRGB: false, isSourcePDF: true);

            return(buffer);
        }
Пример #27
0
        public void TestCreateFromPixelsAndRecompress()
        {
            using (JpegImage jpegImage = createImageFromPixels())
            {
                CompressionParameters compressionParameters = new CompressionParameters();
                compressionParameters.Quality = 20;
                const string output = "JpegImageFromPixels_20.jpg";
                testJpegOutput(jpegImage, compressionParameters, output, m_expectedResults);

                using (JpegImage recompressedImage = new JpegImage(output))
                {
                    Assert.AreEqual(recompressedImage.Colorspace, jpegImage.Colorspace);
                }
            }
        }
 public static void Run()
 {
     // ExStart:AddThumbnailToJFIFSegment
     // The path to the documents directory.
     string dataDir = RunExamples.GetDataDir_JPEG();
     using (MemoryStream stream = new MemoryStream())
     {
         JpegImage thumbnailImage = new JpegImage(100, 100);
         JpegImage image = new JpegImage(1000, 1000);
         image.Jfif = new JFIFData();
         image.Jfif.Thumbnail = thumbnailImage;
         image.Save(dataDir + stream + "_out.jpeg");
     }
     // ExEnd:AddThumbnailToJFIFSegment
 }
        public static void Run()
        {
            // ExStart:ReadAllEXIFTags
            // The path to the documents directory.
            string dataDir = RunExamples.GetDataDir_JPEG();

            // Load a Jpeg image from file path location or stream
            using (JpegImage image = (JpegImage)Image.Load(dataDir + "aspose-logo.jpg"))
            {
                // Perform the automatic rotation on the image depending on the orientation data stored in the EXIF
                image.AutoRotate();
                // Save the result on disc or stream
                image.Save(dataDir + "aspose-logo_out.jpg");
            }
        }
Пример #30
0
        public void TestCompressionResultsSameAsForCJpeg()
        {
            using (JpegImage jpeg = new JpegImage(Tester.MapOpenPath("test24.bmp")))
            {
                testJpegOutput(jpeg, "test24.jpg");

                CompressionParameters parameters = new CompressionParameters();
                parameters.Quality = 25;
                testJpegOutput(jpeg, parameters, "test24_25.jpg");

                parameters = new CompressionParameters();
                parameters.SimpleProgressive = true;
                testJpegOutput(jpeg, parameters, "test24_prog.jpg");
            }
        }
Пример #31
0
 static void Main(string[] args)
 {
     // Calls the Run method on all the example classes
     // The rasterized images will be saved in the applications bin folder by default
     GifWithWebPalette.Run();
     IndexedImage.Run();
     JpegImage.Run();
     MonochromeImage.Run();
     MultiPageTiff.Run();
     PdfPortfolio.Run();
     PngImageWithRgbColor.Run();
     PngImageWithUserPalette.Run();
     RgbaImage.Run();
     TiffImageColorFormat.Run();
 }