int GP_FilterTablesInit(GP_FilterTables *self, const GP_Context *ctx) { unsigned int i; const GP_PixelTypeDescription *desc; GP_DEBUG(2, "Allocating tables for pixel %s", GP_PixelTypeName(ctx->pixel_type)); for (i = 0; i < GP_PIXELTYPE_MAX_CHANNELS; i++) self->table[i] = NULL; desc = GP_PixelTypeDesc(ctx->pixel_type); for (i = 0; i < desc->numchannels; i++) { self->table[i] = create_table(&desc->channels[i]); if (!self->table[i]) { free_tables(self); return 1; } } self->free_table = 0; return 0; }
int GP_WriteJPG(const GP_Context *src, GP_IO *io, GP_ProgressCallback *callback) { struct jpeg_compress_struct cinfo; GP_PixelType out_pix; struct my_jpg_err my_err; struct my_dest_mgr dst; uint8_t buf[1024]; int err; GP_DEBUG(1, "Writing JPG Image to I/O (%p)", io); out_pix = GP_LineConvertible(src->pixel_type, out_pixel_types); if (out_pix == GP_PIXEL_UNKNOWN) { GP_DEBUG(1, "Unsupported pixel type %s", GP_PixelTypeName(src->pixel_type)); errno = ENOSYS; return 1; } if (setjmp(my_err.setjmp_buf)) { errno = EIO; return 1; } cinfo.err = jpeg_std_error(&my_err.error_mgr); my_err.error_mgr.error_exit = my_error_exit; jpeg_create_compress(&cinfo); init_dest_mgr(&dst, io, buf, sizeof(buf)); cinfo.dest = (void*)&dst; cinfo.image_width = src->w; cinfo.image_height = src->h; switch (out_pix) { case GP_PIXEL_BGR888: cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; break; case GP_PIXEL_G8: cinfo.input_components = 1; cinfo.in_color_space = JCS_GRAYSCALE; break; default: GP_BUG("Don't know how to set color_space and compoments"); } jpeg_set_defaults(&cinfo); jpeg_start_compress(&cinfo, TRUE); if (out_pix != src->pixel_type) err = save_convert(&cinfo, src, out_pix, callback); else err = save(&cinfo, src, callback); if (err) { jpeg_destroy_compress(&cinfo); errno = err; return 1; } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); GP_ProgressCallbackDone(callback); return 0; }