Fixed garbage trying to create a FourCC pixel format.
authorSam Lantinga <slouken@libsdl.org>
Sun, 06 Mar 2011 21:47:48 -0800
changeset 5432b3913347f1d7
parent 5431 5369d8f32f08
child 5433 1b6eee840e26
Fixed garbage trying to create a FourCC pixel format.
src/video/SDL_pixels.c
     1.1 --- a/src/video/SDL_pixels.c	Sun Mar 06 21:34:45 2011 -0800
     1.2 +++ b/src/video/SDL_pixels.c	Sun Mar 06 21:47:48 2011 -0800
     1.3 @@ -83,6 +83,12 @@
     1.4  {
     1.5      Uint32 masks[4];
     1.6  
     1.7 +    /* This function doesn't work with FourCC pixel formats */
     1.8 +    if (SDL_ISPIXELFORMAT_FOURCC(format)) {
     1.9 +        SDL_SetError("Unknown pixel format");
    1.10 +        return SDL_FALSE;
    1.11 +    }
    1.12 + 
    1.13      /* Initialize the values here */
    1.14      if (SDL_BYTESPERPIXEL(format) <= 2) {
    1.15          *bpp = SDL_BITSPERPIXEL(format);
    1.16 @@ -444,73 +450,62 @@
    1.17  int
    1.18  SDL_InitFormat(SDL_PixelFormat * format, Uint32 pixel_format)
    1.19  {
    1.20 +    int bpp;
    1.21 +    Uint32 Rmask, Gmask, Bmask, Amask;
    1.22 +    Uint32 mask;
    1.23 +
    1.24 +    if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
    1.25 +                                    &Rmask, &Gmask, &Bmask, &Amask)) {
    1.26 +        SDL_SetError("Unknown pixel format");
    1.27 +        return -1;
    1.28 +    }
    1.29 +
    1.30      /* Set up the format */
    1.31      SDL_zerop(format);
    1.32      format->format = pixel_format;
    1.33 -    format->BitsPerPixel = SDL_BITSPERPIXEL(pixel_format);
    1.34 -    format->BytesPerPixel = SDL_BYTESPERPIXEL(pixel_format);
    1.35 -    if (SDL_ISPIXELFORMAT_INDEXED(pixel_format)) {
    1.36 -        /* Palettized formats have no mask info */
    1.37 -        format->Rloss = 8;
    1.38 -        format->Gloss = 8;
    1.39 -        format->Bloss = 8;
    1.40 -        format->Aloss = 8;
    1.41 -        format->Rshift = 0;
    1.42 -        format->Gshift = 0;
    1.43 -        format->Bshift = 0;
    1.44 -        format->Ashift = 0;
    1.45 -        format->Rmask = 0;
    1.46 -        format->Gmask = 0;
    1.47 -        format->Bmask = 0;
    1.48 -        format->Amask = 0;
    1.49 -    } else {
    1.50 -        int bpp;
    1.51 -        Uint32 Rmask, Gmask, Bmask, Amask;
    1.52 -        Uint32 mask;
    1.53 +    format->BitsPerPixel = bpp;
    1.54 +    format->BytesPerPixel = (bpp + 7) / 8;
    1.55  
    1.56 -        if (!SDL_PixelFormatEnumToMasks(pixel_format, &bpp,
    1.57 -                                        &Rmask, &Gmask, &Bmask, &Amask)) {
    1.58 -            SDL_SetError("Unknown pixel format");
    1.59 -            return -1;
    1.60 -        }
    1.61 +    format->Rmask = Rmask;
    1.62 +    format->Rshift = 0;
    1.63 +    format->Rloss = 8;
    1.64 +    if (Rmask) {
    1.65 +        for (mask = Rmask; !(mask & 0x01); mask >>= 1)
    1.66 +            ++format->Rshift;
    1.67 +        for (; (mask & 0x01); mask >>= 1)
    1.68 +            --format->Rloss;
    1.69 +    }
    1.70  
    1.71 -        format->Rshift = 0;
    1.72 -        format->Rloss = 8;
    1.73 -        if (Rmask) {
    1.74 -            for (mask = Rmask; !(mask & 0x01); mask >>= 1)
    1.75 -                ++format->Rshift;
    1.76 -            for (; (mask & 0x01); mask >>= 1)
    1.77 -                --format->Rloss;
    1.78 -        }
    1.79 -        format->Gshift = 0;
    1.80 -        format->Gloss = 8;
    1.81 -        if (Gmask) {
    1.82 -            for (mask = Gmask; !(mask & 0x01); mask >>= 1)
    1.83 -                ++format->Gshift;
    1.84 -            for (; (mask & 0x01); mask >>= 1)
    1.85 -                --format->Gloss;
    1.86 -        }
    1.87 -        format->Bshift = 0;
    1.88 -        format->Bloss = 8;
    1.89 -        if (Bmask) {
    1.90 -            for (mask = Bmask; !(mask & 0x01); mask >>= 1)
    1.91 -                ++format->Bshift;
    1.92 -            for (; (mask & 0x01); mask >>= 1)
    1.93 -                --format->Bloss;
    1.94 -        }
    1.95 -        format->Ashift = 0;
    1.96 -        format->Aloss = 8;
    1.97 -        if (Amask) {
    1.98 -            for (mask = Amask; !(mask & 0x01); mask >>= 1)
    1.99 -                ++format->Ashift;
   1.100 -            for (; (mask & 0x01); mask >>= 1)
   1.101 -                --format->Aloss;
   1.102 -        }
   1.103 -        format->Rmask = Rmask;
   1.104 -        format->Gmask = Gmask;
   1.105 -        format->Bmask = Bmask;
   1.106 -        format->Amask = Amask;
   1.107 +    format->Gmask = Gmask;
   1.108 +    format->Gshift = 0;
   1.109 +    format->Gloss = 8;
   1.110 +    if (Gmask) {
   1.111 +        for (mask = Gmask; !(mask & 0x01); mask >>= 1)
   1.112 +            ++format->Gshift;
   1.113 +        for (; (mask & 0x01); mask >>= 1)
   1.114 +            --format->Gloss;
   1.115      }
   1.116 +
   1.117 +    format->Bmask = Bmask;
   1.118 +    format->Bshift = 0;
   1.119 +    format->Bloss = 8;
   1.120 +    if (Bmask) {
   1.121 +        for (mask = Bmask; !(mask & 0x01); mask >>= 1)
   1.122 +            ++format->Bshift;
   1.123 +        for (; (mask & 0x01); mask >>= 1)
   1.124 +            --format->Bloss;
   1.125 +    }
   1.126 +
   1.127 +    format->Amask = Amask;
   1.128 +    format->Ashift = 0;
   1.129 +    format->Aloss = 8;
   1.130 +    if (Amask) {
   1.131 +        for (mask = Amask; !(mask & 0x01); mask >>= 1)
   1.132 +            ++format->Ashift;
   1.133 +        for (; (mask & 0x01); mask >>= 1)
   1.134 +            --format->Aloss;
   1.135 +    }
   1.136 +
   1.137      format->palette = NULL;
   1.138      format->refcount = 1;
   1.139      format->next = NULL;