Exemple #1
0
int main()
{
    gdImagePtr im, tile;
    int im_white, im_black, tile_white, tile_black;
    int x,y, error = 0;
    FILE *fp;
    char path[1024];

    fputs("flag 0\n", stdout);
    im = gdImageCreate(150, 150);


    tile = gdImageCreateTrueColor(36, 36);

    tile_white = gdImageColorAllocate(tile,255,255,255);
    tile_black = gdImageColorAllocate(tile,55,0,0);
    im_white = gdImageColorAllocate(im,255,255,255);
    im_black = gdImageColorAllocate(im,0,0,0);

    gdImageFill(tile, 0,0, tile_white);
    gdImageColorTransparent(tile, tile_black);
    gdImageColorTransparent(im, im_black);

    /* create the dots pattern */
    for (x=0; x<36; x+=2) {
        for (y=0; y<36; y+=2) {
            gdImageSetPixel(tile,x,y,tile_black);
        }
    }

    gdImageSetTile(im,tile);
    gdImageRectangle(im, 9,9,139,139, im_black);
    gdImageLine(im, 9,9,139,139, im_black);
    gdImageFill(im, 11,12, gdTiled);


    fputs("flag 1\n", stdout);
    gdImageFill(im, 0, 0, 0xffffff);
    fputs("flag 2\n", stdout);
    gdImageFill(im, 0, 0, 0xffffff);
    fputs("flag 3\n", stdout);

    sprintf(path, "%s/gdimagefill/bug00002_3_exp.png", GDTEST_TOP_DIR);
    if (!gdAssertImageEqualsToFile(path, im)) {
        error = 1;
    }

    /* Destroy it */
    gdImageDestroy(im);
    return error;
}
Exemple #2
0
FILE *piechart_init(char *filename) {
  FILE *pieoutf;
  int xsize, ysize, b1, b2;

  if ((pieoutf = FOPENWB(filename)) == NULL) {
    warn('F', TRUE, "Failed to open pie chart file %s for writing: "
	 "ignoring it", filename);
    return(pieoutf);
  }
  debug('F', "Opening %s as pie chart file", filename);
#ifdef RISCOS
  _swix(OS_File, _INR(0,2), 18, filename, 0xb60);  /* set PNG filetype */
#endif

  xsize = normalchart?XSIZE:SHORTXSIZE;
  ysize = normalchart?YSIZE:SHORTYSIZE;
  im = gdImageCreate(xsize, ysize);
  /* The first colour allocated in a new image is the background colour. */
  white = gdImageColorAllocate(im, 255, 255, 255);           /* white */
  black = gdImageColorAllocate(im, 0, 0, 0);                 /* black */
  grey = gdImageColorAllocate(im, 128, 128, 128);            /* grey */
  lightgrey = gdImageColorAllocate(im, 217, 217, 217);       /* light grey */
  col = 0;
  /* Wedge colours. If these change, so must images/sq*. */
  colours[col++] = gdImageColorAllocate(im, 255, 0, 0);      /* red */
  colours[col++] = gdImageColorAllocate(im, 0, 0, 255);      /* mid blue */
  colours[col++] = gdImageColorAllocate(im, 0, 128, 0);      /* green */
  colours[col++] = gdImageColorAllocate(im, 255, 128, 0);    /* orange */
  colours[col++] = gdImageColorAllocate(im, 0, 0, 128);      /* navy blue */
  colours[col++] = gdImageColorAllocate(im, 0, 255, 0);      /* pale green */
  colours[col++] = gdImageColorAllocate(im, 255, 128, 128);  /* pink */
  colours[col++] = gdImageColorAllocate(im, 0, 255, 255);    /* cyan */
  colours[col++] = gdImageColorAllocate(im, 128, 0, 128);    /* purple */
  colours[col++] = gdImageColorAllocate(im, 255, 255, 0);    /* yellow */
  col = 0;
  totangle = 0.75;  /* starting at the top */
  boxesy = BOXESTOP;
  b1 = xsize - 1 - BORDER;
  b2 = ysize - 1 - BORDER;
  /* Plot outline of pie, and border of image */
  gdImageArc(im, XCENTRE, YCENTRE, DIAMETER + 2, DIAMETER + 2, 0, 360, black);
  gdImageRectangle(im, BORDER, BORDER, b1, b2, black);
  gdImageLine(im, xsize - 1, 0, b1, BORDER, black);
  gdImageLine(im, 0, ysize - 1, BORDER, b2, black);
  gdImageFill(im, 0, 0, lightgrey);
  gdImageFill(im, xsize - 1, ysize - 1, grey);
  gdImageLine(im, 0, 0, BORDER, BORDER, black);
  gdImageLine(im, xsize - 1, ysize - 1, b1, b2, black);
  return(pieoutf);
}
Exemple #3
0
int main()
{
	gdImagePtr im, tile;
	int tile_red, tile_blue;
	int error = 0;
	char path[1024];
	
	im = gdImageCreate(200, 150);

	tile = gdImageCreateTrueColor(2, 2);
	
	tile_red = gdImageColorAllocate(tile, 255, 0, 0);
	tile_blue = gdImageColorAllocate(tile, 0, 0, 255);
	
	gdImageSetPixel(tile, 0, 0, tile_red);
	gdImageSetPixel(tile, 1, 1, tile_red);
	gdImageSetPixel(tile, 1, 0, tile_blue);
	gdImageSetPixel(tile, 0, 1, tile_blue);
	
	gdImageSetTile(im, tile);
	gdImageFill(im, 11, 12, gdTiled);

	sprintf(path, "%s/gdimagefill/bug00104_1_exp.png", GDTEST_TOP_DIR);
	if (!gdAssertImageEqualsToFile(path, im)) {
		error = 1;
	}
	
	gdImageDestroy(im);
	gdImageDestroy(tile);
	return error;
}
Exemple #4
0
/*
 * Bueno, vamos a ver si podemos obtener la pieza
 * */
