Esempio n. 1
0
/* Load a SVG type image from an SDL datasource */
SDL_Surface *IMG_LoadSVG_RW(SDL_RWops *src)
{
    char *data;
    struct NSVGimage *image;
    struct NSVGrasterizer *rasterizer;
    SDL_Surface *surface = NULL;
    float scale = 1.0f;

    data = (char *)SDL_LoadFile_RW(src, NULL, SDL_FALSE);
    if ( !data ) {
        return NULL;
    }

    /* For now just use default units of pixels at 96 DPI */
    image = nsvgParse(data, "px", 96.0f);
    SDL_free(data);
    if ( !image ) {
        IMG_SetError("Couldn't parse SVG image");
        return NULL;
    }

    rasterizer = nsvgCreateRasterizer();
    if ( !rasterizer ) {
        IMG_SetError("Couldn't create SVG rasterizer");
        nsvgDelete( image );
        return NULL;
    }

    surface = SDL_CreateRGBSurface(SDL_SWSURFACE,
                                   (int)(image->width * scale),
                                   (int)(image->height * scale),
                                   32,
                                   0x000000FF,
                                   0x0000FF00,
                                   0x00FF0000,
                                   0xFF000000);
    if ( !surface ) {
        nsvgDeleteRasterizer( rasterizer );
        nsvgDelete( image );
        return NULL;
    }

    nsvgRasterize(rasterizer, image, 0.0f, 0.0f, scale, (unsigned char *)surface->pixels, surface->w, surface->h, surface->pitch);
    nsvgDeleteRasterizer( rasterizer );
    nsvgDelete( image );

    return surface;
}
Esempio n. 2
0
struct NSVGrasterizer* nsvgCreateRasterizer() {
	struct NSVGrasterizer* r = (struct NSVGrasterizer*) malloc(
			sizeof(struct NSVGrasterizer));
	if (r == NULL)
		goto error;
	memset(r, 0, sizeof(struct NSVGrasterizer));

	return r;

	error: nsvgDeleteRasterizer(r);
	return NULL;
}
Esempio n. 3
0
LIBAROMA_CANVASP libaroma_svg_ex(
    LIBAROMA_STREAMP stream,
    byte freeStream,
    byte use_px) {

  LIBAROMA_CANVASP cv = NULL;
  if (!stream) {
    return NULL;
  }
  
  char * data = libaroma_stream_to_string(stream,0);
  if (data){
    NSVGimage *image = NULL;
    if (!use_px){
      image=nsvgParse(data, "dp", ((float) libaroma_fb()->dpi));
    }
    else{
      image=nsvgParse(data, "px", ((float) libaroma_fb()->dpi));
    }
    free(data);
    if (image == NULL) {
  		ALOGW("libaroma_svg: Could not open SVG image.");
  		goto exit;
  	}
  	
  	
  	NSVGrasterizer *rast =nsvgCreateRasterizer();
  	if (rast == NULL) {
  		printf("libaroma_svg: Could not init rasterizer.");
  		nsvgDelete(image);
  		goto exit;
  	}
  	if (!use_px){
  	  cv = libaroma_canvas_ex(libaroma_dp(image->width),libaroma_dp(image->height),1);
  	}
  	else{
  	  cv = libaroma_canvas_ex(image->width,image->height,1);
  	}
  	libaroma_canvas_setcolor(cv,0,0);
  	nsvgRasterize(rast,image,0,0,1,cv);
  	nsvgDelete(image);
  	nsvgDeleteRasterizer(rast);
  }
  
exit:
  if (freeStream) {
    libaroma_stream_close(stream);
  }
  return cv;
}
Esempio n. 4
0
ImTextureID PiGui::RenderSVG(std::string svgFilename, int width, int height) {
	Output("nanosvg: %s %dx%d\n", svgFilename.c_str(), width, height);
	NSVGimage *image = NULL;
	NSVGrasterizer *rast = NULL;
	unsigned char* img = NULL;
	int w, h;
	// size of each icon
	//	int size = 64;
	// 16 columns
	//	int W = 16*size;
	int W = width;
	// 16 rows
	//	int H = 16*size;
	int H = height;
	img = static_cast<unsigned char*>(malloc(W*H*4));
	memset(img, 0, W * H * 4);
	std::string filename = svgFilename; // FileSystem::JoinPath(FileSystem::JoinPath(FileSystem::GetDataDir(), "icons"), "icons.svg");
	image = nsvgParseFromFile(filename.c_str(), "px", 96.0f);
	if (image == NULL) {
		Error("Could not open SVG image.\n");
	}
	w = static_cast<int>(image->width);
	h = static_cast<int>(image->height);

	rast = nsvgCreateRasterizer();
	if (rast == NULL) {
		Error("Could not init rasterizer.\n");
	}

	if (img == NULL) {
		Error("Could not alloc image buffer.\n");
	}
	{
		float scale = double(W)/w;
		float tx = 0;
		float ty = 0;
		nsvgRasterize(rast, image, tx, ty, scale, img, W, H, W*4);
	}
	nsvgDeleteRasterizer(rast);
	nsvgDelete(image);
	return makeTexture(img, W, H);
}
Esempio n. 5
0
int main()
{
	struct NSVGimage *image = NULL;
	struct NSVGrasterizer *rast = NULL;
	unsigned char* img = NULL;
	int w, h;
	const char* filename = "../example/23.svg";

	printf("parsing %s\n", filename);
	image = nsvgParseFromFile(filename, "px", 96.0f);
	if (image == NULL) {
		printf("Could not open SVG image.\n");
		goto error;
	}
	w = image->width;
	h = image->height;

	rast = nsvgCreateRasterizer();
	if (rast == NULL) {
		printf("Could not init rasterizer.\n");
		goto error;
	}

	img = malloc(w*h*4);
	if (img == NULL) {
		printf("Could not alloc image buffer.\n");
		goto error;
	}

	printf("rasterizing image %d x %d\n", w, h);
	nsvgRasterize(rast, image, 0,0,1, img, w, h, w*4);

	printf("writing svg.png\n");
 	stbi_write_png("svg.png", w, h, 4, img, w*4);

error:
	nsvgDeleteRasterizer(rast);
	nsvgDelete(image);

	return 0;
}
Esempio n. 6
0
    void VectorTexture::rasterizeGraphics(NSVGimage* svg, Filtering filtering, Wrap wrap, std::string filepath)
    {
        // TODO: Size depending on image scale. Better depend on rendering resolution

        uint width = (uint)(svg->width);
        uint height = (uint)(svg->height);
        const uint channelCount = 4;

        // Create rasterizer
        NSVGrasterizer* rast = nsvgCreateRasterizer();

        // Prepare memory for image
        std::vector<uchar> image;
        image.resize(width * height * channelCount);

        // Rasterize
        nsvgRasterize(rast, svg, 0, 0, 1, image.data(), width, height, width * channelCount);

        // Flip image
        std::vector<uchar> copyImage(image);

        // Go over lines
        for (uint i = 0; i < height; i++)
        {
            // Go over columns
            for (uint j = 0; j < width; j++)
            {
                // Go over channels
                for (uint k = 0; k < channelCount; k++)
                {
                    image[i * width * channelCount + j * channelCount + k] = copyImage[(height - 1 - i) * width * channelCount + j * channelCount + k];
                }
            }
        }

        // Create OpenGL from image
        createOpenGLTexture(image, filtering, wrap, width, height, channelCount, filepath);

        // Delete NanoSVG stuff
        nsvgDeleteRasterizer(rast);
    }
