Skip to content

Commit

Permalink
Add automatic scaling by an integer factor
Browse files Browse the repository at this point in the history
  • Loading branch information
henryk committed Jun 24, 2015
1 parent 3cf2d3b commit 880bc8e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 15 deletions.
56 changes: 41 additions & 15 deletions src/card_layer.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ CardLayer *card_layer_create(GRect frame) {
layer_set_update_proc(card_layer->layer, background_update_proc);
*(CardLayer **)layer_get_data(card_layer->layer) = card_layer;

card_layer->name_text_layer = text_layer_create(GRect(0, 0, PEBBLE_WIDTH, 22)); // TODO: Fix magic number
card_layer->name_text_layer = text_layer_create(GRect(0, 0, PEBBLE_WIDTH, NAME_LAYER_HEIGHT));
text_layer_set_background_color(card_layer->name_text_layer, GColorBlack);
text_layer_set_font(card_layer->name_text_layer, fonts_get_system_font(FONT_KEY_GOTHIC_18_BOLD));
text_layer_set_overflow_mode(card_layer->name_text_layer, GTextOverflowModeTrailingEllipsis);
Expand All @@ -43,7 +43,7 @@ CardLayer *card_layer_create(GRect frame) {
text_layer_set_text_alignment(card_layer->value_text_layer, GTextAlignmentCenter);
text_layer_set_text_color(card_layer->value_text_layer, GColorBlack);
layer_add_child(card_layer->layer, (Layer *)card_layer->value_text_layer);

return card_layer;
}

Expand All @@ -69,6 +69,25 @@ static void draw_barcode_matrix(CardLayer *card_layer, GContext* ctx) {

int16_t img_pixels = card_layer->barcode_width * card_layer->barcode_height;

// name_text_layer and pager_layer need to be subtracted from the pebble height
// as does twice the WHITE_BORDER
const int16_t MAX_HEIGHT = PEBBLE_HEIGHT - NAME_LAYER_HEIGHT - PAGER_LAYER_HEIGHT;

// The mid point between the black border at the top (name_layer) and at the bottom (pager_layer)
// is not exactly in the middle of the screen
const int16_t MID_HEIGHT = (PEBBLE_HEIGHT - NAME_LAYER_HEIGHT - PAGER_LAYER_HEIGHT) / 2 + NAME_LAYER_HEIGHT;

// Non-linear barcodes are scaled to save persistent storage space. Dynamically calculate
// the maximum integer scaling factor, keep a white border of 2 units around.

const int16_t SCALING_FACTOR_X = PEBBLE_WIDTH / (card_layer->barcode_width + 2 + 2);
const int16_t SCALING_FACTOR_Y = MAX_HEIGHT / (card_layer->barcode_height + 2 + 2);

// actual scaling factor is the least of x and y scaling, but at least 1
const int16_t SCALING_FACTOR = MAX( MIN(SCALING_FACTOR_X, SCALING_FACTOR_Y), 1);

APP_LOG(APP_LOG_LEVEL_DEBUG, "image size %ix%i, scaling by %i", card_layer->barcode_width, card_layer->barcode_height, SCALING_FACTOR);

// The comparison part of this loop adds 7 to the barcode width to allow C
// to ceil the byte count. Since the server will always pad incomplete bytes
// with 0, this is reasonably safe.
Expand All @@ -80,15 +99,15 @@ static void draw_barcode_matrix(CardLayer *card_layer, GContext* ctx) {

for (int8_t p = 0; p < 8; p++) {
if (card_layer->barcode_data[current_byte] & (1 << p)) {
// Non-linear barcodes are scaled 2x to save persistent storage space.

point_x = ( PEBBLE_WIDTH/2 - card_layer->barcode_width ) + raw_x * 2;
point_y = ( PEBBLE_HEIGHT/2 - card_layer->barcode_height ) + raw_y * 2;

graphics_draw_pixel(ctx, GPoint(point_x, point_y));
graphics_draw_pixel(ctx, GPoint(point_x + 1, point_y));
graphics_draw_pixel(ctx, GPoint(point_x + 1, point_y + 1));
graphics_draw_pixel(ctx, GPoint(point_x, point_y + 1));
for(int8_t s_x = 0; s_x < SCALING_FACTOR; s_x++) {
for(int8_t s_y = 0; s_y < SCALING_FACTOR; s_y++) {
point_x = ( PEBBLE_WIDTH/2 - (SCALING_FACTOR * card_layer->barcode_width)/2 ) + SCALING_FACTOR*raw_x + s_x;
point_y = ( MID_HEIGHT - (SCALING_FACTOR * card_layer->barcode_height)/2 ) + SCALING_FACTOR*raw_y + s_y;

graphics_draw_pixel(ctx, GPoint(point_x, point_y));
}
}
}

raw_x++;
Expand All @@ -107,6 +126,10 @@ static void draw_barcode_linear(CardLayer *card_layer, GContext* ctx) {

int16_t img_pixels = card_layer->barcode_width;

// Try to do an integer scale if possible, keep white border of 2 units on each side
const int16_t SCALING_FACTOR_X = PEBBLE_WIDTH / (card_layer->barcode_width + 2 + 2);
const int16_t SCALING_FACTOR = MAX( SCALING_FACTOR_X, 1);

// The comparison part of this loop adds 7 to the barcode width to allow C
// to ceil the byte count. Since the server will always pad incomplete bytes
// with 0, this is reasonably safe.
Expand All @@ -118,11 +141,14 @@ static void draw_barcode_linear(CardLayer *card_layer, GContext* ctx) {

for (int16_t current_pixel = 0; current_pixel < 8; current_pixel++) {
if (card_layer->barcode_data[current_byte] & (1 << current_pixel)) {
for (int16_t current_vertical = 0; current_vertical < card_layer->barcode_height; current_vertical++) {
point_x = ( PEBBLE_WIDTH / 2 - card_layer->barcode_width / 2 ) + raw_x;
point_y = ( PEBBLE_HEIGHT / 2 - card_layer->barcode_height / 2 ) + current_vertical;

graphics_draw_pixel(ctx, GPoint(point_x, point_y));
for(int8_t s_x = 0; s_x < SCALING_FACTOR; s_x++) {
point_x = ( PEBBLE_WIDTH/2 - (SCALING_FACTOR * card_layer->barcode_width)/2 ) + SCALING_FACTOR*raw_x + s_x;

for (int16_t current_vertical = 0; current_vertical < card_layer->barcode_height; current_vertical++) {
point_y = ( PEBBLE_HEIGHT / 2 - card_layer->barcode_height / 2 ) + current_vertical;

graphics_draw_pixel(ctx, GPoint(point_x, point_y));
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#define PEBBLE_HEIGHT 168
#define PEBBLE_WIDTH 144
#define STATUS_HEIGHT 16
#define NAME_LAYER_HEIGHT 22
#define PAGER_LAYER_HEIGHT 30

#define IMG_HEADER_OFFSET 3
#define IMG_BIT_SIZE 8
Expand Down

0 comments on commit 880bc8e

Please sign in to comment.