int    tipojuego_get_tpieza_png( Tipojuego* tj, char* color, char* tpieza, int flags, void** png, int* width, int* height ){
#if GRAPH_ENABLED
    Tipopieza* tp = tj->tipo_piezas->data[ GETTIPOPIEZA(tj,tpieza) ];
    int  col      = GETCOLOR(tj,color);
    gdImagePtr gd = graph_tpieza_get_gd( tp, col );
    int  size = 0;

    if( flags == GETPNG_PIEZA_CAPTURADA ){
        gdImagePtr gd2 = gdImageCreate( gdImageSX( gd ) / 2, gdImageSY( gd ) / 2 );

        int transp = gdImageColorAllocate( gd2, 0, 255, 0 );
        gdImageColorTransparent( gd2, transp );
        gdImageFill( gd2, 0, 0 , transp );

        gdImageCopyResized( gd2, gd, 0, 0, 0, 0,
          gdImageSX(gd2), gdImageSY(gd2),
          gdImageSX(gd), gdImageSY(gd) );
        
        if( png ) *png = gdImagePngPtr( gd2, &size );
        if( width ) *width = gdImageSX( gd2 );
        if( height ) *height = gdImageSY( gd2 );
        gdImageDestroy( gd2 );
        return size;
    } else {
        if( png ) *png = gdImagePngPtr( gd, &size );
        if( width ) *width = gdImageSX( gd );
        if( height ) *height = gdImageSY( gd );
        return size;
    }
#endif
    LOGPRINT( 2, "No compilado con el modulo GD tpieza = %s", tpieza );
    return 0;
}
Exemple #5
0
int CFunctions::imgFill ( lua_State* luaVM )
{
	// bool/int imageFill( userdata im, int x, int y, int color )
    if ( luaVM )
    {
        if ( lua_type ( luaVM, 1 ) == LUA_TLIGHTUSERDATA &&
			lua_type ( luaVM, 2 ) == LUA_TNUMBER &&
			lua_type ( luaVM, 3 ) == LUA_TNUMBER &&
			lua_type ( luaVM, 4 ) == LUA_TNUMBER )
		{
			gdImagePtr im = mImgManager->GetImage( lua_touserdata( luaVM, 1 ) );
			if( im != NULL )
			{
				gdImageFill( im, (int)lua_tonumber( luaVM, 2 ), (int)lua_tonumber( luaVM, 3 ), (int)lua_tonumber( luaVM, 4 ) );
				lua_pushboolean( luaVM, true );
			}
			else
			{
				lua_pushboolean( luaVM, false );
			}
			return 1;
		}
		else
		{
			pModuleManager->DebugPrintf( luaVM, "Incorrect parameters in: imageDestroy" );
		}
    }
	lua_pushboolean( luaVM, false );
    return 0;
}
Exemple #6
0
result_t Image::fill(int32_t x, int32_t y, int32_t color)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    gdImageFill(m_image, x, y, color);
    return 0;
}
Exemple #7
0
int main()
{
	gdImagePtr im, tile;
	int im_black, tile_black;
	int x,y, error = 0;

/*	fputs("flag 0\n", stdout); */
	im = gdImageCreate(150, 150);
	tile = gdImageCreate(36, 36);
	gdImageColorAllocate(tile,255,255,255); /* allocate white for background color */
	tile_black = gdImageColorAllocate(tile,0,0,0);
	gdImageColorAllocate(im,255,255,255); /* allocate white for background color */
	im_black = gdImageColorAllocate(im,0,0,0);


	/* create the dots pattern */
	for (x=0; x<36; x+=2) {
		for (y=0; y<36; y+=2) {
			gdImageSetPixel(tile,x,y,tile_black);
		}
	}

	gdImageSetTile(im,tile);
	gdImageRectangle(im, 9,9,139,139, im_black);
	gdImageLine(im, 9,9,139,139, im_black);
	gdImageFill(im, 11,12, gdTiled);


/*	fputs("flag 1\n", stdout); */
	gdImageFill(im, 0, 0, 0xffffff);
/*	fputs("flag 2\n", stdout); */
	gdImageFill(im, 0, 0, 0xffffff);
/*	fputs("flag 2\n", stdout); */


	if (!gdAssertImageEqualsToFile("gdimagefill/bug00002_2_exp.png", im)) {
		error = 1;
	}

	/* Destroy it */
	gdImageDestroy(im);
	gdImageDestroy(tile);
	return error;
}
Exemple #8
0
/* If palette is true, we convert from truecolor to palette at the end,
   to test gdImageTrueColorToPalette and see file size/
   quality tradeoffs. */
