#include "bmp_lib.nxc"

#define WIDTH  448
#define HEIGHT 448

#define uint32_t unsigned long
#define AdvSize 1.40625           // convert 0-255 to 0-359

uint32_t gray_encode(uint32_t b)
{
    return b ^ (b >> 1);
}

uint32_t gray_decode(uint32_t g)
{
    for (uint32_t bit = 1 << 31; bit > 1; bit >>= 1)
    {
        if (g & bit) g ^= bit >> 1;
    }
    return g;
}

task main(){

  byte handle;
  handle = AcquireBitmapHandle();
  if (handle)
  {
    SetBitmapWidth(handle, WIDTH);
    SetBitmapHeight(handle, HEIGHT);
    AllocateBitmap(handle);

    int halfWidth = GetBitmapWidth(handle)/2;
    int halfHeight = GetBitmapHeight(handle)/2;
    for(float i=0; i<200; i+=0.5){
      byte gray = gray_encode(i);
      for(int ii=0; ii<8; ii++){
        if((gray >> ii) & 1){
          PointOutBitmap(handle,
                         (ii*3+43)*sind(i*AdvSize) + halfWidth,
                         (ii*3+43)*cosd(i*AdvSize) + halfHeight);
        }
      }
    }
    SaveBitmapToFile(handle, "test_image.bmp");
    ReleaseBitmapHandle(handle);
  }


  PlayTone(5000,100);
  Wait(100);
  until(ButtonPressed(BTNCENTER, false));
  until(!ButtonPressed(BTNCENTER, false));
}
