void unblack (Bit2_T bitmap, int cur_x, int cur_y) { int w_pixels = Bit2_width (bitmap); int h_pixels = Bit2_height (bitmap); Seq_T point_queue = Seq_new(w_pixels*h_pixels); assert(point_queue); assert(0 <= cur_x && cur_x < w_pixels); assert(0 <= cur_y && cur_y < h_pixels); if (Bit2_get(bitmap, cur_x, cur_y) != 1) { // if pixel is white assert(point_queue); Seq_free(&point_queue); return; } else { Seq_addhi(point_queue, (void*)makePoint(cur_x,cur_y)); while (Seq_length(point_queue) > 0) { PointPair temp = (PointPair)Seq_remlo(point_queue); assert(temp); int i = temp->i; int j = temp->j; freePoint(temp); if (Bit2_get(bitmap, i, j) == 1) { // if current is black pixel Bit2_put(bitmap, i, j, 0); // set current to white assert(0 <= i && i < w_pixels); assert(0 <= j && j < h_pixels); if (j != 0 && j != h_pixels-1) { // if not a top/bottom pixel if (i+1 < w_pixels && Bit2_get(bitmap, i+1, j) == 1) { // if Seq_addhi(point_queue, (void*)makePoint(i+1,j)); } if (i > 0 && Bit2_get(bitmap, i-1, j) == 1) { Seq_addhi(point_queue, (void*)makePoint(i-1,j)); } } if (i != 0 && i != w_pixels-1) { if (j+1 < h_pixels && Bit2_get(bitmap, i, j+1) == 1) { Seq_addhi(point_queue, (void*)makePoint(i,j+1)); } if (j > 0 && Bit2_get(bitmap, i, j-1) == 1) { Seq_addhi(point_queue, (void*)makePoint(i,j-1)); } } } } assert(point_queue); Seq_free(&point_queue); return; } }
int main(int argc, char *argv[]) { (void)argc; (void)argv; Bit2_T test_array; bool OK = true; int x; test_array = Bit2_new(DIM1, DIM2); OK &= (Bit2_width(test_array) == DIM1); OK &= (Bit2_height(test_array) == DIM2); // Bit2_put(test_array, 0, 0, 1); // printf("(0, 0) = %d\n", Bit2_get(test_array, 0, 0)); // printf("(0, 1) = %d\n", Bit2_get(test_array, 1, 0)); /* Note: we are only setting a value on the corner of the array */ Bit2_put(test_array, DIM1-1, DIM2-1, MARKER); OK &= (Bit2_get(test_array, DIM1-1, DIM2-1) == MARKER); x = Bit2_put(test_array, DIM1-1, DIM2-1, 0); OK &= (x == MARKER); /* hint: put returns previous value */ Bit2_put(test_array, DIM1-1, DIM2-1, MARKER); /* for map test */ printf("Trying column major\n"); Bit2_map_col_major(test_array, check_and_print, &OK); printf("Trying row major\n"); Bit2_map_row_major(test_array, check_and_print, &OK); Bit2_free(&test_array); printf("The array is %sOK!\n", (OK ? "" : "NOT ")); }
Bit2_T pbmread( FILE *input) { Pnmrdr_T r = Pnmrdr_new(input); Pnmrdr_mapdata data = Pnmrdr_data(r); assert(data.type == Pnmrdr_bit); int w_pixels = data.width; int h_pixels = data.height; assert(w_pixels > 0 && h_pixels > 0); Bit2_T bitmap; bitmap = Bit2_new(w_pixels, h_pixels); assert (bitmap); for ( int j = 0; j < h_pixels; j++) { for (int i = 0; i < w_pixels; i++) { int pixel = Pnmrdr_get(r); Bit2_put(bitmap, i, j, pixel); } } Pnmrdr_free(&r); return bitmap; }