Code factorization of the pixel format permutation
authorSylvain Becker <sylvain.becker@gmail.com>
Thu, 07 Feb 2019 21:49:24 +0100
changeset 126047a32748258d4
parent 12603 0e163b7a3a2d
child 12605 f1374904c775
Code factorization of the pixel format permutation
src/video/SDL_blit_N.c
     1.1 --- a/src/video/SDL_blit_N.c	Thu Feb 07 18:52:49 2019 +0100
     1.2 +++ b/src/video/SDL_blit_N.c	Thu Feb 07 21:49:24 2019 +0100
     1.3 @@ -2149,6 +2149,71 @@
     1.4      }
     1.5  }
     1.6  
     1.7 +/* permutation for mapping srcfmt to dstfmt, overloading or not the alpha channel */
     1.8 +static void
     1.9 +get_permutation(SDL_PixelFormat *srcfmt, SDL_PixelFormat *dstfmt, 
    1.10 +        int *_r , int *_g, int *_b, int *_a, int *_missing) 
    1.11 +{
    1.12 +    int missing = 0, r, g, b, a = 0;
    1.13 +    int Pixel = 0x04030201; /* identity permutation */
    1.14 +
    1.15 +    if (srcfmt->Amask) {
    1.16 +        RGBA_FROM_PIXEL(Pixel, srcfmt, r, g, b, a);
    1.17 +    } else {
    1.18 +        RGB_FROM_PIXEL(Pixel, srcfmt, r, g, b);
    1.19 +    }
    1.20 +    
    1.21 +    if (dstfmt->Amask) {
    1.22 +        if (srcfmt->Amask) {
    1.23 +            PIXEL_FROM_RGBA(Pixel, dstfmt, r, g, b, a);
    1.24 +        } else {
    1.25 +            PIXEL_FROM_RGBA(Pixel, dstfmt, r, g, b, 0);
    1.26 +        }
    1.27 +    } else {
    1.28 +        PIXEL_FROM_RGB(Pixel, dstfmt, r, g, b);
    1.29 +    }
    1.30 +
    1.31 +    r = Pixel & 0xFF;
    1.32 +    g = (Pixel >> 8) & 0xFF;
    1.33 +    b = (Pixel >> 16) & 0xFF;
    1.34 +    a = (Pixel >> 24) & 0xFF;
    1.35 +
    1.36 +    {
    1.37 +        int val;
    1.38 +        for (val = 0; val <= 3; val++) {
    1.39 +            if (r != val && g != val && b != val && a != val) {
    1.40 +                missing = val;
    1.41 +                break;
    1.42 +            }
    1.43 +        }
    1.44 +    }
    1.45 +
    1.46 +    if (r == 0) {
    1.47 +        r = missing;
    1.48 +        missing = 0;
    1.49 +    } else if (g == 0) {
    1.50 +        g = missing;
    1.51 +        missing = 1;
    1.52 +    } else if (b == 0) {
    1.53 +        b = missing;
    1.54 +        missing = 2;
    1.55 +    } else if (a == 0) {
    1.56 +        a = missing;
    1.57 +        missing = 3;
    1.58 +    }
    1.59 +
    1.60 +    *_r = r - 1;
    1.61 +    *_g = g - 1;
    1.62 +    *_b = b - 1;
    1.63 +    *_a = a - 1;
    1.64 +
    1.65 +    if (missing) {
    1.66 +        *_missing = missing;
    1.67 +    }
    1.68 +    return;
    1.69 +}
    1.70 +
    1.71 +
    1.72  static void
    1.73  BlitNtoN(SDL_BlitInfo * info)
    1.74  {
    1.75 @@ -2174,45 +2239,8 @@
    1.76  
    1.77          /* Find the appropriate permutation */
    1.78          int missing = 0, r, g, b, a;
    1.79 -        int Pixel = 0x04030201; /* +1 */
    1.80 -        RGB_FROM_PIXEL(Pixel, srcfmt, r, g, b);
    1.81 -
    1.82 -        if (dstfmt->Amask) {
    1.83 -            PIXEL_FROM_RGBA(Pixel, dstfmt, r, g, b, 0);
    1.84 -        } else {
    1.85 -            PIXEL_FROM_RGB(Pixel, dstfmt, r, g, b);
    1.86 -        }
    1.87 -        r = Pixel & 0xFF;
    1.88 -        g = (Pixel >> 8) & 0xFF;
    1.89 -        b = (Pixel >> 16) & 0xFF;
    1.90 -        a = (Pixel >> 24) & 0xFF;
    1.91 -
    1.92 -        {
    1.93 -            int val;
    1.94 -            for (val = 0; val <= 3; val++) {
    1.95 -                if (r != val && g != val && b != val && a != val) {
    1.96 -                    missing = val;
    1.97 -                }
    1.98 -            }
    1.99 -        }
   1.100 -
   1.101 -        if (r == 0) {
   1.102 -            r = missing;
   1.103 -            missing = 0;
   1.104 -        } else if (g == 0) {
   1.105 -            g = missing;
   1.106 -            missing = 1;
   1.107 -        } else if (b == 0) {
   1.108 -            b = missing;
   1.109 -            missing = 2;
   1.110 -        } else if (a == 0) {
   1.111 -            a = missing;
   1.112 -            missing = 3;
   1.113 -        }
   1.114 -
   1.115 -        /* -1 */
   1.116 -        r -= 1; g -= 1; b -= 1; a -= 1;
   1.117 -
   1.118 +        get_permutation(srcfmt, dstfmt, &r, &g, &b, &a, &missing);
   1.119 +        
   1.120          while (height--) {
   1.121              /* *INDENT-OFF* */
   1.122              DUFFS_LOOP(
   1.123 @@ -2456,41 +2484,12 @@
   1.124  
   1.125          Uint32 *src32 = (Uint32*)src;
   1.126          Uint32 *dst32 = (Uint32*)dst;
   1.127 -        
   1.128 + 
   1.129 +        /* Find the appropriate permutation */
   1.130 +        int missing = 0, r, g, b, a;
   1.131 +        get_permutation(srcfmt, dstfmt, &r, &g, &b, &a, &missing);
   1.132 +       
   1.133          if (dstfmt->Amask) {
   1.134 -            /* Find the appropriate permutation */
   1.135 -            int missing = 0, r, g, b, a;
   1.136 -            int Pixel = 0x03020100;
   1.137 -            RGB_FROM_PIXEL(Pixel, srcfmt, r, g, b);
   1.138 -            PIXEL_FROM_RGBA(Pixel, dstfmt, r, g, b, 255);
   1.139 -            r = Pixel & 0xFF;
   1.140 -            g = (Pixel >> 8) & 0xFF;
   1.141 -            b = (Pixel >> 16) & 0xFF;
   1.142 -            a = (Pixel >> 24) & 0xFF;
   1.143 -
   1.144 -            {
   1.145 -                int val;
   1.146 -                for (val = 0; val <= 3; val++) {
   1.147 -                    if (r != val && g != val && b != val && a != val) {
   1.148 -                        missing = val;
   1.149 -                    }
   1.150 -                }
   1.151 -            }
   1.152 -
   1.153 -            if (r == 255) {
   1.154 -                r = missing;
   1.155 -                missing = 0;
   1.156 -            } else if (g == 255) {
   1.157 -                g = missing;
   1.158 -                missing = 1;
   1.159 -            } else if (b == 255) {
   1.160 -                b = missing;
   1.161 -                missing = 2;
   1.162 -            } else if (a == 255) {
   1.163 -                a = missing;
   1.164 -                missing = 3;
   1.165 -            }
   1.166 -
   1.167              while (height--) {
   1.168                  /* *INDENT-OFF* */
   1.169                  DUFFS_LOOP(
   1.170 @@ -2514,34 +2513,6 @@
   1.171          
   1.172              return;
   1.173          } else {
   1.174 -            /* Find the appropriate permutation */
   1.175 -            int missing = 0, r, g, b, a;
   1.176 -            int Pixel = 0x04030201; /* +1 */
   1.177 -            RGBA_FROM_PIXEL(Pixel, srcfmt, r, g, b, a);
   1.178 -            missing = a;
   1.179 -            PIXEL_FROM_RGB(Pixel, dstfmt, r, g, b);
   1.180 -            r = Pixel & 0xFF;
   1.181 -            g = (Pixel >> 8) & 0xFF;
   1.182 -            b = (Pixel >> 16) & 0xFF;
   1.183 -            a = (Pixel >> 24) & 0xFF;
   1.184 -
   1.185 -            if (r == 0) {
   1.186 -                r = missing;
   1.187 -                missing = 0;
   1.188 -            } else if (g == 0) {
   1.189 -                g = missing;
   1.190 -                missing = 1;
   1.191 -            } else if (b == 0) {
   1.192 -                b = missing;
   1.193 -                missing = 2;
   1.194 -            } else if (a == 0) {
   1.195 -                a = missing;
   1.196 -                missing = 3;
   1.197 -            }
   1.198 -        
   1.199 -            /* -1 */
   1.200 -            r -= 1; g -= 1; b -= 1; a -= 1;
   1.201 -
   1.202              while (height--) {
   1.203                  /* *INDENT-OFF* */
   1.204                  DUFFS_LOOP(
   1.205 @@ -2716,13 +2687,7 @@
   1.206  
   1.207          /* Find the appropriate permutation */
   1.208          int r, g, b, a;
   1.209 -        Pixel = 0x03020100;
   1.210 -        RGBA_FROM_PIXEL(Pixel, srcfmt, r, g, b, a);
   1.211 -        PIXEL_FROM_RGBA(Pixel, dstfmt, r, g, b, a);
   1.212 -        r = Pixel & 0xFF;
   1.213 -        g = (Pixel >> 8) & 0xFF;
   1.214 -        b = (Pixel >> 16) & 0xFF;
   1.215 -        a = (Pixel >> 24) & 0xFF;
   1.216 +        get_permutation(srcfmt, dstfmt, &r, &g, &b, &a, NULL);
   1.217  
   1.218          while (height--) {
   1.219              /* *INDENT-OFF* */