Example #1
0
result_t gd_base::color(const char *color, int32_t &retVal)
{
    if (*color == '#')
        color ++;

    if (!qisxdigit(color[0]) || !qisxdigit(color[1]) || !qisxdigit(color[2]))
        return CHECK_ERROR(CALL_E_INVALIDARG);

    if (!color[3])
        retVal = gdTrueColor(qhex(color[0]) * 17,
                             qhex(color[1]) * 17,
                             qhex(color[2]) * 17);
    else
    {
        if (!qisxdigit(color[3]) || !qisxdigit(color[4]) || !qisxdigit(color[5])
                || color[6])
            return CHECK_ERROR(CALL_E_INVALIDARG);

        retVal = gdTrueColor((qhex(color[0]) << 4) | qhex(color[1]),
                             (qhex(color[2]) << 4) | qhex(color[3]),
                             (qhex(color[4]) << 4) | qhex(color[5]));
    }

    return 0;
}
Example #2
0
int32_t my_replacer(gdImagePtr im, int32_t src)
{
    if (src == gdImageGetTransparent(im))
        return gdTrueColor(255, 255, 255);

    return gdAlphaBlend(gdTrueColor(255, 255, 255), src);
}
Example #3
0
static int CMYKToRGB(int c, int m, int y, int k, int inverted)
{
	if(inverted) {
		c = 255 - c;
		m = 255 - m;
		y = 255 - y;
		k = 255 - k;
	}

	return gdTrueColor(
		(255 - c) * (255 - k) / 255,
		(255 - m) * (255 - k) / 255,
		(255 - y) * (255 - k) / 255
	);
#if 0
  if (inverted)
    {
      c = 255 - c;
      m = 255 - m;
      y = 255 - y;
      k = 255 - k;
    }
  c = c * (255 - k) / 255 + k;
  if (c > 255)
    {
      c = 255;
    }
  if (c < 0)
    {
      c = 0;
    }
  m = m * (255 - k) / 255 + k;
  if (m > 255)
    {
      m = 255;
    }
  if (m < 0)
    {
      m = 0;
    }
  y = y * (255 - k) / 255 + k;
  if (y > 255)
    {
      y = 255;
    }
  if (y < 0)
    {
      y = 0;
    }
  c = 255 - c;
  m = 255 - m;
  y = 255 - y;
  return gdTrueColor (c, m, y);
#endif
}
Example #4
0
void CarModule::draw() {

   int color;

   color = gdTrueColor(0, 0, 0);
   gdImageFilledRectangle(img, 0, 0, width, height, color);

   color = gdTrueColor(250, 250, 250);
   draw_string(0, 0, width, height, color, conf->get_value("font"), 18, "Not implemented");

   update();
}
Example #5
0
void CarGPS::draw_map(struct gpf_data *data)
{
   logger->log(DEBUG, "CarGPS::draw_map", "start");

   char str[128], *map_data;
   int sockfd, len, bytes, tot_bytes, map_size;
   struct sockaddr_un address;

   // open a stream socket to TMRS
   sockfd = socket (AF_UNIX, SOCK_STREAM, 0);
   address.sun_family = AF_UNIX;
   strcpy (address.sun_path, "/var/tmrs_socket");

   if (connect (sockfd, (struct sockaddr *) &address, sizeof(address)) == -1)
   {
      gdImageFilledRectangle(img, 0, 0, width, height-100, gdTrueColor(0,0,0));
      gdImageCopy(img, graphic, 0, img->sy - graphic->sy, 0, 0, graphic->sx, 
         graphic->sy);
      draw_string(0, 0, width, height-100, gdTrueColor(250,250,250), 
         conf->get_value("font"), 18, "Mapping Engine is not running");
      update();
      return;
   }

   // send a 'draw map' request to TMRS
   sprintf(str, "M,RAW,%d,%d,100,27971864,-82561903\n", width, height);
   //sprintf(str, "M,RAW,%d,%d,100,27592354,-82226377\n", width, height);
   write (sockfd, str, strlen(str));

   // allocate memory to hold returned image
   map_size = width*height*4; // 4 bytes per pixel
   map_data = (char *) malloc(map_size);  

   // read in data
   tot_bytes = 0;
   do {
      bytes = read(sockfd, &map_data[tot_bytes], map_size-tot_bytes);
      tot_bytes += bytes;
   } while (bytes > 0);

   // close the socket
   close (sockfd);

   // draw the received data to screen buffer
   for (int y = 0; y < height; y++)
      memcpy(&img->tpixels[y][0], &map_data[y*width*4], width*4); 

   update();

   logger->log(DEBUG, "CarGPS::draw_map", "end");
}
Example #6
0
static void
ax203_decode_block_yuv(char *src, int **dest, int dest_x, int dest_y)
{
	int x, y, r, g, b;
	uint8_t Y[4];
	int8_t U, V;

	/* The compressed data consists of blocks of 2x2 pixels, each encoded
	   in 4 bytes The highest 5 bits of each byte encode an Y value, the
	   lowest 3 bits of the first 2 bytes are combined to form U, and of
	   the last 2 bytes to form V */
	for (x = 0; x < 4; x++)
		Y[x] = src[x] & 0xF8;

	U = ((src[0] & 7) << 5) | ((src[1] & 7) << 2);
	V = ((src[2] & 7) << 5) | ((src[3] & 7) << 2);

	/* The Y components are for a 2x2 pixels block like this:
	   1 2
	   3 4  */
	for (y = 0; y < 2; y ++) {
		for (x = 0; x < 2; x ++) {
			r = 1.164 * (Y[y * 2 + x] - 16) + 1.596 * V;
			g = 1.164 * (Y[y * 2 + x] - 16) - 0.391 * U - 0.813 * V;
			b = 1.164 * (Y[y * 2 + x] - 16) + 2.018 * U;
			dest[dest_y + y][dest_x + x] =
				gdTrueColor (CLAMP_U8 (r),
					     CLAMP_U8 (g),
					     CLAMP_U8 (b));
		}
	}
}
Example #7
0
void CarGPS::draw()
{
   logger->log(DEBUG, "CarGPS::draw", "start");

   struct gpf_data data;

   if (sensor->is_connected()) {
      sensor->get_data(&data);

      if (display_type == SENSOR_READOUT)
         draw_readout(&data);
      else
         draw_map(&data);

      return;
   } 

   // sensor is not connected
   draw_string(0, 0, width, height-100, gdTrueColor(250,250,250), 
      conf->get_value("font"), 18, "GPS Receiver not connected");
   gdImageCopy(img, graphic, 0, img->sy - graphic->sy, 0, 0, graphic->sx, 
      graphic->sy);
   update();

   logger->log(DEBUG, "CarGPS::draw", "end");
}
Example #8
0
/**
* Draw the obdii GUI.
*/
void CarOBDII::draw()
{
   logger->log(DEBUG, "CarOBDII::draw", "start");

   int color;

   color = gdTrueColor(0, 0, 0);
   gdImageFilledRectangle(img, 0, 0, width, height, color);

   color = gdTrueColor(250, 250, 250);
   draw_string(0, 0, width, height, color, conf->get_value("font"), 18, "Not implemented");

   update();

   logger->log(DEBUG, "CarOBDII::end", "start");
}
Example #9
0
/*! @brief look up function for color palette in order to allow switching form gray to true color */
int colorLoolUp(int color)
{
#if NUM_COLORS == 1
	return (255-2*color);
#else
	return gdTrueColor(colorLUT[color][0], colorLUT[color][1], colorLUT[color][2]);
#endif
}
Example #10
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);
}
Example #11
0
/* A very basic conversion approach, TBB */
static int CMYKToRGB(int c, int m, int y, int k, int inverted)
{
	if (inverted) {
		c = 255 - c;
		m = 255 - m;
		y = 255 - y;
		k = 255 - k;
	}
	return gdTrueColor((255 - c) * (255 - k) / 255, (255 - m) * (255 - k) / 255, (255 - y) * (255 - k) / 255);
}
Example #12
0
inline int32_t hsb2rgb(double h, double s, double b)
{
    double cr = 0, cg = 0, cb = 0;
    int32_t i = doubleToInt(h / 60) % 6;
    double f = (h / 60) - i;
    double p = b * (1 - s);
    double q = b * (1 - f * s);
    double t = b * (1 - (1 - f) * s);

    switch (i) {
    case 0:
        cr = b;
        cg = t;
        cb = p;
        break;
    case 1:
        cr = q;
        cg = b;
        cb = p;
        break;
    case 2:
        cr = p;
        cg = b;
        cb = t;
        break;
    case 3:
        cr = p;
        cg = q;
        cb = b;
        break;
    case 4:
        cr = t;
        cg = p;
        cb = b;
        break;
    case 5:
        cr = b;
        cg = p;
        cb = q;
        break;
    default:
        break;
    }

    return gdTrueColor(doubleToInt (cr * 255.0),
                       doubleToInt (cg * 255.0),
                       doubleToInt (cb * 255.0));
}
Example #13
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;
}
Example #14
0
void _transparent_bitmap_part(const void *src, int src_x, int src_y,
                                        int stride, int x, int y, int width, int height)
{
    FILE *_image;
    gdImagePtr image;
    int pink;
    
    DEBUGF2("transparent_bitmap_part(const void *src=%s, int src_x=%d, int src_y=%d, int stride=%d, int x=%d, int y=%d, int width=%d, int height=%d\n", (char*)src, src_x, src_y, stride, x, y, width, height);
    
    _image = fopen(src, "rb");
    if(_image == NULL)
        return;
    
    image = gdImageCreateFromBmp(_image);
    fclose(_image);
    
    pink = gdTrueColor(255, 0, 255); 
    gdImageColorTransparent(image, pink);
    
    gdImageCopy(framebuffer, image, x, y, src_x, src_y, width, height);
    
    gdImageDestroy(image);
}
Example #15
0
gdImagePtr gdImageCreateFromJpegCtxEx (gdIOCtx * infile, int ignore_warning)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	jmpbuf_wrapper jmpbufw;
	/* volatile so we can gdFree them after longjmp */
	volatile JSAMPROW row = 0;
	volatile gdImagePtr im = 0;
	JSAMPROW rowptr[1];
	unsigned int i, j;
	int retval;
	JDIMENSION nrows;
	int channels = 3;
	int inverted = 0;

	memset (&cinfo, 0, sizeof (cinfo));
	memset (&jerr, 0, sizeof (jerr));

	jmpbufw.ignore_warning = ignore_warning;

	cinfo.err = jpeg_std_error (&jerr);
	cinfo.client_data = &jmpbufw;

	cinfo.err->emit_message = (void (*)(j_common_ptr,int)) php_jpeg_emit_message;

	if (setjmp (jmpbufw.jmpbuf) != 0) {
		/* we're here courtesy of longjmp */
		if (row) {
			gdFree (row);
		}
		if (im) {
			gdImageDestroy (im);
		}
		return 0;
	}

	cinfo.err->error_exit = fatal_jpeg_error;

	jpeg_create_decompress (&cinfo);

	jpeg_gdIOCtx_src (&cinfo, infile);

	/* 2.0.22: save the APP14 marker to check for Adobe Photoshop CMYK files with inverted components. */
	jpeg_save_markers(&cinfo, JPEG_APP0 + 14, 256);

	retval = jpeg_read_header (&cinfo, TRUE);
	if (retval != JPEG_HEADER_OK) {
		php_gd_error_ex(E_WARNING, "gd-jpeg: warning: jpeg_read_header returned %d, expected %d", retval, JPEG_HEADER_OK);
	}

	if (cinfo.image_height > INT_MAX) {
		php_gd_error_ex(E_WARNING, "gd-jpeg: warning: JPEG image height (%u) is greater than INT_MAX (%d) (and thus greater than gd can handle)", cinfo.image_height, INT_MAX);
	}

	if (cinfo.image_width > INT_MAX) {
		php_gd_error_ex(E_WARNING, "gd-jpeg: warning: JPEG image width (%u) is greater than INT_MAX (%d) (and thus greater than gd can handle)", cinfo.image_width, INT_MAX);
	}

	im = gdImageCreateTrueColor ((int) cinfo.image_width, (int) cinfo.image_height);
	if (im == 0) {
		php_gd_error("gd-jpeg error: cannot allocate gdImage struct");
		goto error;
	}

	/* 2.0.22: very basic support for reading CMYK colorspace files. Nice for
	 * thumbnails but there's no support for fussy adjustment of the
	 * assumed properties of inks and paper. */
	if ((cinfo.jpeg_color_space == JCS_CMYK) || (cinfo.jpeg_color_space == JCS_YCCK)) {
		cinfo.out_color_space = JCS_CMYK;
	} else {
		cinfo.out_color_space = JCS_RGB;
	}

	if (jpeg_start_decompress (&cinfo) != TRUE) {
		php_gd_error("gd-jpeg: warning: jpeg_start_decompress reports suspended data source");
	}

	/* REMOVED by TBB 2/12/01. This field of the structure is
	 * documented as private, and sure enough it's gone in the
	 * latest libjpeg, replaced by something else. Unfortunately
	 * there is still no right way to find out if the file was
	 * progressive or not; just declare your intent before you
	 * write one by calling gdImageInterlace(im, 1) yourself.
	 * After all, we're not really supposed to rework JPEGs and
	 * write them out again anyway. Lossy compression, remember?
	 */
