Pillow-6.0.0/src/libImaging/QuantOctree.c
/* expand or shrink a given cube to level */
expand or shrink a given cube to level
expand or shrink a given cube to level
static ColorCube copy_color_cube(const ColorCube cube,
   int rBits, int gBits, int bBits, int aBits)
{
   unsigned int r, g, b, a;
   long src_pos, dst_pos;
   unsigned int src_reduce[4] = {0}, dst_reduce[4] = {0};
   unsigned int width[4];
   ColorCube result;
   result = new_color_cube(rBits, gBits, bBits, aBits);
   if (!result) return NULL;
   if (cube->rBits > rBits) {
      dst_reduce[0] = cube->rBits - result->rBits;
      width[0] = cube->rWidth;
   } else {
      src_reduce[0] = result->rBits - cube->rBits;
      width[0] = result->rWidth;
   }
   if (cube->gBits > gBits) {
      dst_reduce[1] = cube->gBits - result->gBits;
      width[1] = cube->gWidth;
   } else {
      src_reduce[1] = result->gBits - cube->gBits;
      width[1] = result->gWidth;
   }
   if (cube->bBits > bBits) {
      dst_reduce[2] = cube->bBits - result->bBits;
      width[2] = cube->bWidth;
   } else {
      src_reduce[2] = result->bBits - cube->bBits;
      width[2] = result->bWidth;
   }
   if (cube->aBits > aBits) {
      dst_reduce[3] = cube->aBits - result->aBits;
      width[3] = cube->aWidth;
   } else {
      src_reduce[3] = result->aBits - cube->aBits;
      width[3] = result->aWidth;
   }
   for (r=0; r<width[0]; r++) {
      for (g=0; g<width[1]; g++) {
         for (b=0; b<width[2]; b++) {
            for (a=0; a<width[3]; a++) {
               src_pos = color_bucket_offset_pos(cube,
                                               r>>src_reduce[0],
                                               g>>src_reduce[1],
                                               b>>src_reduce[2],
                                               a>>src_reduce[3]);
               dst_pos = color_bucket_offset_pos(result,
                                               r>>dst_reduce[0],
                                               g>>dst_reduce[1],
                                               b>>dst_reduce[2],
                                               a>>dst_reduce[3]);
               add_bucket_values(
                  &cube->buckets[src_pos],
                  &result->buckets[dst_pos]
               );
            }
         }
      }
   }
   return result;
}
static ColorCube copy_color_cube ( const ColorCube cube , int rBits , int gBits , int bBits , int aBits ) { unsigned int r , g , b , a ; long src_pos , dst_pos ; unsigned int src_reduce [ 4 ] = { 0 } , dst_reduce [ 4 ] = { 0 } ; unsigned int width [ 4 ] ; ColorCube result ; result = new_color_cube ( rBits , gBits , bBits , aBits ) ; if ( ! result ) return NULL ; if ( cube -> rBits > rBits ) { dst_reduce [ 0 ] = cube -> rBits - result -> rBits ; width [ 0 ] = cube -> rWidth ; } else { src_reduce [ 0 ] = result -> rBits - cube -> rBits ; width [ 0 ] = result -> rWidth ; } if ( cube -> gBits > gBits ) { dst_reduce [ 1 ] = cube -> gBits - result -> gBits ; width [ 1 ] = cube -> gWidth ; } else { src_reduce [ 1 ] = result -> gBits - cube -> gBits ; width [ 1 ] = result -> gWidth ; } if ( cube -> bBits > bBits ) { dst_reduce [ 2 ] = cube -> bBits - result -> bBits ; width [ 2 ] = cube -> bWidth ; } else { src_reduce [ 2 ] = result -> bBits - cube -> bBits ; width [ 2 ] = result -> bWidth ; } if ( cube -> aBits > aBits ) { dst_reduce [ 3 ] = cube -> aBits - result -> aBits ; width [ 3 ] = cube -> aWidth ; } else { src_reduce [ 3 ] = result -> aBits - cube -> aBits ; width [ 3 ] = result -> aWidth ; } for ( r = 0 ; r < width [ 0 ] ; r ++ ) { for ( g = 0 ; g < width [ 1 ] ; g ++ ) { for ( b = 0 ; b < width [ 2 ] ; b ++ ) { for ( a = 0 ; a < width [ 3 ] ; a ++ ) { src_pos = color_bucket_offset_pos ( cube , r >> src_reduce [ 0 ] , g >> src_reduce [ 1 ] , b >> src_reduce [ 2 ] , a >> src_reduce [ 3 ] ) ; dst_pos = color_bucket_offset_pos ( result , r >> dst_reduce [ 0 ] , g >> dst_reduce [ 1 ] , b >> dst_reduce [ 2 ] , a >> dst_reduce [ 3 ] ) ; add_bucket_values ( & cube -> buckets [ src_pos ] , & result -> buckets [ dst_pos ] ) ; } } } } return result ; }
expand or shrink a given cube to level
expand or shrink a given cube to level