/* * GetAttr */ static PyObject * PyDiaImage_GetAttr(PyDiaImage *self, gchar *attr) { if (!strcmp(attr, "__members__")) return Py_BuildValue("[ssssss]", "width", "height", "rgb_data", "mask_data", "filename", "uri"); else if (!strcmp(attr, "width")) return PyInt_FromLong(dia_image_width(self->image)); else if (!strcmp(attr, "height")) return PyInt_FromLong(dia_image_height(self->image)); else if (!strcmp(attr, "filename")) { return PyString_FromString(dia_image_filename(self->image)); } else if (!strcmp(attr, "uri")) { GError* error = NULL; char* s = g_filename_to_uri(dia_image_filename(self->image), NULL, &error); if (s) { PyObject* py_s = PyString_FromString(s); g_free(s); return py_s; } else { PyErr_SetString(PyExc_RuntimeError, error->message); g_error_free (error); return NULL; } } else if (!strcmp(attr, "rgb_data")) { unsigned char* s = dia_image_rgb_data(self->image); int len = dia_image_width(self->image) * dia_image_height(self->image) * 3; PyObject* py_s; if (!s) return PyErr_NoMemory(); py_s = PyString_FromStringAndSize((const char *)s, len); g_free (s); return py_s; } else if (!strcmp(attr, "mask_data")) { unsigned char* s = dia_image_mask_data(self->image); int len = dia_image_width(self->image) * dia_image_height(self->image); PyObject* py_s; if (!s) return PyErr_NoMemory(); py_s = PyString_FromStringAndSize((const char *)s, len); g_free (s); return py_s; } PyErr_SetString(PyExc_AttributeError, attr); return NULL; }
static void draw_image(DiaRenderer *self, Point *point, real width, real height, DiaImage *image) { WmfRenderer *renderer = WMF_RENDERER (self); #ifdef DIRECT_WMF /* not yet supported in compatibility mode */ #else W32::HBITMAP hBmp; int iWidth, iHeight; unsigned char* pData = NULL; unsigned char* pImg = NULL; DIAG_NOTE(renderer, "draw_image %fx%f @%f,%f\n", width, height, point->x, point->y); iWidth = dia_image_width(image); iHeight = dia_image_height(image); pImg = dia_image_rgb_data(image); #if 0 /* only working with 24 bit screen resolution */ if ((dia_image_width(image)*3) % 4) { /* transform data to be DWORD aligned */ int x, y; const unsigned char* pIn = NULL; unsigned char* pOut = NULL; pOut = pData = g_new(unsigned char, ((((iWidth*3-1)/4)+1)*4)*iHeight); pIn = pImg; for (y = 0; y < iHeight; y++) { for (x = 0; x < iWidth; x++) { *pOut++ = *pIn++; *pOut++ = *pIn++; *pOut++ = *pIn++; } pOut += (4 - (iWidth*3)%4); } hBmp = W32::CreateBitmap ( iWidth, iHeight, 1, 24, pData); }
static void draw_image(DiaRenderer *self, Point *point, real width, real height, DiaImage *image) { DiaPsRenderer *renderer = DIA_PS_RENDERER(self); int img_width, img_height, img_rowstride; int x, y; guint8 *rgb_data; guint8 *mask_data; gchar d1_buf[DTOSTR_BUF_SIZE]; gchar d2_buf[DTOSTR_BUF_SIZE]; img_width = dia_image_width(image); img_rowstride = dia_image_rowstride(image); img_height = dia_image_height(image); rgb_data = dia_image_rgb_data(image); if (!rgb_data) { dia_context_add_message(renderer->ctx, _("Not enough memory for image drawing.")); return; } mask_data = dia_image_mask_data(image); fprintf(renderer->file, "gs\n"); /* color output only */ fprintf(renderer->file, "/pix %i string def\n", img_width * 3); fprintf(renderer->file, "%i %i 8\n", img_width, img_height); fprintf(renderer->file, "%s %s tr\n", psrenderer_dtostr(d1_buf, point->x), psrenderer_dtostr(d2_buf, point->y) ); fprintf(renderer->file, "%s %s sc\n", psrenderer_dtostr(d1_buf, width), psrenderer_dtostr(d2_buf, height) ); fprintf(renderer->file, "[%i 0 0 %i 0 0]\n", img_width, img_height); fprintf(renderer->file, "{currentfile pix readhexstring pop}\n"); fprintf(renderer->file, "false 3 colorimage\n"); fprintf(renderer->file, "\n"); if (mask_data) { for (y = 0; y < img_height; y++) { for (x = 0; x < img_width; x++) { int i = y*img_rowstride+x*3; int m = y*img_width+x; fprintf(renderer->file, "%02x", 255-(mask_data[m]*(255-rgb_data[i])/255)); fprintf(renderer->file, "%02x", 255-(mask_data[m]*(255-rgb_data[i+1])/255)); fprintf(renderer->file, "%02x", 255-(mask_data[m]*(255-rgb_data[i+2])/255)); } fprintf(renderer->file, "\n"); } } else { for (y = 0; y < img_height; y++) { for (x = 0; x < img_width; x++) { int i = y*img_rowstride+x*3; fprintf(renderer->file, "%02x", (int)(rgb_data[i])); fprintf(renderer->file, "%02x", (int)(rgb_data[i+1])); fprintf(renderer->file, "%02x", (int)(rgb_data[i+2])); } fprintf(renderer->file, "\n"); } } fprintf(renderer->file, "gr\n"); fprintf(renderer->file, "\n"); g_free(rgb_data); g_free(mask_data); }
static void draw_image(DiaRenderer *self, Point *point, real width, real height, DiaImage *image) { WpgRenderer *renderer = WPG_RENDERER (self); WPGBitmap2 bmp; guint8 * pDiaImg = NULL, * pOut = NULL, * pIn = NULL, * p = NULL; guint8 b_1 = 0, b = 0, cnt; int x, y, stride; bmp.Angle = 0; bmp.Left = SCX(point->x); bmp.Top = SCY(-point->y); bmp.Right = SCX(point->x + width); bmp.Bottom = SCY(-point->y - height); bmp.Width = dia_image_width(image); bmp.Height = dia_image_height(image); bmp.Depth = 8; /* maximum allowed */ bmp.Xdpi = 72; /* ??? */ bmp.Ydpi = 72; DIAG_NOTE(g_message("draw_image %fx%f [%d,%d] @%f,%f", width, height, bmp.Width, bmp.Height, point->x, point->y)); pDiaImg = dia_image_rgb_data(image); if (!pDiaImg) { dia_context_add_message(renderer->ctx, _("Not enough memory for image drawing.")); return; } stride = dia_image_rowstride(image); pOut = g_new(guint8, bmp.Width * bmp.Height * 2); /* space for worst case RLE */ p = pOut; for (y = 0; y < bmp.Height; y++) { /* from top to bottom line */ pIn = pDiaImg + stride * y; cnt = 0; /* reset with every line */ for (x = 0; x < bmp.Width; x ++) { /* simple color reduction to default palette */ b = (((CC_LEN - 1) * pIn[0]) / 255) /* red */ + (((CC_LEN - 1) * pIn[1]) / 255) * CC_LEN /* green */ + (((CC_LEN - 1) * pIn[2]) / 255) * CC_LEN * CC_LEN /* blue */ + WPG_NUM_DEF_COLORS; pIn += 3; /* increase read pointer */ if (cnt > 0) { if ((b == b_1) && (cnt < 127)) cnt++; /* increase counter*/ else { *p++ = (cnt | 0x80); /* write run length and value */ *p++ = b_1; cnt = 1; /* reset */ b_1 = b; } } else { b_1 = b; cnt = 1; } } *p++ = (cnt | 0x80); /* write last value(s) */ *p++ = b; } DIAG_NOTE(g_message( "Width x Height: %d RLE: %d", bmp.Width * bmp.Height, p - pOut)); if ((p - pOut) > 32767) { dia_context_add_message(renderer->ctx, "Bitmap size exceeds blocksize. Ignored."); } else { WriteRecHead(renderer, WPG_BITMAP2, sizeof(WPGBitmap2) + (p - pOut)); #if 0 fwrite(&bmp, sizeof(WPGBitmap2), 1, renderer->file); #else g_assert(20 == sizeof(WPGBitmap2)); fwrite(&bmp, sizeof(guint16), sizeof(WPGBitmap2) / sizeof(guint16), renderer->file); #endif fwrite(pOut, sizeof(guint8), p - pOut, renderer->file); } #if 0 /* RLE diagnose */ { FILE* f; int i, j; f = g_fopen("c:\\temp\\wpg_bmp.raw", "wb"); j = p - pOut; for (i = 0; i < j; i+=2) { for (x = 0; x < (pOut[i] & 0x7F); x++) fwrite(&pOut[i+1], 1, 1, f); } fclose(f); } #endif g_free(pDiaImg); g_free(pOut); }
static void draw_image(DiaRenderer *self, Point *point, real width, real height, DiaImage *image) { DiaCairoRenderer *renderer = DIA_CAIRO_RENDERER (self); cairo_surface_t *surface; guint8 *data; int w = dia_image_width(image); int h = dia_image_height(image); int rs = dia_image_rowstride(image); DIAG_NOTE(g_message("draw_image %fx%f [%d(%d),%d] @%f,%f", width, height, w, rs, h, point->x, point->y)); if (dia_image_rgba_data (image)) { const guint8 *p1 = dia_image_rgba_data (image); /* we need to make a copy to rearrange channels * (also need to use malloc, cause Cairo insists to free() it) */ guint8 *p2 = data = g_malloc (h * rs); int i; for (i = 0; i < (h * rs) / 4; i++) { # if G_BYTE_ORDER == G_LITTLE_ENDIAN p2[0] = p1[2]; /* b */ p2[1] = p1[1]; /* g */ p2[2] = p1[0]; /* r */ p2[3] = p1[3]; /* a */ # else p2[3] = p1[2]; /* b */ p2[2] = p1[1]; /* g */ p2[1] = p1[0]; /* r */ p2[0] = p1[3]; /* a */ # endif p1+=4; p2+=4; } surface = cairo_image_surface_create_for_data (data, CAIRO_FORMAT_ARGB32, w, h, rs); } else { guint8 *p, *p2; guint8 *p1 = data = dia_image_rgb_data (image); /* need to copy to be owned by cairo/pixman, urgh. * Also cairo wants RGB24 32 bit aligned */ int x, y; p = p2 = g_malloc(h*w*4); for (y = 0; y < h; y++) { for (x = 0; x < w; x++) { #if G_BYTE_ORDER == G_LITTLE_ENDIAN /* apparently BGR is required */ p2[x*4 ] = p1[x*3+2]; p2[x*4+1] = p1[x*3+1]; p2[x*4+2] = p1[x*3 ]; p2[x*4+3] = 0x80; /* should not matter */ #else p2[x*4+3] = p1[x*3+2]; p2[x*4+2] = p1[x*3+1]; p2[x*4+1] = p1[x*3 ]; p2[x*4+0] = 0x80; /* should not matter */ #endif } p2 += (w*4); p1 += rs; } surface = cairo_image_surface_create_for_data (p, CAIRO_FORMAT_RGB24, w, h, w*4); g_free (data); data = p; } cairo_save (renderer->cr); cairo_translate (renderer->cr, point->x, point->y); cairo_scale (renderer->cr, width/w, height/h); cairo_move_to (renderer->cr, 0.0, 0.0); /* maybe just the second set_filter is required */ #if 0 cairo_surface_set_filter (renderer->surface, CAIRO_FILTER_BEST); cairo_surface_set_filter (surface, CAIRO_FILTER_BEST); #endif cairo_set_source_surface (renderer->cr, surface, 0.0, 0.0); cairo_paint (renderer->cr); cairo_restore (renderer->cr); cairo_surface_destroy (surface); g_free (data); DIAG_STATE(renderer->cr); }