#if 0
  gdImageInterlace (im, cinfo.progressive_mode != 0);
#endif

	if (cinfo.out_color_space == JCS_RGB) {
		if (cinfo.output_components != 3) {
			php_gd_error_ex(E_WARNING, "gd-jpeg: error: JPEG color quantization request resulted in output_components == %d (expected 3 for RGB)", cinfo.output_components);
			goto error;
		}
		channels = 3;
	} else if (cinfo.out_color_space == JCS_CMYK) {
		jpeg_saved_marker_ptr marker;
		if (cinfo.output_components != 4)  {
			php_gd_error_ex(E_WARNING, "gd-jpeg: error: JPEG color quantization request resulted in output_components == %d (expected 4 for CMYK)", cinfo.output_components);
			goto error;
		}
		channels = 4;
		marker = cinfo.marker_list;
		while (marker) {
			if ((marker->marker == (JPEG_APP0 + 14)) && (marker->data_length >= 12) && (!strncmp((const char *) marker->data, "Adobe", 5))) {
				inverted = 1;
				break;
			}
			marker = marker->next;
		}
	} else {
		php_gd_error_ex(E_WARNING, "gd-jpeg: error: unexpected colorspace.");
		goto error;
	}

#if BITS_IN_JSAMPLE == 12
	php_gd_error("gd-jpeg: error: jpeg library was compiled for 12-bit precision. This is mostly useless, because JPEGs on the web are 8-bit and such versions of the jpeg library won't read or write them. GD doesn't support these unusual images. Edit your jmorecfg.h file to specify the correct precision and completely 'make clean' and 'make install' libjpeg again. Sorry.");
	goto error;
