src/video/SDL_surface.c
changeset 3434 147d6ef5be03
parent 3103 7be21a78777e
child 3436 0538362b98d3
     1.1 --- a/src/video/SDL_surface.c	Sun Nov 15 08:01:34 2009 +0000
     1.2 +++ b/src/video/SDL_surface.c	Sun Nov 15 09:21:46 2009 +0000
     1.3 @@ -863,6 +863,109 @@
     1.4  }
     1.5  
     1.6  /*
     1.7 + * Create a surface on the stack for quick blit operations
     1.8 + */
     1.9 +static __inline__ SDL_bool
    1.10 +SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
    1.11 +                         void * pixels, int pitch, SDL_Surface * surface, 
    1.12 +                         SDL_PixelFormat * format, SDL_BlitMap * blitmap)
    1.13 +{
    1.14 +    int bpp;
    1.15 +    Uint32 Rmask, Gmask, Bmask, Amask;
    1.16 +
    1.17 +    if (!SDL_PixelFormatEnumToMasks(pixel_format,
    1.18 +                                    &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
    1.19 +        return SDL_FALSE;
    1.20 +    }
    1.21 +    if (bpp <= 8) {
    1.22 +        SDL_SetError("Indexed pixel formats not supported");
    1.23 +        return SDL_FALSE;
    1.24 +    }
    1.25 +
    1.26 +    SDL_zerop(surface);
    1.27 +    surface->flags = SDL_PREALLOC;
    1.28 +    surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
    1.29 +    surface->pixels = pixels;
    1.30 +    surface->w = width;
    1.31 +    surface->h = height;
    1.32 +    surface->pitch = pitch;
    1.33 +    /* We don't actually need to set up the clip rect for our purposes */
    1.34 +    /*SDL_SetClipRect(surface, NULL);*/
    1.35 +
    1.36 +    /* Allocate an empty mapping */
    1.37 +    SDL_zerop(blitmap);
    1.38 +    blitmap->info.r = 0xFF;
    1.39 +    blitmap->info.g = 0xFF;
    1.40 +    blitmap->info.b = 0xFF;
    1.41 +    blitmap->info.a = 0xFF;
    1.42 +    surface->map = blitmap;
    1.43 +    SDL_FormatChanged(surface);
    1.44 +
    1.45 +    /* The surface is ready to go */
    1.46 +    surface->refcount = 1;
    1.47 +    return SDL_TRUE;
    1.48 +}
    1.49 +
    1.50 +/*
    1.51 + * Copy a block of pixels of one format to another format
    1.52 + */
    1.53 +int SDL_ConvertPixels(int width, int height,
    1.54 +                      Uint32 src_format, const void * src, int src_pitch,
    1.55 +                      Uint32 dst_format, void * dst, int dst_pitch)
    1.56 +{
    1.57 +    SDL_Surface src_surface, dst_surface;
    1.58 +    SDL_PixelFormat src_fmt, dst_fmt;
    1.59 +    SDL_BlitMap src_blitmap, dst_blitmap;
    1.60 +    SDL_Rect rect;
    1.61 +
    1.62 +    /* Fast path for same format copy */
    1.63 +    if (src_format == dst_format) {
    1.64 +        int bpp;
    1.65 +
    1.66 +        if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
    1.67 +            switch (src_format) {
    1.68 +            case SDL_PIXELFORMAT_YV12:
    1.69 +            case SDL_PIXELFORMAT_IYUV:
    1.70 +            case SDL_PIXELFORMAT_YUY2:
    1.71 +            case SDL_PIXELFORMAT_UYVY:
    1.72 +            case SDL_PIXELFORMAT_YVYU:
    1.73 +                bpp = 2;
    1.74 +            default:
    1.75 +                SDL_SetError("Unknown FOURCC pixel format");
    1.76 +                return -1;
    1.77 +            }
    1.78 +        } else {
    1.79 +            bpp = SDL_BYTESPERPIXEL(src_format);
    1.80 +        }
    1.81 +        width *= bpp;
    1.82 +
    1.83 +        while (height-- > 0) {
    1.84 +            SDL_memcpy(dst, src, width);
    1.85 +            src = (Uint8*)src + src_pitch;
    1.86 +            dst = (Uint8*)dst + dst_pitch;
    1.87 +        }
    1.88 +        return SDL_TRUE;
    1.89 +    }
    1.90 +
    1.91 +    if (!SDL_CreateSurfaceOnStack(width, height, src_format, (void*)src,
    1.92 +                                  src_pitch,
    1.93 +                                  &src_surface, &src_fmt, &src_blitmap)) {
    1.94 +        return -1;
    1.95 +    }
    1.96 +    if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
    1.97 +                                  &dst_surface, &dst_fmt, &dst_blitmap)) {
    1.98 +        return -1;
    1.99 +    }
   1.100 +
   1.101 +    /* Set up the rect and go! */
   1.102 +    rect.x = 0;
   1.103 +    rect.y = 0;
   1.104 +    rect.w = width;
   1.105 +    rect.h = width;
   1.106 +    return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
   1.107 +}
   1.108 +
   1.109 +/*
   1.110   * Free a surface created by the above function.
   1.111   */
   1.112  void