Beispiel #1
0
        private bool ImageOutputIsOk(int outputBufferSize, TurboJpegBuffer outputBuffer, int width, int height, int pitch, int pixelSize)
        {
            var results = new byte[outputBufferSize];

            Marshal.Copy((IntPtr)outputBuffer, results, 0, outputBufferSize);
            var image = new Bitmap(width, height, SdiPixelFormat.Format32bppArgb);

            for (var y = 0; y < height; ++y)
            {
                for (var x = 0; x < width; ++x)
                {
                    var baseOffset = y * pitch + x * pixelSize;
                    var colour     = Color.FromArgb(results[baseOffset + TurboJpegInterop.RedOffset[TjPixelFormat.Rgba]],
                                                    results[baseOffset + TurboJpegInterop.GreenOffset[TjPixelFormat.Rgba]],
                                                    results[baseOffset + TurboJpegInterop.BlueOffset[TjPixelFormat.Rgba]]);
                    image.SetPixel(x, y, colour);
                }
            }

            bool isOk;

            using (var form = new Form
            {
                Text = String.Format("Width: {0}, Height: {1}", image.Width, image.Height),
                ClientSize = new Size(Math.Max(image.Width, 64), Math.Max(image.Height, 64)),
                FormBorderStyle = FormBorderStyle.FixedToolWindow
            })
            {
                using (
                    var pictureBox = new PictureBox
                {
                    Image = image,
                    Parent = form,
                    Dock = DockStyle.Fill,
                    SizeMode = PictureBoxSizeMode.Zoom
                })
                {
                    form.Show();
                    isOk =
                        MessageBox.Show("Does the image look reasonable?",
                                        "Eyeballing…",
                                        MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question) == DialogResult.Yes;
                }

                form.Close();
            }
            return(isOk);
        }
 public void AllocAndSaveInBuffer()
 {
     const int bufsize = 1024;
     var deadbeef = new byte[] { 0xde, 0xad, 0xbe, 0xef };
     var pointerToMemory = NativeMethods.alloc(bufsize);
     using (var buffer = new TurboJpegBuffer(pointerToMemory, bufsize))
     {
         Assert.IsTrue(buffer.BufferSize == bufsize);
         pointerToMemory.Initialise(bufsize);
         var bufferData = buffer.ToArray();
         for (var i = 0; i < bufsize; ++i)
         {
             Assert.IsTrue(bufferData[i] == deadbeef[i % 4]);
         }
     }
 }
        public void AllocAndSaveInBuffer()
        {
            const int bufsize         = 1024;
            var       deadbeef        = new byte[] { 0xde, 0xad, 0xbe, 0xef };
            var       pointerToMemory = NativeMethods.alloc(bufsize);

            using (var buffer = new TurboJpegBuffer(pointerToMemory, bufsize))
            {
                Assert.IsTrue(buffer.BufferSize == bufsize);
                pointerToMemory.Initialise(bufsize);
                var bufferData = buffer.ToArray();
                for (var i = 0; i < bufsize; ++i)
                {
                    Assert.IsTrue(bufferData[i] == deadbeef[i % 4]);
                }
            }
        }
Beispiel #4
0
        public void DecompressAndScaleImageByUnsupportedScaleFactor()
        {
            const int width            = 509; // prime
            const int height           = 509;
            var       pixelSize        = TurboJpegInterop.PixelSize[TjPixelFormat.Rgba];
            var       pitch            = width * pixelSize;
            var       outputBufferSize = pitch * height;
            var       outputBuffer     = new TurboJpegBuffer(outputBufferSize);

            outputBuffer.Buffer.Initialise(outputBufferSize);
            var success = NativeMethods.decompress((IntPtr)this.handle,
                                                   imageData,
                                                   imageData.Length,
                                                   (IntPtr)outputBuffer,
                                                   width,
                                                   pitch,
                                                   height,
                                                   TjPixelFormat.Rgba,
                                                   TurboJpegFlags.None);

            Assert.IsTrue(success == 0);
            Assert.IsFalse(this.ImageOutputIsOk(outputBufferSize, outputBuffer, width, height, pitch, pixelSize));
        }
        private bool ImageOutputIsOk(int outputBufferSize, TurboJpegBuffer outputBuffer, int width, int height, int pitch, int pixelSize)
        {

            var results = new byte[outputBufferSize];
            Marshal.Copy((IntPtr)outputBuffer, results, 0, outputBufferSize);
            var image = new Bitmap(width, height, SdiPixelFormat.Format32bppArgb);
            for (var y = 0; y < height; ++y)
            {
                for (var x = 0; x < width; ++x)
                {
                    var baseOffset = y * pitch + x * pixelSize;
                    var colour = Color.FromArgb(results[baseOffset + TurboJpegInterop.RedOffset[TjPixelFormat.Rgba]],
                                                results[baseOffset + TurboJpegInterop.GreenOffset[TjPixelFormat.Rgba]],
                                                results[baseOffset + TurboJpegInterop.BlueOffset[TjPixelFormat.Rgba]]);
                    image.SetPixel(x, y, colour);
                }
            }

            bool isOk;
            using(var form = new Form
                             {
                                 Text = String.Format("Width: {0}, Height: {1}", image.Width, image.Height),
                                 ClientSize = new Size(Math.Max(image.Width, 64), Math.Max(image.Height, 64)),
                                 FormBorderStyle = FormBorderStyle.FixedToolWindow
                             })
            {
                using(
                    var pictureBox = new PictureBox
                                     {
                                         Image = image,
                                         Parent = form,
                                         Dock = DockStyle.Fill,
                                         SizeMode = PictureBoxSizeMode.Zoom
                                     })
                {
                    form.Show();
                    isOk =
                        MessageBox.Show("Does the image look reasonable?",
                                        "Eyeballing…",
                                        MessageBoxButtons.YesNo,
                                        MessageBoxIcon.Question) == DialogResult.Yes;
                }

                form.Close();
            }
            return isOk;
        }
 public void DecompressAndScaleImageByUnsupportedScaleFactor()
 {
     const int width = 509;      // prime
     const int height = 509;
     var pixelSize = TurboJpegInterop.PixelSize[TjPixelFormat.Rgba];
     var pitch = width * pixelSize;
     var outputBufferSize = pitch * height;
     var outputBuffer = new TurboJpegBuffer(outputBufferSize);
     outputBuffer.Buffer.Initialise(outputBufferSize);
     var success = NativeMethods.decompress((IntPtr)this.handle,
                                               imageData,
                                               imageData.Length,
                                               (IntPtr)outputBuffer,
                                               width,
                                               pitch,
                                               height,
                                               TjPixelFormat.Rgba,
                                               TurboJpegFlags.None);
     Assert.IsTrue(success == 0);
     Assert.IsFalse(this.ImageOutputIsOk(outputBufferSize, outputBuffer, width, height, pitch, pixelSize));
 }