#endif /* BITS_IN_JSAMPLE == 12 */

	row = safe_emalloc(cinfo.output_width * channels, sizeof(JSAMPLE), 0);
	memset(row, 0, cinfo.output_width * channels * sizeof(JSAMPLE));
	rowptr[0] = row;

	if (cinfo.out_color_space == JCS_CMYK) {
		for (i = 0; i < cinfo.output_height; i++) {
			register JSAMPROW currow = row;
			register int *tpix = im->tpixels[i];
			nrows = jpeg_read_scanlines (&cinfo, rowptr, 1);
			if (nrows != 1) {
				php_gd_error_ex(E_WARNING, "gd-jpeg: error: jpeg_read_scanlines returns %u, expected 1", nrows);
				goto error;
			}
			for (j = 0; j < cinfo.output_width; j++, currow += 4, tpix++) {
				*tpix = CMYKToRGB (currow[0], currow[1], currow[2], currow[3], inverted);
			}
		}
	} else {
		for (i = 0; i < cinfo.output_height; i++) {
			register JSAMPROW currow = row;
			register int *tpix = im->tpixels[i];
			nrows = jpeg_read_scanlines (&cinfo, rowptr, 1);
			if (nrows != 1) {
				php_gd_error_ex(E_WARNING, "gd-jpeg: error: jpeg_read_scanlines returns %u, expected 1", nrows);
				goto error;
			}
			for (j = 0; j < cinfo.output_width; j++, currow += 3, tpix++) {
				*tpix = gdTrueColor (currow[0], currow[1], currow[2]);
			}
		}
	} 

	if (jpeg_finish_decompress (&cinfo) != TRUE) {
		php_gd_error("gd-jpeg: warning: jpeg_finish_decompress reports suspended data source");
	}
	if (!ignore_warning) {
		if (cinfo.err->num_warnings > 0) {
			goto error;
		}
	}
	
	jpeg_destroy_decompress (&cinfo);
	gdFree (row);

	return im;

error:
	jpeg_destroy_decompress (&cinfo);
	if (row) {
		gdFree (row);
	}
	if (im) {
		gdImageDestroy (im);
	}
	return 0;
}
Example #16
0
/*********************************************************************//*!
 * @brief Query the current state of the application and see what else
 * we need to get from it
 *
 * Depending on the current state of the application, other additional
 * parameters may be queried.
 *
 * @return SUCCESS or an appropriate error code otherwise
 *//*********************************************************************/
static OSC_ERR QueryApp()
{
	OSC_ERR err;

	/* First, get the current state of the algorithm. */
	err = OscIpcGetParam(cgi.ipcChan, &cgi.appState, GET_APP_STATE, sizeof(struct APPLICATION_STATE));
	if (err != SUCCESS)
	{
		/* This request is defined in all states, and thus must succeed. */
		OscLog(ERROR, "CGI: Error querying application! (%d)\n", err);
		return err;
	}

	switch(cgi.appState.enAppMode)
	{
	case APP_OFF:
		/* Algorithm is off, nothing else to do. */
		break;
	case APP_CAPTURE_ON:
		if (cgi.appState.bNewImageReady)
		{
			FILE* F;
			uint16 r,c;
			uint8* pData;
			uint32 dataSiz, i;
			uint16 oType;

			/* If there is a new image ready, request it from the application. */
			/* We get TWICE the size of an image because metadata might be available */
			err = OscIpcGetParam(cgi.ipcChan, cgi.imgBuf, GET_NEW_IMG, NUM_COLORS*nc*nr*2);
			if (err != SUCCESS)
			{
				OscLog(DEBUG, "CGI: Getting new image failed! (%d)\n", err);
				return err;
			}
//we have to take care of the different ways gdlib treats gray and color data
#if NUM_COLORS == 1
			//create gd image and ...
			gdImagePtr im_out =  gdImageCreate(nc, nr);
			//initialize with sensor image
			for(r = 0; r < nr; r++)
			{
				//in case the original image should not be modified replace the following loop by the memcpy statement
				//memcpy(im_out->pixels[r], cgi.imgBuf+r*nc, nc*sizeof(uint8));
				for(c = 0; c < nc; c++)
				{
					im_out->pixels[r][c] = (*(cgi.imgBuf+r*nc+c) & 0xfe);//mask out first bit -> only even gray values
				}
			}
			//allocate color palette (255 is red -> we did not change the sensor image!! should rather use a LUT)
			for(c = 0; c < 256; c++)
			{
				if((c%2) && c > 255-2*MAX_NUM_COLORS){
					i = (255-c)/2;
					gdImageColorAllocate (im_out, colorLUT[i][0], colorLUT[i][1], colorLUT[i][2]);
				} else {
					gdImageColorAllocate (im_out, c, c, c);
				}
			}
#else
			//create gd image and ...
			gdImagePtr im_out =  gdImageCreateTrueColor(nc, nr);
			//initialize with sensor image
			for(r = 0; r < nr; r++)
			{
				for(c = 0; c < nc; c++)
				{
					uint8* p = (cgi.imgBuf+r*3*nc+3*c);
					im_out->tpixels[r][c] = gdTrueColor(p[2], p[1], p[0]);
				}
			}


#endif
			//there might be additional data to be written to image
			pData = (uint8*) (cgi.imgBuf+NUM_COLORS*nc*nr);
			memcpy(&dataSiz, pData, sizeof(uint32));
			//OscLog(DEBUG, "received %d number of bytes\n", dataSiz);
			pData += sizeof(uint32);//skip dataSiz
			if(dataSiz)
			{
				i = 0;
				while(i < dataSiz)
				{
					memcpy(&oType, pData+i, sizeof(uint16));
					i += sizeof(uint16);
					switch(oType) {
						case OBJ_LINE:
						{
							struct IMG_LINE imgLine;
							memcpy(&imgLine, pData+i, sizLine);
							i += sizLine;
							//OscLog(DEBUG, "received line (%d,%d)-(%d,%d), color(%d)\n", imgLine.x1, imgLine.y1, imgLine.x2, imgLine.y2, (int) imgLine.color);
							gdImageLine(im_out, imgLine.x1, imgLine.y1, imgLine.x2, imgLine.y2, colorLoolUp(imgLine.color));
							break;
						}
						case OBJ_RECT:
						{
							struct IMG_RECT imgRect;
							memcpy(&imgRect, pData+i, sizRect);
							i += sizRect;
							//OscLog(DEBUG, "received rect (%d,%d)-(%d,%d), %s, color(%d)\n", imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, imgRect.recFill ? "fill" : "not fill", (int) imgRect.color);
							if(imgRect.recFill) {
								gdImageFilledRectangle(im_out, imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, colorLoolUp(imgRect.color));
							} else {
								gdImageRectangle(im_out, imgRect.left, imgRect.bottom, imgRect.right, imgRect.top, colorLoolUp(imgRect.color));
							}
							break;
						}
						case OBJ_STRING:
						{
							gdFontPtr font = gdFontSmall;
							struct IMG_STRING imgString;
							memcpy(&imgString, pData+i, sizString);
							i += sizString;
							//OscLog(DEBUG, "received string (%d,%d), font %d, %s, color(%d)\n", imgString.xPos, imgString.yPos, imgString.font, pData+i, imgString.color);
							switch(imgString.font)
							{
								case GIANT:
									font = gdFontGiant;
									break;
								case LARGE:
									font = gdFontLarge;
									break;
								case MEDIUMBOLD:
									font = gdFontMediumBold;
									break;
								case SMALL:
									font = gdFontSmall;
									break;
								case TINY:
									font = gdFontTiny;
									break;
								default:
									break;//set in definition of font
							}
							gdImageString(im_out, font, imgString.xPos, imgString.yPos, pData+i, colorLoolUp(imgString.color));
							i += imgString.len;
						}
					}
				}
			}

			F = fopen(IMG_FN, "wb");
			//gdImageGif(im_out, F);
			gdImageJpeg(im_out, F, 80);
			fclose(F);
			gdImageDestroy(im_out);

			return SUCCESS;
		}
		break;
	default:
		OscLog(ERROR, "%s: Invalid application mode (%d)!\n", __func__, cgi.appState.enAppMode);
		break;
	}
	return SUCCESS;
}
Example #17
0
/*
	Function: gdImageCreateFromTgaCtx

	Creates a gdImage from a gdIOCtx referencing a TGA binary file.

	Parameters:
		ctx - Pointer to a gdIOCtx structure
 */
