Oyranos Color Management System API - Version 0.9.6
image2pdf.c

Combine images into a PDF using Cairo.

# include <stddef.h>
#define __USE_POSIX2 1
#include <stdio.h> /* popen() */
FILE *popen ( const char *__command, const char *__modes);
#include <math.h>
#include <string.h>
#include <oyConversion_s.h>
#include <oyProfile_s.h> /* Oyranos headers */
#include <cairo.h> /* Cairo headers */
#include <cairo-pdf.h>
#define MIN(a,b) ((a<b)?(a):(b))
#define MAX(a,b) ((a>b)?(a):(b))
int main (int argc, char ** argv)
{
int result = 0;
int i,j,o, error;
cairo_t * cr = 0;
cairo_surface_t * surface = NULL, * image_surf = NULL;
cairo_status_t status;
double page_w = 210.0, /* page width in mm */
page_h = 297.0,
resolution = 72.0, /* Cairo PDF surface resolution */
scale = 1.0,
frame = 0;
int pixel_w, pixel_h, /* page size in pixel */
x,y,w=0,h=0, /* image dimensions */
to_moni = 0;
size_t size = 0;
unsigned char * image_data = 0,
rgba[4] = {127,127,127,255};
oyProfile_s * monitor, * print, * output;
oyConversion_s * to_output = 0;
oyConfig_s * device = 0;
uint32_t icc_profile_flags = 0;
if(argc < 2)
{
printf("Merge some CamerRAW images into one output image and use\n");
printf("Oyranos CMS settings to obtain the result.\n");
printf("\n");
printf("Usage of the image2pdf example application:");
printf(" To obtain a PDF (test.pdf):\n");
printf(" image2pdf imageA.raw imageB.raw\n");
printf(" To obtain a monitor preview (test.png):\n");
printf(" image2pdf --monitor imageA.raw imageB.raw\n");
return 1;
}
o = 1;
if(strcmp(argv[o],"--monitor") == 0 || strcmp(argv[o],"-m") == 0)
{
++o;
to_moni = 1;
resolution = 96;
}
pixel_w = page_w / 25.4 * resolution;
pixel_h = page_h / 25.4 * resolution;
/* create a surface to place our images on */
if(to_moni)
surface = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, pixel_w,pixel_h);
else
surface = cairo_pdf_surface_create( "test.pdf",
pixel_w,
pixel_h );
status = cairo_surface_status( surface );
if(status) return 1;
/* select profiles matching actual capabilities */
"//" OY_TYPE_STD "/icc_color", NULL, 0 );
/* The monitor profile is located in the Xserver. For details see:
* http://www.freedesktop.org/wiki/Specifications/icc_profiles_in_x_spec
*/
error = oyDeviceGet( OY_TYPE_STD, "monitor", 0, 0,
&device );
error = oyDeviceGetProfile( device, 0, &monitor );
if(error > 0)
fprintf(stderr, "oyDeviceGetProfile error: %d\n", error);
printf("monitor: %s\n", oyProfile_GetText( monitor, oyNAME_DESCRIPTION ) );
/* The output profile is equal to sRGB, as output profiles are curently not
* supported in Cairo.
*/
print = oyProfile_FromStd( oyASSUMED_WEB, icc_profile_flags, 0 );
printf("print: %s\n", oyProfile_GetText( print, oyNAME_DESCRIPTION ));
cr = cairo_create( surface );
cairo_set_source_rgba( cr, rgba[0]/255., rgba[1]/255., rgba[2]/255., 1.0 );
cairo_rectangle( cr, 0, 0, pixel_w, pixel_h );
cairo_fill( cr );
for ( i=0; i < argc-o; ++i )
{
const char * filename = argv[i+o];
oyOptions_s * options = NULL;
oyImage_s * in = NULL, * out = NULL;
error = oyImage_FromFile( filename, icc_profile_flags, &in, NULL );
w = oyImage_GetWidth( in );
h = oyImage_GetHeight( in );
/* create a Cairo image */
image_surf = cairo_image_surface_create( CAIRO_FORMAT_ARGB32, w, h );
status = cairo_surface_status( image_surf ); if(status) return 1;
/* write our dcraw stream on the Cairo image */
image_data = cairo_image_surface_get_data( image_surf );
size = w*h;
/* build the color context */
if( to_moni )
{
output = oyProfile_Copy( monitor, 0 );
} else
{
output = oyProfile_Copy( print, 0 );
}
out = oyImage_Create( w, h,
image_data,
output,
0 );
/* create a processing graph to convert from one image to an other */
to_output = oyConversion_CreateBasicPixels( in, out, options, 0 );
/* tell Oyranos to apply defaults */
oyConversion_Correct( to_output, "//" OY_TYPE_STD "/icc_color",
/* transform colors */
oyConversion_RunPixels( to_output, NULL );
oyConversion_Release( &to_output );
/*oyImage_WritePPM( out, "test_out.ppm", "out" );*/
oyImage_Release( &out );
/* Cairo uses a Blue Green Red Alpha channel layout */
#pragma omp parallel for
for(j = 0; j < size; ++j)
{
unsigned char t = image_data[j*4+2];
image_data[j*4+2] = image_data[j*4+0];
/*image_data[j*4+1] = 0;*/
image_data[j*4+0] = t;
image_data[j*4+3] = 255;
}
cairo_surface_mark_dirty( image_surf );
/* place our images on a sheet */
if(argc-o > 1)
{
/* place in contact sheed style */
scale = (pixel_w - pixel_w/10.0)/4.0/(double)MAX(w,h);
cairo_save( cr );
x = i%4 * (pixel_w - pixel_w/20.0)/4.0 + pixel_w/30.0;
y = i/4 * (pixel_w - pixel_w/20.0)/4.0
+ ((pixel_w - pixel_w/20.0)/4.0 - MIN(w,h)*scale)/2.0
+ pixel_w/30.0;
} else {
/* single image */
scale = (pixel_w - pixel_w/10.0)/(double)MAX(w,h);
x = pixel_w/20.0;
y = ((pixel_w - pixel_w/20.0) - MIN(w,h)*scale)/2.0
+ pixel_w/20.0;
}
/* draw a frame around the image */
frame = pixel_w/20.0 * scale;
cairo_set_source_rgba( cr, .0, .0, .0, 1.0);
cairo_set_line_width (cr, 1.);
cairo_rectangle( cr, x - frame, y - frame,
w*scale + 2*frame, h*scale + 2*frame);
cairo_stroke(cr);
/* draw the image */
cairo_translate( cr, x, y );
cairo_scale( cr, scale, scale );
cairo_set_source_surface( cr, image_surf, 0,0 );
cairo_paint( cr );
cairo_restore( cr );
/* small clean */
cairo_surface_destroy( image_surf );
}
if(to_moni)
cairo_surface_write_to_png( surface, "test.png" );
cairo_surface_finish( surface );
/* clean */
cairo_surface_destroy( surface );
oyProfile_Release( &monitor );
oyProfile_Release( &print );
return result;
}
oyChannels_m
#define oyChannels_m(c)
Definition: oyranos_image.h:135
OY_TYPE_STD
#define OY_TYPE_STD
Definition: oyranos_definitions.h:118
oyImage_s::oyImage_GetWidth
int oyImage_GetWidth(oyImage_s *image)
Get the width in pixel.
Definition: oyImage_s.c:1426
oyNAME_DESCRIPTION
@ oyNAME_DESCRIPTION
Definition: oyranos_core.h:75
oyConfig_s
A group of options for a device.
Definition: oyConfig_s.h:66
oyConversion_s::oyConversion_Correct
int oyConversion_Correct(oyConversion_s *conversion, const char *registration, uint32_t flags, oyOptions_s *options)
Check for correctly adhering to policies.
Definition: oyConversion_s.c:143
oyranos_devices.h
oyProfile_s::oyProfile_Release
OYAPI int OYEXPORT oyProfile_Release(oyProfile_s **profile)
release and possibly deallocate a oyProfile_s object
Definition: oyProfile_s.c:89
oyICCProfileSelectionFlagsFromOptions
uint32_t oyICCProfileSelectionFlagsFromOptions(const char *db_base_key, const char *base_pattern, oyOptions_s *options, int select_core)
Get valid profile selection flags from node options and fallbacks.
Definition: oyranos_devices.c:3323
oyOPTIONATTRIBUTE_ADVANCED
@ oyOPTIONATTRIBUTE_ADVANCED
Definition: oyranos_object.h:258
oyProfile_s::oyProfile_Copy
oyProfile_Copy
Copy or Reference a Profile object.
oyImage_s::oyImage_GetHeight
int oyImage_GetHeight(oyImage_s *image)
Get the width in pixel.
Definition: oyImage_s.c:1446
oyUINT8
@ oyUINT8
Definition: oyranos_image.h:44
oyOptions_s
generic Options
Definition: oyOptions_s.h:80
oyConversion_s::oyConversion_Release
OYAPI int OYEXPORT oyConversion_Release(oyConversion_s **conversion)
release and possibly deallocate a oyConversion_s object
Definition: oyConversion_s.c:93
oyProfile_s::oyProfile_GetText
OYAPI const oyChar *OYEXPORT oyProfile_GetText(oyProfile_s *profile, oyNAME_e type)
Get a presentable name.
Definition: oyProfile_s.c:1315
oyImage_s::oyImage_Create
oyImage_s * oyImage_Create(int width, int height, oyPointer pixels, oyPixel_t pixel_layout, oyProfile_s *profile, oyObject_s object)
collect infos about a image
Definition: oyImage_s.c:335
oyDataType_m
#define oyDataType_m(t)
Definition: oyranos_image.h:139
oyProfile_s.h
oyConversion_s::oyConversion_RunPixels
int oyConversion_RunPixels(oyConversion_s *conversion, oyPixelAccess_s *pixel_access)
Iterate over a conversion graph.
Definition: oyConversion_s.c:668
oyASSUMED_WEB
@ oyASSUMED_WEB
Definition: oyranos.h:206
oyConversion_s.h
oyImage_s::oyImage_FromFile
int oyImage_FromFile(const char *file_name, int icc_profile_flags, oyImage_s **image, oyObject_s obj)
generate a Oyranos image from a file name
Definition: oyImage_s.c:1894
oyProfile_s
A profile and its attributes.
Definition: oyProfile_s.h:95
oyDeviceGetProfile
OYAPI int OYEXPORT oyDeviceGetProfile(oyConfig_s *device, oyOptions_s *options, oyProfile_s **profile)
order a device profile
Definition: oyranos_devices.c:805
oyDeviceGet
OYAPI int OYEXPORT oyDeviceGet(const char *device_type, const char *device_class, const char *device_name, oyOptions_s *options, oyConfig_s **device)
ask a module for device informations or other direct calls
Definition: oyranos_devices.c:181
oyImage_s
A reference struct to gather information for image transformation.
Definition: oyImage_s.h:215
oyImage_s::oyImage_Release
OYAPI int OYEXPORT oyImage_Release(oyImage_s **image)
release and possibly deallocate a oyImage_s object
Definition: oyImage_s.c:82
oyConversion_s::oyConversion_CreateBasicPixels
oyConversion_s * oyConversion_CreateBasicPixels(oyImage_s *input, oyImage_s *output, oyOptions_s *options, oyObject_s object)
Allocate initialise a basic oyConversion_s object.
Definition: oyConversion_s.c:231
oyProfile_s::oyProfile_FromStd
OYAPI oyProfile_s *OYEXPORT oyProfile_FromStd(oyPROFILE_e type, uint32_t flags, oyObject_s object)
Create from default color space settings.
Definition: oyProfile_s.c:123
oyProfile_s::oyProfile_GetChannelsCount
OYAPI int OYEXPORT oyProfile_GetChannelsCount(oyProfile_s *profile)
Number of channels in a color space.
Definition: oyProfile_s.c:818
oyConversion_s
A filter chain or graph to manipulate a image.
Definition: oyConversion_s.h:184
OY_CMM_STD
#define OY_CMM_STD
Definition: oyranos_definitions.h:131