Boost logo

Boost Users :

Subject: Re: [Boost-users] [GIL] Array to image
From: Erik Hvatum (ice.rikh_at_[hidden])
Date: 2011-11-29 11:25:05


Hi Mark,

You may construct an image view backed by your array. ie, make an image_view that is a view into your array. This way, you avoid a superfluous memory copy. Here's an example of the idea from some of my own code:

bgra8c_view_t imageView( interleaved_view( qImage.width(),
                                           qImage.height(),
                                           reinterpret_cast<bgra8c_pixel_t*>(qImage.constBits()),
                                           qImage.bytesPerLine() ) );
You may need to change bgra8c_pixel_t to match the DIB layout. If the DIB does include an alpha channel, be aware that boost::gil expects pre-multiplied alpha. Here is a little code of I wrote to help with that:

// GIL's from-RGBA color converter assumes premultiplied alpha, but we do not use premultiplied alpha. Consequently, GIL does
// not produce the desired results when it converts pixels from RGBA or ARGB color space to RGB. This may be remedied by
// using the ConvertRGBA color convertor. For example: copy_and_convert_pixels(srcRGBAView, dstRGBView, ConvertRGBA())

// Default to color converters provided by GIL
template<typename TSrcColorSpace, typename TDstColorSpace>
struct ConvertRGBA_impl
  : public default_color_converter_impl<TSrcColorSpace, TDstColorSpace> {};

// Override GIL's from-RGBA color converter
template<typename TDstColorSpace>
struct ConvertRGBA_impl<rgba_t, TDstColorSpace>
{
        template<typename TSrcPixel, typename TDstPixel>
        void operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const;
};

struct ConvertRGBA
{
        template<typename TSrcPixel, typename TDstPixel>
        void operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const;
};

template<typename TDstColorSpace>
template<typename TSrcPixel, typename TDstPixel>
void ConvertRGBA_impl<rgba_t, TDstColorSpace>::operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const
{
        default_color_converter_impl<rgb_t, TDstColorSpace>()
        (
                pixel<typename channel_type<TSrcPixel>::type, rgb_layout_t>
                (
                        get_color(srcPixel, red_t()),
                        get_color(srcPixel, green_t()),
                        get_color(srcPixel, blue_t())
                ),
                dstPixel
        );
}

template<typename TSrcPixel, typename TDstPixel>
void ConvertRGBA::operator () (const TSrcPixel& srcPixel, TDstPixel& dstPixel) const
{
        typedef typename color_space_type<TSrcPixel>::type TSrcColorSpace;
        typedef typename color_space_type<TDstPixel>::type TDstColorSpace;
        ConvertRGBA_impl<TSrcColorSpace, TDstColorSpace>()(srcPixel, dstPixel);
}

-Erik

On Tuesday 29 November 2011 4:29:40 PM Sapp, Mark wrote:
> Hi All,
>
>
>
> Can anyone point me in the right directions for copying or assigning an
> array in memory to an image? I'm reading an frame out of an AVI file
> using Microsoft's vfw32.lib/vfw.h and the function that grabs the frame
> returns a pointer to a bitmap in memory, like this:
>
>
>
> BYTE* pDIB = (BYTE*) AVIStreamGetFrame(pFrame, index);
>
>
>
> And I need to transfer that information into an image. I have extracted
> the bitmap header and have a pointer to the first pixel, so I suppose I
> could iterate over each pixel and channel and populate the image by
> hand, but I imagine there is a better way.
>
>
>
> Thanks,
>
> Mark Sapp
>
>
>
>
> Notice: This e-mail is intended solely for use of the individual or entity to which it is
> addressed and may contain information that is proprietary, privileged, company confidential
> and/or exempt from disclosure under applicable law. If the reader is not the intended
> recipient or agent responsible for delivering the message to the intended recipient, you are
> hereby notified that any dissemination, distribution or copying of this communication is
> strictly prohibited. If this communication has been transmitted from a U.S. location it may
> also contain data subject to the International Traffic in Arms Regulations or U.S. Export
> Administration Regulations and cannot be disseminated, distributed or copied to foreign
> nationals, residing in the U.S. or abroad, without the prior approval of the U.S. Department
> of State or appropriate export licensing authority. If you have received this communication
> in error, please notify the sender by reply e-mail or collect telephone call and delete or
> destroy all copies of this e-mail message, any physical copies made of this e-mail message
> and/or any file attachment(s).
>



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net