File: imagetosurface.cpp

package info (click to toggle)
openmw 0.49.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,992 kB
  • sloc: cpp: 372,479; xml: 2,149; sh: 1,403; python: 797; makefile: 26
file content (30 lines) | stat: -rw-r--r-- 1,025 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "imagetosurface.hpp"

#include <SDL_surface.h>
#include <osg/Image>

namespace SDLUtil
{

    SurfaceUniquePtr imageToSurface(osg::Image* image, bool flip)
    {
        int width = image->s();
        int height = image->t();
        SDL_Surface* surface
            = SDL_CreateRGBSurface(0, width, height, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF);

        for (int x = 0; x < width; ++x)
            for (int y = 0; y < height; ++y)
            {
                osg::Vec4f clr = image->getColor(x, flip ? ((height - 1) - y) : y);
                int bpp = surface->format->BytesPerPixel;
                Uint8* p = (Uint8*)surface->pixels + y * surface->pitch + x * bpp;
                *(Uint32*)(p)
                    = SDL_MapRGBA(surface->format, static_cast<Uint8>(clr.r() * 255), static_cast<Uint8>(clr.g() * 255),
                        static_cast<Uint8>(clr.b() * 255), static_cast<Uint8>(clr.a() * 255));
            }

        return SurfaceUniquePtr(surface, SDL_FreeSurface);
    }

}