BGD_DECLARE(gdImagePtr) gdImageCreateFromTgaCtx(gdIOCtx* ctx)
{
	int bitmap_caret = 0;
	oTga *tga = NULL;
	/*	int pixel_block_size = 0;
		int image_block_size = 0; */
	volatile gdImagePtr image = NULL;
	int x = 0;
	int y = 0;

	tga = (oTga *) gdMalloc(sizeof(oTga));
	if (!tga) {
		return NULL;
	}

	tga->bitmap = NULL;
	tga->ident = NULL;

	if (read_header_tga(ctx, tga) < 0) {
		free_tga(tga);
		return NULL;
	}

	/*TODO: Will this be used?
		pixel_block_size = tga->bits / 8;
		image_block_size = (tga->width * tga->height) * pixel_block_size;
	*/

	if (read_image_tga(ctx, tga) < 0) {
		free_tga(tga);
		return NULL;
	}

	image = gdImageCreateTrueColor((int)tga->width, (int)tga->height );

	if (image == 0) {
		free_tga( tga );
		return NULL;
	}

	/*!	\brief Populate GD image object
	 *  Copy the pixel data from our tga bitmap buffer into the GD image
	 *  Disable blending and save the alpha channel per default
	 */
	if (tga->alphabits) {
		gdImageAlphaBlending(image, 0);
		gdImageSaveAlpha(image, 1);
	}

	/* TODO: use alphabits as soon as we support 24bit and other alpha bps (ie != 8bits) */
	for (y = 0; y < tga->height; y++) {
		register int *tpix = image->tpixels[y];
		for ( x = 0; x < tga->width; x++, tpix++) {
			if (tga->bits == TGA_BPP_24) {
				*tpix = gdTrueColor(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret]);
				bitmap_caret += 3;
			} else if (tga->bits == TGA_BPP_32 && tga->alphabits) {
				register int a = tga->bitmap[bitmap_caret + 3];

				*tpix = gdTrueColorAlpha(tga->bitmap[bitmap_caret + 2], tga->bitmap[bitmap_caret + 1], tga->bitmap[bitmap_caret], gdAlphaMax - (a >> 1));
				bitmap_caret += 4;
			}
		}
	}

	if (tga->flipv && tga->fliph) {
		gdImageFlipBoth(image);
	} else if (tga->flipv) {
		gdImageFlipVertical(image);
	} else if (tga->fliph) {
		gdImageFlipHorizontal(image);
	}

	free_tga(tga);

	return image;
}
Example #18
0
BGD_DECLARE(gdImagePtr) gdImageCreateFromJpegCtxEx(gdIOCtx *infile, int ignore_warning)
{
	struct jpeg_decompress_struct cinfo;
	struct jpeg_error_mgr jerr;
	jmpbuf_wrapper jmpbufw;
	/* volatile so we can gdFree them after longjmp */
	volatile JSAMPROW row = 0;
	volatile gdImagePtr im = 0;
	JSAMPROW rowptr[1];
	JDIMENSION i, j;
	int retval;
	JDIMENSION nrows;
	int channels = 3;
	int inverted = 0;

#ifdef JPEG_DEBUG
	gd_error_ex(GD_DEBUG, "gd-jpeg: gd JPEG version %s\n", GD_JPEG_VERSION);
	gd_error_ex(GD_DEBUG, "gd-jpeg: JPEG library version %d, %d-bit sample values\n", JPEG_LIB_VERSION, BITS_IN_JSAMPLE);
	gd_error_ex(GD_DEBUG, "sizeof: %d\n", sizeof(struct jpeg_decompress_struct));
#endif

	memset(&cinfo, 0, sizeof(cinfo));
	memset(&jerr, 0, sizeof(jerr));

	jmpbufw.ignore_warning = ignore_warning;

	cinfo.err = jpeg_std_error(&jerr);
	cinfo.client_data = &jmpbufw;

	cinfo.err->emit_message = jpeg_emit_message;

	if(setjmp(jmpbufw.jmpbuf) != 0) {
		/* we're here courtesy of longjmp */
		if(row) {
			gdFree(row);
		}
		if(im) {
			gdImageDestroy(im);
		}
		return 0;
	}

	cinfo.err->error_exit = fatal_jpeg_error;

	jpeg_create_decompress(&cinfo);

	jpeg_gdIOCtx_src(&cinfo, infile);

	/* 2.0.22: save the APP14 marker to check for Adobe Photoshop CMYK
	 * files with inverted components.
	 */
	jpeg_save_markers(&cinfo, JPEG_APP0 + 14, 256);

	retval = jpeg_read_header(&cinfo, TRUE);
	if(retval != JPEG_HEADER_OK) {
		gd_error("gd-jpeg: warning: jpeg_read_header returns"
		         " %d, expected %d\n", retval, JPEG_HEADER_OK);
	}

	if(cinfo.image_height > INT_MAX) {
		gd_error("gd-jpeg: warning: JPEG image height (%u) is"
		         " greater than INT_MAX (%d) (and thus greater than"
		         " gd can handle)", cinfo.image_height, INT_MAX);
	}

	if(cinfo.image_width > INT_MAX) {
		gd_error("gd-jpeg: warning: JPEG image width (%u) is"
		         " greater than INT_MAX (%d) (and thus greater than"
		         " gd can handle)\n", cinfo.image_width, INT_MAX);
	}

	im = gdImageCreateTrueColor((int)cinfo.image_width, (int)cinfo.image_height);
	if(im == 0) {
		gd_error("gd-jpeg error: cannot allocate gdImage struct\n");
		goto error;
	}

	/* check if the resolution is specified */
	switch (cinfo.density_unit) {
	case 1:
		im->res_x = cinfo.X_density;
		im->res_y = cinfo.Y_density;
		break;
	case 2:
		im->res_x = DPCM2DPI(cinfo.X_density);
		im->res_y = DPCM2DPI(cinfo.Y_density);
		break;
	}

	/* 2.0.22: very basic support for reading CMYK colorspace files. Nice for
	 * thumbnails but there's no support for fussy adjustment of the
	 * assumed properties of inks and paper.
	 */
	if((cinfo.jpeg_color_space == JCS_CMYK) || (cinfo.jpeg_color_space == JCS_YCCK)) {
		cinfo.out_color_space = JCS_CMYK;
	} else {
		cinfo.out_color_space = JCS_RGB;
	}

	if(jpeg_start_decompress(&cinfo) != TRUE) {
		gd_error("gd-jpeg: warning: jpeg_start_decompress"
		        " reports suspended data source\n");
	}

#ifdef JPEG_DEBUG
	gd_error_ex(GD_DEBUG, "gd-jpeg: JPEG image information:");
	if(cinfo.saw_JFIF_marker) {
		gd_error_ex(GD_DEBUG, " JFIF version %d.%.2d", (int)cinfo.JFIF_major_version, (int)cinfo.JFIF_minor_version);
	} else if(cinfo.saw_Adobe_marker) {
		gd_error_ex(GD_DEBUG, " Adobe format");
	} else {
		gd_error_ex(GD_DEBUG, " UNKNOWN format");
	}

	gd_error_ex(GD_DEBUG, " %ux%u (raw) / %ux%u (scaled) %d-bit", cinfo.image_width,
		    cinfo.image_height, cinfo.output_width,
		    cinfo.output_height, cinfo.data_precision
		);
	gd_error_ex(GD_DEBUG, " %s", (cinfo.progressive_mode ? "progressive" : "baseline"));
	gd_error_ex(GD_DEBUG, " image, %d quantized colors, ", cinfo.actual_number_of_colors);

	switch(cinfo.jpeg_color_space) {
	case JCS_GRAYSCALE:
		gd_error_ex(GD_DEBUG, "grayscale");
		break;

	case JCS_RGB:
		gd_error_ex(GD_DEBUG, "RGB");
		break;

	case JCS_YCbCr:
		gd_error_ex(GD_DEBUG, "YCbCr (a.k.a. YUV)");
		break;

	case JCS_CMYK:
		gd_error_ex(GD_DEBUG, "CMYK");
		break;

	case JCS_YCCK:
		gd_error_ex(GD_DEBUG, "YCbCrK");
		break;

	default:
		gd_error_ex(GD_DEBUG, "UNKNOWN (value: %d)", (int)cinfo.jpeg_color_space);
		break;
	}

	gd_error_ex(GD_DEBUG, " colorspace\n");
	fflush(stdout);
#endif /* JPEG_DEBUG */

	/* REMOVED by TBB 2/12/01. This field of the structure is
	 * documented as private, and sure enough it's gone in the
	 * latest libjpeg, replaced by something else. Unfortunately
	 * there is still no right way to find out if the file was
	 * progressive or not; just declare your intent before you
	 * write one by calling gdImageInterlace(im, 1) yourself.
	 * After all, we're not really supposed to rework JPEGs and
	 * write them out again anyway. Lossy compression, remember? */
#if 0
	gdImageInterlace (im, cinfo.progressive_mode != 0);
#endif
	if(cinfo.out_color_space == JCS_RGB) {
		if(cinfo.output_components != 3) {
			gd_error("gd-jpeg: error: JPEG color quantization"
			         " request resulted in output_components == %d"
			         " (expected 3 for RGB)\n", cinfo.output_components);
			goto error;
		}
		channels = 3;
	} else if(cinfo.out_color_space == JCS_CMYK) {
		jpeg_saved_marker_ptr marker;
		if(cinfo.output_components != 4) {
			gd_error("gd-jpeg: error: JPEG color quantization"
			         " request resulted in output_components == %d"
			         " (expected 4 for CMYK)\n", cinfo.output_components);
			goto error;
		}
		channels = 4;

		marker = cinfo.marker_list;
		while(marker) {
			if(	(marker->marker == (JPEG_APP0 + 14)) &&
			        (marker->data_length >= 12) &&
			        (!strncmp((const char *)marker->data, "Adobe", 5))) {
				inverted = 1;
				break;
			}
			marker = marker->next;
		}
	} else {
		gd_error("gd-jpeg: error: unexpected colorspace\n");
		goto error;
	}
#if BITS_IN_JSAMPLE == 12
	gd_error_ex(GD_ERROR,
		    "gd-jpeg: error: jpeg library was compiled for 12-bit\n"
		    "precision. This is mostly useless, because JPEGs on the web are\n"
		    "8-bit and such versions of the jpeg library won't read or write\n"
		    "them. GD doesn't support these unusual images. Edit your\n"
		    "jmorecfg.h file to specify the correct precision and completely\n"
		    "'make clean' and 'make install' libjpeg again. Sorry.\n");
	goto error;
#endif /* BITS_IN_JSAMPLE == 12 */

	row = gdCalloc(cinfo.output_width *channels, sizeof(JSAMPLE));
	if(row == 0) {
		gd_error("gd-jpeg: error: unable to allocate row for"
		         " JPEG scanline: gdCalloc returns NULL\n");
		goto error;
	}
	rowptr[0] = row;
	if(cinfo.out_color_space == JCS_CMYK) {
		for(i = 0; i < cinfo.output_height; i++) {
			register JSAMPROW currow = row;
			register int *tpix = im->tpixels[i];
			nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
			if(nrows != 1) {
				gd_error("gd-jpeg: error: jpeg_read_scanlines"
				         " returns %u, expected 1\n", nrows);
				goto error;
			}
			for(j = 0; j < cinfo.output_width; j++, currow += 4, tpix++) {
				*tpix = CMYKToRGB(currow[0], currow[1], currow[2], currow[3], inverted);
			}
		}
	} else {
		for(i = 0; i < cinfo.output_height; i++) {
			register JSAMPROW currow = row;
			register int *tpix = im->tpixels[i];
			nrows = jpeg_read_scanlines(&cinfo, rowptr, 1);
			if(nrows != 1) {
				gd_error("gd-jpeg: error: jpeg_read_scanlines"
				         " returns %u, expected 1\n", nrows);
				goto error;
			}
			for(j = 0; j < cinfo.output_width; j++, currow += 3, tpix++) {
				*tpix = gdTrueColor(currow[0], currow[1], currow[2]);
			}
		}
	}

	if(jpeg_finish_decompress (&cinfo) != TRUE) {
		gd_error("gd-jpeg: warning: jpeg_finish_decompress"
		         " reports suspended data source\n");
	}
	/* TBB 2.0.29: we should do our best to read whatever we can read, and a
	 * warning is a warning. A fatal error on warnings doesn't make sense. */
#if 0
	/* This was originally added by Truxton Fulton */
	if (cinfo.err->num_warnings > 0)
		goto error;
#endif

	jpeg_destroy_decompress(&cinfo);
	gdFree(row);
	return im;

error:
	jpeg_destroy_decompress(&cinfo);

	if(row) {
		gdFree(row);
	}
	if(im) {
		gdImageDestroy(im);
	}

	return 0;
}
Example #19
0
 int color2gd(const Color& color) {return gdTrueColor(color.red, color.green, color.blue);} 