Esempio n. 7
0
static int
RasterizeSVG(
    Tcl_Interp *interp,
    Tk_PhotoHandle imageHandle,
    NSVGimage *nsvgImage,
    int destX, int destY,
    int width, int height,
    int srcX, int srcY,
    RastOpts *ropts)
{
    int w, h, c;
    NSVGrasterizer *rast;
    unsigned char *imgData;
    Tk_PhotoImageBlock svgblock;

    w = (int) ceil(nsvgImage->width * ropts->scale);
    h = (int) ceil(nsvgImage->height * ropts->scale);
    rast = nsvgCreateRasterizer();
    if (rast == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot initialize rasterizer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "RASTERIZER_ERROR",
		NULL);
	goto cleanAST;
    }
    imgData = attemptckalloc(w * h *4);
    if (imgData == NULL) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj("cannot alloc image buffer", -1));
	Tcl_SetErrorCode(interp, "TK", "IMAGE", "SVG", "OUT_OF_MEMORY", NULL);
	goto cleanRAST;
    }
    nsvgRasterize(rast, nsvgImage, (float) ropts->x, (float) ropts->y,
	    (float) ropts->scale, imgData, w, h, w * 4);
    /* transfer the data to a photo block */
    svgblock.pixelPtr = imgData;
    svgblock.width = w;
    svgblock.height = h;
    svgblock.pitch = w * 4;
    svgblock.pixelSize = 4;
    for (c = 0; c <= 3; c++) {
	svgblock.offset[c] = c;
    }
    if (Tk_PhotoExpand(interp, imageHandle,
		destX + width, destY + height) != TCL_OK) {
	goto cleanRAST;
    }
    if (Tk_PhotoPutBlock(interp, imageHandle, &svgblock, destX, destY,
		width, height, TK_PHOTO_COMPOSITE_SET) != TCL_OK) {
	goto cleanimg;
    }
    ckfree(imgData);
    nsvgDeleteRasterizer(rast);
    nsvgDelete(nsvgImage);
    return TCL_OK;

cleanimg:
    ckfree(imgData);

cleanRAST:
    nsvgDeleteRasterizer(rast);

cleanAST:
    nsvgDelete(nsvgImage);
    return TCL_ERROR;
}
Esempio n. 8
0
SVGRasterizer::~SVGRasterizer() {
	nsvgDeleteRasterizer(rasterizer);
}