| 1 |
/* |
| 2 |
* render.c - rendering a split image into a bitmap |
| 3 |
*/ |
| 4 |
|
| 5 |
#include "../base/mdjvucfg.h" |
| 6 |
#include <minidjvu/minidjvu.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <string.h> |
| 9 |
|
| 10 |
MDJVU_IMPLEMENT mdjvu_bitmap_t mdjvu_render(mdjvu_image_t img) |
| 11 |
{ |
| 12 |
int32 width = mdjvu_image_get_width (img); |
| 13 |
int32 height = mdjvu_image_get_height(img); |
| 14 |
unsigned char **b = mdjvu_create_2d_array(width, height); |
| 15 |
unsigned char *row_buffer = (unsigned char *) malloc(width); |
| 16 |
int32 blit_count = mdjvu_image_get_blit_count(img); |
| 17 |
int32 i; |
| 18 |
mdjvu_bitmap_t result = mdjvu_bitmap_create(width, height); |
| 19 |
|
| 20 |
/* Fill the canvas with white */ |
| 21 |
for (i = 0; i < height; i++) |
| 22 |
memset(b[i], 0, width); |
| 23 |
|
| 24 |
/* Render the split image blit by blit */ |
| 25 |
for (i = 0; i < blit_count; i++) |
| 26 |
{ |
| 27 |
int32 x = mdjvu_image_get_blit_x(img, i); |
| 28 |
int32 y = mdjvu_image_get_blit_y(img, i); |
| 29 |
mdjvu_bitmap_t current_bitmap = mdjvu_image_get_blit_bitmap(img, i); |
| 30 |
int32 w = mdjvu_bitmap_get_width(current_bitmap); |
| 31 |
int32 h = mdjvu_bitmap_get_height(current_bitmap); |
| 32 |
|
| 33 |
/* Now w and h are dimensions of the current shape, |
| 34 |
* and width and height are dimensions of the whole image. |
| 35 |
*/ |
| 36 |
|
| 37 |
int32 min_col = x >= 0 ? 0 : - x; |
| 38 |
int32 max_col_plus_one = x + w <= width ? w : width - x; |
| 39 |
|
| 40 |
int32 min_row = y >= 0 ? 0 : - y; |
| 41 |
int32 max_row_plus_one = y + h <= height ? h : height - y; |
| 42 |
|
| 43 |
int32 row; |
| 44 |
|
| 45 |
/* Render the current blit row by row */ |
| 46 |
for (row = min_row; row < max_row_plus_one; row++) |
| 47 |
{ |
| 48 |
int32 col; |
| 49 |
unsigned char *target = b[y + row] + x; |
| 50 |
mdjvu_bitmap_unpack_row(current_bitmap, row_buffer, row); |
| 51 |
for (col = min_col; col < max_col_plus_one; col++) |
| 52 |
target[col] |= row_buffer[col]; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
free(row_buffer); |
| 57 |
|
| 58 |
/* Convert 2D array to a Bitmap and return it */ |
| 59 |
mdjvu_bitmap_pack_all(result, b); |
| 60 |
mdjvu_destroy_2d_array(b); |
| 61 |
return result; |
| 62 |
} |