Jump to content

Bit-Plane Shuffler


Recommended Posts

This effect essentially about swapping binary numbers or swapping bitplanes of image.

 

Wikipedia Link for bit plane - https://en.wikipedia.org/wiki/Bit_plane

 

Preview of Effect:

 

unknown.png

Plugin Download Link - BitPlaneShuffler.zip

 

Instruction:

1) Install

2) Find under Stylize.

 

Source Code:

------------------

License : CeCiLL v2.0 - https://cecill.info/licences/Licence_CeCILL_V2-en.html

 

CodeLab:

Spoiler
// Name: Bit-Plane Shuffler
// Submenu: Stylize
// Author: Reptorian
// Title: Bit-Plane Shuffler
// Version: 1
// Desc: Shuffle Bit-Planes
// Keywords:
// URL:
// Help:
#region UICode
IntSliderControl index = 100; // [1,40320] Index
ListBoxControl direction = 1; // Bit-Plane Shuffle Direction|Backward|Forward
CheckboxControl use_alpha = true; // Alpha Mode
#endregion
#if DEBUG
#endif

int old_index=0;
bool old_reverse_mode=false;
int[] factorials=new int[] { 5040, 720, 120, 24, 6, 2, 1, 1 };
int[] order = new int[8];
int[] reverse_order = new int[8];

ColorBgra bitplane_shuffle_without_alpha (int r, int g, int b,int a) {
    int nr,ng,nb;

    nr=0;
    ng=0;
    nb=0;


    if (direction==1)
    {
        for ( int p = 0 ; p < 8 ; ++p)
        {
            nr += ((r >> p)&1)<<order[p];
            ng += ((g >> p)&1)<<order[p];
            nb += ((b >> p)&1)<<order[p];
        }
    }
    else 
    {
        for ( int p = 0 ; p < 8 ; ++p)
        {
            nr += ((r >> p)&1)<<reverse_order[p];
            ng += ((g >> p)&1)<<reverse_order[p];
            nb += ((b >> p)&1)<<reverse_order[p];
        }
    }

    return ColorBgra.FromBgra((byte)(nb),(byte)(ng),(byte)(nr),(byte)(a));
}

ColorBgra bitplane_shuffle_use_alpha (int r, int g, int b,int a) {
    int nr,ng,nb,na;

    nr=0;
    ng=0;
    nb=0;
    na=0;


    if (direction==1)
    {
        for ( int p = 0 ; p < 8 ; ++p)
        {
            nr += ((r >> p)&1)<<order[p];
            ng += ((g >> p)&1)<<order[p];
            nb += ((b >> p)&1)<<order[p];
            na += ((a >> p)&1)<<order[p];
        }
    }
    else 
    {
        for ( int p = 0 ; p < 8 ; ++p)
        {
            nr += ((r >> p)&1)<<reverse_order[p];
            ng += ((g >> p)&1)<<reverse_order[p];
            nb += ((b >> p)&1)<<reverse_order[p];
            na += ((a >> p)&1)<<reverse_order[p];
        }
    }

    return ColorBgra.FromBgra((byte)(nb),(byte)(ng),(byte)(nr),(byte)(na));
}

void PreRender(Surface dst, Surface src)
{
    int division_number,pos,index_permutation;

    if (index != old_index)
    {
        List<int> list_of_num = new List <int>{ 0, 1, 2, 3, 4, 5, 6, 7};

        index_permutation=index - 1;

        for (int iter=0 ; iter < 7 ; ++iter ) 
        {
            if (IsCancelRequested) return;
            division_number=factorials[iter];
            pos=index_permutation/division_number;
            order[iter]=list_of_num[pos];
            reverse_order[pos]=iter;
            list_of_num.RemoveAt(pos);
            index_permutation%=division_number;
        }

        order[7]=list_of_num[0];

        for (int p=0 ; p < 8 ; ++p)
        {
            reverse_order[order[p]]=p;
        }
    }

    old_index=index;
}

void Render(Surface dst, Surface src, Rectangle rect)
{

    int process_r,process_g,process_b,process_a;

    ColorBgra currentPixel;

    if (index != 1) {
        for (int y = rect.Top; y < rect.Bottom; y++)
        {
            if (IsCancelRequested) return;
            for (int x = rect.Left; x < rect.Right; x++)
            {
                currentPixel=src[x,y];
                process_r=currentPixel.R;
                process_g=currentPixel.G;
                process_b=currentPixel.B;
                process_a=currentPixel.A;
                if (use_alpha) {dst[x,y] = bitplane_shuffle_use_alpha(process_r,process_g,process_b,process_a);}
                else {dst[x,y] = bitplane_shuffle_without_alpha(process_r,process_g,process_b,process_a);}
                

            }
        }
    }
    else {
        for (int y = rect.Top; y < rect.Bottom; y++)
        {
            if (IsCancelRequested) return;
            for (int x = rect.Left; x < rect.Right; x++)
            {
                dst[x,y]=src[x,y];
            }

        }
    }
}

 

G'MIC:

Spoiler
#@cli rep_bitplane_shuffle: direction={ 0=backward | 1=forward },index_0...index_inf
#@cli : Bit Plane shuffle according to the order of index specified by user.
rep_bitplane_shuffle:
num_of_ind,direction={$#-1},{$1&1}
whole_numbers=[{expr('x',$num_of_ind)}]
order=[${2--1}]

if $#==2 error insuf_args fi
if sort($order)!=$whole_numbers error inv_ind_args fi
if $order==$whole_numbers return fi

if !$direction order=[${rep_inverse_permutation\ ${2--1}}] fi

f ":"sum((i>>$whole_numbers&1)<<$order);
#@cli rep_extract_permutation_order: number_of_item,permutation_number
#@cli : Return permutation order at index permutation_number.
rep_extract_permutation_order:

num_of_items={max(1,int(abs($1)))}

if $num_of_items==1 u 0
else

 permutation_number={$2%fact($num_of_items)}

 1

 eval "
  const num_of_items=$num_of_items;

  da_push(#-1,"{expr('x',$num_of_items)}");
  new_arr=vector(#num_of_items,0);

  fn=num_of_items-1;

  permutation_position=$permutation_number;

  repeat(fn,iter,
   division_number=fact(fn);
   pos=int(permutation_position/division_number);
   new_arr_num=i[#-1,pos];
   new_arr[iter]=new_arr_num;
   da_remove(#-1,pos);
   permutation_position%=division_number;
   --fn;
  );

  new_arr[iter]=i[#-1,0];

  new_arr;"

  rm.

fi

 

 

Edited by Reptillian
  • Like 1
  • Upvote 3

G'MIC Filter Developer

Link to comment
Share on other sites

Good job @Reptillian!  👍  I've gotten some very interesting textures using this on scenic images and playing with layer blend modes.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...