void
testDrawing (gdImagePtr im_in,
	     double scale, int blending, int palette, char *filename)
{
	gdImagePtr im_out;
	FILE *out;
	/* Create output image. */
	im_out = gdImageCreateTrueColor ((int) (gdImageSX (im_in) * scale),
					 (int) (gdImageSY (im_in) * scale));
	/*
	   Request alpha blending. This causes future
	   drawing operations to perform alpha channel blending
	   with the background, resulting in an opaque image.
	   Without this call, pixels in the foreground color are
	   copied literally, *including* the alpha channel value,
	   resulting in an output image which is potentially
	   not opaque. This flag can be set and cleared as often
	   as desired. */
	gdImageAlphaBlending (im_out, blending);

	/* Flood with light blue. */
	gdImageFill (im_out, (int) (gdImageSX (im_in) * scale / 2),
		     (int) (gdImageSY (im_in) * scale / 2),
		     gdTrueColor (192, 192, 255));
	/* Copy the source image. Alpha blending should result in
	   compositing against red. With blending turned off, the
	   browser or viewer will composite against its preferred
	   background, or, if it does not support an alpha channel,
	   we will see the original colors for the pixels that
	   ought to be transparent or semitransparent. */
	gdImageCopyResampled (im_out, im_in,
			      0, 0,
			      0, 0,
			      (int) (gdImageSX (im_in) * scale),
			      (int) (gdImageSY (im_in) * scale), gdImageSX (im_in),
			      gdImageSY (im_in));
	/* Write PNG */
	out = fopen (filename, "wb");

	/* If this image is the result of alpha channel blending,
	   it will not contain an interesting alpha channel itself.
	   Save a little file size by not saving the alpha channel.
	   Otherwise the file would typically be slightly larger. */
	gdImageSaveAlpha (im_out, !blending);

	/* If requested, convert from truecolor to palette. */
	if (palette) {
		/* Dithering, 256 colors. */
		gdImageTrueColorToPalette (im_out, 1, 256);
	}

	gdImagePng (im_out, out);
	fclose (out);

	gdImageDestroy (im_out);
}
Exemple #9
0
void doFill(FILE *stream) {
  float x, y;
  int c;

  x = getFloat(stream);
  y = getFloat(stream);
  c = getColor(getNumber(stream));

  gdImageFill(image, viewx(x), viewy(y), c);
}
Exemple #10
0
int main()
{
    gdImagePtr im;
    FILE *fp;
    int red, blue, white, black, error = 0;
    char path[1024];

    im = gdImageCreate(50,100);
    red = gdImageColorAllocate(im, 255, 0, 0);
    blue = gdImageColorAllocate(im, 0,0,255);
    white  = gdImageColorAllocate(im, 255,255,255);
    black = gdImageColorAllocate(im, 0,0,0);
    gdImageFill(im, 0,0, black);

    gdImageLine(im, 20,20,180,20, white);
    gdImageLine(im, 20,20,20,70, blue);
    gdImageLine(im, 20,70,180,70, red);
    gdImageLine(im, 180,20,180,45, white);
    gdImageLine(im, 180,70,180,45, red);
    gdImageLine(im, 20,20,100,45, blue);
    gdImageLine(im, 20,70,100,45, blue);
    gdImageLine(im, 100,45,180,45, red);

    gdImageFill(im, 21,45, blue);
    gdImageFill(im, 100,69, red);
    gdImageFill(im, 100,21, white);

    sprintf(path, "%s/gdimagefill/bug00002_4_exp.png", GDTEST_TOP_DIR);
    if (!gdAssertImageEqualsToFile(path, im)) {
        error = 1;
    }

    /* Destroy it */
    gdImageDestroy(im);
    return error;
}
Exemple #11
0
int
main (int argc, char *argv[])
{
#ifdef HAVE_LIBFREETYPE
  	FILE *out;
	int transparent, green, black;
	gdImagePtr im;

	im = gdImageCreateTrueColor(100,100);

        black =  gdImageColorResolveAlpha(im, 0, 0, 0, gdAlphaOpaque);
        green =  gdImageColorResolveAlpha(im, 0, gdGreenMax, 0, gdAlphaOpaque);
        transparent = gdImageColorResolveAlpha(im,
                        gdRedMax-1, gdGreenMax, gdBlueMax, gdAlphaTransparent);
        gdImageColorTransparent(im, transparent);

	/* Blending must be off to lay a transparent basecolor.
                Nothing to blend with anyway. */
        gdImageAlphaBlending(im, FALSE);
        gdImageFill (im, im->sx/2, im->sy/2, transparent);
        /* Blend everything else together,
                especially fonts over non-transparent backgrounds */
        gdImageAlphaBlending(im, TRUE);

	gdImageFilledRectangle (im, 30, 30, 70, 70, green);
	gdImageStringFT (im, NULL, black, "Times", 18, 0, 50, 50, "Hello");

	gdImageSaveAlpha (im, TRUE);
#ifdef HAVE_LIBPNG
	out = fopen ("testtr.png", "wb");
	gdImagePng (im, out);
	fclose (out);
#else
	fprintf(stderr, "Compiled without libpng support\n");
#endif /* HAVE_LIBPNG */
	gdImageDestroy (im);

	return 0;
#else
	fprintf(stderr, "Compiled without freetype support\n");
	return 0;
#endif /* HAVE_LIBFREETYPE */
}
Exemple #12
0
/* Labyrinth als png-Bild abspeichern */
void labyrinth2png(struct rectangle *r, char *filename)
{
    FILE *png;
    gdImagePtr image;
    int white, yellow, gray, black, edge, width, height, x, y;

    edge = 210;

    width  = r->width  * edge;
    height = r->height * edge;

    if ((png = fopen(filename, "w")) == NULL) {
        perror("fopen");
        exit(EXIT_FAILURE);
    }

    if ((image = gdImageCreate(width + 1, height + 1)) == NULL) {
        perror("gdImageCreate");
        exit(EXIT_FAILURE);
    }

    /* Farben initialisieren */
    white  = gdImageColorAllocate(image, RGB_WHITE);
    yellow = gdImageColorAllocate(image, RGB_YELLOW);
    gray   = gdImageColorAllocate(image, RGB_GRAY);
    black  = gdImageColorAllocate(image, RGB_BLACK);

    gdImageFill(image, width, height, white);

    for (y = 0; y < r->height; y++)
        for (x = 0; x < r->width; x++)
            if (r->cube[y][x].type != LEER)
                draw_cube(image, &(r->cube[y][x]),
                          x * edge, y * edge, edge,
                          yellow, gray, black);

    gdImagePng(image, png);
    fclose(png);
    gdImageDestroy(image);
}
Exemple #13
0
int main()
{
	gdImagePtr im, tile;
	char exp[] = "bug00032_exp.png";
	FILE *fp;

	tile = gdImageCreateTrueColor(10, 10);
	gdImageFill(tile, 0, 0, 0xFFFFFF);
	gdImageLine(tile, 0,0, 9,9, 0xff0000);
	gdImageColorTransparent(tile, 0xFFFFFF);

	im = gdImageCreateTrueColor(50, 50);
	gdImageFilledRectangle(im, 0, 0, 25, 25, 0x00FF00);

	gdImageSetTile(im, tile);
	gdImageFilledRectangle(im, 10, 10, 49, 49, gdTiled);

	gdAssertImageEqualsToFile(exp_img, im);

 	/* Destroy it */
 	gdImageDestroy(im);
 	gdImageDestroy(tile);
	return 0;
}
Exemple #14
0
// Prints the error string if in debug, otherwise generates an error image
void Error(const char *pcText) {
	if (g_oCGI.Debug()) {
		printf("Error: %s\n\n", pcText);
		printf("Usage: id=<counter_id> [font=<font_name>] [digits=<num_of_digits>]\n");
		printf("Note: out.gif will be placed in %s\n", COUNTER_ROOT);
	}
	else {
		gdImagePtr pOutImg = NULL;
		pOutImg = gdImageCreate(20, 20);
		if (pOutImg) {
			int iBlack = gdImageColorAllocate(pOutImg, 0, 0, 0);
			int iRed = gdImageColorAllocate(pOutImg, 255, 0, 0);
			gdImageFill(pOutImg, 0, 0, iBlack);
			gdImageLine(pOutImg, 0, 0, 19, 19, iRed);
			gdImageLine(pOutImg, 19, 0, 0, 19, iRed);
			gdImageInterlace(pOutImg, 1);
#ifdef WIN32
			_setmode(_fileno(stdout), _O_BINARY);
#endif
			printf("Content-type: image/gif\n\n");
			gdImageGif(pOutImg, stdout);
		}
	}
} // Error
Exemple #15
0
// Generates the count image from the count string, returns true if all ok
bool CreateImage(const char *pcCount, const char *pcFont, int iDigits) {
	// Set the spare value 
	int iCountLen = strlen(pcCount);
	if (iDigits < iCountLen)
		iDigits = iCountLen;
	int iSpare = 0;
	if (iDigits > iCountLen)
		iSpare = iDigits - iCountLen;
	// Loop vars
	char pcDigitName[STR_SIZE] = "";
	gdImagePtr pImg = NULL;
	FILE *pPic = NULL;
	// Load the zero digit image
	sprintf(pcDigitName, COUNTER_ZERO, pcFont);
	pPic = fopen(pcDigitName, "rb+");
	if (pPic == NULL)
		return false;
	pImg = gdImageCreateFromGif(pPic);
	fclose(pPic);
	if (pImg == NULL)
		return false;
	// Create the output image
	gdImagePtr pOutImg = NULL;
	pOutImg = gdImageCreate(iDigits * pImg->sx, pImg->sy);
	if (pOutImg == NULL) {
		gdImageDestroy(pImg);
		return false;
	}
	// Copy the palette info from one to the other...
	int iColTotal = gdImageColorsTotal(pImg);
	for (int i = 0; i < iColTotal; i++) {
		gdImageColorAllocate(pOutImg, gdImageRed(pImg, i), gdImageGreen(pImg, i), gdImageBlue(pImg, i));
	}
	int iTransCol = gdImageGetTransparent(pImg);
	if (iTransCol >= 0) {
		gdImageColorTransparent(pOutImg, iTransCol);
		gdImageFill(pOutImg, 0, 0, iTransCol);
	}
	// Loop through each leading zero
	int iPos = 0;
	while (iSpare-- > 0) {
		// paste in the image
		gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy);
		iPos += pImg->sx;
	}
	// Delete the zero image
	gdImageDestroy(pImg);
	// Loop through each counter character
	const char *pcPos = pcCount;
	while (*pcPos != '\0') {
		sprintf(pcDigitName, COUNTER_PIC, pcFont, *pcPos);
		// Load the image
		pPic = fopen(pcDigitName, "rb+");
		if (pPic == NULL) {
			gdImageDestroy(pOutImg);
			return false;
		}
		pImg = gdImageCreateFromGif(pPic);
		if (pImg == NULL) {
			gdImageDestroy(pOutImg);
			return false;
		}
		// Paste in the image
		gdImageCopy(pOutImg, pImg, iPos, 0, 0, 0, pImg->sx, pImg->sy);
		iPos += pImg->sx;
		// Delete the image
		gdImageDestroy(pImg);
		// Update the position counter
		pcPos++;
	}
	// Write out the output image
	if (g_oCGI.Debug()) {
		char pcGIFName[STR_SIZE] = COUNTER_ROOT;
		strcat(pcGIFName, "out.gif");
		FILE *pGIFFile = NULL;
		pGIFFile = fopen(pcGIFName, "wb");
		if (pGIFFile) {
			gdImageGif(pOutImg, pGIFFile);
			fclose(pGIFFile);
		}
	}
	else {
		gdImageInterlace(pOutImg, 1);
#ifdef WIN32
		_setmode(_fileno(stdout), _O_BINARY);
#endif
		printf("Content-type: image/gif\n\n");
		gdImageGif(pOutImg, stdout);
	}
	// Destroy the image
	gdImageDestroy(pOutImg);

	return true;
} // CreateImage
Exemple #16
0
static void gd_begin_graph(GVC_t * gvc, graph_t * g, box bb, point pb)
{
    char *bgcolor_str = NULL;
    char *truecolor_str;
    bool truecolor_p = FALSE;	/* try to use cheaper paletted mode */
    bool bg_transparent_p = FALSE;
    int bgcolor;

    external_surface = gvc->job->external_surface;

    init1_gd(gvc, g, bb, pb);

    if (external_surface) {
        im = (gdImagePtr)gvc->job->surface;
    } else {
        truecolor_str = agget(g, "truecolor");	/* allow user to force truecolor */
        bgcolor_str = agget(g, "bgcolor");

        if (truecolor_str && truecolor_str[0])
            truecolor_p = mapbool(truecolor_str);

        if (bgcolor_str && strcmp(bgcolor_str, "transparent") == 0) {
            bg_transparent_p = TRUE;
            if (is_format_truecolor_capable(Output_lang))
                truecolor_p = TRUE;	/* force truecolor */
        }

        if (GD_has_images(g))
            truecolor_p = TRUE;	/* force truecolor */

        if (truecolor_p) {
            if (Verbose)
                fprintf(stderr, "%s: allocating a %dK TrueColor GD image\n",
                        CmdName, ROUND(Viewport.x * Viewport.y * 4 / 1024.));
            im = gdImageCreateTrueColor(Viewport.x, Viewport.y);
        } else {
            if (Verbose)
                fprintf(stderr, "%s: allocating a %dK PaletteColor GD image\n",
                        CmdName, ROUND(Viewport.x * Viewport.y / 1024.));
            im = gdImageCreate(Viewport.x, Viewport.y);
        }
        if (!im) {
            agerr(AGERR, "gdImageCreate returned NULL. Malloc problem?\n");
            return;
        }
    }

    init2_gd(im);

    if (! external_surface) {
        if (bgcolor_str && bgcolor_str[0])
            if (bg_transparent_p)
                bgcolor = transparent;
            else
                bgcolor = gd_resolve_color(bgcolor_str);
        else
            bgcolor = white;

        cstk[0].fillcolor = bgcolor;

        /* Blending must be off to lay a transparent bgcolor.
           Nothing to blend with anyway. */
        gdImageAlphaBlending(im, FALSE);

        gdImageFill(im, im->sx / 2, im->sy / 2, bgcolor);

        /* Blend everything else together,
           especially fonts over non-transparent backgrounds */
        gdImageAlphaBlending(im, TRUE);
    }

#ifdef MYTRACE
    fprintf(stderr, "gd_begin_graph\n");
#endif
}
static ngx_int_t
ngx_http_qrcode_handler(ngx_http_request_t* r)
{
	ngx_http_qrcode_loc_conf_t *qlcf;
	ngx_int_t 	code_size;
	ngx_int_t 	img_margin, img_width;
	ngx_int_t 	fg_color, bg_color;
	ngx_int_t 	x, y, posx, posy;
	int 		img_stream_len;
	u_char		*encoded_txt;
	ngx_int_t	rc;

	qlcf = ngx_http_get_module_loc_conf(r, ngx_http_qrcode_module);

	/* compile args */
	rc = ngx_http_qrcode_compile_args(r, qlcf);
	if (rc != NGX_OK)  {
		ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to compile args.");
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	encoded_txt = ngx_pcalloc(r->pool, qlcf->txt.len + 1);
	ngx_sprintf(encoded_txt, "%V", &qlcf->txt);

	QRcode *code;
	code = QRcode_encodeString((char*)encoded_txt,
			qlcf->version, qlcf->level, qlcf->hint, qlcf->casesensitive);

	if(code == NULL) {
		ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
				"Failed to encode content.exception raised by libqrencode: %s", strerror(errno));
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	code_size	= ((qlcf->size > 2048) ? 1024 : qlcf->size) / code->width;
	code_size   = (code_size == 0) ? 1 : code_size;
	img_margin	= qlcf->margin;
	img_width	= code->width * code_size + 2 * img_margin;

	gdImagePtr img;
	img	= gdImageCreate(img_width, img_width);

	fg_color = gdImageColorAllocate(img,
			qlcf->fg_color[0], qlcf->fg_color[1], qlcf->fg_color[2]);

	bg_color = gdImageColorAllocate(img,
		   	qlcf->bg_color[0], qlcf->bg_color[1], qlcf->bg_color[2]);

	gdImageFill(img, 0, 0, bg_color);

	u_char *p = code->data;
	for (y = 0; y < code->width; y++)
	{
		for (x = 0; x < code->width; x++)
		{
			if (*p & 1) {
				posx = x * code_size + img_margin;
				posy = y * code_size + img_margin;

				gdImageFilledRectangle(img, posx, posy,
						posx + code_size, posy + code_size, fg_color);
			}
			p++;
		}
	}

	u_char *img_stream;
    img_stream = gdImagePngPtr(img, &img_stream_len);

	gdImageDestroy(img);
	QRcode_free(code);

	r->headers_out.status = NGX_HTTP_OK;
	r->headers_out.content_length_n = img_stream_len;

	ngx_str_set(&r->headers_out.content_type, "image/png");
	ngx_http_send_header(r);

	ngx_buf_t* buffer;
	buffer = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));

	if (buffer == NULL) {
		ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
				"Failed to allocate response buffer");
		return NGX_HTTP_INTERNAL_SERVER_ERROR;
	}

	//set up the buffer chain
	ngx_chain_t out;
	buffer->pos = img_stream;
	buffer->last = img_stream + img_stream_len;
	buffer->memory = 1;
	buffer->last_buf = 1;
	out.buf = buffer;
	out.next = NULL;

	return ngx_http_output_filter(r, &out);
}
Exemple #18
0
int
main (void)
{
#ifdef HAVE_LIBPNG
  /* Input and output files */
  FILE *in;
  FILE *out;

  /* Input and output images */
  gdImagePtr im_in = 0, im_out = 0;

  /* Brush image */
  gdImagePtr brush;

  /* Color indexes */
  int white;
  int blue;
  int red;
  int green;

  /* Points for polygon */
  gdPoint points[3];
  int i;

  /* Create output image, in true color. */
  im_out = gdImageCreateTrueColor (256 + 384, 384);
  /* 2.0.2: first color allocated would automatically be background in a 
     palette based image. Since this is a truecolor image, with an 
     automatic background of black, we must fill it explicitly. */
  white = gdImageColorAllocate (im_out, 255, 255, 255);
  gdImageFilledRectangle (im_out, 0, 0, gdImageSX (im_out),
			  gdImageSY (im_out), white);

  /* Set transparent color. */
  gdImageColorTransparent (im_out, white);

  /* Try to load demoin.png and paste part of it into the
     output image. */
  in = fopen ("demoin.png", "rb");
  if (!in)
    {
      fprintf (stderr, "Can't load source image; this demo\n");
      fprintf (stderr, "is much more impressive if demoin.png\n");
      fprintf (stderr, "is available.\n");
      im_in = 0;
    }
  else
    {
      int a;
      im_in = gdImageCreateFromPng (in);
      fclose (in);
      /* Now copy, and magnify as we do so */
      gdImageCopyResampled (im_out, im_in, 32, 32, 0, 0, 192, 192, 255, 255);
      /* Now display variously rotated space shuttles in a circle of our own */
      for (a = 0; (a < 360); a += 45)
	{
	  int cx = cos (a * .0174532925) * 128;
	  int cy = -sin (a * .0174532925) * 128;
	  gdImageCopyRotated (im_out, im_in,
			      256 + 192 + cx, 192 + cy,
			      0, 0, gdImageSX (im_in), gdImageSY (im_in), a);
	}
    }
  red = gdImageColorAllocate (im_out, 255, 0, 0);
  green = gdImageColorAllocate (im_out, 0, 255, 0);
  blue = gdImageColorAllocate (im_out, 0, 0, 255);
  /* Fat Rectangle */
  gdImageSetThickness (im_out, 4);
  gdImageLine (im_out, 16, 16, 240, 16, green);
  gdImageLine (im_out, 240, 16, 240, 240, green);
  gdImageLine (im_out, 240, 240, 16, 240, green);
  gdImageLine (im_out, 16, 240, 16, 16, green);
  gdImageSetThickness (im_out, 1);
  /* Circle */
  gdImageArc (im_out, 128, 128, 60, 20, 0, 720, blue);
  /* Arc */
  gdImageArc (im_out, 128, 128, 40, 40, 90, 270, blue);
  /* Flood fill: doesn't do much on a continuously
     variable tone jpeg original. */
  gdImageFill (im_out, 8, 8, blue);
  /* Polygon */
  points[0].x = 64;
  points[0].y = 0;
  points[1].x = 0;
  points[1].y = 128;
  points[2].x = 128;
  points[2].y = 128;
  gdImageFilledPolygon (im_out, points, 3, green);
  /* 2.0.12: Antialiased Polygon */
  gdImageSetAntiAliased (im_out, green);
  for (i = 0; (i < 3); i++)
    {
      points[i].x += 128;
    }
  gdImageFilledPolygon (im_out, points, 3, gdAntiAliased);
  /* Brush. A fairly wild example also involving a line style! */
  if (im_in)
    {
      int style[8];
      brush = gdImageCreateTrueColor (16, 16);
      gdImageCopyResized (brush, im_in,
			  0, 0, 0, 0,
			  gdImageSX (brush), gdImageSY (brush),
			  gdImageSX (im_in), gdImageSY (im_in));
      gdImageSetBrush (im_out, brush);
      /* With a style, so they won't overprint each other.
         Normally, they would, yielding a fat-brush effect. */
      style[0] = 0;
      style[1] = 0;
      style[2] = 0;
      style[3] = 0;
      style[4] = 0;
      style[5] = 0;
      style[6] = 0;
      style[7] = 1;
      gdImageSetStyle (im_out, style, 8);
      /* Draw the styled, brushed line */
      gdImageLine (im_out, 0, 255, 255, 0, gdStyledBrushed);
    }
  /* Text (non-truetype; see gdtestft for a freetype demo) */
  gdImageString (im_out, gdFontGiant, 32, 32, (unsigned char *) "hi", red);
  gdImageStringUp (im_out, gdFontSmall, 64, 64, (unsigned char *) "hi", red);
  /* Random antialiased lines; coordinates all over the image, 
    but the output will respect a small clipping rectangle */
  gdImageSetClip(im_out, 0, gdImageSY(im_out) - 100,
    100, gdImageSY(im_out)); 
  /* Fixed seed for reproducibility of results */
  srand(100);
  for (i = 0; (i < 100); i++) {
    int x1 = rand() % gdImageSX(im_out);
    int y1 = rand() % gdImageSY(im_out);
    int x2 = rand() % gdImageSX(im_out);
    int y2 = rand() % gdImageSY(im_out);
    gdImageSetAntiAliased(im_out, white);
    gdImageLine (im_out, x1, y1, x2, y2, gdAntiAliased);
  }
  /* Make output image interlaced (progressive, in the case of JPEG) */
  gdImageInterlace (im_out, 1);
  out = fopen ("demoout.png", "wb");
  /* Write PNG */
  gdImagePng (im_out, out);
  fclose (out);
  /* 2.0.12: also write a paletteized version */
  out = fopen ("demooutp.png", "wb");
  gdImageTrueColorToPalette (im_out, 0, 256);
  gdImagePng (im_out, out);
  fclose (out);
  gdImageDestroy (im_out);
  if (im_in)
    {
      gdImageDestroy (im_in);
    }
#else
  fprintf (stderr, "No PNG library support.\n");
#endif /* HAVE_LIBPNG */
  return 0;
}
Exemple #19
0
int main(void)
{
	/* Input and output files */
	FILE *in;
	FILE *out;

	/* Input and output images */
	gdImagePtr im_in, im_out;

	/* Brush image */
	gdImagePtr brush;

	/* Color indexes */
	int white;
	int blue;
	int red;
	int green;

	/* Points for polygon */
	gdPoint points[3];

	/* Create output image, 128 by 128 pixels. */
	im_out = gdImageCreate(128, 128);

	/* First color allocated is background. */
	white = gdImageColorAllocate(im_out, 255, 255, 255);

	/* Set transparent color. */
	gdImageColorTransparent(im_out, white);

	/* Try to load demoin.png and paste part of it into the
		output image. */

	in = fopen("demoin.png", "rb");
	if (!in) {
		fprintf(stderr, "Can't load source image; this demo\n");
		fprintf(stderr, "is much more impressive if demoin.png\n");
		fprintf(stderr, "is available.\n");
		im_in = 0;
	} else {
		im_in = gdImageCreateFromPng(in);
		fclose(in);
		/* Now copy, and magnify as we do so */
		gdImageCopyResized(im_out, im_in, 
			16, 16, 0, 0, 96, 96, 127, 127);		
	}
	red = gdImageColorAllocate(im_out, 255, 0, 0);
	green = gdImageColorAllocate(im_out, 0, 255, 0);
	blue = gdImageColorAllocate(im_out, 0, 0, 255);
	/* Rectangle */
	gdImageLine(im_out, 8, 8, 120, 8, green);	
	gdImageLine(im_out, 120, 8, 120, 120, green);	
	gdImageLine(im_out, 120, 120, 8, 120, green);	
	gdImageLine(im_out, 8, 120, 8, 8, green);	
	/* Circle */
	gdImageArc(im_out, 64, 64, 30, 10, 0, 360, blue);
	/* Arc */
	gdImageArc(im_out, 64, 64, 20, 20, 45, 135, blue);
	/* Flood fill */
	gdImageFill(im_out, 4, 4, blue);
	/* Polygon */
	points[0].x = 32;
	points[0].y = 0;
	points[1].x = 0;
	points[1].y = 64;	
	points[2].x = 64;
	points[2].y = 64;	
	gdImageFilledPolygon(im_out, points, 3, green);
	/* Brush. A fairly wild example also involving a line style! */
	if (im_in) {
		int style[8];
		brush = gdImageCreate(8, 8);
		gdImageCopyResized(brush, im_in,
			0, 0, 0, 0, 
			gdImageSX(brush), gdImageSY(brush),
			gdImageSX(im_in), gdImageSY(im_in));
		gdImageSetBrush(im_out, brush);	
		/* With a style, so they won't overprint each other.
			Normally, they would, yielding a fat-brush effect. */
		style[0] = 0;
		style[1] = 0;
		style[2] = 0;
		style[3] = 0;
		style[4] = 0;
		style[5] = 0;
		style[6] = 0;
		style[7] = 1;
		gdImageSetStyle(im_out, style, 8);
		/* Draw the styled, brushed line */
		gdImageLine(im_out, 0, 127, 127, 0, gdStyledBrushed);
	}
	/* Text */
	gdImageString(im_out, gdFontGiant, 16, 16, 
		(unsigned char *) "hi", red);
	gdImageStringUp(im_out, gdFontSmall, 32, 32, 
		(unsigned char *) "hi", red);
	/* Make output image interlaced (allows "fade in" in some viewers,
		and in the latest web browsers) */
	gdImageInterlace(im_out, 1);
	out = fopen("demoout.png", "wb");
	/* Write PNG */
	gdImagePng(im_out, out);
	fclose(out);
	gdImageDestroy(im_out);
	if (im_in) {
		gdImageDestroy(im_in);
	}
	return 0;
}
Exemple #20
0
int piechart_wedge(FILE *outf, Outchoices *od, double angle, char *s) {
  /* The angle is expressed between 0 and 1. Returns col if wedge was big
     enough to be coloured in, NO_COLOURS for grey, else -1. */
  double x, y, newangle, medangle;
  int colour = black, rc = -1;
  char t[MAXCHARS];
  size_t len;

  if (angle < 0) {
    angle = 1.75 - totangle;  /* rest of the circle because started at 0.75 */
    colour = grey;
  }
  else if (col >= NO_COLOURS)
    angle = 0;  /* As a signal not to make a wedge. Can this happen? */
  else if (angle >= MINANGLE)
    colour = colours[col];

  if (angle >= MINANGLE || (colour == grey && angle > EPSILON)) {
    /* plot one edge of wedge */
    x = (double)XCENTRE + (double)DIAMETER * cos(totangle * TWOPI) / 2. +
      PIE_EPSILON;
    y = (double)YCENTRE + (double)DIAMETER * sin(totangle * TWOPI) / 2. +
      PIE_EPSILON;
    gdImageLine(im, XCENTRE, YCENTRE, (int)x, (int)y, black);

    /* plot other edge of wedge */
    newangle = totangle + angle;
    x = (double)XCENTRE + (double)DIAMETER * cos(newangle * TWOPI) / 2. +
      PIE_EPSILON;
    y = (double)YCENTRE + (double)DIAMETER * sin(newangle * TWOPI) / 2. +
      PIE_EPSILON;
    gdImageLine(im, XCENTRE, YCENTRE, (int)x, (int)y, black);

    /* Fill wedge */
    medangle = totangle + angle / 2.;
    x = (double)XCENTRE + (double)DIAMETER * cos(medangle * TWOPI) / 2.5;
    y = (double)YCENTRE + (double)DIAMETER * sin(medangle * TWOPI) / 2.5;
    if (gdImageGetPixel(im, (int)x, (int)y) == white) {  /* room to colour */
      gdImageFill(im, (int)x, (int)y, colour);
      /* Make label for wedge. If !normalchart, this is done in piechart_key()
	 below instead. (See long comment near bottom of piechart().) */
      if (normalchart) {
	gdImageFilledRectangle(im, BOXESLEFT, boxesy, BOXESLEFT + BOXESSIZE,
			       boxesy + BOXESSIZE, colour);
	if ((len = strlen(s)) <= MAXCHARS - 1)
	  strcpy(t, s);
	else {
	  strncpy(t, s, (MAXCHARS - 3) / 2);
	  strcpy(t + (MAXCHARS - 3) / 2, "...");
	  strncpy(t + (MAXCHARS + 3) / 2, s + len - (MAXCHARS - 4) / 2,
		  (MAXCHARS - 4) / 2);
	  t[MAXCHARS - 1] = '\0';
	}
#ifdef EBCDIC
	strtoascii(t);
#endif
	gdImageString(im, font, TEXTLEFT, boxesy + TEXTOFFSET,
		      (unsigned char *)t, black);
	boxesy += TEXTGAP;
      }
      rc = (colour == grey)?NO_COLOURS:col;
    }   /* end if (room to colour) */
    totangle = newangle;
    col++;
  }
  return(rc);
}
Exemple #21
0
void graficotorta(int n, long cantidad[], int estado[]) {

    gdImagePtr imagen;
    FILE *archivo;
    char titulo[513];
    char etiqueta[512];
    int blanco, negro, color;
    gdFontPtr fuente = gdFontGetSmall();
    
    imagen = gdImageCreateTrueColor(IMG_WIDTH, IMG_HEIGHT);

    int i;
    int angEtiqueta, xEtiqueta, yEtiqueta;
    float eqgrados;
    int iniciotrozo, fintrozo;
    long suma = 0;
    int aprox;
    for (i=0; i<n; i++) {    
        suma = suma + cantidad[i];
    }
    eqgrados = 360.0/suma;
    
    if (imagen) {
        blanco = gdImageColorAllocate(imagen, 255, 255, 255);
        negro = gdImageColorAllocate(imagen, 0, 0, 0);
        // Pintamos el fondo Blanco
        gdImageFill(imagen, 0, 0, blanco);
        iniciotrozo = 0;
        for (i=0; i<n; i++) {
            fintrozo = (int) iniciotrozo+cantidad[i]*eqgrados;
            angEtiqueta = (iniciotrozo + fintrozo) / 2;
            xEtiqueta = cos(angEtiqueta*PI/180) * 220 + 400;
            yEtiqueta = sin(angEtiqueta*PI/180) * 220 + 290;
            // Color
            color = gdImageColorAllocate(imagen, color_aleatoreo(), color_aleatoreo(), color_aleatoreo());
            //Pintamos fondo el trozo
            gdImageFilledArc(imagen, 400, 300, 400, 400, iniciotrozo, fintrozo, color, gdArc);       
            //Etiqueta de peticion
            memset(etiqueta, 0, 513);
            snprintf(etiqueta, 512, "peticion %s",intStr(estado[i]));
            gdImageString(imagen, fuente, xEtiqueta-25, yEtiqueta, (unsigned char *) etiqueta, negro);
            //Correccion de aproximacion para el porcentaje
            aprox = cantidad[i] * 1000 / suma;
            aprox = aprox%10;
            if (aprox>=5)
                aprox = 1;
            else
                aprox = 0;
            //Etiqueta de porcentaje
            memset(etiqueta, 0, 513);
            snprintf(etiqueta, 512, "%s%%",longStr(cantidad[i]*100/suma+aprox));
            if (cantidad[i]*100/suma<3){      //Para que la etiqueta sea legible
                xEtiqueta = xEtiqueta + 52;
                yEtiqueta = yEtiqueta - 15;
            }
            gdImageString(imagen, fuente, xEtiqueta, yEtiqueta+15, (unsigned char *) etiqueta, negro);
            iniciotrozo = (int) iniciotrozo+cantidad[i]*eqgrados;
        }

        //Pintamos borde del circulo
        gdImageArc(imagen, 400, 300, 400, 400, 0, 360, negro);
        // Coloco el título
        memset(titulo, 0, 513);
        snprintf(titulo, 512, "Peticiones Por Estado");
        gdImageString(imagen, fuente, (int) IMG_WIDTH * 0.4, 25, (unsigned char *) titulo, negro);
        // Pintamos Borde
        gdImageLine(imagen, BORDE_ANCHO, BORDE_ALTO, (IMG_WIDTH - BORDE_ANCHO), BORDE_ALTO, negro); //linea horiz superior
        gdImageLine(imagen, BORDE_ANCHO, (IMG_HEIGHT - BORDE_ALTO), (IMG_WIDTH - BORDE_ANCHO), (IMG_HEIGHT - BORDE_ALTO), negro); //linea horiz inferior
        gdImageLine(imagen, BORDE_ANCHO, BORDE_ALTO, BORDE_ANCHO, (IMG_HEIGHT - BORDE_ALTO), negro); //linea vertical izquierda
        gdImageLine(imagen, (IMG_WIDTH - BORDE_ANCHO), BORDE_ALTO, (IMG_WIDTH - BORDE_ANCHO), (IMG_HEIGHT - BORDE_ALTO), negro); //linea horiz derecha
        // Guardar imagen
        archivo = fopen("graficot.jpg", "wb");
        if (archivo != NULL) {
            gdImageJpeg(imagen, archivo, 100);
            fclose(archivo);
        }
        gdImageDestroy(imagen);
    }
 
}