Exemple #1
0
 public static unsafe void WriteImage(
     byte[] buffer, int width, int height,
     SurfaceFormat sourceFormat, Stream stream,
     ImageWriteFormat format = ImageWriteFormat.PNG, int jpegQuality = 75
     ) {
     fixed(byte *pBuffer = buffer)
     WriteImage(pBuffer, buffer.Length, width, height, sourceFormat, stream, format, jpegQuality);
 }
Exemple #2
0
 public static void WriteImage(
     Texture2D tex, string filename,
     ImageWriteFormat format = ImageWriteFormat.PNG, int jpegQuality = 75
     )
 {
     using (var stream = File.Create(filename))
         WriteImage(tex, stream, format);
 }
Exemple #3
0
        public static void WriteImage(
            Texture2D tex, Stream stream,
            ImageWriteFormat format = ImageWriteFormat.PNG, int jpegQuality = 75
            )
        {
            var buffer = GetTextureData(tex);

            WriteImage(buffer, tex.Width, tex.Height, tex.Format, stream, format);
        }
        public IImageWriter GetImageWriter(ImageWriteFormat imageType, string outputPath)
        {
            switch (imageType)
            {
            case ImageWriteFormat.BMP: return(new BmpWriter(outputPath));

            default: throw new Exception("Not such type");
            }
        }
Exemple #5
0
        public static unsafe void WriteImage(
            byte *data, int dataLength, int width, int height,
            SurfaceFormat sourceFormat, Stream stream,
            ImageWriteFormat format = ImageWriteFormat.PNG, int jpegQuality = 75
            )
        {
            int numComponents;
            var bytesPerPixel = Evil.TextureUtils.GetBytesPerPixelAndComponents(sourceFormat, out numComponents);

            if (dataLength < (bytesPerPixel * width * height))
            {
                throw new ArgumentException("buffer");
            }

            using (var scratch = BufferPool <byte> .Allocate(1024 * 64))
                fixed(byte *_pScratch = scratch.Data)
                {
                    Native.WriteCallback callback = (pScratch, pData, count) => {
                        int offset = 0;
                        while (count > 0)
                        {
                            var copySize = Math.Min(count, scratch.Data.Length);
                            Buffer.MemoryCopy(pData + offset, pScratch, copySize, copySize);
                            stream.Write(scratch.Data, 0, copySize);
                            count  -= copySize;
                            offset += copySize;
                        }
                    };

                    switch (format)
                    {
                    case ImageWriteFormat.HDR:
                        if (bytesPerPixel != 16)
                        {
                            throw new NotImplementedException("Non-vector4");
                        }
                        Native.API.stbi_write_hdr_to_func(callback, _pScratch, width, height, numComponents, (float *)(void *)data);
                        break;

                    case ImageWriteFormat.PNG:
                        if (bytesPerPixel != 4)
                        {
                            throw new NotImplementedException("Non-rgba32");
                        }
                        Native.API.stbi_write_png_to_func(callback, _pScratch, width, height, numComponents, data, width * bytesPerPixel);
                        break;

                    case ImageWriteFormat.BMP:
                        if (bytesPerPixel != 4)
                        {
                            throw new NotImplementedException("Non-rgba32");
                        }
                        Native.API.stbi_write_bmp_to_func(callback, _pScratch, width, height, numComponents, data);
                        break;

                    case ImageWriteFormat.TGA:
                        if (bytesPerPixel != 4)
                        {
                            throw new NotImplementedException("Non-rgba32");
                        }
                        Native.API.stbi_write_tga_to_func(callback, _pScratch, width, height, numComponents, data);
                        break;

                    case ImageWriteFormat.JPEG:
                        if (bytesPerPixel != 4)
                        {
                            throw new NotImplementedException("Non-rgba32");
                        }
                        Native.API.stbi_write_jpg_to_func(callback, _pScratch, width, height, numComponents, data, jpegQuality);
                        break;

                    default:
                        throw new ArgumentOutOfRangeException("format");
                    }
                }
        }