Example #20
0
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 */
}
Example #21
0
void plD_bop_png(PLStream *pls)
{
    png_Dev *dev;

    plGetFam(pls);
/* force new file if pls->family set for all subsequent calls to plGetFam
 * n.b. putting this after plGetFam call is important since plinit calls
 * bop, and you don't want the familying sequence started until after
 * that first call to bop.*/

/* n.b. pls->dev can change because of an indirect call to plD_init_png
 * from plGetFam if familying is enabled.  Thus, wait to define dev until
 * now. */

    dev = (png_Dev *) pls->dev;

    pls->famadv = 1;

    pls->page++;

if (dev->black15) plD_black15_gd(pls);
if (dev->red15) plD_red15_gd(pls);

#if GD2_VERS >= 2
  if ( ( (((dev->truecolour>0) && (dev->palette>0)) ||     /* In an EXTREMELY convaluted */
         ((dev->truecolour==0) && (dev->palette==0))) &&   /* manner, all this is just   */
          ((pls->ncol1+pls->ncol0) <= 256) ) ||             /* asking the question, do we */
       (((dev->palette>0) && (dev->truecolour==0)))  )   /* want truecolour or not ?   */
        {
#endif

           dev->im_out = gdImageCreate(pls->xlength, pls->ylength);

           setcmap(pls);

#if GD2_VERS >= 2
         }
       else
         {
         dev->im_out = gdImageCreateTrueColor(pls->xlength, pls->ylength);

/*
 * In truecolour mode, the background colour GD makes is ALWAYS black, so to
 * "simulate" (stimulate?) a background colour other than black, we will just
 * draw a dirty big rectange covering the whole image and colour it in
 * whatever colour cmap0[0] happens to be.
 *
 * Question to C gurus: while it is slightly illogical and ugly, would:
 *   if ((pls->cmap0[0].r+pls->cmap0[0].g+pls->cmap0[0].b)!=0)
 * be more computationally efficient than:
 *   if ((pls->cmap0[0].r!=0)||(pls->cmap0[0].g!=0)||(pls->cmap0[0].b!=0))
 *  ???
 */

         if ( (pls->cmap0[0].r!=0)||(pls->cmap0[0].g!=0)||
              (pls->cmap0[0].b!=0) )
            {
             gdImageFilledRectangle(dev->im_out,0,0, pls->xlength-1, pls->ylength-1,
                                    gdTrueColor(pls->cmap0[0].r,pls->cmap0[0].g,
                                                pls->cmap0[0].b));
            }

         }


/* This ensures the line width is set correctly at the beginning of
 *    each page */

   plD_state_png(pls, PLSTATE_WIDTH);

#endif


}
Example #22
0
void
plD_state_png(PLStream *pls, PLINT op)
{
png_Dev *dev=(png_Dev *)pls->dev;
PLFLT tmp_colour_pos;
#if GD2_VERS >= 2
long temp_col;
#endif


    switch (op) {

#if GD2_VERS >= 2
    case PLSTATE_WIDTH:
        gdImageSetThickness(dev->im_out, pls->width);
	break;
#endif

    case PLSTATE_COLOR0:
#if GD2_VERS >= 2

	if ( (pls->icol0 == PL_RGB_COLOR)||     /*  Should never happen since PL_RGB_COLOR is depreciated, but here for backwards compatibility */
             (gdImageTrueColor(dev->im_out)) )  /*  We will do this if we are in "TrueColour" mode */
           {
	    if ( (dev->totcol < NCOLOURS)||         /* See if there are slots left, if so we will allocate a new colour */
                 (gdImageTrueColor(dev->im_out)) )  /* In TrueColour mode we allocate each colour as we come to it */
	       {
	        /* Next allocate a new colour to a temporary slot since what we do with it will varay depending on if its a pallter index or truecolour */
                temp_col=gdImageColorAllocate(dev->im_out,pls->curcolor.r,
                                             pls->curcolor.g, pls->curcolor.b);

                if (gdImageTrueColor(dev->im_out))
                    dev->colour = temp_col;     /* If it's truecolour, then we will directly set dev->colour to our "new" colour */
                else
                    {
                     dev->colour = dev->totcol;  /* or else, we will just set it to the last colour */
                     dev->totcol++;              /* Bump the total colours for next time round */
                    }
	       }

           }
         else  /* just a normal colour allocate, so don't worry about the above stuff, just grab the index */
           {
            dev->colour = pls->icol0;
           }

#else
	dev->colour = pls->icol0;
	if (dev->colour == PL_RGB_COLOR)
           {
	    if (dev->totcol < NCOLOURS)
	       {
                gdImageColorAllocate(dev->im_out,pls->curcolor.r, pls->curcolor.g,  pls->curcolor.b);
		dev->colour = dev->totcol;
	       }

           }
#endif
	break;

    case PLSTATE_COLOR1:

#if GD2_VERS >= 2
       if (!gdImageTrueColor(dev->im_out))
          {
#endif
           /*
            * Start by checking to see if we have to compensate for cases where
            * we don't have the full dynamic range of cmap1 at our disposal
            */
           if (dev->ncol1<pls->ncol1)
              {
               tmp_colour_pos=dev->ncol1*((PLFLT)pls->icol1/(pls->ncol1>0 ? pls->ncol1 : 1));
               dev->colour = pls->ncol0 + (int)tmp_colour_pos;
              }
           else
              dev->colour = pls->ncol0 + pls->icol1;
#if GD2_VERS >= 2
           }
        else    /* it is a truecolour image */
           {
             dev->colour = gdTrueColor(pls->curcolor.r, pls->curcolor.g, pls->curcolor.b);
           }
#endif
	break;


    case PLSTATE_CMAP0:
    case PLSTATE_CMAP1:

#if GD2_VERS >= 2
       if ((dev->im_out != NULL) && !gdImageTrueColor(dev->im_out))
          {
#endif

    /*
     *  Code to redefine the entire palette
     */


	if (pls->color)
	    setcmap(pls);

#if GD2_VERS >= 2
}
#endif

	break;
    }
}
Example #23
0
void CarApp::draw_buttons(int selection)
{
   logger->log(DEBUG, "CarApp::draw_buttons", "start");

   int i, top, col, blue;
   gdPoint points[3];
   int brect[8];
   int white = gdTrueColor(255,255,255);
   char *font = conf->get_value("font");

   col = gdTrueColor(64,64,198);
   blue = gdTrueColor(64,64,198);

   for(i=0; i<6; i++) 
   {  
      top = i * button_height;
      if (i == selection)
         col = gdTrueColor(100, 110, 216);  
      else
         col = gdTrueColor(57, 76, 116);

      /* special colors for the mute button) */
      if (sound->is_mute() && (i == MUTE) && (i != selection))
         col = gdTrueColor(200, 94, 94);
      else if (sound->is_mute() && (i == MUTE) && (i == selection))   
         col = gdTrueColor(255, 120, 120);

      gdImageFilledRectangle(fb->img, 1, top+1, button_width - 2, 
         top + button_height - 2, col);   

      if (i == MUTE)
      {
         gdImageStringFT(fb->img, &brect[0], white, font, 14, 0.0, 20, 45, 
            "Mute");
      }
      else if (i == UP) {
         col = gdTrueColor(180, 60, 60);
         points[0].x = MARGIN + (button_width / 2);
         points[0].y = top + 20;
         points[1].x = MARGIN + 20;
         points[1].y = top + button_height - 20;
         points[2].x = MARGIN + button_width - 20;
         points[2].y = top + button_height - 20;
         gdImageFilledPolygon(fb->img, points, 3, col);
         gdImagePolygon(fb->img, points, 3, 0);
      }	
      else if (i ==DOWN) {
         col = gdTrueColor(180, 60, 60);
         points[0].x = MARGIN + 20;
         points[0].y = top + 20;
         points[1].x = MARGIN + button_width - 20;
         points[1].y = top + 20;
         points[2].x = MARGIN + (button_width / 2);
         points[2].y = top + button_height - 20;
         gdImageFilledPolygon(fb->img, points, 3, col);
         gdImagePolygon(fb->img, points, 3, 0);
      }	
      else if (i == RIGHT) {
         col = gdTrueColor(100, 160, 100);
         points[0].x = MARGIN + 20;
         points[0].y = top + 20;
         points[1].x = MARGIN + button_width - 20;
         points[1].y = top + (button_height) / 2;
         points[2].x = MARGIN + 20;
         points[2].y = top + button_height - 20;
         gdImageFilledPolygon(fb->img, points, 3, col);
         gdImagePolygon(fb->img, points, 3, 0);
      }
      else if (i == LEFT) {
         col = gdTrueColor(100, 160, 100);
         points[0].x = MARGIN + button_width - 20;
         points[0].y = top + 20;
         points[1].x = MARGIN +  20;
         points[1].y = top + (button_height) / 2;
         points[2].x = MARGIN + button_width - 20;
         points[2].y = top + button_height - 20;
         gdImageFilledPolygon(fb->img, points, 3, col);
         gdImagePolygon(fb->img, points, 3, 0);
      }
      else if (i == MENU) 
      {
         gdImageStringFT(fb->img, &brect[0], white, font, 14, 0.0, 17, 445, 
            "Menu");
      }
   }

   fb->update(0, 0, button_width, fb->height-1);

   logger->log(DEBUG, "CarApp::draw_buttons", "end");
}
Example #24
0
static int drv_IMG_flush_PNG(void)
{
    static int seq = 0;
    int xsize, ysize, row, col;
    char path[256], tmp[256];
    FILE *fp;
    int fd;
    gdImagePtr im;

    xsize = 2 * border + (DCOLS / XRES - 1) * cgap + DCOLS * pixel + (DCOLS - 1) * pgap;
    ysize = 2 * border + (DROWS / YRES - 1) * rgap + DROWS * pixel + (DROWS - 1) * pgap;

    im = gdImageCreateTrueColor(xsize, ysize);
    gdImageFilledRectangle(im, 0, 0, xsize, ysize, gdTrueColor(BC.R, BC.G, BC.B));

    for (row = 0; row < DROWS; row++) {
	int y = border + (row / YRES) * rgap + row * (pixel + pgap);
	for (col = 0; col < DCOLS; col++) {
	    int x = border + (col / XRES) * cgap + col * (pixel + pgap);
	    RGBA p = drv_IMG_FB[row * DCOLS + col];
	    int c = gdTrueColor(p.R, p.G, p.B);
	    gdImageFilledRectangle(im, x, y, x + pixel - 1, y + pixel - 1, c);
	}
    }

    snprintf(path, sizeof(path), output, seq++);
    qprintf(tmp, sizeof(tmp), "%s.tmp", path);

    /* remove the file */
    unlink(tmp);

    /* avoid symlink security hole:  */
    /* open it with O_EXCL will fail if the file exists.  */
    /* This should not happen because we just unlinked it. */
    if ((fd = open(tmp, O_WRONLY | O_CREAT | O_EXCL, 0644)) < 0) {
	error("%s: open(%s) failed: %s", Name, tmp, strerror(errno));
	return -1;
    }

    if ((fp = fdopen(fd, "w")) == NULL) {
	error("%s: fdopen(%s) failed: %s\n", Name, tmp, strerror(errno));
	close(fd);
	return -1;
    }

    gdImagePng(im, fp);
    gdImageDestroy(im);


    if (fclose(fp) != 0) {
	error("%s: fclose(%s) failed: %s\n", Name, tmp, strerror(errno));
	return -1;
    }

    if (rename(tmp, path) < 0) {
	error("%s: rename(%s) failed: %s\n", Name, tmp, strerror(errno));
	return -1;
    }

    return 0;
}
Example #25
0
void CarGPS::draw_readout(struct gpf_data *data)
{
   logger->log(DEBUG, "CarGPS::draw_readout", "start");

   char str[128];
   char *f = conf->get_value("font");
   int white = gdTrueColor(250,250,250);
   int gray = gdTrueColor(200, 250, 200);
   int black = gdTrueColor(0,0,0);
   int cx, cy;

   // time
   int time = (int)data->time;
   int hour = time/10000;
   int minute  = time/100 - (hour*100);
   int second = time - (hour*10000) - (minute*100);
   sprintf(str, "Time: %.2d:%.2d:%.2d UTC", hour, minute, second); 
   gdImageFilledRectangle(img, 0, height - 80, width, height, black);
   draw_string(0, height-40, width, 40, white, f, 18, str);

   // position
   sprintf(str, "Position: %.2f %c, %.2f %c", data->latitude/100.0, 
      data->latitude_ns, data->longitude/100.0, data->longitude_ew);
   gdImageFilledRectangle(img, 0, 0, width, 40, black);
   draw_string(0, 0, width, 40, white, f, 18, str);

   // altitude
   sprintf(str, "Altitude: %d m", data->altitude); 
   //draw_string(0, 150, width, 30, white, f, 18, str);

   // altitude
   sprintf(str, "Satellites in view: %d", data->num_satellites); 
   //draw_string(0, 200, width, 30, white, f, 18, str);

   // draw the new satellite map only if new data is available
   if (data->new_info) 
   {
      gdImageFilledRectangle(img, 0, 40, width, height-80, black);

      //initialize useful variables
      cx = width / 2;
      cy = height / 2;

      // draw the concentric circles
      gdImageArc(img, cx, cy, 100, 100, 0, 360, gray);
      gdImageArc(img, cx, cy, 200, 200, 0, 360, gray);
      gdImageArc(img, cx, cy, 300, 300, 0, 360, gray);

      // crosshair
      gdImageLine(img, cx-150, cy, cx+150, cy, gray);
      gdImageLine(img, cx, cy-150, cx, cy+150, gray);

      // label the directions
      draw_string(cx-20, cy-185, 40, 40, gray, f, 14, "N");
      draw_string(cx+150, cy-20, 40, 40, gray, f, 14, "E");
      draw_string(cx-20, cy+150, 40, 40, gray, f, 14, "S");
      draw_string(cx-185, cy-20, 40, 40, gray, f, 14, "W");

      // draw the satellites
      int x, y;
      float r, theta;

      for (int i = 0; i < data->num_satellites; i++)
      {
         r = 150.0 - (float)data->satellite[i].elevation * 150.0 / 90.0;
         theta = (float)(data->satellite[i].azimuth-90) * 2.0 * 3.14159 / 360.0;

         x = (int)(r * cos(theta)) + cx;
         y = (int)(r * sin(theta)) + cy;

         sprintf(str, "%d", data->satellite[i].id);
         gdImageFilledArc(img, x, y, 5, 5, 0, 360, white, gdArc);
         draw_string(x, y, 18, 18, gray, f, 10, str);
      }
   }

   update();
   logger->log(DEBUG, "CarGPS::draw_readout", "end");
}
Example #26
0
/* This routine is based in part on the Chapter 13 demo code in "PNG: The
 *  Definitive Guide" (http://www.cdrom.com/pub/png/pngbook.html).
 */
