예제 #1
0
        public override ImageData Read(IBinaryStream stream, ImageMetaData info)
        {
            var meta = (WbmMetaData)info;

            var section = WpxSection.Find(meta.Header, 0x11, meta.EntryCount, meta.EntrySize);

            if (null == section)
            {
                throw new InvalidFormatException();
            }

            PixelFormat format;
            int         pixel_size;

            switch (meta.BPP)
            {
            case 24:
                format     = PixelFormats.Bgr24;
                pixel_size = 3;
                break;

            case 32:
                format     = PixelFormats.Bgr32;
                pixel_size = 4;
                break;

            case 16:
                format     = PixelFormats.Bgr555;
                pixel_size = 2;
                break;

            case 8:
                format     = PixelFormats.Indexed8;
                pixel_size = 1;
                break;

            default:
                throw new NotSupportedException("Not supported WBM bitdepth");
            }
            int stride = ((int)meta.Width * pixel_size + 3) & -4;
            var reader = new WbmReader(stream, section);
            var pixels = reader.Unpack(stride, pixel_size, section.DataFormat);

            if (null == pixels)
            {
                throw new InvalidFormatException();
            }

            if (8 == meta.BPP)
            {
                section = WpxSection.Find(meta.Header, 0x12, meta.EntryCount, meta.EntrySize);
                if (null == section)
                {
                    return(ImageData.Create(info, PixelFormats.Gray8, null, pixels, stride));
                }
                reader = new WbmReader(stream, section);
                var palette_data = reader.Unpack(48, 3, section.DataFormat);
                var palette      = CreatePalette(palette_data);
                return(ImageData.Create(info, PixelFormats.Indexed8, palette, pixels, stride));
            }

            if (meta.BPP < 24)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }
            section = WpxSection.Find(meta.Header, 0x13, meta.EntryCount, meta.EntrySize);
            if (null == section)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }

            int alpha_stride = ((int)meta.Width + 3) & -4;

            byte[] alpha = null;
            try
            {
                reader = new WbmReader(stream, section);
                alpha  = reader.Unpack(alpha_stride, 1, section.DataFormat);
            }
            catch { }
            if (null == alpha)
            {
                return(ImageData.Create(info, format, null, pixels, stride));
            }

            byte[] alpha_image = new byte[4 * meta.Width * meta.Height];
            int    dst         = 0;

            for (int y = 0; y < meta.Height; ++y)
            {
                int alpha_src = y * alpha_stride;
                int src       = y * stride;
                for (int x = 0; x < meta.Width; ++x)
                {
                    alpha_image[dst++] = pixels[src];
                    alpha_image[dst++] = pixels[src + 1];
                    alpha_image[dst++] = pixels[src + 2];
                    alpha_image[dst++] = alpha[alpha_src + x];
                    src += pixel_size;
                }
            }
            return(ImageData.Create(info, PixelFormats.Bgra32, null, alpha_image, (int)meta.Width * 4));
        }
예제 #2
0
파일: ImageWBM.cs 프로젝트: Casidi/GARbro
        public override ImageData Read(Stream stream, ImageMetaData info)
        {
            var meta = info as WbmMetaData;
            if (null == meta)
                throw new ArgumentException ("WbmFormat.Read should be supplied with WbmMetaData", "info");

            var section = WpxSection.Find (meta.Header, 0x11, meta.EntryCount, meta.EntrySize);
            if (null == section)
                throw new InvalidFormatException();

            PixelFormat format;
            int pixel_size;
            switch (meta.BPP)
            {
            case 24:
                format = PixelFormats.Bgr24;
                pixel_size = 3;
                break;
            case 32:
                format = PixelFormats.Bgr32;
                pixel_size = 4;
                break;
            case 16:
                format = PixelFormats.Bgr555;
                pixel_size = 2;
                break;
            default:
                throw new NotSupportedException ("Not supported WBM bitdepth");
            }
            int stride = ((int)meta.Width * pixel_size + 3) & -4;
            var reader = new WbmReader (stream, section);
            var pixels = reader.Unpack (stride, pixel_size, section.DataFormat);
            if (null == pixels)
                throw new InvalidFormatException();
            if (meta.BPP < 24)
                return ImageData.Create (info, format, null, pixels, stride);
            section = WpxSection.Find (meta.Header, 0x13, meta.EntryCount, meta.EntrySize);
            if (null == section)
                return ImageData.Create (info, format, null, pixels, stride);

            int alpha_stride = ((int)meta.Width + 3) & -4;
            byte[] alpha = null;
            try
            {
                reader = new WbmReader (stream, section);
                alpha = reader.Unpack (alpha_stride, 1, section.DataFormat);
            }
            catch { }
            if (null == alpha)
                return ImageData.Create (info, format, null, pixels, stride);

            byte[] alpha_image = new byte[4*meta.Width*meta.Height];
            int dst = 0;
            for (int y = 0; y < meta.Height; ++y)
            {
                int alpha_src = y * alpha_stride;
                int src = y * stride;
                for (int x = 0; x < meta.Width; ++x)
                {
                    alpha_image[dst++] = pixels[src];
                    alpha_image[dst++] = pixels[src+1];
                    alpha_image[dst++] = pixels[src+2];
                    alpha_image[dst++] = alpha[alpha_src+x];
                    src += pixel_size;
                }
            }
            return ImageData.Create (info, PixelFormats.Bgra32, null, alpha_image, (int)meta.Width*4);
        }