static int file_to_fb(const char * srcpath) { int ret = -1; BMP_READ * bmp = NULL; struct FB * fb = NULL; int sw, sh; int srcbpp, dstbpp; void * pdata = NULL, * bmpdata = NULL; RGB_CONVERT_FUN convert_func = NULL; do { bmp = bmp_open(srcpath); if (!bmp) { break; } fb = fb_create(0); if (!fb) { break; } sw = bmp_width(bmp); sh = bmp_height(bmp); bmpdata = bmp_data(bmp); srcbpp = bmp_bpp(bmp); dstbpp = fb_bpp(fb); convert_func = get_convert_func(srcbpp, dstbpp); if (convert_func) { pdata = convert_func(bmpdata, sw, sh); bmpdata = pdata; } if (!bmp_forward(bmp)) { line_reversal(bmpdata, sw, sh, dstbpp); } rgb_copy(bmpdata, fb_bits(fb), sw, sh, fb_width(fb), fb_height(fb), dstbpp); ret = 0; } while (0); fb_destory(fb); bmp_close(bmp); if (pdata) { free(pdata); } return ret; }
static int file_to_file(const char * srcpath, const char * dstpath, int output_rgb) { int ret = -1; BMP_READ * bmp = NULL; int w, h; int srcbpp, dstbpp; void * pdata = NULL, * bmpdata = NULL; RGB_CONVERT_FUN convert_func = NULL; do { bmp = bmp_open(srcpath); if (!bmp) { break; } w = bmp_width(bmp); h = bmp_height(bmp); bmpdata = bmp_data(bmp); srcbpp = bmp_bpp(bmp); dstbpp = g_rgbbpp[output_rgb]; convert_func = get_convert_func(srcbpp, dstbpp); if (convert_func) { pdata = convert_func(bmpdata, w, h); bmpdata = pdata; } if (!bmp_forward(bmp)) { line_reversal(bmpdata, w, h, dstbpp); } ret = save_bmp(dstpath, w, h, bmpdata, dstbpp); } while (0); bmp_close(bmp); if (pdata) { free(pdata); } return ret; }
/* * Copy a tile from our tile image, loaded previously, into tile RAM. In the * case of tiles with a width or height larger than 8 pixels, this will result * in multiple tile positions being occupied in video RAM. */ void nds_load_graphics_tile(int tile_idx, coord_t coords) { int glyph = map->glyphs[coords.y][coords.x]; int i, j, x; int width, height, bpp, row_bytes; int bmp_tile_x, bmp_tile_y; u8 *bmp_row_start; u16 *tile_row_start; /* Next, get a few fields from the BMP headers that we'll need. */ width = bmp_width(&tiles); height = bmp_height(&tiles); bpp = bmp_bpp(&tiles); /* * Compute the number of bytes in each row of 8-pixel tiles. * * This works explicitely because each tile component is 8 pixels wide. * Thus, the number of bytes in a row is (8 * (bpp / 8)), which just * ends up being bpp. */ row_bytes = tile_width_in_tiles * bpp; /* Now calculate the pointer which points to the start of the BMP row */ bmp_tile_y = glyph2tile[glyph] / width_in_tiles; bmp_tile_x = width_in_tiles - glyph2tile[glyph] % width_in_tiles; bmp_row_start = tiles.bitmap + tiles.bitmap_length - bmp_tile_y * TILE_HEIGHT * width_in_tiles * row_bytes - bmp_tile_x * row_bytes ; for (j = 0; j < tile_height_in_tiles; j++) { int y; for (y = 0; y < 8; y++) { for (i = 0; i < tile_width_in_tiles; i++) { tile_row_start = tile_ram + (tile_idx + j * tile_width_in_tiles + i) * bpp * 8 / 2 + y * bpp / 2; /* Again, this works because 8 * (bpp / 8) == bpp */ for (x = 0; x < bpp; x += 2, bmp_row_start += 2) { if (bpp == 4) { u16 a, b, c, d; a = (bmp_row_start[0] & 0xF0) >> 4; b = (bmp_row_start[0] & 0x0F); c = (bmp_row_start[1] & 0xF0) >> 4; d = (bmp_row_start[1] & 0x0F); tile_row_start[x / 2] = (d << 12) | (c << 8) | (b << 4) | (a << 0); } else { tile_row_start[x / 2] = (bmp_row_start[1] << 8) | bmp_row_start[0]; } } } bmp_row_start -= row_bytes * width_in_tiles + row_bytes; }