예제 #1
0
        private Stream ResizeImage(Stream image_stream, int width, int height, int quality, ImageType type)
        {
            try {
                // Load the image as a writeablebitmap
                WriteableBitmap writableBitmap;
                BitmapImage bitmapImage = new BitmapImage();
                bitmapImage.SetSource(image_stream);
                writableBitmap = new WriteableBitmap(bitmapImage);

                double scale = Math.Min((double) width / writableBitmap.PixelWidth, (double) height / writableBitmap.PixelHeight);

                // No resize needed
                if (scale >= 1.0)
                    return image_stream;

                // Setup shorter names and pixelbuffers
                int w = writableBitmap.PixelWidth;
                int h = writableBitmap.PixelHeight;
                int[] p = writableBitmap.Pixels;
                byte[][,] imageRaster = new byte[3][,]; // RGB colors
                imageRaster[0] = new byte[w, h];
                imageRaster[1] = new byte[w, h];
                imageRaster[2] = new byte[w, h];

                // Copy WriteableBitmap data into buffer for FluxJpeg
                int i = 0;
                for (int y = 0; y < h; y++) {
                    for (int x = 0; x < w; x++) {
                        int color = p[i++];

                        imageRaster[0][x, y] = (byte) (color >> 16); // R
                        imageRaster[1][x, y] = (byte) (color >> 8);  // G
                        imageRaster[2][x, y] = (byte) (color);       // B
                    }
                }

                // Create new FluxJpeg image based on pixel data
                FluxJpeg.Core.Image jpegImage = new FluxJpeg.Core.Image(new ColorModel {
                    colorspace = ColorSpace.RGB
                }, imageRaster);

                // Calc new proportional size
                width = (int) Math.Round(writableBitmap.PixelWidth * scale);
                height = (int) Math.Round(writableBitmap.PixelHeight * scale);

                // Resize the image
                ImageResizer resizer = new ImageResizer(jpegImage);
                Image resizedImage = resizer.Resize(width, height, FluxJpeg.Core.Filtering.ResamplingFilters.LowpassAntiAlias);
                Stream imageStream = new MemoryStream();

                if (type == ImageType.Jpeg) {
                    // Encode the resized image as Jpeg
                    JpegEncoder jpegEncoder = new JpegEncoder(resizedImage, quality, imageStream);
                    jpegEncoder.Encode();
                } else {
                    int[] pixelBuffer = new int[resizedImage.Height * resizedImage.Width];
                    byte[][,] resizedRaster = resizedImage.Raster;

                    // Convert FJCore raster to PixelBuffer
                    for (int y = 0; y < resizedImage.Height; y++) {
                        for (int x = 0; x < resizedImage.Width; x++) {
                            int color = 0;

                            color = color | resizedRaster[0][x, y] << 16; // R
                            color = color | resizedRaster[1][x, y] << 8;  // G
                            color = color | resizedRaster[2][x, y];       // B

                            pixelBuffer[(y * resizedImage.Width) + x] = color;
                        }
                    }

                    // Encode the resized image as Png
                    PngEncoder pngEncoder = new PngEncoder(pixelBuffer, resizedImage.Width, resizedImage.Height, false, PngEncoder.FILTER_NONE, Deflater.BEST_COMPRESSION);
                    byte[] pngBuffer = pngEncoder.pngEncode();
                    imageStream.Write(pngBuffer, 0, pngBuffer.Length);
                }

                return imageStream;
            } catch {
                // Ignore the error and let the server resize the image
            }

            return image_stream;
        }
예제 #2
0
        /// <summary>
        /// This is a javascript application.
        /// </summary>
        /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param>
        public Application(IApp page)
        {
            // jsc able to take a random open source project
            // to encode a png file on the client, worker?
            // the code seems to be pre compiling.
            // how to use it?


            //Error   9   Unsafe code may only appear if compiling with /unsafe   X:\jsc.svn\examples\javascript\io\synergy\PNGEncoderExperiment\PNGEncoderExperiment\opensource\FJCore\Image.cs  147 13  PNGEncoderExperiment
            //Error   1   'FluxJpeg.Core.HuffmanTable' does not contain a definition for 'Decode' and no extension method 'Decode' accepting a first argument of type 'FluxJpeg.Core.HuffmanTable' could be found(are you missing a using directive or an assembly reference ?)	X:\jsc.svn\examples\javascript\io\synergy\PNGEncoderExperiment\PNGEncoderExperiment\opensource\FJCore\Decoder\JpegComponent.cs  468 29  PNGEncoderExperiment

            // X:\jsc.svn\examples\javascript\io\synergy\PNGEncoderExperiment\PNGEncoderExperiment\opensource\FJCore\Image.cs
            // 147

            // is jsc ready for unsafe byte opcodes yet?
            // be have byref, async, why not also support pointers then?
            // #if SILVERLIGHT


            //0200002b Plupload.PngEncoder.PngEncoder
            //script: error JSC1000: Missing Script Attribute? Native constructor was invoked, please implement[System.IO.MemoryStream..ctor(System.Int32)]


            //    0200002b Plupload.PngEncoder.PngEncoder
            //    script: error JSC1000: opcode unsupported - [0x01d7] bne.un + 0 - 2{[0x01ce]
            //rem
            //  script: error JSC1000: error at Plupload.PngEncoder.PngEncoder.WriteImageData,

            // seems to be return from within try.
            // could the rewriter fix it for us?

            // X:\jsc.svn\examples\javascript\Test\TestReturnFromWithinTry\TestReturnFromWithinTry\Application.cs
            // or we fix this source for now?

            var w = 128;
            var h = 128;


            // black pixels?
            var data = new int[w * h];

            var p = new PngEncoder(

                data,
                w,
                h,
                true,
                PngEncoder.FILTER_NONE,
                0
            );

 //           script: error JSC1000: unsupported flow detected, try to simplify.
 //Assembly U:\PNGEncoderExperiment.Application.exe
 //DeclaringType Plupload.PngEncoder.DeflaterHuffman + Tree, PNGEncoderExperiment.Application,
 //OwnerMethod BuildTree
 //            Offset 030d

            
            var bytes = p.pngEncode();

            new IHTMLPre { new { bytes } }.AttachToDocument();

        }