static int load_cmyk(struct jpeg_decompress_struct *cinfo, GP_Context *ret, GP_ProgressCallback *callback) { while (cinfo->output_scanline < cinfo->output_height) { uint32_t y = cinfo->output_scanline; JSAMPROW addr = (void*)GP_PIXEL_ADDR(ret, 0, y); jpeg_read_scanlines(cinfo, &addr, 1); unsigned int i; uint8_t *buf = GP_PIXEL_ADDR(ret, 0, y); for (i = 0; i < ret->w; i++) { unsigned int j = 4 * i; buf[j] = 0xff - buf[j]; buf[j+1] = 0xff - buf[j+1]; buf[j+2] = 0xff - buf[j+2]; buf[j+3] = 0xff - buf[j+3]; } if (GP_ProgressCallbackReport(callback, y, ret->h, ret->w)) { GP_DEBUG(1, "Operation aborted"); return ECANCELED; } } return 0; }
static int save_convert(struct jpeg_compress_struct *cinfo, const GP_Context *src, GP_PixelType out_pix, GP_ProgressCallback *callback) { uint8_t tmp[(src->w * GP_PixelSize(out_pix)) / 8 + 1]; GP_LineConvert Convert; Convert = GP_LineConvertGet(src->pixel_type, out_pix); while (cinfo->next_scanline < cinfo->image_height) { uint32_t y = cinfo->next_scanline; void *in = GP_PIXEL_ADDR(src, 0, y); Convert(in, tmp, src->w); JSAMPROW row = (void*)tmp; jpeg_write_scanlines(cinfo, &row, 1); if (GP_ProgressCallbackReport(callback, y, src->h, src->w)) { GP_DEBUG(1, "Operation aborted"); return ECANCELED; } } return 0; }
static int read_convert_bitmap(gp_pixmap *res, gp_progress_cb *callback, png_structp png, int passes, double gamma) { uint16_t *row; if (passes > 1) { GP_DEBUG(1, "Interlaced 16 bit PNG not supported"); return ENOSYS; } row = malloc(6 * res->w); if (!row) { GP_DEBUG(1, "Malloc failed :("); return ENOMEM; } unsigned int y; if (gamma < 0.01) gp_pixmap_set_gamma(res, 2.2); for (y = 0; y < res->h; y++) { png_read_row(png, (void*)row, NULL); uint8_t *rrow = GP_PIXEL_ADDR(res, 0, y); unsigned int x = 0; if (gamma > 0.1) { for (x = 0; x < res->w; x++) { rrow[3*x] = row[3 * x]>>8; rrow[3*x + 1] = row[3 * x + 1]>>8; rrow[3*x + 2] = row[3 * x + 2]>>8; } } else { for (x = 0; x < res->w; x++) { rrow[3*x] = gp_linear16_to_gamma8(row[3 * x]); rrow[3*x + 1] = gp_linear16_to_gamma8(row[3 * x + 1]); rrow[3*x + 2] = gp_linear16_to_gamma8(row[3 * x + 2]); } } if (gp_progress_cb_report(callback, y, res->h, res->w)) { GP_DEBUG(1, "Operation aborted"); free(row); return ECANCELED; } }
static int load(struct jpeg_decompress_struct *cinfo, GP_Context *ret, GP_ProgressCallback *callback) { while (cinfo->output_scanline < cinfo->output_height) { uint32_t y = cinfo->output_scanline; JSAMPROW addr = (void*)GP_PIXEL_ADDR(ret, 0, y); jpeg_read_scanlines(cinfo, &addr, 1); if (GP_ProgressCallbackReport(callback, y, ret->h, ret->w)) { GP_DEBUG(1, "Operation aborted"); return ECANCELED; } } return 0; }
static int save(struct jpeg_compress_struct *cinfo, const GP_Context *src, GP_ProgressCallback *callback) { while (cinfo->next_scanline < cinfo->image_height) { uint32_t y = cinfo->next_scanline; JSAMPROW row = (void*)GP_PIXEL_ADDR(src, 0, y); jpeg_write_scanlines(cinfo, &row, 1); if (GP_ProgressCallbackReport(callback, y, src->h, src->w)) { GP_DEBUG(1, "Operation aborted"); return ECANCELED; } } return 0; }
static int read_bitmap(gp_pixmap *res, gp_progress_cb *callback, png_structp png, int passes) { uint32_t y; int p; /* * The passes are needed for adam7 interlacing. */ for (p = 0; p < passes; p++) { for (y = 0; y < res->h; y++) { png_bytep row = GP_PIXEL_ADDR(res, 0, y); png_read_row(png, row, NULL); if (gp_progress_cb_report(callback, y + res->h * p, res->h * passes, res->w)) { GP_DEBUG(1, "Operation aborted"); return ECANCELED; } } } return 0; }
static void fb_wait(GP_Backend *self) { struct fb_priv *fb = GP_BACKEND_PRIV(self); struct pollfd fd = {.fd = fb->con_fd, .events = POLLIN, .revents = 0}; if (poll(&fd, 1, -1) > 0) fb_poll(self); else GP_WARN("poll(): %s", strerror(errno)); } static void fb_exit(GP_Backend *self) { struct fb_priv *fb = GP_BACKEND_PRIV(self); if (fb->flags & GP_FB_SHADOW) free(fb->context.pixels); /* unmap framebuffer */ munmap(fb->fb_mem, fb->bsize); close(fb->fb_fd); if (fb->flags & GP_FB_INPUT_KBD) exit_kbd(fb); free_console(fb); free(self); } static void fb_flip_shadow(GP_Backend *self) { struct fb_priv *fb = GP_BACKEND_PRIV(self); GP_DEBUG(2, "Flipping buffer"); memcpy(fb->fb_mem, fb->context.pixels, fb->bsize); } static void fb_update_rect_shadow(GP_Backend *self, GP_Coord x0, GP_Coord y0, GP_Coord x1, GP_Coord y1) { struct fb_priv *fb = GP_BACKEND_PRIV(self); GP_DEBUG(2, "Flipping buffer"); size_t size = ((x1 - x0) * fb->context.bpp) / 8; for (;y0 <= y1; y0++) { void *src = GP_PIXEL_ADDR(&fb->context, x0, y0); void *dst = (char*)fb->fb_mem + y0 * fb->context.bytes_per_row + (x0 * fb->context.bpp)/8; memcpy(dst, src, size); } } GP_Backend *GP_BackendLinuxFBInit(const char *path, int flags) { GP_Backend *backend; struct fb_priv *fb; struct fb_fix_screeninfo fscri; struct fb_var_screeninfo vscri; int fd; backend = malloc(sizeof(GP_Backend) + sizeof(struct fb_priv) + strlen(path) + 1); if (backend == NULL) return NULL; fb = GP_BACKEND_PRIV(backend); if (allocate_console(fb, flags)) goto err0; if (flags & GP_FB_INPUT_KBD) { if (init_kbd(fb)) goto err1; } /* open and mmap framebuffer */ GP_DEBUG(1, "Opening framebuffer '%s'", path); fd = open(path, O_RDWR); if (fd < 0) { GP_DEBUG(1, "Opening framebuffer failed: %s", strerror(errno)); goto err2; } if (ioctl(fd, FBIOGET_FSCREENINFO, &fscri) < 0) { GP_DEBUG(1, "Failed to ioctl FBIOGET_FSCREENINFO: %s", strerror(errno)); goto err3; } if (ioctl(fd, FBIOGET_VSCREENINFO, &vscri) < 0) { GP_DEBUG(1, "Failed to ioctl FBIOGET_VSCREENINFO: %s", strerror(errno)); goto err3; } GP_DEBUG(1, "Have framebufer %ix%i %s %ibpp", vscri.xres, vscri.yres, vscri.grayscale ? "Gray" : "RGB", vscri.bits_per_pixel); /* * Framebuffer is grayscale. */ if (vscri.grayscale) { GP_WARN("Grayscale not implemented!"); goto err3; } enum GP_PixelType pixel_type; pixel_type = GP_PixelRGBLookup(vscri.red.length, vscri.red.offset, vscri.green.length, vscri.green.offset, vscri.blue.length, vscri.blue.offset, vscri.transp.length, vscri.transp.offset, vscri.bits_per_pixel); if (pixel_type == GP_PIXEL_UNKNOWN) { GP_DEBUG(1, "Unknown pixel type\n"); goto err3; } if (flags & GP_FB_SHADOW) { fb->context.pixels = malloc(fscri.smem_len); if (!fb->context.pixels) { GP_DEBUG(1, "Malloc failed :("); goto err3; } } fb->fb_mem = mmap(NULL, fscri.smem_len, PROT_READ | PROT_WRITE, MAP_FILE | MAP_SHARED, fd, 0); if (fb->fb_mem == MAP_FAILED) { GP_DEBUG(1, "mmaping framebuffer failed: %s", strerror(errno)); goto err4; } fb->fb_fd = fd; fb->bsize = fscri.smem_len; strcpy(fb->path, path); fb->flags = flags; if (!(flags & GP_FB_SHADOW)) fb->context.pixels = fb->fb_mem; fb->context.w = vscri.xres; fb->context.h = vscri.yres; fb->context.axes_swap = 0; fb->context.x_swap = 0; fb->context.y_swap = 0; fb->context.bpp = vscri.bits_per_pixel; fb->context.bytes_per_row = fscri.line_length; fb->context.pixel_type = pixel_type; int shadow = flags & GP_FB_SHADOW; int kbd = flags & GP_FB_INPUT_KBD; /* update API */ backend->name = "Linux FB"; backend->context = &fb->context; backend->Flip = shadow ? fb_flip_shadow : NULL; backend->UpdateRect = shadow ? fb_update_rect_shadow : NULL; backend->Exit = fb_exit; backend->SetAttributes = NULL; backend->ResizeAck = NULL; backend->Poll = kbd ? fb_poll : NULL; backend->Wait = kbd ? fb_wait : NULL; backend->fd = fb->con_fd; backend->timers = NULL; GP_EventQueueInit(&backend->event_queue, vscri.xres, vscri.yres, 0); return backend; err4: if (flags & GP_FB_SHADOW) free(fb->context.pixels); err3: close(fd); err2: if (flags & GP_FB_INPUT_KBD) exit_kbd(fb); err1: free_console(fb); err0: free(backend); return NULL; }