示例#1
0
文件: edit.cpp 项目: cran/magick
// [[Rcpp::export]]
XPtrImage magick_image_readbitmap_raster2(Rcpp::CharacterMatrix x){
  std::vector<rcolor> y(x.size());
  for(size_t i = 0; i < y.size(); i++)
    y[i] = R_GE_str2col(x[i]);
  Rcpp::IntegerVector dims(x.attr("dim"));
  return magick_image_bitmap(y.data(), Magick::CharPixel, 4, dims[1], dims[0]);
}
示例#2
0
// [[Rcpp::export]]
bool devSVG_(std::string file, std::string bg_, int width, int height,
             int pointsize, bool standalone) {

    int bg = R_GE_str2col(bg_.c_str());

    R_GE_checkVersionOrDie(R_GE_version);
    R_CheckDeviceAvailable();
    BEGIN_SUSPEND_INTERRUPTS {
        pDevDesc dev = svg_driver_new(file, bg, width, height, pointsize, standalone);
        if (dev == NULL)
            Rcpp::stop("Failed to start SVG device");

        pGEDevDesc dd = GEcreateDevDesc(dev);
        GEaddDevice2(dd, "devSVG");
        GEinitDisplayList(dd);

    } END_SUSPEND_INTERRUPTS;

    return true;
}
示例#3
0
static Rboolean SWF_Setup( pDevDesc deviceInfo, const char *fileName,
	double width, double height, const char *bg, const char *fg, 
	double frameRate, SEXP fontFileList, const char *logFileName ){

	/* 
	 * Create swfInfo, this variable contains information which is
	 * unique to the implementation of the SWF Device. The deviceInfo
	 * variable contains a slot into which swfInfo can be placed so that
	 * this information persists and is retrievable during the lifespan
	 * of this device.
	 *
	 * More information on the components of the deviceInfo structure,
	 * which is a pointer to a DevDesc variable, can be found under
	 * struct _DevDesc in the R header file GraphicsDevice.h
	 *
	 * swfInfo is a structure which is defined in the file swfDevice.h
	 *
	*/
	swfDevDesc *swfInfo;
	pGEcontext plotParams;

	/*
	 * pGEcontext is actually a *pointer* to a structure of type
	 * R_GE_gcontext. If we don't allocate it, it will be passed
	 * into the initialization routine without actually pointing
	 * to anything. This causes nasty crashes- for some reason
	 * only on Windows and Linux...
  */	
	if( !( plotParams = (pGEcontext) malloc(sizeof(pGEcontext)) ) )
		return FALSE;
		
	/* 
	 * Initialize swfInfo, return false if this fails. A false return
	 * value will cause the whole device initialization routine to fail.
	*/
	if( !( swfInfo = (swfDevDesc *) malloc(sizeof(swfDevDesc)) ) )
		return FALSE;

	/* Copy SWF-specific information to the swfInfo variable. */
	strcpy( swfInfo->outFileName, fileName);
	strcpy( swfInfo->logFileName, logFileName);
	swfInfo->debug = DEBUG;
	swfInfo->nFrames = 0;
	swfInfo->frameRate = frameRate;
	swfInfo->haveControls = FALSE;
	/*Initilize the SWF movie version 8 so more line styles can be used*/
	swfInfo->m = newSWFMovieWithVersion(8);
	
	//const char *ss = CHAR(asChar(getListElement(fontFileList,"ss")));
	//Rprintf("%s\n",ss);
	swfInfo->ss = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"ss"))));
	swfInfo->ss_b = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"ss_b"))));
	swfInfo->ss_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"ss_i"))));
	swfInfo->ss_b_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"ss_b_i"))));
	swfInfo->mo = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"mo"))));
	swfInfo->mo_b = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"mo_b"))));
	swfInfo->mo_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"mo_i"))));
	swfInfo->mo_b_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"mo_b_i"))));
	swfInfo->se = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"se"))));
	swfInfo->se_b = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"se_b"))));
	swfInfo->se_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"se_i"))));
	swfInfo->se_b_i = 
	newSWFFont_fromFile(CHAR(asChar(getListElement(fontFileList,"se_b_i"))));
	
	swfInfo->displayListHead = NULL; 
	swfInfo->displayListTail = NULL;

	/* Incorporate swfInfo into deviceInfo. */
	deviceInfo->deviceSpecific = (void *) swfInfo;

	/* 
	 * These next statements define the capabilities of the device.
	 * These capabilities include:
	 *	-Device/user interaction
	 *	-Gamma correction
	 *	-Clipping abilities
	 *	-UTF8 support
	 *  -Text justification/alignment abilities
	*/

	/* 
	 * Define the gamma factor- used to adjust the luminosity of an image. 
	 * Set to 1 since there is no gamma correction in the SWF device. Also,
	 * canChangeGamma is set to FALSE to disallow user adjustment of this
	 * default
	*/
	deviceInfo->startgamma = 1;
	deviceInfo->canChangeGamma = FALSE;

	/*
	 * canHAdj is an integer specifying the level of horizontal adjustment
	 * or justification provided by this device. Currently set to 0 as this
	 * is not implemented. 
	*/
	deviceInfo->canHAdj = 0;

	/*
	 * useRotatedTextInContour specifies if the text function along with
	 * rotation parameters should be used over Hershey fonts when printing
	 * contour plot labels.
	*/
	deviceInfo->useRotatedTextInContour = TRUE; 

	/*
	 * canClip specifies whether the device implements routines for filtering
	 * plotting input such that it falls within a rectangular clipping area.
	 * Implementing this leads to an interesting design choice- to implement
	 * clipping here in the C code or hand it off to the SWF clipping 
	 * routines.  Clipping at the C level may reduce  and simplify the final 
	 * output file by not printing objects that fall outside the plot 
	 * boundaries. 
	*/
	deviceInfo->canClip = FALSE;


	/*
	 * These next parameters speficy if the device reacts to keyboard and 
	 * mouse events. Since this device outputs to a file, not a screen window, 
	 * these actions are disabled.
	*/
	deviceInfo->canGenMouseDown = FALSE;
	deviceInfo->canGenMouseMove = FALSE;
	deviceInfo->canGenMouseUp = FALSE;
	deviceInfo->canGenKeybd = FALSE;

	/* 
	 * This parameter specifies whether the device is set up to handle UTF8
	 * characters. This makes a difference in the complexity of the text
	 * handling functions that must be built into the device. If set to true
	 * both hook functions textUTF8 and strWidthUTF8 must be implemented.
	 * Compared to ASCII, which only has 128 character values, UTF8 has
	 * thousends. This will require a fairly sophisticated function for
	 * calculating string widths.
	 *
	 * UTF8 support would be a great feature to include as it would make
	 * this device useful for an international audience. For now only
	 * the ASCII character set will be used as it is easy to implement.
	 * 
	 * wantSymbolUTF8 indicates if mathematical symbols should be treated
	 * as UTF8 characters.
	*/
	deviceInfo->hasTextUTF8 = FALSE;
	deviceInfo->wantSymbolUTF8 = FALSE;

	/*
	 * Initialize device parameters. These concern properties such as the 
	 * plotting canvas size, the initial foreground and background colors and 
	 * the initial clipping area. Other parameters related to fonts and text 
	 * output are also included.
	*/

	/*
	 * Set canvas size. The bottom left corner is considered the origin and 
	 * assigned the value of 0pt, 0pt. The upper right corner is assigned by 
	 * converting the specified height and width of the device to points.
	*/
	deviceInfo->bottom = 0;
	deviceInfo->left = 0;
	deviceInfo->top = dim2dev( height );
	deviceInfo->right = dim2dev( width );

	/* Set default character size in pixels. */
	deviceInfo->cra[0] = 9;
	deviceInfo->cra[1] = 12;

	/* Set initial font. */
	deviceInfo->startfont = 1;

	/* Set initial font size. */
	deviceInfo->startps = 10;

	/* 
	 * Apparently these are supposed to center text strings over the points at
	 * which they are plotted. SWF does this automagically.
	 *
	 * I hope.
	 *
	*/
	deviceInfo->xCharOffset = 0;	
	deviceInfo->yCharOffset = 0;	
	deviceInfo->yLineBias = 0;	

	/* Specify the number of inches per pixel in the x and y directions. */
	deviceInfo->ipr[0] = 1/dim2dev(1);
	deviceInfo->ipr[1] = 1/dim2dev(1);

	/* Set initial foreground and background colors. */
	deviceInfo->startfill = R_GE_str2col( bg );
	deviceInfo->startcol = R_GE_str2col( fg );

	/* Set initial line type. */
	deviceInfo->startlty = 0;


	/* 
	 * Connect R graphic function hooks to SWF Routines implemented in this
	 * file. Each routine performs a specific function such as adding text, 
	 * drawing a line or reporting/adjusting the status of the device.
	*/

	/* Utility routines. */
	deviceInfo->close = SWF_Close;
	deviceInfo->newPage = SWF_NewPage;
	deviceInfo->clip = SWF_Clip;
	deviceInfo->size = SWF_Size;

	/* Text routines. */
	deviceInfo->metricInfo = SWF_MetricInfo;
	deviceInfo->strWidth = SWF_StrWidth;
	deviceInfo->text = SWF_Text;

	/* Drawing routines. */
	deviceInfo->line = SWF_Line;
	deviceInfo->circle = SWF_Circle;
	deviceInfo->rect = SWF_Rectangle;
	deviceInfo->polyline = SWF_Polyline;
	deviceInfo->polygon = SWF_Polygon;

	/* Dummy routines. These are mainly used by GUI graphics devices. */
	deviceInfo->activate = SWF_Activate;
	deviceInfo->deactivate = SWF_Deactivate;
	deviceInfo->locator = SWF_Locator;
	deviceInfo->mode = SWF_Mode;

	/* Call SWF_Open to create and initialize the output file. */
	if( !SWF_Open( deviceInfo ) )
		return FALSE;

	return TRUE;

}