예제 #1
0
int main()
{
	gdImagePtr im;
	int error = 0;
	int c, c1, c2, c3, c4, color, i;

	im = gdImageCreateTrueColor(5, 5);
	c = gdImageColorExact(im, 255, 0, 255);
	c2 = gdImageColorExactAlpha(im, 255, 0, 255, 100);
	gdImageDestroy(im);

	if (gdTestAssert(c == 0xFF00FF) != 1) {
		error = -1;
	}
	if (gdTestAssert(c2 == 0x64FF00FF) != 1) {
		error = -1;
	}

	im = gdImageCreate(5, 5);
	c1 = gdImageColorAllocate(im, 255, 0, 255);
	c2 = gdImageColorAllocate(im, 255, 200, 0);
	c3 = gdImageColorAllocateAlpha(im, 255, 0, 255, 100);

	c1 = gdImageColorExact(im, 255, 0, 255);
	c2 = gdImageColorExact(im, 255, 200, 0);
	c3 = gdImageColorExactAlpha(im, 255, 0, 255, 100);
	c4 = gdImageColorExactAlpha(im, 255, 34, 255, 100);

	if (gdTestAssert(c1 == 0) != 1) {
		error = -1;
	}
	if (gdTestAssert(c2 == 1) != 1) {
		error = -1;
	}
	if (gdTestAssert(c3 == 2) != 1) {
		error = -1;
	}
	if (gdTestAssert(c4 == -1) != 1) {
		error = -1;
	}

	color = gdTrueColorAlpha(gdImageRed(im, c1), gdImageGreen(im, c1),
					gdImageBlue(im, c1), 0);
	if (gdTestAssert(color == 0xFF00FF) != 1) {
		error = -1;
	}
	color = gdTrueColorAlpha(gdImageRed(im, c2), gdImageGreen(im, c2),
					gdImageBlue(im, c2), 0);
	if (gdTestAssert(color == 0xFFC800) != 1) {
		error = -1;
	}
	color = gdTrueColorAlpha(gdImageRed(im, c3), gdImageGreen(im, c3),
					gdImageBlue(im, c3), 0);
	if (gdTestAssert(color == 0xFF00FF) != 1) {
		error = -1;
	}
	gdImageDestroy(im);

	return error;
}
예제 #2
0
파일: bug00132.c 프로젝트: LuaDist/libgd
int main()
{
 	gdImagePtr im, im2;
 	int error = 0;
	char path[2048];
	const char *file_exp = "bug00132_exp.png";
	char *ret = NULL;

	im = gdImageCreateTrueColor(50, 30);

	if (!im) {
		printf("can't get truecolor image\n");
		return 1;
	}

	gdImageAlphaBlending(im, 0);
	gdImageFilledRectangle(im, 0, 0, 200, 200, gdTrueColorAlpha(0, 0, 0, 127));

	sprintf(path, "%s/freetype/DejaVuSans.ttf", GDTEST_TOP_DIR);

	ret = gdImageStringFT(im, NULL,  - 0xFFFFFF, path, 14.0, 0.0, 10, 20, "ϑ θ");
	if (ret) {
		error = 1;
		printf(ret);
	} else {
		sprintf(path, "%s/freetype/%s", GDTEST_TOP_DIR, file_exp);
		if (!gdAssertImageEqualsToFile(path, im)) {
			error = 1;
			printf("Reference image and destination differ\n");
		}
	}
	gdImageDestroy(im);
	return error;
}
예제 #3
0
int main(void)
{
	/* 2.0.22: can't depend on PNG either  */
#ifndef HAVE_LIBPNG
	fprintf(stderr, "Requires PNG support, gd was compiled without it\n");
	return 0;
#else
	char *error;
	FILE *in = 0;
	FILE *out;
	gdImagePtr im;
	int radius;
	/* Create an image of text on a circle, with an
	 * alpha channel so that we can copy it onto a
	 * background
	 * TBB: 2.0.18: shouldn't depend on JPEG
	 */
#ifdef HAVE_LIBJPEG
	in = fopen("eleanor.jpg", "rb");
	if(!in) {
		im = gdImageCreateTrueColor(300, 300);
	} else {
		im = gdImageCreateFromJpeg(in);
		fclose(in);
	}
#else
	im = gdImageCreateTrueColor(300, 300);
#endif /* HAVE_LIBJPEG */
	if(!im) {
		fprintf(stderr, "gdImageCreateTrueColor failed \n");
		return 1;
	}
	if(gdImageSX(im) < gdImageSY(im)) {
		radius = gdImageSX(im) / 2;
	} else {
		radius = gdImageSY(im) / 2;
	}

	error = gdImageStringFTCircle(im,
	                              gdImageSX(im) / 2, gdImageSY(im) / 2,
	                              radius, radius / 2,
	                              0.8, "arial", 24, "top text", "bottom text",
	                              gdTrueColorAlpha(192, 100, 255, 32)
	                             );
	if(error)  {
		fprintf(stderr, "gdImageStringFTEx error: %s\n", error);
	}

	out = fopen("gdfx.png", "wb");
	if(!out) {
		fprintf(stderr, "Can't create gdfx.png\n");
		return 1;
	}

	gdImagePng(im, out);
	fclose(out);
	gdImageDestroy(im);
#endif /* HAVE_LIBPNG */
	return 0;
}
예제 #4
0
static void qb_copy_y_pixel_to_scanline_F64(void *scanline, int *tpixels, uint32_t width) {
	float64_t *p = scanline;
	uint32_t i;
	int r;
	for(i = 0; i < width; i++) {
		r = qb_clamp_float64(p[0], gdRedMax);
		tpixels[i] = gdTrueColorAlpha(r, r, r, 255);
		p += 1;
	}
}
예제 #5
0
static void qb_copy_ya_pixel_to_scanline_F64(void *scanline, int *tpixels, uint32_t width) {
	float64_t *p = scanline;
	uint32_t i;
	int r, a;
	for(i = 0; i < width; i++) {
		r = qb_clamp_float64(p[0], gdRedMax);
		a = gdAlphaTransparent - qb_clamp_float64(p[1], 255);
		tpixels[i] = gdTrueColorAlpha(r, r, r, a);
		p += 2;
	}
}
예제 #6
0
static void qb_copy_rgb_pixel_to_scanline_F64(void *scanline, int *tpixels, uint32_t width) {
	float64_t *p = scanline;
	uint32_t i;
	int r, g, b;
	for(i = 0; i < width; i++) {
		r = qb_clamp_float64(p[0], gdRedMax);
		g = qb_clamp_float64(p[1], gdGreenMax);
		b = qb_clamp_float64(p[2], gdBlueMax);
		tpixels[i] = gdTrueColorAlpha(r, g, b, 255);
		p += 3;
	}
}
예제 #7
0
static void qb_copy_rgba_pixel_to_scanline_U08(void *scanline, int *tpixels, uint32_t width) {
	uint8_t *p = scanline;
	uint32_t i;
	int r, g, b, a;
	for(i = 0; i < width; i++) {
		r = p[3];
		g = p[2];
		b = p[1];
		a = (int) (((float32_t) p[0]) * (255.0f / 127.0f));
		tpixels[i] = gdTrueColorAlpha(r, g, b, a);
		p += 4;
	}
}
예제 #8
0
int gen_image(const char* filename, int idx, int reverse_x, int width, int height, int bgd)
{
   double gradient = height / (width*2.0);
   int offset = idx*width;
	int x1,y1,x2,y2, error = 0;

   gdImagePtr im = gdImageCreateTrueColor(width,height);
   if (bgd==1) {
		gdImageFilledRectangle(im,0,0,width-1,height-1, gdTrueColorAlpha(255, 255, 255, 0));
   } else {
   	gdImageFilledRectangle(im,0,0,width-1,height-1, gdTrueColorAlpha(255, 255, 0, 0));
   }

   gdImageSetAntiAliased(im, gdTrueColorAlpha(0,0,0,0));

   /*
   test for potential segfault (introduced with AA improvements, fixed
	with the same patch - but I didn't notice it until later).*/
   gdImageLine(im,-1,-1,-1,-1,gdAntiAliased);

	x1 = floor(reverse_x * -width + 0.5);
	y1 = (offset-width) * gradient + 0.5;

	x2 = floor(reverse_x *  width*2 + 0.5);
	y2 = floor((offset+width*2) * gradient + 0.5);

   // draw an AA line
   gdImageLine(im, x1, y1, x2, y2, gdAntiAliased);

	gdImageLine(im, 0, im->sy - 1, im->sx, im->sy - 1, 0x40FF0000);

	if (!gdAssertImageEqualsToFile(filename, im)) {
		printf("%s failed\n", filename);
		error = 1;
	}
   gdImageDestroy(im);
   return error;
}
예제 #9
0
void set_image_RGBY(image_info *image_RGBY_in,
                    image_info *image_RGB_in,
                    image_info *image_Y_in,
                    double      RGB_min,
                    double      RGB_max,
                    double      Y_min,
                    double      Y_max){
  double *image_RGBY;
  double *image_RGB;
  double *image_Y;
  int     i_x,i_y,i_pixel;
  double  RGB_range;
  double  Y_range;
  double  brightness;
  int     pixel_value;
  int     red,green,blue,alpha;
  int     n_colours;
  
  image_RGBY=image_RGBY_in->values;
  image_RGB =image_RGB_in->values;
  image_Y   =image_Y_in->values;
  RGB_range =RGB_max-RGB_min;
  Y_range   =Y_max  -Y_min;
  n_colours =image_RGB_in->n_colours-1;
  for(i_x=0,i_pixel=0;i_x<image_RGBY_in->width;i_x++){
    for(i_y=0;i_y<image_RGBY_in->height;i_y++,i_pixel++){

       // Set the pixel colour
       pixel_value=(int)(n_colours*(image_RGB[i_pixel]-RGB_min)/RGB_range);
       pixel_value=MAX(0,MIN(pixel_value,n_colours));
       red        =(int)image_RGB_in->colour_table[0][pixel_value];
       green      =(int)image_RGB_in->colour_table[1][pixel_value];
       blue       =(int)image_RGB_in->colour_table[2][pixel_value];
       alpha      =gdAlphaOpaque;

       // Compute the brightness of the pixel [0.->1.]
       brightness =(image_Y[i_pixel]-Y_min)/Y_range;
       brightness =MAX(0.,MIN(brightness,1.));

       // Scale the pixel colours by the brightness
       red  =(int)(brightness*(double)red);
       green=(int)(brightness*(double)green);
       blue =(int)(brightness*(double)blue);

       gdImageSetPixel(image_RGBY_in->gd_ptr,i_x,i_y,gdTrueColorAlpha(red,green,blue,alpha));
    }
  }
}
예제 #10
0
gdImagePtr renderDrawing(ei::DnaDrawing *d)
{
    // make new image, true color, with alpha support
    gdImagePtr img = 0;
    img = gdImageCreateTrueColor(ei::Tools::maxWidth, ei::Tools::maxHeight);
    if (0 == img)
    {
        std::cout << "Could not create image" << std::endl;
        return 0;
    }
    gdImageAlphaBlending(img, 1);
    int black = gdTrueColor(0,0,0);         // also acts as background

    // render image:
    // * for each polygon:
    ei::DnaPolygonList *polys = d->polygons();
    ei::DnaPolygonList::iterator iter;
    for (iter = polys->begin(); iter != polys->end(); iter++)
    {
        ei::DnaPolygon *poly = *iter;
        // ** allocate its color & alpha
        ei::DnaBrush *brush = poly->brush();
        int color = gdTrueColorAlpha(brush->r, brush->g, brush->b, brush->a);
        // ** render a closed polygon:
        // *** make array of gdPoints
        ei::DnaPointList *points = poly->points();
        gdPoint gdPts[points->size()];
        // *** translate points
        ei::DnaPointList::iterator eachPt;
        for (int i=0; i < points->size(); i++)
        {
            gdPts[i].x = (*points)[i]->x;
            gdPts[i].y = (*points)[i]->y;
        }
        // *** render via gdImageFilledPolygon().
        gdImageFilledPolygon(img, gdPts, points->size(), color);
    }

    return img;
}
예제 #11
0
파일: rotated.c 프로젝트: libgd/gd-pango
int main(int argc, char *argv[])
{
	gdImagePtr im;
	char *text;
	FILE *fp;

	int w,h;
	int margin_x, margin_y;
	gdPangoContext *context;
	gdPangoColors default_colors;
	PangoContext *pangocontext;
	PangoMatrix rotated_matrix = PANGO_MATRIX_INIT;
	PangoLayout *layout;

	gdPangoInit();

	default_colors.fg = gdTrueColorAlpha(0, 255, 255, 0);
	default_colors.bg = gdTrueColorAlpha(255, 255, 255, 0);
	default_colors.alpha = 0;
/*
	text = readFile("russian.txt");
	text = readFile("arabic.txt");
	text = readFile("hebrew.txt");
	text = readFile("german.txt");
 	text = readFile("english.txt");
	text = readFile("japanese.txt");
*/

	text = readFile("english.txt");

	context = gdPangoCreateContext();

	gdPangoSetDpi(context, 96, 96);
	gdPangoSetMinimumSize(context, 800, 200);

	gdPangoSetDefaultColor(context, &default_colors);
	gdPangoSetMarkup(context, text, -1);
	free(text);

	pangocontext = gdPangoGetPangoContext(context);
	layout = gdPangoGetPangoLayout(context);
	pango_context_set_base_dir(pangocontext, PANGO_DIRECTION_LTR);


	w = gdPangoGetLayoutWidth(context);
	h = gdPangoGetLayoutHeight(context);
   margin_x  = 0;
   margin_y  = 0;
   im = gdImageCreateTrueColor(800, 800);
	gdPangoRenderTo(context, im, margin_x, margin_y);

	fp = fopen("c.png", "wb");
	gdImagePng(im, fp);
	fclose(fp);

	gdImageDestroy(im);

	context->angle = -30;

	if (context->angle != 0.0) {
		pango_matrix_rotate (&rotated_matrix, context->angle);
		pango_context_set_matrix(pangocontext, &rotated_matrix);
		pango_layout_set_alignment(layout, PANGO_ALIGN_RIGHT);
		pango_layout_context_changed (layout);
		context->matrix = &rotated_matrix;
	}

	/* Render to a new image, sized for a rotated text */
	im = gdPangoCreateSurfaceDraw(context);
	fp = fopen("d.png", "wb");
	gdImagePng(im, fp);
	fclose(fp);
	gdPangoFreeContext(context);
	gdImageDestroy(im);
	return 0;
}
예제 #12
0
파일: gdtest.c 프로젝트: ebichu/dd-wrt
/* Compare two buffers, returning the number of pixels that are
 * different and the maximum difference of any single color channel in
 * result_ret.
 *
 * This function should be rewritten to compare all formats supported by
 * cairo_format_t instead of taking a mask as a parameter.
 */
