[Doxygen-develop] Basic SVG support for Image.h/Image.cpp
Brought to you by:
dimitri
From: Fred P. <j2...@ho...> - 2004-03-18 12:43:35
|
/****************************************************************************** * * $Id: image.h,v 1.7 2001/03/19 19:27:40 root Exp $ * * * Copyright (C) 1997-2004 by Dimitri van Heesch. * * Permission to use, copy, modify, and distribute this software and its * documentation under the terms of the GNU General Public License is hereby * granted. No representations are made about the suitability of this software * for any purpose. It is provided "as is" without express or implied warranty. * See the GNU General Public License for more details. * * Documents produced by Doxygen are derivative works derived from the * input used in their production; they are not affected by this license. * */ #ifndef _IMAGE_H #define _IMAGE_H #include <qglobal.h> #include <stdio.h> class Image { public: Image(int w,int h); ~Image(); void setPixel(int x,int y,uchar val); uchar getPixel(int x,int y) const; void writeChar(int x,int y,char c,uchar fg); void writeString(int x,int y,const char *s,uchar fg); void drawHorzLine(int y,int xs,int xe,uchar colIndex,uint mask); void drawHorzArrow(int y,int xs,int xe,uchar colIndex,uint mask); void drawVertLine(int x,int ys,int ye,uchar colIndex,uint mask); void drawVertArrow(int x,int ys,int ye,uchar colIndex,uint mask); void drawRect(int x,int y,int width,int height,uchar colIndex,uint mask); void fillRect(int x,int y,int width,int height,uchar colIndex,uint mask); bool save(const char *fileName,int mode=0); friend uint stringLength(const char *s); uint getWidth() const { return width; } uint getHeight() const { return height; } uchar *getData() const { return data; } static uint stringLength(const char *s); private: int width; int height; uchar *data; // Added SVG Support: public: void svgPixel( int x, int y, uchar val ); void svgRect( int x, int y, int w, int h, uchar val ); private: FILE* file; }; #endif // EOF /****************************************************************************** * * $Id: image.cpp,v 1.14 2001/03/19 19:27:40 root Exp $ * * * Copyright (C) 1997-2004 by Dimitri van Heesch. * * Copyright (C) 2004 by Fred P. - Added Basic SVG support for ASV3 or ASV6pr1. * * Permission to use, copy, modify, and distribute this software and its * documentation under the terms of the GNU General Public License is hereby * granted. No representations are made about the suitability of this software * for any purpose. It is provided "as is" without express or implied warranty. * See the GNU General Public License for more details. * * Documents produced by Doxygen are derivative works derived from the * input used in their production; they are not affected by this license. * */ /** * GOTCHA: * This file is used mainly for drawing UML diagram in PNG or SVG format, * currently pixel by pixel, when HAVE_DOT = NO. * This file is not used at all if HAVE_DOT = YES. * * To increase SVG support another add-on must be included * into the generating DOT UML format source code. * * More SVG optimization should be provided, * most drawing are simple polygon, * text could be predefined in a <defs> and called with <use id='#f'/> * or used as <text> with normal fonts or create a custom SVG font. * Only letters which are used are required. * * NOTICE: * <defs> and <font definition> can be written anywhere in any order * inside the <svg>..</svg> tags. * */ #include "qtbc.h" #include "image.h" #include "pngenc.h" #include <qglobal.h> #include <stdio.h> #include <string.h> #include "config.h" const int charSetWidth = 80; const int charHeight = 12; const int numChars = 96; unsigned short charPos[numChars] = { 0, 5, 8, 13, 20, 27, 38, 47, 50, 54, 58, 65, 72, 76, 83, 87, 91, 98,105,112,119,126,133,140, 147,154,161,164,167,174,181,188, 195,207,216,224,233,242,250,258, 267,276,279,286,294,301,312,321, 331,339,349,357,365,372,380,389, 400,409,418,427,430,434,437,443, 450,453,460,467,474,481,488,492, 499,506,509,512,518,521,530,537, 544,551,557,562,568,571,578,585, 594,600,607,613,617,620,624,631 }; unsigned char charWidth[numChars] = { 5, 3, 5, 7, 7,11, 9, 3, 4, 4, 7, 7, 4, 7, 4, 4, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 3, 3, 7, 7, 7, 7, 12, 9, 8, 9, 9, 8, 8, 9, 9, 3, 7, 8, 7,11, 9,10, 8,10, 8, 8, 7, 8, 9,11, 9, 9, 9, 3, 4, 3, 6, 7, 3, 7, 7, 7, 7, 7, 4, 7, 7, 3, 3, 6, 3, 9, 7, 7, 7, 6, 5, 6, 3, 7, 7, 9, 6, 7, 6, 4, 3, 4, 7, 5 }; unsigned char fontRaw[charSetWidth*charHeight] = { 0x02, 0x50, 0x01, 0x06, 0x20, 0x60, 0xc6, 0x04, 0x00, 0x00, 0x00, 0x27, 0x04, 0x1c, 0x38, 0x11, 0xf1, 0xc7, 0xc7, 0x0e, 0x00, 0x00, 0x00, 0x03, 0x81, 0xf0, 0x10, 0x7c, 0x1e, 0x3e, 0x1f, 0x9f, 0x87, 0x88, 0x24, 0x09, 0x09, 0x02, 0x02, 0x41, 0x0f, 0x0f, 0x83, 0xc3, 0xe1, 0xe7, 0xf4, 0x24, 0x12, 0x22, 0x41, 0x20, 0x9f, 0xce, 0x30, 0x00, 0x10, 0x04, 0x00, 0x01, 0x00, 0x30, 0x08, 0x12, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x01, 0xac, 0x00, 0x00, 0x02, 0x51, 0x43, 0x89, 0x40, 0x90, 0x49, 0x15, 0x00, 0x00, 0x00, 0x28, 0x9c, 0x22, 0x44, 0x31, 0x02, 0x20, 0x48, 0x91, 0x00, 0x00, 0x00, 0x04, 0x46, 0x08, 0x28, 0x42, 0x21, 0x21, 0x10, 0x10, 0x08, 0x48, 0x24, 0x09, 0x11, 0x03, 0x06, 0x61, 0x10, 0x88, 0x44, 0x22, 0x12, 0x10, 0x84, 0x24, 0x12, 0x22, 0x22, 0x20, 0x80, 0x4a, 0x11, 0x00, 0x20, 0x04, 0x00, 0x01, 0x00, 0x40, 0x08, 0x00, 0x41, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x02, 0x22, 0x00, 0x00, 0x02, 0x51, 0x45, 0x49, 0x40, 0x90, 0x89, 0x0a, 0x00, 0x00, 0x00, 0x48, 0x84, 0x02, 0x04, 0x51, 0x02, 0x00, 0x88, 0x91, 0x00, 0x00, 0x00, 0x04, 0x44, 0xd4, 0x28, 0x42, 0x40, 0x20, 0x90, 0x10, 0x10, 0x08, 0x24, 0x09, 0x21, 0x03, 0x06, 0x51, 0x20, 0x48, 0x48, 0x12, 0x12, 0x00, 0x84, 0x22, 0x22, 0x22, 0x22, 0x11, 0x00, 0x89, 0x12, 0x80, 0x31, 0xc5, 0x87, 0x0d, 0x1c, 0xe3, 0x4b, 0x12, 0x49, 0x29, 0x16, 0x1c, 0x58, 0x69, 0x4c, 0xe8, 0x91, 0x44, 0x61, 0x44, 0xf2, 0x22, 0x00, 0x00, 0x02, 0x07, 0xe5, 0x06, 0x80, 0x60, 0x10, 0x95, 0x08, 0x00, 0x00, 0x48, 0x84, 0x04, 0x18, 0x51, 0xe2, 0xc0, 0x87, 0x11, 0x24, 0x18, 0x03, 0x00, 0x89, 0x24, 0x44, 0x42, 0x40, 0x20, 0x90, 0x10, 0x10, 0x08, 0x24, 0x09, 0x41, 0x02, 0x8a, 0x51, 0x20, 0x48, 0x48, 0x12, 0x11, 0x80, 0x84, 0x22, 0x21, 0x24, 0x14, 0x11, 0x01, 0x09, 0x14, 0x40, 0x02, 0x26, 0x48, 0x93, 0x22, 0x44, 0xcc, 0x92, 0x51, 0x36, 0x99, 0x22, 0x64, 0x99, 0x92, 0x48, 0x91, 0x44, 0x52, 0x44, 0x12, 0x22, 0x00, 0x00, 0x02, 0x01, 0x43, 0x80, 0x80, 0xa0, 0x10, 0x84, 0x08, 0x00, 0x00, 0x88, 0x84, 0x08, 0x04, 0x90, 0x13, 0x21, 0x08, 0x8f, 0x00, 0x61, 0xf0, 0xc0, 0x8a, 0x24, 0x44, 0x7c, 0x40, 0x20, 0x9f, 0x9f, 0x11, 0xcf, 0xe4, 0x09, 0xc1, 0x02, 0x8a, 0x49, 0x20, 0x4f, 0x88, 0x13, 0xe0, 0x60, 0x84, 0x22, 0x21, 0x54, 0x08, 0x0a, 0x02, 0x08, 0x90, 0x00, 0x00, 0x24, 0x48, 0x11, 0x22, 0x44, 0x48, 0x92, 0x61, 0x24, 0x91, 0x22, 0x44, 0x89, 0x10, 0x48, 0x91, 0x24, 0x8c, 0x44, 0x22, 0x22, 0x64, 0x00, 0x02, 0x07, 0xe1, 0x41, 0x31, 0x14, 0x10, 0x80, 0x3e, 0x07, 0xc0, 0x88, 0x84, 0x10, 0x05, 0x10, 0x12, 0x21, 0x08, 0x81, 0x01, 0x80, 0x00, 0x31, 0x0a, 0x24, 0x7c, 0x42, 0x40, 0x20, 0x90, 0x10, 0x10, 0x48, 0x24, 0x09, 0x21, 0x02, 0x52, 0x45, 0x20, 0x48, 0x08, 0x92, 0x20, 0x10, 0x84, 0x21, 0x41, 0x54, 0x14, 0x04, 0x04, 0x08, 0x90, 0x00, 0x01, 0xe4, 0x48, 0x11, 0x3e, 0x44, 0x48, 0x92, 0x61, 0x24, 0x91, 0x22, 0x44, 0x89, 0x0c, 0x48, 0x8a, 0x24, 0x8c, 0x48, 0x44, 0x21, 0x98, 0x00, 0x02, 0x02, 0x85, 0x41, 0x49, 0x08, 0x10, 0x80, 0x08, 0x00, 0x00, 0x88, 0x84, 0x20, 0x45, 0xf9, 0x12, 0x21, 0x08, 0x81, 0x00, 0x61, 0xf0, 0xc1, 0x0a, 0x68, 0x82, 0x42, 0x40, 0x20, 0x90, 0x10, 0x10, 0x48, 0x24, 0x89, 0x11, 0x02, 0x52, 0x45, 0x20, 0x48, 0x08, 0x52, 0x12, 0x10, 0x84, 0x21, 0x40, 0x88, 0x22, 0x04, 0x08, 0x08, 0x90, 0x00, 0x02, 0x24, 0x48, 0x11, 0x20, 0x44, 0x48, 0x92, 0x51, 0x24, 0x91, 0x22, 0x44, 0x89, 0x02, 0x48, 0x8a, 0x2a, 0x92, 0x28, 0x42, 0x22, 0x00, 0x00, 0x00, 0x02, 0x85, 0x41, 0x49, 0x18, 0x10, 0x80, 0x08, 0x00, 0x01, 0x08, 0x84, 0x20, 0x44, 0x11, 0x12, 0x22, 0x08, 0x91, 0x00, 0x18, 0x03, 0x00, 0x09, 0xb0, 0x82, 0x42, 0x21, 0x21, 0x10, 0x10, 0x08, 0xc8, 0x24, 0x89, 0x09, 0x02, 0x22, 0x43, 0x10, 0x88, 0x04, 0x22, 0x12, 0x10, 0x84, 0x20, 0x80, 0x88, 0x22, 0x04, 0x10, 0x08, 0x50, 0x00, 0x02, 0x26, 0x48, 0x93, 0x22, 0x44, 0xc8, 0x92, 0x49, 0x24, 0x91, 0x22, 0x64, 0x99, 0x12, 0x49, 0x84, 0x11, 0x21, 0x28, 0x82, 0x22, 0x00, 0x00, 0x02, 0x02, 0x83, 0x82, 0x30, 0xe4, 0x10, 0x80, 0x00, 0x20, 0x05, 0x07, 0x04, 0x3e, 0x38, 0x10, 0xe1, 0xc2, 0x07, 0x0e, 0x24, 0x00, 0x00, 0x01, 0x04, 0x00, 0x82, 0x7c, 0x1e, 0x3e, 0x1f, 0x90, 0x07, 0x48, 0x24, 0x71, 0x05, 0xf2, 0x22, 0x41, 0x0f, 0x08, 0x03, 0xd2, 0x11, 0xe0, 0x83, 0xc0, 0x80, 0x88, 0x41, 0x04, 0x1f, 0xc8, 0x50, 0x00, 0x01, 0xd5, 0x87, 0x0d, 0x1c, 0x43, 0x48, 0x92, 0x45, 0x24, 0x91, 0x1c, 0x58, 0x69, 0x0c, 0x66, 0x84, 0x11, 0x21, 0x10, 0xf2, 0x22, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x02, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x10, 0x1f, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x04, 0x40, 0x02, 0x00, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x02, 0x22, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0c, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x04, 0x00, 0x00, 0x00, 0x00, 0x40, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x01, 0xac, 0x00, 0x00 }; static Color palette[] = { { 0xff, 0xff, 0xff }, { 0x00, 0x00, 0x00 }, { 0xff, 0xff, 0xc0 }, { 0x9f, 0x9f, 0x60 }, { 0x90, 0x00, 0x00 }, { 0x00, 0x90, 0x00 }, { 0x00, 0x00, 0x90 }, { 0xc0, 0xc0, 0xc0 } }; static Color palette2[] = { { 0xff, 0xff, 0xff }, { 0xe0, 0xe0, 0xe0 }, { 0xd0, 0xd0, 0xd0 }, { 0xc0, 0xc0, 0xc0 }, { 0xb0, 0xb0, 0xb0 }, { 0xa0, 0xa0, 0xa0 }, { 0x90, 0x90, 0x90 }, { 0x80, 0x80, 0x80 }, { 0x70, 0x70, 0x70 }, { 0x60, 0x60, 0x60 }, { 0x50, 0x50, 0x50 }, { 0x40, 0x40, 0x40 }, { 0x30, 0x30, 0x30 }, { 0x20, 0x20, 0x20 }, { 0x10, 0x10, 0x10 }, { 0x00, 0x00, 0x00 } }; static int image_counter = 1; Image::Image(int w,int h) { data = new uchar[w*h]; memset(data,0,w*h); width = w; height = h; QCString &outputDirectory = Config_getString("OUTPUT_DIRECTORY"); char buffer[500]; _snprintf( buffer, 500, "%s\\svg\\image%d_%dx%d.svg", (const char*)outputDirectory, image_counter++, w,h ); file = fopen( buffer, "w+" ); if ( file ) { fprintf( file, "<?xml version='1.0'?>\n" "<svg width='%d' height='%d' \n" "enableZoomAndPanControls='false' \n" "xmlns:xlink='http://www.w3.org/2000/xlink/namespace/' \n" "xmlns:svg='http://www.w3.org/2000/svg' \n" "style='text-rendering: optimizeLegibility; shape-rendering: optimizeSpeed; " "font-family: Arial; font-size: 12;' >", w, h ); } } Image::~Image() { if ( file ) { fprintf( file, "</svg>" ); fclose( file ); } delete[] data; } void Image::setPixel(int x,int y,uchar val) { if (x>=0 && x<width && y>=0 && y<height) { data[y*width+x] = val; } } void Image::svgPixel( int x, int y, uchar val ) { svgRect( x, y, 1, 1, val ); } void Image::svgRect( int x, int y, int w, int h, uchar val ) { if (file && x>=0 && x<width && y>=0 && y<height) { char buffer[500]; Color c = palette[val]; _snprintf( buffer, 500, "\n<rect x='%d' y='%d' width='%d' height='%d' fill='#%.2x%.2x%.2x' color='%d'/>", x,y,w,h,c.red,c.green,c.blue,val); fprintf( file, "%s", buffer ); } } uchar Image::getPixel(int x,int y) const { if (x>=0 && x<width && y>=0 && y<height) return data[y*width+x]; else return 0; } void Image::writeChar(int x,int y,char c,uchar fg) { if (c>=' ') { int xf,yf,ci=c-' '; int rowOffset=0; int cw = charWidth[ci]; int cp = charPos[ci]; for (yf=0;yf<charHeight;yf++) { unsigned short bitPattern=0; int bitsLeft=cw; int byteOffset = rowOffset+(cp>>3); int bitOffset = cp&7; // get the bit pattern for row yf of the character from the font data while (bitsLeft>0) { int bits=8-bitOffset; if (bits>bitsLeft) bits=bitsLeft; bitPattern<<=bits; bitPattern|=((fontRaw[byteOffset]<<bitOffset)&0xff)>>(8-bits); bitsLeft-=bits; bitOffset=0; byteOffset++; } int mask=1<<(cw-1); // draw character row yf uchar font_color = 0; uchar font_last_color = 0; uchar font_last_x = 0; for (xf=0;xf<cw;xf++) { font_color = (bitPattern&mask) ? fg : getPixel(x+xf,y+yf); setPixel(x+xf, y+yf, font_color); svgPixel(x+xf, y+yf, font_color); /* if ( xf > 0 && font_color != font_last_color ) { int wf = xf - font_last_x; // == (xf-1) - font_last_x + 1; svgRect(x+font_last_x, y+yf, wf, 1, font_color); font_last_color = font_color; font_last_x = xf; } */ mask>>=1; } /* // Save the last one if ( xf > 0 && font_color != font_last_color ) { int wf = xf - font_last_x; // == (xf-1) - font_last_x + 1; svgRect(x+font_last_x, y+yf, wf, 1, font_color); font_last_color = font_color; font_last_x = xf; } */ rowOffset+=charSetWidth; } } } // 0 1 2 3 4 5 void Image::writeString(int x,int y,const char *s,uchar fg) { if (s) { char c; while ((c=*s++)) { writeChar(x,y,c,fg); x+=charWidth[c-' ']; } } } uint Image::stringLength(const char *s) { int w=0; if (s) { char c; while ((c=*s++)) w+=charWidth[c-' ']; } return w; } void Image::drawHorzLine(int y,int xs,int xe,uchar colIndex,uint mask) { int x,i=0,j=0; svgRect( xs, y, xe-xs+1, 1, colIndex ); for (x=xs;x<=xe;x++,j++) { if (j&1) i++; if (mask&(1<<(i&0x1f))) { setPixel(x,y,colIndex); } else { svgPixel(x,y,0); // MASK } } } void Image::drawHorzArrow(int y,int xs,int xe,uchar colIndex,uint mask) { drawHorzLine(y,xs,xe,colIndex,mask); int i; for (i=0;i<6;i++) { int h=i>>1; drawVertLine(xe-i,y-h,y+h,colIndex,0xffffffff); } } void Image::drawVertLine(int x,int ys,int ye,uchar colIndex,uint mask) { int y,i=0; svgRect( x, ys, 1, ye-ys+1, colIndex ); for (y=ys;y<=ye;y++,i++) { if (mask&(1<<(i&0x1f))) { setPixel(x,y,colIndex); } else { svgPixel(x,y,0); // MASK } } } void Image::drawVertArrow(int x,int ys,int ye,uchar colIndex,uint mask) { drawVertLine(x,ys,ye,colIndex,mask); int i; for (i=0;i<6;i++) { int h=i>>1; drawHorzLine(ys+i,x-h,x+h,colIndex,0xffffffff); } } void Image::drawRect(int x,int y,int w,int h,uchar colIndex,uint mask) { drawHorzLine(y,x,x+w-1,colIndex,mask); drawHorzLine(y+h-1,x,x+w-1,colIndex,mask); drawVertLine(x,y,y+h-1,colIndex,mask); drawVertLine(x+w-1,y,y+h-1,colIndex,mask); } void Image::fillRect(int x,int y,int lwidth,int lheight,uchar colIndex,uint mask) { int xp,yp,xi,yi; svgRect( x, y, lwidth, lheight, colIndex ); for (yp=y,yi=0;yp<y+lheight;yp++,yi++) { for (xp=x,xi=0;xp<x+lwidth;xp++,xi++) { if (mask&(1<<((xi+yi)&0x1f))) { setPixel(xp,yp,colIndex); } else { svgPixel(xp,yp,0); // MASK } } } } bool Image::save(const char *fileName,int mode) { PngEncoder enc(data, mode==0 ? palette : palette2, width,height, mode==0 ? 3 : 4, 0); if ( file ) { fprintf( file, "\n<desc filename='%s' mode='%d' " "x='%d' y='%d'\n>" "File: %s - Mode: %d</desc>\n", fileName, mode, 0, height-10, fileName, mode ); } enc.write(fileName); return TRUE; } // EOF _________________________________________________________________ Add photos to your e-mail with MSN Premium. Get 2 months FREE* http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines |