summaryrefslogtreecommitdiff
path: root/test/testdither.c
diff options
context:
space:
mode:
authorRoger Leigh <rleigh@debian.org>2008-10-26 16:11:05 +0000
committerRoger Leigh <rleigh@debian.org>2008-10-26 16:11:05 +0000
commit3b59bb0a607ec27ea60f07d1cd5d1bbb4483c832 (patch)
treec119edaa8374e7b6387de7aa7d65b143732af5db /test/testdither.c
parenteb5718390731a9746c556317e641320b671f2091 (diff)
Imported Upstream version 4.2.7
Diffstat (limited to 'test/testdither.c')
-rw-r--r--test/testdither.c319
1 files changed, 221 insertions, 98 deletions
diff --git a/test/testdither.c b/test/testdither.c
index 2733f8b..52c2ea4 100644
--- a/test/testdither.c
+++ b/test/testdither.c
@@ -1,5 +1,5 @@
/*
- * "$Id: testdither.c,v 1.11 2001/09/02 13:30:27 rlk Exp $"
+ * "$Id: testdither.c,v 1.11.4.1 2002/05/03 01:30:29 rlk Exp $"
*
* Test/profiling program for dithering code.
*
@@ -35,12 +35,6 @@
#include <unistd.h>
/*
- * NOTE: writing of 2-bit dither images is currently broken due to the
- * separated planes generated by the dither functions.
- */
-
-
-/*
* Definitions for dither test...
*/
@@ -57,6 +51,9 @@
#define DITHER_GRAY 0 /* Dither grayscale pixels */
#define DITHER_COLOR 1 /* Dither color pixels */
#define DITHER_PHOTO 2 /* Dither photo pixels */
+#define DITHER_MONOCHROME 3 /* Dither photo pixels */
+#define DITHER_CMYK 4 /* Dither photo pixels */
+#define DITHER_PHOTO_CMYK 5 /* Dither photo pixels */
/*
@@ -65,38 +62,39 @@
int image_type = IMAGE_MIXED;
int dither_type = DITHER_COLOR;
+const char *dither_name = NULL;
int dither_bits = 1;
-unsigned short white_line[IMAGE_WIDTH * 3],
- black_line[IMAGE_WIDTH * 3],
- color_line[IMAGE_WIDTH * 3],
- random_line[IMAGE_WIDTH * 3];
+unsigned short white_line[IMAGE_WIDTH * 4],
+ black_line[IMAGE_WIDTH * 4],
+ color_line[IMAGE_WIDTH * 4],
+ random_line[IMAGE_WIDTH * 4];
stp_simple_dither_range_t normal_1bit_ranges[] =
{
- { 1.0, 0x1, 1, 1 }
+ { 1.0, 0x1, 0, 1 }
};
stp_simple_dither_range_t normal_2bit_ranges[] =
{
- { 0.45, 0x1, 1, 1 },
- { 0.68, 0x2, 1, 2 },
- { 1.0, 0x3, 1, 3 }
+ { 0.45, 0x1, 0, 1 },
+ { 0.68, 0x2, 0, 2 },
+ { 1.0, 0x3, 0, 3 }
};
stp_simple_dither_range_t photo_1bit_ranges[] =
{
- { 0.33, 0x1, 0, 1 },
- { 1.0, 0x1, 1, 1 }
+ { 0.33, 0x1, 1, 1 },
+ { 1.0, 0x1, 0, 1 }
};
stp_simple_dither_range_t photo_2bit_ranges[] =
{
- { 0.15, 0x1, 0, 1 },
- { 0.227, 0x2, 0, 2 },
- { 0.45, 0x1, 1, 1 },
- { 0.68, 0x2, 1, 2 },
- { 1.0, 0x3, 1, 3 }
+ { 0.15, 0x1, 1, 1 },
+ { 0.227, 0x2, 1, 2 },
+ { 0.45, 0x1, 0, 1 },
+ { 0.68, 0x2, 0, 2 },
+ { 1.0, 0x3, 0, 3 }
};
@@ -118,6 +116,13 @@ compute_interval(struct timeval *tv1, struct timeval *tv2)
((double) tv1->tv_sec + (double) tv1->tv_usec / 1000000.);
}
+static void
+writefunc(void *file, const char *buf, size_t bytes)
+{
+ FILE *prn = (FILE *)file;
+ fwrite(buf, 1, bytes, prn);
+}
+
/*
* 'main()' - Test dithering code for performance measurement.
*/
@@ -134,27 +139,31 @@ main(int argc, /* I - Number of command-line arguments */
lmagenta[BUFFER_SIZE], /* Light magenta bitmap data */
yellow[BUFFER_SIZE]; /* Yellow bitmap data */
void *dither; /* Dither data */
- unsigned short rgb[IMAGE_WIDTH * 3], /* RGB buffer */
+ unsigned short rgb[IMAGE_WIDTH * 4], /* RGB buffer */
gray[IMAGE_WIDTH]; /* Grayscale buffer */
int write_image; /* Write the image to disk? */
- FILE *fp; /* PPM/PGM output file */
+ FILE *fp = NULL; /* PPM/PGM output file */
char filename[1024]; /* Name of file */
stp_vars_t v; /* Dither variables */
static const char *dither_types[] = /* Different dithering modes */
{
"gray",
"color",
- "photo"
+ "photo",
+ "monochrome",
+ "cmyk",
+ "photocmyk"
};
static const char *image_types[] = /* Different image types */
{
"mixed",
"white",
"black",
- "color",
+ "colorimage",
"random"
};
struct timeval tv1, tv2;
+ stp_dither_data_t *dt;
/*
* Initialise libgimpprint
@@ -189,11 +198,11 @@ main(int argc, /* I - Number of command-line arguments */
continue;
}
- for (j = 0; j < 3; j ++)
+ for (j = 0; j < 6; j ++)
if (strcmp(argv[i], dither_types[j]) == 0)
break;
- if (j < 3)
+ if (j < 6)
{
dither_type = j;
continue;
@@ -203,12 +212,22 @@ main(int argc, /* I - Number of command-line arguments */
if (strcmp(argv[i], image_types[j]) == 0)
break;
- if (j < 3)
+ if (j < 5)
{
image_type = j;
continue;
}
+ for (j = 0; j < stp_dither_algorithm_count(); j ++)
+ if (strcmp(argv[i], stp_dither_algorithm_name(j)) == 0)
+ break;
+
+ if (j < stp_dither_algorithm_count())
+ {
+ dither_name = stp_dither_algorithm_name(j);
+ continue;
+ }
+
printf("Unknown option \"%s\" ignored!\n", argv[i]);
}
@@ -217,12 +236,35 @@ main(int argc, /* I - Number of command-line arguments */
*/
image_init();
+ stp_set_outfunc(v, writefunc);
+ stp_set_errfunc(v, writefunc);
+ stp_set_outdata(v, stdout);
+ stp_set_errdata(v, stderr);
/*
* Output the page...
*/
- stp_set_dither_algorithm(v, "Adaptive Hybrid");
+ if (dither_name)
+ stp_set_dither_algorithm(v, dither_name);
+
+ switch (dither_type)
+ {
+ case DITHER_GRAY:
+ stp_set_output_type(v, OUTPUT_GRAY);
+ break;
+ case DITHER_MONOCHROME:
+ stp_set_output_type(v, OUTPUT_MONOCHROME);
+ break;
+ case DITHER_COLOR:
+ case DITHER_PHOTO:
+ stp_set_output_type(v, OUTPUT_COLOR);
+ break;
+ case DITHER_CMYK:
+ case DITHER_PHOTO_CMYK:
+ stp_set_output_type(v, OUTPUT_RAW_CMYK);
+ break;
+ }
dither = stp_init_dither(IMAGE_WIDTH, IMAGE_WIDTH, 1, 1, v);
@@ -239,6 +281,7 @@ main(int argc, /* I - Number of command-line arguments */
switch (dither_type)
{
case DITHER_GRAY :
+ case DITHER_MONOCHROME :
switch (dither_bits)
{
case 1 :
@@ -251,6 +294,7 @@ main(int argc, /* I - Number of command-line arguments */
}
break;
case DITHER_COLOR :
+ case DITHER_CMYK :
switch (dither_bits)
{
case 1 :
@@ -269,6 +313,7 @@ main(int argc, /* I - Number of command-line arguments */
}
break;
case DITHER_PHOTO :
+ case DITHER_PHOTO_CMYK :
switch (dither_bits)
{
case 1 :
@@ -295,37 +340,60 @@ main(int argc, /* I - Number of command-line arguments */
* Open the PPM/PGM file...
*/
- if (write_image)
- {
- sprintf(filename, "%s-%s-%dbit.%s", image_types[image_type],
- dither_types[dither_type], dither_bits,
- dither_type == DITHER_GRAY ? "pgm" : "ppm");
- if ((fp = fopen(filename, "wb")) != NULL)
+ sprintf(filename, "%s-%s-%s-%dbit.%s", image_types[image_type],
+ dither_types[dither_type],
+ dither_name ? dither_name : stp_default_dither_algorithm(),
+ dither_bits,
+ (dither_type == DITHER_GRAY || dither_type == DITHER_MONOCHROME) ?
+ "pgm" : "ppm");
+
+ printf("%s ", filename);
+
+ if (write_image)
{
- puts(filename);
- if (dither_type == DITHER_GRAY)
- fputs("P5\n", fp);
- else
- fputs("P6\n", fp);
+ if ((fp = fopen(filename, "wb")) != NULL)
+ {
+ puts(filename);
+ if (dither_type == DITHER_GRAY || dither_type == DITHER_MONOCHROME)
+ fputs("P5\n", fp);
+ else
+ fputs("P6\n", fp);
- fprintf(fp, "%d\n%d\n255\n", IMAGE_WIDTH, IMAGE_HEIGHT);
+ fprintf(fp, "%d\n%d\n255\n", IMAGE_WIDTH, IMAGE_HEIGHT);
+ }
+ else
+ perror("Create");
}
- else
- perror(filename);
- }
- else
- fp = NULL;
/*
* Now dither the "page"...
*/
+ dt = stp_create_dither_data();
+ switch (dither_type)
+ {
+ case DITHER_PHOTO:
+ case DITHER_PHOTO_CMYK :
+ stp_add_channel(dt, lcyan, ECOLOR_C, 1);
+ stp_add_channel(dt, lmagenta, ECOLOR_M, 1);
+ /* FALLTHROUGH */
+ case DITHER_COLOR:
+ case DITHER_CMYK :
+ stp_add_channel(dt, cyan, ECOLOR_C, 0);
+ stp_add_channel(dt, magenta, ECOLOR_M, 0);
+ stp_add_channel(dt, yellow, ECOLOR_Y, 0);
+ /* FALLTHROUGH */
+ case DITHER_GRAY:
+ case DITHER_MONOCHROME:
+ stp_add_channel(dt, black, ECOLOR_K, 0);
+ }
+
(void) gettimeofday(&tv1, NULL);
for (i = 0; i < IMAGE_HEIGHT; i ++)
{
- if ((i & 15) == 0)
+ if ((i & 63) == 0)
{
printf("\rProcessing row %d...", i);
fflush(stdout);
@@ -334,22 +402,23 @@ main(int argc, /* I - Number of command-line arguments */
switch (dither_type)
{
case DITHER_GRAY :
+ case DITHER_MONOCHROME :
image_get_row(gray, i);
- stp_dither(gray, i, dither, 0, 0, 0, 0, 0, 0, black, 0, 0);
+ stp_dither(gray, i, dither, dt, 0, 0);
if (fp)
write_gray(fp, black);
break;
case DITHER_COLOR :
+ case DITHER_CMYK :
image_get_row(rgb, i);
- stp_dither(rgb, i, dither, cyan, 0, magenta, 0,
- yellow, 0, black, 0, 0);
+ stp_dither(rgb, i, dither, dt, 0, 0);
if (fp)
write_color(fp, cyan, magenta, yellow, black);
break;
case DITHER_PHOTO :
+ case DITHER_PHOTO_CMYK :
image_get_row(rgb, i);
- stp_dither(rgb, i, dither, cyan, lcyan, magenta, lmagenta,
- yellow, 0, black, 0, 0);
+ stp_dither(rgb, i, dither, dt, 0, 0);
if (fp)
write_photo(fp, cyan, lcyan, magenta, lmagenta, yellow, black);
break;
@@ -358,13 +427,14 @@ main(int argc, /* I - Number of command-line arguments */
(void) gettimeofday(&tv2, NULL);
+ stp_free_dither_data(dt);
stp_free_dither(dither);
if (fp != NULL)
fclose(fp);
- printf("\rTotal dither time for %d pixels is %.3f seconds, or %.2f pixels/sec.\n",
- IMAGE_WIDTH * IMAGE_HEIGHT, compute_interval(&tv1, &tv2),
+ printf("\r%-40s Total dither time for %d pixels is %.3f seconds, or %.2f pixels/sec.\n",
+ filename, IMAGE_WIDTH * IMAGE_HEIGHT, compute_interval(&tv1, &tv2),
(float)(IMAGE_WIDTH * IMAGE_HEIGHT) / compute_interval(&tv1, &tv2));
return 0;
}
@@ -412,10 +482,21 @@ image_get_row(unsigned short *data,
break;
}
- if (dither_type == DITHER_GRAY)
- memcpy(data, src, IMAGE_WIDTH * 2);
- else
- memcpy(data, src, IMAGE_WIDTH * 6);
+ switch (dither_type)
+ {
+ case DITHER_GRAY:
+ case DITHER_MONOCHROME:
+ memcpy(data, src, IMAGE_WIDTH * 2);
+ break;
+ case DITHER_COLOR:
+ case DITHER_PHOTO:
+ memcpy(data, src, IMAGE_WIDTH * 6);
+ break;
+ case DITHER_CMYK:
+ case DITHER_PHOTO_CMYK:
+ memcpy(data, src, IMAGE_WIDTH * 8);
+ break;
+ }
}
@@ -431,8 +512,8 @@ image_init(void)
* Set the white and black line data...
*/
- memset(white_line, 255, sizeof(white_line));
- memset(black_line, 0, sizeof(black_line));
+ memset(white_line, 0, sizeof(white_line));
+ memset(black_line, 255, sizeof(black_line));
/*
* Fill in the color and random data...
@@ -446,25 +527,34 @@ image_init(void)
j = i / (IMAGE_WIDTH / 64);
- if (dither_type == DITHER_GRAY)
- *cptr++ = 65535 * j / 63;
- else
- {
- *cptr++ = 65535 * (j >> 4) / 3;
- *cptr++ = 65535 * ((j >> 2) & 3) / 3;
- *cptr++ = 65535 * (j & 3) / 3;
- }
-
- /*
- * Do random colors over the line...
- */
-
- *rptr++ = 65535 * (rand() & 255) / 255;
- if (dither_type != DITHER_GRAY)
- {
- *rptr++ = 65535 * (rand() & 255) / 255;
- *rptr++ = 65535 * (rand() & 255) / 255;
- }
+ switch (dither_type)
+ {
+ case DITHER_GRAY:
+ case DITHER_MONOCHROME:
+ *cptr++ = 65535 * j / 63;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ break;
+ case DITHER_COLOR:
+ case DITHER_PHOTO:
+ *cptr++ = 65535 * (j >> 4) / 3;
+ *cptr++ = 65535 * ((j >> 2) & 3) / 3;
+ *cptr++ = 65535 * (j & 3) / 3;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ break;
+ case DITHER_CMYK:
+ case DITHER_PHOTO_CMYK:
+ *cptr++ = 65535 * (j >> 4) / 3;
+ *cptr++ = 65535 * ((j >> 2) & 3) / 3;
+ *cptr++ = 65535 * (j & 3) / 3;
+ *cptr++ = 65535 * j / 63;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ *rptr++ = 65535 * (rand() & 255) / 255;
+ break;
+ }
}
}
@@ -499,7 +589,10 @@ write_gray(FILE *fp,
}
else
{
- for (count = IMAGE_WIDTH, byte = *black++, shift = 6; count > 0; count --)
+ unsigned char kb[BUFFER_SIZE];
+ unsigned char *kbuf = kb;
+ stp_fold(black, IMAGE_WIDTH / 8, kbuf);
+ for (count = IMAGE_WIDTH, byte = *kbuf++, shift = 6; count > 0; count --)
{
putc(255 - 255 * ((byte >> shift) & 3) / 3, fp);
@@ -507,7 +600,7 @@ write_gray(FILE *fp,
shift -= 2;
else
{
- byte = *black++;
+ byte = *kbuf++;
shift = 6;
}
}
@@ -577,8 +670,20 @@ write_color(FILE *fp,
}
else
{
- for (count = IMAGE_WIDTH, cbyte = *cyan++, mbyte = *magenta++,
- ybyte = *yellow++, kbyte = *black++, shift = 6;
+ unsigned char kb[BUFFER_SIZE];
+ unsigned char cb[BUFFER_SIZE];
+ unsigned char mb[BUFFER_SIZE];
+ unsigned char yb[BUFFER_SIZE];
+ unsigned char *kbuf = kb;
+ unsigned char *cbuf = cb;
+ unsigned char *mbuf = mb;
+ unsigned char *ybuf = yb;
+ stp_fold(black, IMAGE_WIDTH / 8, kbuf);
+ stp_fold(cyan, IMAGE_WIDTH / 8, cbuf);
+ stp_fold(magenta, IMAGE_WIDTH / 8, mbuf);
+ stp_fold(yellow, IMAGE_WIDTH / 8, ybuf);
+ for (count = IMAGE_WIDTH, cbyte = *cbuf++, mbyte = *mbuf++,
+ ybyte = *ybuf++, kbyte = *kbuf++, shift = 6;
count > 0;
count --)
{
@@ -606,10 +711,10 @@ write_color(FILE *fp,
shift -= 2;
else
{
- cbyte = *cyan++;
- mbyte = *magenta++;
- ybyte = *yellow++;
- kbyte = *black++;
+ cbyte = *cbuf++;
+ mbyte = *mbuf++;
+ ybyte = *ybuf++;
+ kbyte = *kbuf++;
shift = 6;
}
}
@@ -690,9 +795,27 @@ write_photo(FILE *fp,
}
else
{
- for (count = IMAGE_WIDTH, cbyte = *cyan++, lcbyte = *lcyan++,
- mbyte = *magenta++, lmbyte = *lmagenta++,
- ybyte = *yellow++, kbyte = *black++, shift = 6;
+ unsigned char kb[BUFFER_SIZE];
+ unsigned char cb[BUFFER_SIZE];
+ unsigned char mb[BUFFER_SIZE];
+ unsigned char lcb[BUFFER_SIZE];
+ unsigned char lmb[BUFFER_SIZE];
+ unsigned char yb[BUFFER_SIZE];
+ unsigned char *kbuf = kb;
+ unsigned char *cbuf = cb;
+ unsigned char *mbuf = mb;
+ unsigned char *lcbuf = lcb;
+ unsigned char *lmbuf = lmb;
+ unsigned char *ybuf = yb;
+ stp_fold(black, IMAGE_WIDTH / 8, kbuf);
+ stp_fold(cyan, IMAGE_WIDTH / 8, cbuf);
+ stp_fold(magenta, IMAGE_WIDTH / 8, mbuf);
+ stp_fold(yellow, IMAGE_WIDTH / 8, ybuf);
+ stp_fold(lcyan, IMAGE_WIDTH / 8, lcbuf);
+ stp_fold(lmagenta, IMAGE_WIDTH / 8, lmbuf);
+ for (count = IMAGE_WIDTH, cbyte = *cbuf++, mbyte = *mbuf++,
+ ybyte = *ybuf++, kbyte = *kbuf++, lmbyte = *lmbuf++,
+ lcbyte = *lcyan++, shift = 6;
count > 0;
count --)
{
@@ -722,12 +845,12 @@ write_photo(FILE *fp,
shift -= 2;
else
{
- cbyte = *cyan++;
- lcbyte = *lcyan++;
- mbyte = *magenta++;
- lmbyte = *lmagenta++;
- ybyte = *yellow++;
- kbyte = *black++;
+ cbyte = *cbuf++;
+ mbyte = *mbuf++;
+ ybyte = *ybuf++;
+ kbyte = *kbuf++;
+ lmbyte = *lmbuf++;
+ lcbyte = *lcbuf++;
shift = 6;
}
}
@@ -736,5 +859,5 @@ write_photo(FILE *fp,
/*
- * End of "$Id: testdither.c,v 1.11 2001/09/02 13:30:27 rlk Exp $".
+ * End of "$Id: testdither.c,v 1.11.4.1 2002/05/03 01:30:29 rlk Exp $".
*/