TDB_EXPORT tdb_error tdb_cons_finalize(tdb_cons *cons) { struct tdb_file items_mmapped; uint64_t num_events = cons->events.next; int ret = 0; memset(&items_mmapped, 0, sizeof(struct tdb_file)); /* finalize event items */ if ((ret = arena_flush(&cons->items))) goto done; if (cons->items.fd && fclose(cons->items.fd)) { cons->items.fd = NULL; ret = TDB_ERR_IO_CLOSE; goto done; } cons->items.fd = NULL; if (cons->tempfile[0]){ if (num_events && cons->num_ofields) { if (file_mmap(cons->tempfile, NULL, &items_mmapped, NULL)){ ret = TDB_ERR_IO_READ; goto done; } } TDB_TIMER_DEF TDB_TIMER_START if ((ret = store_lexicons(cons))) goto done; TDB_TIMER_END("encoder/store_lexicons") TDB_TIMER_START if ((ret = store_uuids(cons))) goto done; TDB_TIMER_END("encoder/store_uuids") TDB_TIMER_START if ((ret = store_version(cons))) goto done; TDB_TIMER_END("encoder/store_version") TDB_TIMER_START if ((ret = tdb_encode(cons, (const tdb_item*)items_mmapped.data))) goto done; TDB_TIMER_END("encoder/encode") } done: if (items_mmapped.ptr) munmap(items_mmapped.ptr, items_mmapped.mmap_size); if (cons->tempfile[0]) unlink(cons->tempfile); if (!ret){ #ifdef HAVE_ARCHIVE_H if (cons->output_format == TDB_OPT_CONS_OUTPUT_FORMAT_PACKAGE) ret = cons_package(cons); #endif } return ret; }
int cavan_font_load_bmp(struct cavan_font *font, const char *bmp, int lines) { int fd; int ret; size_t size; struct bmp_file_header *file_hdr; struct bmp_info_header *info_hdr; struct bmp_header *header; fd = file_mmap(bmp, (void **) &header, &size, 0); if (fd < 0) { pr_red_info("file_mmap"); return fd; } file_hdr = &header->file_hdr; info_hdr = &header->info_hdr; bmp_show_file_header(file_hdr); bmp_show_info_header(info_hdr); font->name = "BMP"; font->lines = lines; font->width = info_hdr->width; font->height = info_hdr->height; font->cwidth = font->width / 96; font->cheight = info_hdr->height / lines; font->rundata = NULL; font->rundata_size = 0; font->body = NULL; ret = cavan_font_init(font); if (ret < 0) { pr_red_info("cavan_font_init"); goto out_file_unmap; } switch (info_hdr->bit_count) { case 8: { const u8 *p, *file_end; byte *body = font->body + font->width * font->height; struct bmp_color_table_entry *colors = (struct bmp_color_table_entry *) (header + 1); p = ((u8 *) header) + file_hdr->offset; file_end = p + font->width * font->height; while (1) { const u8 *line_end = p + font->width; if (line_end > file_end) { break; } body -= font->width; while (p < line_end) { struct bmp_color_table_entry *color = colors + (*p); if (color->red > 128 && color->green > 128 && color->blue > 128) { *body = 0xFF; } else { *body = 0; } p++; body++; } body -= font->width; } } break; case 16: { const u16 *p, *file_end; byte *body = font->body + font->width * font->height; p = (u16 *) (((byte *) header) + file_hdr->offset); file_end = p + font->width * font->height; while (1) { const u16 *line_end = p + font->width; if (line_end > file_end) { break; } body -= font->width; while (p < line_end) { if (*p) { *body = 0xFF; } else { *body = 0; } p++; body++; } body -= font->width; } } break; case 24: { const u8 *p, *file_end; byte *body = font->body + font->width * font->height; p = (u8 *) (((byte *) header) + file_hdr->offset); file_end = p + font->width * font->height * 3; while (1) { const u8 *line_end = p + font->width * 3; if (line_end > file_end) { break; } body -= font->width; while (p < line_end) { int brightness; brightness = cavan_display_cal_brightness(p[0], p[1], p[2]); if (brightness > 64) { *body = 0xFF; } else { *body = 0; } p += 3; body++; } body -= font->width; } } break; case 32: { const u8 *p, *file_end; byte *body = font->body + font->width * font->height; p = (u8 *) (((byte *) header) + file_hdr->offset); file_end = p + font->width * font->height * 4; while (1) { const u8 *line_end = p + font->width * 4; if (line_end > file_end) { break; } body -= font->width; while (p < line_end) { if (p[0] > 128 && p[1] > 128 && p[2] > 128) { *body = 0xFF; } else { *body = 0; } p += 4; body++; } body -= font->width; } } break; default: ret = -EINVAL; pr_red_info("unknown bit_count = %d", info_hdr->bit_count); goto out_cavan_font_deinit; } file_unmap(fd, header, size); return 0; out_cavan_font_deinit: cavan_font_deinit(font); out_file_unmap: file_unmap(fd, header, size); return ret; }
int cavan_font_save_bmp(struct cavan_font *font, const char *pathname, int bit_count) { int fd; int ret; size_t size; size_t color_table_size; struct bmp_header *header; color_table_size = bmp_get_color_table_size(bit_count); size = sizeof(*header) + font->width * font->height * bit_count / 8; size += color_table_size * sizeof(struct bmp_color_table_entry); fd = file_mmap(pathname, (void **) &header, &size, O_RDWR | O_CREAT | O_TRUNC); if (fd < 0) { pr_red_info("file_mmap"); return fd; } bmp_header_init(header, font->width, font->height, bit_count); switch (bit_count) { case 8: { u8 *pixel; const byte *body; struct bmp_color_table_entry *colors = (struct bmp_color_table_entry *) (header + 1); colors[0].red = colors[0].green = colors[0].blue = 0x00; colors[1].red = colors[1].green = colors[1].blue = 0xFF; colors[0].reserved = colors[1].reserved = 0x00; pixel = (u8 *) (colors + color_table_size); for (body = font->body + font->width * (font->height - 1); body >= font->body; body -= font->width * 2) { const byte *body_end; for (body_end = body + font->width; body < body_end; body++, pixel++) { *pixel = (*body) == 0 ? 0 : 1; } } } break; case 16: { u16 *pixel = (u16 *) (header + 1); const byte *body; for (body = font->body + font->width * (font->height - 1); body >= font->body; body -= font->width * 2) { const byte *body_end; for (body_end = body + font->width; body < body_end; body++, pixel++) { *pixel = (*body) == 0 ? 0x0000 : 0xFFFF; } } } break; case 24: { byte *pixel = (byte *) (header + 1); const byte *body; for (body = font->body + font->width * (font->height - 1); body >= font->body; body -= font->width * 2) { const byte *body_end; for (body_end = body + font->width; body < body_end; body++, pixel += 3) { pixel[0] = pixel[1] = pixel[2] = (*body) == 0 ? 0x00 : 0xFF; } } } break; case 32: { u32 *pixel = (u32 *) (header + 1); const byte *body; for (body = font->body + font->width * (font->height - 1); body >= font->body; body -= font->width * 2) { const byte *body_end; for (body_end = body + font->width; body < body_end; body++, pixel++) { *pixel = (*body) == 0 ? 0x00000000 : 0xFFFFFFFF; } } } break; default: pr_red_info("unknown bit_count = %d", bit_count); ret = -EINVAL; goto out_file_unmap; } ret = 0; out_file_unmap: file_unmap(fd, header, size); return ret; }
int mmap(int fd, void *addr) { return file_mmap(fd,(void *)addr); }