summaryrefslogtreecommitdiff
path: root/src/cairo/cdcairoirgb.c
blob: e5f2c2af543fdb8f6af76d46e1fbe9b4b5c239bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/** \file
 * \brief Cairo IMAGERGB Driver
 *
 * See Copyright Notice in cd.h
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "cd.h"
#include "cdcairo.h"
#include "cdcairoctx.h"


static char* get_stride_attrib(cdCtxCanvas* ctxcanvas)
{
  static char data[100];
  sprintf(data, "%d", cairo_image_surface_get_stride(cairo_get_target(ctxcanvas->cr)));
  return data;
}

static cdAttribute stride_attrib =
{
  "STRIDE",
  NULL,
  get_stride_attrib
}; 

static void set_write2png_attrib(cdCtxCanvas *ctxcanvas, char* data)
{
  if (data)
    cairo_surface_write_to_png(cairo_get_target(ctxcanvas->cr), data);
}

static cdAttribute write2png_attrib =
{
  "WRITE2PNG",
  set_write2png_attrib,
  NULL
}; 

static char* get_data_attrib(cdCtxCanvas* ctxcanvas)
{
  return (char*)ctxcanvas->rgb;
}

static cdAttribute data_attrib =
{
  "RGBDATA",
  NULL,
  get_data_attrib
}; 

static void cdkillcanvas (cdCtxCanvas *ctxcanvas)
{
  if (!ctxcanvas->user_image)
    free(ctxcanvas->rgb);

  cdcairoKillCanvas(ctxcanvas);
}

static void cdcreatecanvas(cdCanvas* canvas, void* data)
{
  cdCtxCanvas* ctxcanvas;
  cairo_surface_t *surface;
  int w = 0, h = 0, use_alpha = 0;
  float res = (float)3.78;
  unsigned char *rgb = NULL;
  char* str_data = (char*)data;
  char* res_ptr = NULL;
  cairo_format_t format = CAIRO_FORMAT_RGB24;

  /* Starting parameters */
  if (str_data == NULL) 
    return;

  if (strstr(str_data, "-a"))
    use_alpha = 1;

  res_ptr = strstr(str_data, "-r");
  if (res_ptr)
    sscanf(res_ptr+2, "%g", &res);

  /* size and rgb */
#ifdef SunOS_OLD
  sscanf(str_data, "%dx%d %d", &w, &h, &rgb);
#else
  sscanf(str_data, "%dx%d %p", &w, &h, &rgb);
#endif

  if (w == 0 || h == 0)
    return;

  canvas->w = w;
  canvas->h = h;
  canvas->yres = res;
  canvas->xres = res;
  canvas->w_mm = ((double)w) / res;
  canvas->h_mm = ((double)h) / res;
  if (use_alpha)
  {
    canvas->bpp = 32;
    format = CAIRO_FORMAT_ARGB32;
  }
  else
    canvas->bpp = 24;  /* fake value, image bpp is always 32 */

  if (rgb)
    surface = cairo_image_surface_create_for_data(rgb, format, w, h, w*32);
  else
  	surface = cairo_image_surface_create(format, canvas->w, canvas->h);

  /* Starting Cairo driver */
  ctxcanvas = cdcairoCreateCanvas(canvas, cairo_create(surface));
  cairo_surface_destroy(surface);

  if (rgb)
  {
    ctxcanvas->user_image = 1;
    ctxcanvas->rgb = rgb;
  }
  else
  {
    ctxcanvas->user_image = 0;
    ctxcanvas->rgb = cairo_image_surface_get_data(cairo_get_target(ctxcanvas->cr));

    /* fill with white */
    /* transparent, this is the normal alpha coding */
    cairo_set_source_rgba(ctxcanvas->cr, 1.0, 1.0, 1.0, 0.0);
    cairo_rectangle(ctxcanvas->cr, 0, 0, canvas->w, canvas->h);
    cairo_fill(ctxcanvas->cr);
  }
                                      
  cdRegisterAttribute(canvas, &stride_attrib);
  cdRegisterAttribute(canvas, &write2png_attrib);
  cdRegisterAttribute(canvas, &data_attrib);
}

static void cdinittable(cdCanvas* canvas)
{
  cdcairoInitTable(canvas);
  canvas->cxKillCanvas = cdkillcanvas;
}

static cdContext cdCairoImageRGBContext =
{
  CD_CAP_ALL & ~(CD_CAP_PLAY | CD_CAP_YAXIS | CD_CAP_REGION | CD_CAP_WRITEMODE | CD_CAP_PALETTE),
  CD_CTX_IMAGE,  /* not a plus driver */
  cdcreatecanvas,  
  cdinittable,
  NULL,                 
  NULL
};

cdContext* cdContextCairoImageRGB(void)
{
  return &cdCairoImageRGBContext;
}