|
|
@@ -119,11 +119,79 @@ end: |
|
|
|
|
|
|
|
int pipi_save_jpeg(pipi_image_t *img, const char *name) |
|
|
|
{ |
|
|
|
return 0; |
|
|
|
int quality = 75; /* FIXME */ |
|
|
|
|
|
|
|
struct jpeg_compress_struct cinfo; |
|
|
|
struct jpeg_error_mgr jerr; |
|
|
|
unsigned char *data = NULL; |
|
|
|
unsigned char *line = NULL; |
|
|
|
|
|
|
|
pipi_pixels_t *pixels = pipi_getpixels(img, PIPI_PIXELS_RGBA_C); |
|
|
|
|
|
|
|
if (!pixels) |
|
|
|
return 0; |
|
|
|
|
|
|
|
data = pixels->pixels; |
|
|
|
|
|
|
|
|
|
|
|
line = malloc(img->w * 3 * sizeof(unsigned char)); |
|
|
|
if (!line) |
|
|
|
return 0; |
|
|
|
|
|
|
|
FILE *fp = fopen(name, "wb"); |
|
|
|
if (!fp) { |
|
|
|
free(line); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
jerr.error_exit = error_msg; |
|
|
|
jerr.emit_message = emit_msg; |
|
|
|
jerr.output_message = output_msg; |
|
|
|
|
|
|
|
cinfo.err = jpeg_std_error(&(jerr)); |
|
|
|
|
|
|
|
jpeg_create_compress(&cinfo); |
|
|
|
jpeg_stdio_dest(&cinfo, fp); |
|
|
|
cinfo.image_width = img->w; |
|
|
|
cinfo.image_height = img->h; |
|
|
|
cinfo.input_components = 3; |
|
|
|
cinfo.in_color_space = JCS_RGB; |
|
|
|
|
|
|
|
jpeg_set_defaults(&cinfo); |
|
|
|
jpeg_set_quality(&cinfo, quality, TRUE); |
|
|
|
jpeg_start_compress(&cinfo, TRUE); |
|
|
|
|
|
|
|
int j, i, y = 0; |
|
|
|
uint32_t *ptr = (uint32_t*)data; |
|
|
|
JSAMPROW *jbuf; |
|
|
|
|
|
|
|
while (cinfo.next_scanline < cinfo.image_height) |
|
|
|
{ |
|
|
|
for (j = 0, i = 0; i < img->w; i++) |
|
|
|
{ |
|
|
|
line[j++] = ((*ptr) >> 16) & 0xff; |
|
|
|
line[j++] = ((*ptr) >> 8) & 0xff; |
|
|
|
line[j++] = ((*ptr)) & 0xff; |
|
|
|
ptr++; |
|
|
|
} |
|
|
|
/* write scanline */ |
|
|
|
jbuf = (JSAMPROW *) (&line); |
|
|
|
jpeg_write_scanlines(&cinfo, jbuf, 1); |
|
|
|
y++; |
|
|
|
} |
|
|
|
|
|
|
|
jpeg_finish_compress(&cinfo); |
|
|
|
jpeg_destroy_compress(&cinfo); |
|
|
|
free(line); |
|
|
|
fclose(fp); |
|
|
|
|
|
|
|
return 1; |
|
|
|
} |
|
|
|
|
|
|
|
static int pipi_free_jpeg(pipi_image_t *img) |
|
|
|
{ |
|
|
|
if(img->p[PIPI_PIXELS_RGBA_C].pixels) |
|
|
|
free(img->p[PIPI_PIXELS_RGBA_C].pixels); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|