#include <stdio.h>

#include "bitmap_8bit.h" 
#include "bitmap_2bit.h" 
#include "threshold.h"


Bitmap_2bit * threshold_8bit(Bitmap_8bit *source, unsigned char threshold) {
  Bitmap_2bit *result;
  int y, x;
  unsigned char value;

  result = create_bmp2bit(source->width, source->height);
  if(result == NULL) {
    perror("threshold_8bit, create_bmp2bit");
    return NULL;
  }

  for(y=0; y < source->height; y++) {
    for(x=0; x < source->width; x++) {
      value = getPixel_bmp8bit(source, x, y);
      if(value < threshold) {
	setPixel_bmp2bit(result, x, y, 0);
      }
      else {
	setPixel_bmp2bit(result, x, y, 1);
      } 
    }
  }

  return result;
}