void gdTestImageDiff(gdImagePtr buf_a, gdImagePtr buf_b,
                     gdImagePtr buf_diff, CuTestImageResult *result_ret)
{
	int x, y;
	int c1, c2;

	for (y = 0; y < gdImageSY(buf_a); y++) {
		for (x = 0; x < gdImageSX(buf_a); x++) {
			c1 = gdImageGetTrueColorPixel(buf_a, x, y);
			c2 = gdImageGetTrueColorPixel(buf_b, x, y);

			/* check if the pixels are the same */
			if (c1 != c2) {
				int r1,b1,g1,a1,r2,b2,g2,a2;
				unsigned int diff_a,diff_r,diff_g,diff_b;

				a1 = gdTrueColorGetAlpha(c1);
				a2 = gdTrueColorGetAlpha(c2);
				diff_a = abs (a1 - a2);
				diff_a *= 4;  /* emphasize */

				if (diff_a) {
					diff_a += 128; /* make sure it's visible */
				}
				if (diff_a > gdAlphaMax) {
					diff_a = gdAlphaMax/2;
				}

				r1 = gdTrueColorGetRed(c1);
				r2 = gdTrueColorGetRed(c2);
				diff_r = abs (r1 - r2);
				/* diff_r *= 4;  /* emphasize */
				if (diff_r) {
					diff_r += gdRedMax/2; /* make sure it's visible */
				}
				if (diff_r > 255) {
					diff_r = 255;
				}

				g1 = gdTrueColorGetGreen(c1);
				g2 = gdTrueColorGetGreen(c2);
				diff_g = abs (g1 - g2);
				diff_g *= 4;  /* emphasize */
				if (diff_g) {
					diff_g += gdGreenMax/2; /* make sure it's visible */
				}
				if (diff_g > 255) {
					diff_g = 255;
				}

				b1 = gdTrueColorGetBlue(c1);
				b2 = gdTrueColorGetBlue(c2);
				diff_b = abs (b1 - b2);
				diff_b *= 4;  /* emphasize */
				if (diff_b) {
					diff_b += gdBlueMax/2; /* make sure it's visible */
				}
				if (diff_b > 255) {
					diff_b = 255;
				}

				result_ret->pixels_changed++;
				if (buf_diff) gdImageSetPixel(buf_diff, x,y, gdTrueColorAlpha(diff_r, diff_g, diff_b, diff_a));
			} else {
				if (buf_diff) gdImageSetPixel(buf_diff, x,y, gdTrueColorAlpha(255,255,255,0));
			}
		}
	}
}
예제 #13
0
파일: img.c 프로젝트: kwolekr/imgcmp
void gdImageCopyResampled(gdImagePtr dst, gdImagePtr src,
						  int dstX, int dstY, int srcX, int srcY, 
						  int dstW, int dstH, int srcW, int srcH) {
	int x, y;
	double sy1, sy2, sx1, sx2;

	for (y = dstY; y < dstY + dstH; y++) {
		sy1 = ((double)y       - (double)dstY) * (double)srcH / (double)dstH;
		sy2 = ((double)(y + 1) - (double)dstY) * (double)srcH / (double)dstH;

		for (x = dstX; x < dstX + dstW; x++) {
			double sx, sy;
			double spixels = 0;
			double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;

			sx1 = ((double)x       - (double)dstX) * (double)srcW / (double)dstW;
			sx2 = ((double)(x + 1) - (double)dstX) * (double)srcW / (double)dstW;

			sy = sy1;
			do {
				double yportion;

				if (floor2(sy) == floor2(sy1)) {
					yportion = 1.0 - (sy - floor2(sy));
					if (yportion > sy2 - sy1)
						yportion = sy2 - sy1;
					sy = floor2(sy);
				} else if (sy == floor2(sy2)) {
					yportion = sy2 - floor2(sy2);
				} else {
					yportion = 1.0;
				}

				sx = sx1;
				do {
					double xportion;
					double pcontribution;
					int p;

					if (floor2(sx) == floor2(sx1)) {
						xportion = 1.0 - (sx - floor2(sx));
						if (xportion > sx2 - sx1)
							xportion = sx2 - sx1;
						sx = floor2 (sx);
					} else if (sx == floor2(sx2)) {
						xportion = sx2 - floor2(sx2);
					} else {
						xportion = 1.0;
					}
					pcontribution = xportion * yportion;

					p = gdImageGetTrueColorPixel(src, (int)sx + srcX, (int)sy + srcY);

					red   += gdTrueColorGetRed (p) * pcontribution;
					green += gdTrueColorGetGreen (p) * pcontribution;
					blue  += gdTrueColorGetBlue (p) * pcontribution;
					spixels += xportion * yportion;

					sx += 1.0;
				} while (sx < sx2);
				sy += 1.0;
			} while (sy < sy2);

			if (spixels != 0.0) {
				red   /= spixels;
				green /= spixels;
				blue  /= spixels;
			}

			if (red > 255.0)
				red = 255.0;
			if (green > 255.0)
				green = 255.0;
			if (blue > 255.0)
				blue = 255.0;

			gdImageSetPixel(dst, x, y, gdTrueColorAlpha((int)red, (int)green, (int)blue, 0));
		}
	}
}
예제 #14
0
파일: annotate.c 프로젝트: Gismo88/libgd
int main(int argc, char *argv[])
{
#ifndef HAVE_LIBFREETYPE
	(void)argc;
	(void)argv;

	/* 2.0.12 */
	fprintf(stderr, "annotate is not useful without freetype.\n"
	         "Install freetype, then './configure; make clean; make install'\n"
	         "the gd library again.\n"
	        );
	return 1;
#else
	gdImagePtr im;
	char *iin, *iout;
	FILE *in, *out;
	char s[1024];
	int bounds[8];
	int lines = 1;
	int color = gdTrueColor(0, 0, 0);
	char font[1024];
	int size = 12;
	int align = left;
	int x = 0, y = 0;
	char *fontError;

	strcpy(font, "times");

	if(argc != 3) {
		fprintf(stderr, "Usage: annotate imagein.jpg imageout.jpg\n\n");
		fprintf(stderr, "Standard input should consist of\n");
		fprintf(stderr, "lines in the following formats:\n");
		fprintf(stderr, "color r g b (0-255 each) [a (0-127, 0 is opaque)]\n");
		fprintf(stderr, "font fontname (max name length 1024)\n");
		fprintf(stderr, "size pointsize\n");
		fprintf(stderr, "align (left|right|center)\n");
		fprintf(stderr, "move x y\n");
		fprintf(stderr, "text actual-output-text\n\n");
		fprintf(stderr,
		        "If the file 'paris.ttf' exists in /usr/share/fonts/truetype or in a\n");
		fprintf(stderr,
		        "location specified in the GDFONTPATH environment variable, 'font paris' is\n");
		fprintf(stderr,
		        "sufficient. You may also specify the full, rooted path of a font file.\n");
		exit(1);
	}

	iin = argv[1];
	iout = argv[2];

	in = fopen(iin, "rb");
	if(!in) {
		fprintf(stderr, "Couldn't open %s\n", iin);
		exit(2);
	}

#ifdef HAVE_LIBJPEG
	im = gdImageCreateFromJpeg(in);
#else
	fprintf(stderr, "No JPEG library support available.\n");
	exit(1);
#endif

	fclose(in);

	if(!im) {
		fprintf(stderr, "%s did not load properly\n", iin);
		exit(3);
	}

	while(fgets(s, sizeof(s), stdin)) {
		char *st;
		char *text;

		st = strtok(s, " \t\r\n");
		if(!st) {
			/* Be nice about blank lines */
			continue;
		}

		if(!strcmp(st, "font")) {
			char *st = strtok(0, " \t\r\n");
			if(!st) {
				goto badLine;
			} else {
				const unsigned int font_len = strlen(st);
				if (font_len >= 1024) {
					fprintf(stderr, "Font maximum length is 1024, %d given\n", font_len);
					goto badLine;
				}
				strncpy(font, st, font_len);
			}
		} else if(!strcmp(st, "align")) {
			char *st = strtok(0, " \t\r\n");

			if(!st) {
				goto badLine;
			}

			if(!strcmp(st, "left")) {
				align = 0;
			} else if(!strcmp(st, "center")) {
				align = 1;
			} else if(!strcmp(st, "right")) {
				align = 2;
			}
		} else if(!strcmp(st, "size")) {
			char *st = strtok(0, " \t\r\n");

			if(!st) {
				goto badLine;
			}

			size = atoi(st);
		} else if(!strcmp(st, "color")) {
			char *st = strtok(0, "\r\n");
			int r, g, b, a = 0;

			if(!st) {
				goto badLine;
			}

			if(sscanf(st, "%d %d %d %d", &r, &g, &b, &a) < 3) {
				fprintf(stderr, "Bad color at line %d\n", lines);
				exit(2);
			}

			color = gdTrueColorAlpha(r, g, b, a);
		} else if(!strcmp(st, "move")) {
			char *st = strtok(0, "\r\n");

			if(!st) {
				goto badLine;
			}

			if(sscanf(st, "%d %d", &x, &y) != 2) {
				fprintf(stderr, "Missing coordinates at line %d\n", lines);
				exit(3);
			}
		} else if(!strcmp(st, "text")) {
			int rx = x;

			text = strtok(0, "\r\n");
			if(!text) {
				text = "";
			}

			gdImageStringFT(0, bounds, color, font, size, 0, x, y, text);

			switch(align) {
			case left:
				break;

			case center:
				rx -= (bounds[2] - bounds[0]) / 2;
				break;

			case right:
				rx -= (bounds[2] - bounds[0]);
				break;
			}

			fontError = gdImageStringFT(im, 0, color, font, size, 0, rx, y, text);
			if(fontError) {
				fprintf(stderr, "font error at line %d: %s\n", lines, fontError);
				exit(7);
			}

			y -= (bounds[7] - bounds[1]);
		} else {
			goto badLine;
		}

		lines++;
		continue;

badLine:
		fprintf(stderr, "Bad syntax, line %d\n", lines);
		exit(4);
	}

	out = fopen(iout, "wb");
	if(!out) {
		fprintf(stderr, "Cannot create %s\n", iout);
		exit(5);
	}
#ifdef HAVE_LIBJPEG
	gdImageJpeg(im, out, 95);
#else
	fprintf(stderr, "No JPEG library support available.\n");
#endif
	gdImageDestroy(im);
	fclose(out);
	return 0;
#endif /* HAVE_LIBFREETYPE */
}
예제 #15
0
void gdImageCopyResampled (uint8_t *dst,
                      uint8_t *src,
                      int dstX, int dstY,
                      int srcX, int srcY,
                      int dstW, int dstH, int srcW, int srcH)
{
  int x, y;
  double sy1, sy2, sx1, sx2;
  for (y = dstY; (y < dstY + dstH); y++)
    {
      sy1 = ((double) y - (double) dstY) * (double) srcH / (double) dstH;
      sy2 = ((double) (y + 1) - (double) dstY) * (double) srcH /
        (double) dstH;
      for (x = dstX; (x < dstX + dstW); x++)
        {
          double sx, sy;
          double spixels = 0;
          double red = 0.0, green = 0.0, blue = 0.0, alpha = 0.0;
          sx1 = ((double) x - (double) dstX) * (double) srcW / dstW;
          sx2 = ((double) (x + 1) - (double) dstX) * (double) srcW / dstW;
          sy = sy1;
          do
            {
              double yportion;
              if (floor2 (sy) == floor2 (sy1))
                {
                  yportion = 1.0 - (sy - (double)floor2 (sy));
                  if (yportion > sy2 - sy1)
                    {
                      yportion = sy2 - sy1;
                    }
                  sy = (double)floor2 (sy);
                }
              else if (sy == floor2 (sy2))
                {
                  yportion = sy2 - (double)floor2 (sy2);
                }
              else
                {
                  yportion = 1.0;
                }
              sx = sx1;
              do
                {
                  double xportion;
                  double pcontribution;
                  int p;
                  if (floor2 (sx) == floor2 (sx1))
                    {
                      xportion = 1.0 - (sx - (double)floor2 (sx));
                      if (xportion > sx2 - sx1)
                        {
                          xportion = sx2 - sx1;
                        }
                      sx = (double)floor2 (sx);
                    }
                  else if (sx == floor2 (sx2))
                    {
                      xportion = sx2 - (double)floor2 (sx2);
                    }
                  else
                    {
                      xportion = 1.0;
                    }
                  pcontribution = xportion * yportion;
                  /* 2.08: previously srcX and srcY were ignored.
                     Andrew Pattison */
                  p = gdImageGetTrueColorPixel (src,
                                                (int) sx + srcX,
                                                (int) sy + srcY, srcW);
                  red += gdTrueColorGetRed (p) * pcontribution;
                  green += gdTrueColorGetGreen (p) * pcontribution;
                  blue += gdTrueColorGetBlue (p) * pcontribution;
                  alpha += gdTrueColorGetAlpha (p) * pcontribution;
                  spixels += xportion * yportion;
                  sx += 1.0;
                }
              while (sx < sx2);
              sy += 1.0;
            }
          while (sy < sy2);
          if (spixels != 0.0)
            {
              red /= spixels;
              green /= spixels;
              blue /= spixels;
              alpha /= spixels;
            }
          /* Clamping to allow for rounding errors above */
          if (red > 255.0)
            {
              red = 255.0;
            }
          if (green > 255.0)
            {
              green = 255.0;
            }
          if (blue > 255.0)
            {
              blue = 255.0;
            }
          if (alpha > gdAlphaMax)
            {
              alpha = gdAlphaMax;
            }
          gdImageSetPixel (dst,
                           x, y,
                           gdTrueColorAlpha ((int) red,
                                             (int) green,
                                             (int) blue, (int) alpha), dstW);
        }
    }
}
예제 #16
0
int
main (int argc, char ** argv)
{
  if (argc != 3) {
    usage ();
  }
  #ifdef DEBUG_WIN_X
  if (fork() != 0) {
    printf ("Forked to background, exiting.\n");
    exit (0);
  }
  #endif
  log_open (LOGPATH);
  int fill_color = gdTrueColorAlpha (100, 100, 100, gdAlphaTransparent / 2);
  int line_color = gdTrueColorAlpha (175, 175, 175, gdAlphaOpaque);
  int text_color = gdTrueColorAlpha (255, 255, 255, gdAlphaOpaque);
  FILE * img_out;
  int img_time;
  char img_filename[64];
  gdImagePtr camera_1;
  gdImagePtr camera_2;
  gdImagePtr output;
  int numloops = 0;
  int fpstimer;
  double a1, a2;
  tolerance_file_t * tol;
  int alliance;
  tol = get_tolerance (TOLPATH);
  log_write ("Got tolerance values from %s.", TOLPATH);
  if (argv[1][0] == 98) {
    log_write ("Seeking blue targets.");
    alliance = ALLIANCE_BLUE;
  } else {
    log_write ("Seeking red targets.");
    alliance = ALLIANCE_RED;
  }
  char * jpeg_buff;
  char * jpeg_buff_2;
  int jpeg_buff_size;
  int jpeg_buff_size_2;
  int x, y;
  blob * cam1_green;
  blob * cam2_green;
  blob * cam1_red;
  blob * cam2_red;
  blob * target_blob;
  int loop_ctr;
  loop_ctr = 0;
  float lastfps;
  log_write ("Beginning main loop.");
  while (1) {
    #ifndef DISABLE_IMG
    output = gdImageCreateTrueColor (1280, 480);
    #endif

    #ifndef DEBUG_WIN
    
    log_write ("Getting first jpeg.");
    jpeg_buff = get_jpeg (CAM1, &jpeg_buff_size);
    if (jpeg_buff == NULL) {
      log_write ("First jpeg buffer is null!");
      exit (1);
    }
    if (jpeg_buff_size <= 0) {
      log_write ("First jpeg buffer size is invalid!");
      exit (1);
    }
    log_write ("First jpeg received. Size: %d", jpeg_buff_size);
    camera_1 = gdImageCreateFromJpegPtr (jpeg_buff_size, jpeg_buff);
    if (camera_1 == NULL) {
      log_write ("gdImageCreateFromJpegPtr () failed for jpeg 1!");
      exit (1);
    }
    log_write ("Getting second jpeg.");
    jpeg_buff_2 = get_jpeg (CAM2, &jpeg_buff_size_2);
    if (jpeg_buff_2 == NULL) {
      log_write ("Second jpeg buffer is null!");
      exit (1);
    }
    if (jpeg_buff_size_2 <= 0) {
      log_write ("Second jpeg buffer size is invalid!");
      exit (1);
    }
    log_write ("Second jpeg received. Size: %d", jpeg_buff_size_2);
    camera_2 = gdImageCreateFromJpegPtr (jpeg_buff_size_2, jpeg_buff_2);
    if (camera_2 == NULL) {
      log_write ("gdImageCreateFromJpegPtr () failed for jpeg 2!");
      exit (1);
    }
    
    #endif // !DEBUG_WIN
    
    #ifdef DEBUG_WIN
    
    img_out = fopen ("cam1.jpg", "rb");
    camera_1 = gdImageCreateFromJpeg (img_out);
    fclose (img_out);
    img_out = fopen ("cam2.jpg", "rb");
    camera_2 = gdImageCreateFromJpeg (img_out);
    fclose (img_out);
    if (camera_1 == NULL) {
      log_write ("Camera 1 image did not load properly.");
      exit (1);
    }
    if (camera_2 == NULL) {
      log_write ("Camera 2 image did not load properly.");
      exit (1);
    }
    
    #endif // DEBUG_WIN
    
    log_write ("Detecting blobs on camera 1.");
    cam1_red = find (camera_1, &(tol->cam1_red));
    cam1_green = find (camera_1, &(tol->cam1_green));
    log_write ("Detecting blobs on camera 2.");
    cam2_red = find (camera_2, &(tol->cam2_red));
    cam2_green = find (camera_2, &(tol->cam2_green));
    if ((target_blob = target (cam1_red, cam1_green, alliance)) != NULL) {
      if ((target_blob->center_x + 50) > 400) {
        log_write ("Sending left motor command.");
        gpio (59, GPIO_ON);
        gpio (58, GPIO_OFF);
      }
      else if ((target_blob->center_x + 50) < 240) {
        log_write ("Sending right motor command.");
        gpio (58, GPIO_ON);
        gpio (59, GPIO_OFF);
      }
      else {
        log_write ("Sending left & right motor commands.");
        gpio (58, GPIO_ON);
        gpio (59, GPIO_ON);
      }
      #ifndef DISABLE_IMG
      //print_blobs (camera_1, target_blob, 100, 100, 100);
      #endif
      free_blobs (target_blob);
    } else {
      log_write ("EE: No targets found.");
    }
    if (cam1_red != NULL && cam1_green != NULL) {
      log_write ("Blob detection completed.  Building image.");
      #ifndef DISABLE_IMG
      print_blobs (camera_1, cam1_red, 200, 0, 0);
      //print_blobs (camera_2, cam2_red, 200, 0, 0);
      print_blobs (camera_1, cam1_green, 0, 180, 20);
      //print_blobs (camera_2, cam2_green, 0, 180, 20);
      #endif
      free_blobs (cam1_red);
      free_blobs (cam2_red);
      free_blobs (cam1_green);
      free_blobs (cam2_green);
      #ifndef DISABLE_IMG
      gdImageCopy (output, camera_1, 0, 0, 0, 0, 640, 480);
      gdImageCopy (output, camera_2, 640, 0, 0, 0, 640, 480);
      #endif
      /*gdImageFilledRectangle (output, 540, 340, 760, 450, fill_color);
      gdImageRectangle (output, 540, 340, 760, 450, line_color);
      time_t rawtime;
      time (&rawtime);
      char * time_str = ctime (&rawtime);
      time_str[strlen(time_str)-1] = '\0';
      render_text (output, text_color, 550, 360, "%s", time_str);
      if (cam1_red != NULL && cam2_red != NULL) {
        render_text (output, text_color, 550, 375, "Left Centroid: 300, 424", cam1_red->center_x, cam1_red->center_y);
        render_text (output, text_color, 550, 390, "Right Centroid: 422, 233", cam2_red->center_x, cam2_red->center_y);
        a1 = image_angle (cam1_red->center_x);
        a2 = image_angle (cam2_red->center_x);
        render_text (output, text_color, 550, 405, "Left Angle: %f", a1 * (180/PI));
        render_text (output, text_color, 550, 420, "Right Angle: %f", a2 * (180/PI));
        render_text (output, text_color, 550, 435, "Depth: %f", find_depth (a1, a2));
      }*/
      #ifndef DISABLE_IMG
      img_time = time (NULL);
      snprintf (img_filename, sizeof (img_filename), "%s%s%d.jpg", OUTPATH, argv[2], img_time);
      log_write ("Image built. Writing to file: %s", img_filename);
      img_out = fopen (img_filename, "wb");
      gdImageJpeg (output, img_out, 100);
      fclose (img_out);
      #endif
    } else {
      log_write ("EE: Some cams didn't detect red+green blobs.");
    }
    #ifndef DISABLE_IMG
    gdImageDestroy (output);
    #endif
    gdImageDestroy (camera_1);
    gdImageDestroy (camera_2);
    log_write ("Loop %d finished.", loop_ctr);
    loop_ctr++;
  }
  return 0;
}