BGD_DECLARE(gdImagePtr) gdImageCreateFromPngCtx (gdIOCtx * infile)
{
  png_byte sig[8];
  png_structp png_ptr;
  png_infop info_ptr;
  png_uint_32 width, height, rowbytes;
  int bit_depth, color_type, interlace_type;
  int num_palette, num_trans;
  png_colorp palette;
  png_color_16p trans_gray_rgb;
  png_color_16p trans_color_rgb;
  png_bytep trans;
  png_bytep image_data = NULL;
  png_bytepp row_pointers = NULL;
  gdImagePtr im = NULL;
  int i, j, *open = NULL;
  volatile int transparent = -1;
  volatile int palette_allocated = FALSE;

  /* Make sure the signature can't match by dumb luck -- TBB */
  /* GRR: isn't sizeof(infile) equal to the size of the pointer? */
  memset (infile, 0, sizeof (infile));

  /* first do a quick check that the file really is a PNG image; could
   * have used slightly more general png_sig_cmp() function instead */
  gdGetBuf (sig, 8, infile);
  if (!png_check_sig (sig, 8))
    return NULL;		/* bad signature */

#ifndef PNG_SETJMP_NOT_SUPPORTED
  png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, &gdPngJmpbufStruct,
				    gdPngErrorHandler, NULL);
