Improved (optional) main function to choose parameters based on the file extension.

This commit is contained in:
rahra 2016-01-15 14:36:30 +01:00
parent f96da76566
commit b6b0c5f3d1

View file

@ -136,9 +136,11 @@ cairo_status_t cairo_image_surface_write_to_jpeg_mem(cairo_surface_t *sfc, unsig
cinfo.image_width = cairo_image_surface_get_width(sfc);
cinfo.image_height = cairo_image_surface_get_height(sfc);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
cinfo.in_color_space = JCS_EXT_BGRX;
//cinfo.in_color_space = JCS_EXT_BGRX;
cinfo.in_color_space = cairo_image_surface_get_format(sfc) == CAIRO_FORMAT_ARGB32 ? JCS_EXT_BGRA : JCS_EXT_BGRX;
#else
cinfo.in_color_space = JCS_EXT_XRGB;
//cinfo.in_color_space = JCS_EXT_XRGB;
cinfo.in_color_space = cairo_image_surface_get_format(sfc) == CAIRO_FORMAT_ARGB32 ? JCS_EXT_ARGB : JCS_EXT_XRGB;
#endif
cinfo.input_components = 4;
jpeg_set_defaults(&cinfo);
@ -447,6 +449,14 @@ cairo_surface_t *cairo_image_surface_create_from_jpeg(const char *filename)
#ifdef CAIRO_JPEG_MAIN
#include <string.h>
int strrcasecmp(const char *s1, const char *s2)
{
size_t off = strlen(s1) - strlen(s2);
return strcasecmp(s1 + (off < 0 ? 0 : off), s2);
}
/*! Main routine, only for testing. #undef CAIRO_JPEG_MAIN or simply delete
* this part if you link this file to your own program.
*/
@ -455,45 +465,55 @@ int main(int argc, char **argv)
cairo_surface_t *sfc;
#ifndef CAIRO_JPEG_TEST_SIMILAR
if (argc < 4)
if (argc < 3)
{
fprintf(stderr, "usage: %s <'r' | 'w'> <infile> <outfile>\n", argv[0]);
fprintf(stderr, "usage: %s <infile> <outfile>\n", argv[0]);
return 1;
}
// check for write mode
if (*argv[1] == 'w')
// test input file type and read file
if (!strrcasecmp(argv[1], ".png"))
{
// read PNG file
sfc = cairo_image_surface_create_from_png(argv[2]);
if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
{
fprintf(stderr, "error loading image: %s", cairo_status_to_string(cairo_surface_status(sfc)));
return 2;
}
// write JPEG file
cairo_image_surface_write_to_jpeg(sfc, argv[3], 90);
cairo_surface_destroy(sfc);
sfc = cairo_image_surface_create_from_png(argv[1]);
}
// check for read mode
else if (*argv[1] == 'r')
else if (!strrcasecmp(argv[1], ".jpg"))
{
// read JPEG file
sfc = cairo_image_surface_create_from_jpeg(argv[2]);
if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
{
fprintf(stderr, "error loading image: %s", cairo_status_to_string(cairo_surface_status(sfc)));
return 3;
}
// write PNG file
cairo_surface_write_to_png(sfc, argv[3]);
cairo_surface_destroy(sfc);
sfc = cairo_image_surface_create_from_jpeg(argv[1]);
}
else
{
fprintf(stderr, "1st argument must be one of 'r' or 'w'.\n");
fprintf(stderr, "source file is neither JPG nor PNG\n");
return 1;
}
// check surface status
if (cairo_surface_status(sfc) != CAIRO_STATUS_SUCCESS)
{
fprintf(stderr, "error loading image: %s", cairo_status_to_string(cairo_surface_status(sfc)));
return 2;
}
// test output file type and write file
if (!strrcasecmp(argv[2], ".png"))
{
// write PNG file
cairo_surface_write_to_png(sfc, argv[2]);
}
else if (!strrcasecmp(argv[2], ".jpg"))
{
// write JPEG file
cairo_image_surface_write_to_jpeg(sfc, argv[2], 90);
}
else
{
fprintf(stderr, "destination file is neither JPG nor PNG\n");
return 1;
}
cairo_surface_destroy(sfc);
#else
sfc = cairo_pdf_surface_create("xyz.pdf", 595.276, 841.890);