src/video/SDL_surface.c
changeset 3434 147d6ef5be03
parent 3103 7be21a78777e
child 3436 0538362b98d3
equal deleted inserted replaced
3433:ad845d9835aa 3434:147d6ef5be03
   861     /* We're ready to go! */
   861     /* We're ready to go! */
   862     return (convert);
   862     return (convert);
   863 }
   863 }
   864 
   864 
   865 /*
   865 /*
       
   866  * Create a surface on the stack for quick blit operations
       
   867  */
       
   868 static __inline__ SDL_bool
       
   869 SDL_CreateSurfaceOnStack(int width, int height, Uint32 pixel_format,
       
   870                          void * pixels, int pitch, SDL_Surface * surface, 
       
   871                          SDL_PixelFormat * format, SDL_BlitMap * blitmap)
       
   872 {
       
   873     int bpp;
       
   874     Uint32 Rmask, Gmask, Bmask, Amask;
       
   875 
       
   876     if (!SDL_PixelFormatEnumToMasks(pixel_format,
       
   877                                     &bpp, &Rmask, &Gmask, &Bmask, &Amask)) {
       
   878         return SDL_FALSE;
       
   879     }
       
   880     if (bpp <= 8) {
       
   881         SDL_SetError("Indexed pixel formats not supported");
       
   882         return SDL_FALSE;
       
   883     }
       
   884 
       
   885     SDL_zerop(surface);
       
   886     surface->flags = SDL_PREALLOC;
       
   887     surface->format = SDL_InitFormat(format, bpp, Rmask, Gmask, Bmask, Amask);
       
   888     surface->pixels = pixels;
       
   889     surface->w = width;
       
   890     surface->h = height;
       
   891     surface->pitch = pitch;
       
   892     /* We don't actually need to set up the clip rect for our purposes */
       
   893     /*SDL_SetClipRect(surface, NULL);*/
       
   894 
       
   895     /* Allocate an empty mapping */
       
   896     SDL_zerop(blitmap);
       
   897     blitmap->info.r = 0xFF;
       
   898     blitmap->info.g = 0xFF;
       
   899     blitmap->info.b = 0xFF;
       
   900     blitmap->info.a = 0xFF;
       
   901     surface->map = blitmap;
       
   902     SDL_FormatChanged(surface);
       
   903 
       
   904     /* The surface is ready to go */
       
   905     surface->refcount = 1;
       
   906     return SDL_TRUE;
       
   907 }
       
   908 
       
   909 /*
       
   910  * Copy a block of pixels of one format to another format
       
   911  */
       
   912 int SDL_ConvertPixels(int width, int height,
       
   913                       Uint32 src_format, const void * src, int src_pitch,
       
   914                       Uint32 dst_format, void * dst, int dst_pitch)
       
   915 {
       
   916     SDL_Surface src_surface, dst_surface;
       
   917     SDL_PixelFormat src_fmt, dst_fmt;
       
   918     SDL_BlitMap src_blitmap, dst_blitmap;
       
   919     SDL_Rect rect;
       
   920 
       
   921     /* Fast path for same format copy */
       
   922     if (src_format == dst_format) {
       
   923         int bpp;
       
   924 
       
   925         if (SDL_ISPIXELFORMAT_FOURCC(src_format)) {
       
   926             switch (src_format) {
       
   927             case SDL_PIXELFORMAT_YV12:
       
   928             case SDL_PIXELFORMAT_IYUV:
       
   929             case SDL_PIXELFORMAT_YUY2:
       
   930             case SDL_PIXELFORMAT_UYVY:
       
   931             case SDL_PIXELFORMAT_YVYU:
       
   932                 bpp = 2;
       
   933             default:
       
   934                 SDL_SetError("Unknown FOURCC pixel format");
       
   935                 return -1;
       
   936             }
       
   937         } else {
       
   938             bpp = SDL_BYTESPERPIXEL(src_format);
       
   939         }
       
   940         width *= bpp;
       
   941 
       
   942         while (height-- > 0) {
       
   943             SDL_memcpy(dst, src, width);
       
   944             src = (Uint8*)src + src_pitch;
       
   945             dst = (Uint8*)dst + dst_pitch;
       
   946         }
       
   947         return SDL_TRUE;
       
   948     }
       
   949 
       
   950     if (!SDL_CreateSurfaceOnStack(width, height, src_format, (void*)src,
       
   951                                   src_pitch,
       
   952                                   &src_surface, &src_fmt, &src_blitmap)) {
       
   953         return -1;
       
   954     }
       
   955     if (!SDL_CreateSurfaceOnStack(width, height, dst_format, dst, dst_pitch,
       
   956                                   &dst_surface, &dst_fmt, &dst_blitmap)) {
       
   957         return -1;
       
   958     }
       
   959 
       
   960     /* Set up the rect and go! */
       
   961     rect.x = 0;
       
   962     rect.y = 0;
       
   963     rect.w = width;
       
   964     rect.h = width;
       
   965     return SDL_LowerBlit(&src_surface, &rect, &dst_surface, &rect);
       
   966 }
       
   967 
       
   968 /*
   866  * Free a surface created by the above function.
   969  * Free a surface created by the above function.
   867  */
   970  */
   868 void
   971 void
   869 SDL_FreeSurface(SDL_Surface * surface)
   972 SDL_FreeSurface(SDL_Surface * surface)
   870 {
   973 {