#else
  png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
#endif
  if (png_ptr == NULL)
    {
      fprintf (stderr, "gd-png error: cannot allocate libpng main struct\n");
      return NULL;
    }

  info_ptr = png_create_info_struct (png_ptr);
  if (info_ptr == NULL)
    {
      fprintf (stderr, "gd-png error: cannot allocate libpng info struct\n");
      png_destroy_read_struct (&png_ptr, NULL, NULL);
      return NULL;
    }

  /* we could create a second info struct here (end_info), but it's only
   * useful if we want to keep pre- and post-IDAT chunk info separated
   * (mainly for PNG-aware image editors and converters) */

  /* setjmp() must be called in every non-callback function that calls a
   * PNG-reading libpng function */
#ifndef PNG_SETJMP_NOT_SUPPORTED
  if (setjmp (gdPngJmpbufStruct.jmpbuf))
    {
      fprintf (stderr, "gd-png error: setjmp returns error condition\n");
      png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
      return NULL;
    }
#endif

  png_set_sig_bytes (png_ptr, 8);	/* we already read the 8 signature bytes */

  png_set_read_fn (png_ptr, (void *) infile, gdPngReadData);
  png_read_info (png_ptr, info_ptr);	/* read all PNG info up to image data */

  png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
		&interlace_type, NULL, NULL);
  if ((color_type == PNG_COLOR_TYPE_RGB) ||
      (color_type == PNG_COLOR_TYPE_RGB_ALPHA))
    {
      im = gdImageCreateTrueColor ((int) width, (int) height);
    }
  else
    {
      im = gdImageCreate ((int) width, (int) height);
    }
  if (im == NULL)
    {
      fprintf (stderr, "gd-png error: cannot allocate gdImage struct\n");
      png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
      gdFree (image_data);
      gdFree (row_pointers);
      return NULL;
    }

  if (bit_depth == 16)
    png_set_strip_16 (png_ptr);
  else if (bit_depth < 8)
    png_set_packing (png_ptr);	/* expand to 1 byte per pixel */
  switch (color_type)
    {
    case PNG_COLOR_TYPE_PALETTE:
      png_get_PLTE (png_ptr, info_ptr, &palette, &num_palette);
#ifdef DEBUG
      fprintf (stderr, "gd-png color_type is palette, colors: %d\n",
	       num_palette);
#endif /* DEBUG */
      if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
	{
	  /* gd 2.0: we support this rather thoroughly now. Grab the
	     first fully transparent entry, if any, as the value of 
	     the simple-transparency index, mostly for backwards
	     binary compatibility. The alpha channel is where it's
	     really at these days. */
	  int firstZero = 1;
	  png_get_tRNS (png_ptr, info_ptr, &trans, &num_trans, NULL);
	  for (i = 0; i < num_trans; ++i)
	    {
	      im->alpha[i] = gdAlphaMax - (trans[i] >> 1);
	      if ((trans[i] == 0) && (firstZero))
		{
		  /* 2.0.5: long-forgotten patch from Wez Furlong */
		  transparent = i;
		  firstZero = 0;
		}
	    }
	}
      break;

    case PNG_COLOR_TYPE_GRAY:
    case PNG_COLOR_TYPE_GRAY_ALPHA:
      /* create a fake palette and check for single-shade transparency */
      if ((palette =
	   (png_colorp) gdMalloc (256 * sizeof (png_color))) == NULL)
	{
	  fprintf (stderr, "gd-png error: cannot allocate gray palette\n");
	  png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
	  return NULL;
	}
      palette_allocated = TRUE;
      if (bit_depth < 8)
	{
	  num_palette = 1 << bit_depth;
	  for (i = 0; i < 256; ++i)
	    {
	      j = (255 * i) / (num_palette - 1);
	      palette[i].red = palette[i].green = palette[i].blue = j;
	    }
	}
      else
	{
	  num_palette = 256;
	  for (i = 0; i < 256; ++i)
	    {
	      palette[i].red = palette[i].green = palette[i].blue = i;
	    }
	}
      if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
	{
	  png_get_tRNS (png_ptr, info_ptr, NULL, NULL, &trans_gray_rgb);
	  if (bit_depth == 16)	/* png_set_strip_16() not yet in effect */
	    transparent = trans_gray_rgb->gray >> 8;
	  else
	    transparent = trans_gray_rgb->gray;
	  /* Note slight error in 16-bit case:  up to 256 16-bit shades
	   * may get mapped to a single 8-bit shade, and only one of them
	   * is supposed to be transparent.  IOW, both opaque pixels and
	   * transparent pixels will be mapped into the transparent entry.
	   * There is no particularly good way around this in the case
	   * that all 256 8-bit shades are used, but one could write some
	   * custom 16-bit code to handle the case where there are gdFree
	   * palette entries.  This error will be extremely rare in
	   * general, though.  (Quite possibly there is only one such
	   * image in existence.) */
	}
      break;

    case PNG_COLOR_TYPE_RGB:
    case PNG_COLOR_TYPE_RGB_ALPHA:
      /* gd 2.0: we now support truecolor. See the comment above
         for a rare situation in which the transparent pixel may not
         work properly with 16-bit channels. */
      if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
	{
	  png_get_tRNS (png_ptr, info_ptr, NULL, NULL, &trans_color_rgb);
	  if (bit_depth == 16)	/* png_set_strip_16() not yet in effect */
	    transparent = gdTrueColor (trans_color_rgb->red >> 8,
				       trans_color_rgb->green >> 8,
				       trans_color_rgb->blue >> 8);
	  else
	    transparent = gdTrueColor (trans_color_rgb->red,
				       trans_color_rgb->green,
				       trans_color_rgb->blue);
	}