Exemple #1
0
result_t Image::get_progressive(bool& retVal)
{
    if (!m_image)
        return CHECK_ERROR(CALL_E_INVALID_CALL);

    retVal = gdImageGetInterlaced(m_image) != 0;
    return 0;
}
void
PrintGIFInfo
	(
	gdImagePtr image
	)
{
	cout << endl;
	cout << "Index\tRed\tGreen\tBlue" << endl;

	const JSize colorCount = gdImageColorsTotal(image);
	for (JIndex i=0; i<colorCount; i++)
		{
		cout << i;
		cout << '\t' << gdImageRed  (image, i);
		cout << '\t' << gdImageGreen(image, i);
		cout << '\t' << gdImageBlue (image, i);
		cout << endl;
		}

	cout << endl;

	cout << "Number of colors:        " << colorCount << endl;

	const int transparentColor = gdImageGetTransparent(image);
	cout << "Transparent color index: ";
	if (transparentColor == kNoTransparentColor)
		{
		cout << "none";
		}
	else
		{
		cout << transparentColor;
		}
	cout << endl;

	cout << endl;

	cout << "Width:      " << gdImageSX(image) << endl;
	cout << "Height:     " << gdImageSY(image) << endl;
	cout << "Interlaced: ";
	if (gdImageGetInterlaced(image))
		{
		cout << "yes";
		}
	else
		{
		cout << "no";
		}
	cout << endl;

	cout << endl;
}
Exemple #3
0
BGD_DECLARE(void) gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
{
	struct jpeg_compress_struct cinfo;
	struct jpeg_error_mgr jerr;
	int i, j, jidx;
	/* volatile so we can gdFree it on return from longjmp */
	volatile JSAMPROW row = 0;
	JSAMPROW rowptr[1];
	jmpbuf_wrapper jmpbufw;
	JDIMENSION nlines;
	char comment[255];

#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);
	if (!im->trueColor) {
		for(i = 0; i < im->colorsTotal; i++) {
			if(!im->open[i]) {
				gd_error_ex(GD_DEBUG, "gd-jpeg: gd colormap index %d: (%d, %d, %d)\n", i, im->red[i], im->green[i], im->blue[i]);
			}
		}
	}
#endif /* JPEG_DEBUG */

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

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

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

	cinfo.err->emit_message = jpeg_emit_message;
	cinfo.err->error_exit = fatal_jpeg_error;

	jpeg_create_compress(&cinfo);

	cinfo.image_width = im->sx;
	cinfo.image_height = im->sy;
	cinfo.input_components = 3; /* # of color components per pixel */
	cinfo.in_color_space = JCS_RGB; /* colorspace of input image */

	jpeg_set_defaults(&cinfo);

	cinfo.density_unit = 1;
	cinfo.X_density = im->res_x;
	cinfo.Y_density = im->res_y;

	if(quality >= 0) {
		jpeg_set_quality(&cinfo, quality, TRUE);
	}

	/* If user requests interlace, translate that to progressive JPEG */
	if(gdImageGetInterlaced(im)) {
#ifdef JPEG_DEBUG
		gd_error_ex(GD_DEBUG, "gd-jpeg: interlace set, outputting progressive JPEG image\n");
#endif
		jpeg_simple_progression(&cinfo);
	}

	jpeg_gdIOCtx_dest(&cinfo, outfile);

	row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components * sizeof(JSAMPLE));
	if(row == 0) {
		gd_error("gd-jpeg: error: unable to allocate JPEG row structure: gdCalloc returns NULL\n");
		jpeg_destroy_compress(&cinfo);
		return;
	}

	rowptr[0] = row;

	jpeg_start_compress(&cinfo, TRUE);

	sprintf(comment, "CREATOR: gd-jpeg v%s (using IJG JPEG v%d),", GD_JPEG_VERSION, JPEG_LIB_VERSION);

	if(quality >= 0) {
		sprintf (comment + strlen(comment), " quality = %d\n", quality);
	} else {
		strcat(comment + strlen(comment), " default quality\n");
	}

	jpeg_write_marker(&cinfo, JPEG_COM, (unsigned char *) comment, (unsigned int)strlen(comment));

	if(im->trueColor) {
#if BITS_IN_JSAMPLE == 12
		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 */
		for(i = 0; i < im->sy; i++) {
			for(jidx = 0, j = 0; j < im->sx; j++) {
				int val = im->tpixels[i][j];
				row[jidx++] = gdTrueColorGetRed(val);
				row[jidx++] = gdTrueColorGetGreen(val);
				row[jidx++] = gdTrueColorGetBlue(val);
			}

			nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);

			if(nlines != 1) {
				gd_error("gd_jpeg: warning: jpeg_write_scanlines returns %u -- expected 1\n", nlines);
			}
		}
	} else {
		for(i = 0; i < im->sy; i++) {
			for(jidx = 0, j = 0; j < im->sx; j++) {
				int idx = im->pixels[i][j];

				/*
				 * NB: Although gd RGB values are ints, their max value is
				 * 255 (see the documentation for gdImageColorAllocate())
				 * -- perfect for 8-bit JPEG encoding (which is the norm)
				 */
#if BITS_IN_JSAMPLE == 8
				row[jidx++] = im->red[idx];
				row[jidx++] = im->green[idx];
				row[jidx++] = im->blue[idx];
#elif BITS_IN_JSAMPLE == 12
				row[jidx++] = im->red[idx] << 4;
				row[jidx++] = im->green[idx] << 4;
				row[jidx++] = im->blue[idx] << 4;
#else
#error IJG JPEG library BITS_IN_JSAMPLE value must be 8 or 12
#endif
			}

			nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
			if(nlines != 1) {
				gd_error("gd_jpeg: warning: jpeg_write_scanlines"
				         " returns %u -- expected 1\n", nlines);
			}
		}
	}

	jpeg_finish_compress(&cinfo);
	jpeg_destroy_compress(&cinfo);
	gdFree(row);
}
Exemple #4
0
int main(int argc, char **argv)
{
	FILE *in;
	FILE *out;
	/* Declare our image pointer */
	gdImagePtr im = 0;
	int i;
       /* We'll clear 'no' once we know the user has made a
		reasonable request. */
	int no = 1;
	/* We'll set 'write' once we know the user's request
		requires that the image be written back to disk. */
	int write = 0;
	/* C programs always get at least one argument; we want at
		least one more (the image), more in practice. */
	if (argc < 2) {
		no = 1;	
		goto usage;
	}
	/* The last argument should be the image. Open the file. */
	in = fopen(argv[argc-1], "rb");	
	if (!in) {
		fprintf(stderr,
			"Error: can't open file %s.\n", argv[argc-1]);
	}
	/* Now load the image. */	
	im = gdImageCreateFromGif(in);
	fclose(in);
	/* If the load failed, it must not be a GIF file. */
	if (!im) {
		fprintf(stderr,
			"Error: %s is not a valid gif file.\n", argv[1]);
		exit(1);	
	}
	/* Consider each argument in turn. */
	for (i=1; (i < (argc-1)); i++) {
		/* -i turns on and off interlacing. */
		if (!strcmp(argv[i], "-i")) {
			if (i == (argc-2)) {
				fprintf(stderr, 
				"Error: -i specified without y or n.\n");
				no = 1;
				goto usage;
			}
			if (!strcmp(argv[i+1], "y")) {
				/* Set interlace. */
				gdImageInterlace(im, 1);
			} else if (!strcmp(argv[i+1], "n")) {
				/* Clear interlace. */
				gdImageInterlace(im, 0);
			} else {
				fprintf(stderr,
				"Error: -i specified without y or n.\n");
				no = 1;
				goto usage;
			}
			i++;
			no = 0;
			write = 1;
		} else if (!strcmp(argv[i], "-t")) {
			/* Set transparent index (or none). */
			int index;
			if (i == (argc-2)) {
				fprintf(stderr,
		"Error: -t specified without a color table index.\n");
				no = 1;
				goto usage;
			}
			if (!strcmp(argv[i+1], "none")) {
				/* -1 means not transparent. */
				gdImageColorTransparent(im, -1);
			} else {
				/* OK, get an integer and set the index. */
				index = atoi(argv[i+1]);
				gdImageColorTransparent(im, index);
			}
			i++;
			write = 1;
			no = 0;
		} else if (!strcmp(argv[i], "-l")) {
			/* List the colors in the color table. */
			int j;
			/* Tabs used below. */
			printf("Index	Red	Green	Blue\n");
			for (j=0; (j < gdImageColorsTotal(im)); j++) {
				/* Use access macros to learn colors. */
				printf("%d	%d	%d	%d\n",
					j, 
					gdImageRed(im, j),
					gdImageGreen(im, j),
					gdImageBlue(im, j));
			}
			no = 0;
		} else if (!strcmp(argv[i], "-d")) {
			/* Output dimensions, etc. */
			int t;
			printf("Width: %d Height: %d Colors: %d\n",
				gdImageSX(im), gdImageSY(im),
				gdImageColorsTotal(im));
			t = gdImageGetTransparent(im);
			if (t != (-1)) {
				printf("Transparent index: %d\n", t);
			} else {
				/* -1 means the image is not transparent. */
				printf("Transparent index: none\n");
			}
			if (gdImageGetInterlaced(im)) {
				printf("Interlaced: yes\n");	
			} else {
				printf("Interlaced: no\n");	
			}
			no = 0;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", argv[i]);
			break;	
		}
	}
usage:
	if (no) {
		/* If the command failed, output an explanation. */
		fprintf(stderr, 
	"Usage: webgif [-i y|n ] [-l] [-t index|off ] [-d] gifname.gif\n");
		fprintf(stderr, 
	"Where -i controls interlace (specify y or n for yes or no),\n");
		fprintf(stderr, 
	"-l outputs a table of color indexes, -t sets the specified\n");
		fprintf(stderr, 
	"color index (0-255 or none) to be the transparent color, and\n");
		fprintf(stderr,
	"-d reports the dimensions and other characteristics of the image.\n");
		fprintf(stderr, 
	"Note: you may wish to pipe to \"more\" when using the -l option.\n");
	} 
	if (write) {
		/* Open a temporary file. */
		out = fopen("temp.tmp", "wb");
		if (!out) {
			fprintf(stderr,
				"Unable to write to temp.tmp -- exiting\n");
			exit(1);
		}
		/* Write the new gif. */
		gdImageGif(im, out);
		fclose(out);
		/* Erase the old gif. */
		unlink(argv[argc-1]);
		/* Rename the new to the old. */
		rename("temp.tmp", argv[argc-1]);
	}
	/* Delete the image from memory. */
	if (im) {
		gdImageDestroy(im);
	}
	/* All's well that ends well. */
	return 0;
}
int main(int argc, char **argv)
{
	FILE *in;
	FILE *out;
	char outFn[20];
	int useStdinStdout=0;

	/* Declare our image pointer */
	gdImagePtr im = 0;
	int i;
       /* We'll clear 'no' once we know the user has made a
		reasonable request. */
	int no = 1;
	/* We'll set 'write' once we know the user's request
		requires that the image be written back to disk. */
	int write = 0;
	/* C programs always get at least one argument; we want at
		least one more (the image), more in practice. */
	if (argc < 2 || !strcmp(argv[1], "--help")) {
		no = 1;	
		goto usage;
	}

	/* The last argument should be the image. Open the file. */
	if (strcmp("-", argv[argc-1])==0) { /* - is synonymous with STDIN */
	  useStdinStdout = 1;
	  in = stdin;
	} else {
	  in = fopen(argv[argc-1], "rb");	
	}
	if (!in) {
		fprintf(stderr,
			"Error: can't open file %s.\n", argv[argc-1]);
		exit(1);
	}
	/* Now load the image. */	
	im = gdImageCreateFromPng(in);
	fclose(in);
	/* If the load failed, it must not be a PNG file. */
	if (!im) {
		fprintf(stderr,
			"Error: %s is not a valid PNG file.\n", argv[argc-1]);
		exit(1);	
	}
	/* Consider each argument in turn. */
	for (i=1; (i < (argc-1)); i++) {
		/* -i turns on and off interlacing. */
		if (!strcmp(argv[i], "--help")) { 
		  /* Every program should use this for help! :) */
		  no = 1;
		  goto usage;
		} else if (!strcmp(argv[i], "-i")) {
			if (i == (argc-2)) {
				fprintf(stderr, 
				"Error: -i specified without y or n.\n");
				no = 1;
				goto usage;
			}
			if (!strcmp(argv[i+1], "y")) {
				/* Set interlace. */
				gdImageInterlace(im, 1);
			} else if (!strcmp(argv[i+1], "n")) {
				/* Clear interlace. */
				gdImageInterlace(im, 0);
			} else {
				fprintf(stderr,
				"Error: -i specified without y or n.\n");
				no = 1;
				goto usage;
			}
			i++;
			no = 0;
			write = 1;
		} else if (!strcmp(argv[i], "-t")) {
			/* Set transparent index (or none). */
			int index;
			if (i == (argc-2)) {
				fprintf(stderr,
		"Error: -t specified without a color table index.\n");
				no = 1;
				goto usage;
			}
			if (!strcmp(argv[i+1], "none")) {
				/* -1 means not transparent. */
				gdImageColorTransparent(im, -1);
			} else {
				/* OK, get an integer and set the index. */
				index = atoi(argv[i+1]);
				gdImageColorTransparent(im, index);
			}
			i++;
			write = 1;
			no = 0;
		} else if (!strcmp(argv[i], "-l")) {
			/* List the colors in the color table. */
			int j;
			/* Tabs used below. */
			printf("Index	Red	Green	Blue\n");
			for (j=0; (j < gdImageColorsTotal(im)); j++) {
				/* Use access macros to learn colors. */
				printf("%d	%d	%d	%d\n",
					j, 
					gdImageRed(im, j),
					gdImageGreen(im, j),
					gdImageBlue(im, j));
			}
			no = 0;
		} else if (!strcmp(argv[i], "-d")) {
			/* Output dimensions, etc. */
			int t;
			printf("Width: %d Height: %d Colors: %d\n",
				gdImageSX(im), gdImageSY(im),
				gdImageColorsTotal(im));
			t = gdImageGetTransparent(im);
			if (t != (-1)) {
				printf("Transparent index: %d\n", t);
			} else {
				/* -1 means the image is not transparent. */
				printf("Transparent index: none\n");
			}
			if (gdImageGetInterlaced(im)) {
				printf("Interlaced: yes\n");	
			} else {
				printf("Interlaced: no\n");	
			}
			no = 0;
		} else {
			fprintf(stderr, "Unknown argument: %s\n", argv[i]);
			break;	
		}
	}
usage:
	if (no) {
		/* If the command failed, output an explanation. */
		fprintf(stderr, 
"Usage: webpng [-i y|n ] [-l] [-t index|none ] [-d] pngname.png\n"

"  -i [y|n]   Turns on/off interlace\n"
"  -l         Prints the table of color indexes\n"
"  -t [index] Set the transparent color to the specified index (0-255 or \"none\")\n"
"  -d         Reports the dimensions and other characteristics of the image.\n"
"\n"
"If you specify '-' as the input file, stdin/stdout will be used input/output.\n"
);
	} 
	if (write) {
	  if (useStdinStdout) {
	    out = stdout;
	  } else {
	    /* Open a temporary file. */

	    /* "temp.tmp" is not good temporary filename. */
	    sprintf(outFn, "webpng.tmp%d", getpid());
	    out = fopen(outFn, "wb"); 

	    if (!out) {
	      fprintf(stderr,
		      "Unable to write to %s -- exiting\n", outFn);
	      exit(1);
	    }
	  }

	  /* Write the new PNG. */
	  gdImagePng(im, out);

	  if (!useStdinStdout) {
	    fclose(out);
	    /* Erase the old PNG. */
	    unlink(argv[argc-1]);
	    /* Rename the new to the old. */
	    if (rename(outFn, argv[argc-1])!=0) {
	      perror("rename");
	      exit(1);
	    }
	  }
	}
	/* Delete the image from memory. */
	if (im) {
		gdImageDestroy(im);
	}
	/* All's well that ends well. */
	return 0;
}
void
gdImageJpegCtx(gdImagePtr im, gdIOCtx *outfile, int quality)
{
    struct jpeg_compress_struct cinfo;
    struct jpeg_error_mgr jerr;
    int i, j, jidx;
    /* volatile so we can gdFree it on return from longjmp */
    volatile JSAMPROW row = 0;
    JSAMPROW rowptr[1];
    jmpbuf_wrapper jmpbufw;
    JDIMENSION nlines;
    char comment[255];

#ifdef JPEG_DEBUG
    printf("gd-jpeg: gd JPEG version %s\n", GD_JPEG_VERSION);
    printf("gd-jpeg: JPEG library version %d, %d-bit sample values\n",
	   JPEG_LIB_VERSION, BITS_IN_JSAMPLE);

    for (i = 0; i < im->colorsTotal; i++) {
	if (!im->open[i])
	    printf("gd-jpeg: gd colormap index %d: (%d, %d, %d)\n", i,
		   im->red[i], im->green[i], im->blue[i]);
    }
#endif /* JPEG_DEBUG */

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

    cinfo.err = jpeg_std_error(&jerr);
    cinfo.client_data = &jmpbufw;
    if (setjmp(jmpbufw.jmpbuf) != 0) {
	/* we're here courtesy of longjmp */
	if (row)
	    gdFree(row);
	return;
    }

    cinfo.err->error_exit = fatal_jpeg_error;

    jpeg_create_compress(&cinfo);

    cinfo.image_width = im->sx;
    cinfo.image_height = im->sy;
    cinfo.input_components = 3;	/* # of color components per pixel */
    cinfo.in_color_space = JCS_RGB; /* colorspace of input image */
    jpeg_set_defaults(&cinfo);
    if (quality >= 0)
	jpeg_set_quality(&cinfo, quality, TRUE);

    /* If user requests interlace, translate that to progressive JPEG */
    if (gdImageGetInterlaced(im)) {
#ifdef JPEG_DEBUG
	printf("gd-jpeg: interlace set, outputting progressive"
	       " JPEG image\n"); 
#endif	
	jpeg_simple_progression(&cinfo);
    }

    jpeg_gdIOCtx_dest(&cinfo, outfile);

    row = (JSAMPROW)gdCalloc(1, cinfo.image_width * cinfo.input_components
			   * sizeof(JSAMPLE));
    if (row == 0) {
	fprintf(stderr, "gd-jpeg: error: unable to allocate JPEG row "
		"structure: gdCalloc returns NULL\n");
	jpeg_destroy_compress(&cinfo);
	return;
    }

    rowptr[0] = row;

    jpeg_start_compress(&cinfo, TRUE);

    sprintf(comment, "CREATOR: gd-jpeg v%s (using IJG JPEG v%d),",
	    GD_JPEG_VERSION, JPEG_LIB_VERSION);
    if (quality >= 0)
	sprintf(comment + strlen(comment), " quality = %d\n",
		quality);
    else
	strcat(comment + strlen(comment), " default quality\n");
    jpeg_write_marker(&cinfo, JPEG_COM, (unsigned char *)comment,
		      (unsigned int)strlen(comment));

    for (i = 0; i < im->sy; i++) {
	for (jidx = 0, j = 0; j < im->sx; j++) {
	    int idx = im->pixels[i][j];

	    /*
	     * NB: Although gd RGB values are ints, their max value is
	     * 255 (see the documentation for gdImageColorAllocate())
	     * -- perfect for 8-bit JPEG encoding (which is the norm)
	     */
#if BITS_IN_JSAMPLE == 8
	    row[jidx++] = im->red[idx];
	    row[jidx++] = im->green[idx];
	    row[jidx++] = im->blue[idx];
#elif BITS_IN_JSAMPLE == 12
	    row[jidx++] = im->red[idx] << 4;
	    row[jidx++] = im->green[idx] << 4;
	    row[jidx++] = im->blue[idx] << 4;
#else
#error IJG JPEG library BITS_IN_JSAMPLE value must be 8 or 12
#endif
	}

	nlines = jpeg_write_scanlines(&cinfo, rowptr, 1);
	if (nlines != 1)
	    fprintf(stderr, "gd_jpeg: warning: jpeg_write_scanlines"
		    " returns %u -- expected 1\n", nlines);
    }

    jpeg_finish_compress(&cinfo);
    jpeg_destroy_compress(&cinfo);
    gdFree(row);
}