Skip to content

Commit 1b35321

Browse files
committed
Add PngSuite to testpdfio (Issue #90)
1 parent 990342f commit 1b35321

File tree

1 file changed

+210
-15
lines changed

1 file changed

+210
-15
lines changed

testpdfio.c

Lines changed: 210 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ static int write_header_footer(pdfio_stream_t *st, const char *title, int number
4747
static pdfio_obj_t *write_image_object(pdfio_file_t *pdf, _pdfio_predictor_t predictor);
4848
static int write_images_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
4949
static int write_jpeg_test(pdfio_file_t *pdf, const char *title, int number, pdfio_obj_t *font, pdfio_obj_t *image);
50-
static int write_png_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
50+
static int write_png_tests(pdfio_file_t *pdf, int number, pdfio_obj_t *font);
5151
static int write_text_test(pdfio_file_t *pdf, int first_page, pdfio_obj_t *font, const char *filename);
5252
static int write_unit_file(pdfio_file_t *inpdf, const char *outname, pdfio_file_t *outpdf, size_t *num_pages, size_t *first_image);
5353

@@ -2989,19 +2989,84 @@ write_jpeg_test(pdfio_file_t *pdf, // I - PDF file
29892989

29902990

29912991
//
2992-
// 'write_png_test()' - Write a page of PNG test images.
2992+
// 'write_png_tests()' - Write pages of PNG test images.
29932993
//
29942994

29952995
static int // O - 0 on success, 1 on failure
2996-
write_png_test(pdfio_file_t *pdf, // I - PDF file
2997-
int number, // I - Page number
2998-
pdfio_obj_t *font) // I - Page number font
2996+
write_png_tests(pdfio_file_t *pdf, // I - PDF file
2997+
int number, // I - Page number
2998+
pdfio_obj_t *font) // I - Page number font
29992999
{
30003000
pdfio_dict_t *dict; // Page dictionary
30013001
pdfio_stream_t *st; // Page contents stream
30023002
pdfio_obj_t *color, // pdfio-color.png
30033003
*gray, // pdfio-gray.png
30043004
*indexed; // pdfio-indexed.png
3005+
#ifdef HAVE_LIBPNG
3006+
size_t i; // Looping var
3007+
char imgname[32]; // Image name
3008+
pdfio_obj_t *pngsuite[80]; // PngSuite test file objects
3009+
static const char * const pngsuite_files[80] =
3010+
{ // PngSuite test filenames
3011+
"testfiles/pngsuite/basi0g01.png", "testfiles/pngsuite/basi0g02.png",
3012+
"testfiles/pngsuite/basi0g04.png", "testfiles/pngsuite/basi0g08.png",
3013+
"testfiles/pngsuite/basi2c08.png", "testfiles/pngsuite/basi3p01.png",
3014+
"testfiles/pngsuite/basi3p02.png", "testfiles/pngsuite/basi3p04.png",
3015+
"testfiles/pngsuite/basi3p08.png", "testfiles/pngsuite/basi4a08.png",
3016+
"testfiles/pngsuite/basi6a08.png", "testfiles/pngsuite/basn0g01.png",
3017+
"testfiles/pngsuite/basn0g02.png", "testfiles/pngsuite/basn0g04.png",
3018+
"testfiles/pngsuite/basn0g08.png", "testfiles/pngsuite/basn2c08.png",
3019+
"testfiles/pngsuite/basn3p01.png", "testfiles/pngsuite/basn3p02.png",
3020+
"testfiles/pngsuite/basn3p04.png", "testfiles/pngsuite/basn3p08.png",
3021+
"testfiles/pngsuite/basn4a08.png", "testfiles/pngsuite/basn6a08.png",
3022+
"testfiles/pngsuite/exif2c08.png", "testfiles/pngsuite/g03n2c08.png",
3023+
"testfiles/pngsuite/g03n3p04.png", "testfiles/pngsuite/g04n2c08.png",
3024+
"testfiles/pngsuite/g04n3p04.png", "testfiles/pngsuite/g05n2c08.png",
3025+
"testfiles/pngsuite/g05n3p04.png", "testfiles/pngsuite/g07n2c08.png",
3026+
"testfiles/pngsuite/g07n3p04.png", "testfiles/pngsuite/g10n2c08.png",
3027+
"testfiles/pngsuite/g10n3p04.png", "testfiles/pngsuite/g25n2c08.png",
3028+
"testfiles/pngsuite/g25n3p04.png", "testfiles/pngsuite/s02i3p01.png",
3029+
"testfiles/pngsuite/s02n3p01.png", "testfiles/pngsuite/s03i3p01.png",
3030+
"testfiles/pngsuite/s03n3p01.png", "testfiles/pngsuite/s04i3p01.png",
3031+
"testfiles/pngsuite/s04n3p01.png", "testfiles/pngsuite/s05i3p02.png",
3032+
"testfiles/pngsuite/s05n3p02.png", "testfiles/pngsuite/s06i3p02.png",
3033+
"testfiles/pngsuite/s06n3p02.png", "testfiles/pngsuite/s07i3p02.png",
3034+
"testfiles/pngsuite/s07n3p02.png", "testfiles/pngsuite/s08i3p02.png",
3035+
"testfiles/pngsuite/s08n3p02.png", "testfiles/pngsuite/s09i3p02.png",
3036+
"testfiles/pngsuite/s09n3p02.png", "testfiles/pngsuite/s32i3p04.png",
3037+
"testfiles/pngsuite/s32n3p04.png", "testfiles/pngsuite/s33i3p04.png",
3038+
"testfiles/pngsuite/s33n3p04.png", "testfiles/pngsuite/s34i3p04.png",
3039+
"testfiles/pngsuite/s34n3p04.png", "testfiles/pngsuite/s35i3p04.png",
3040+
"testfiles/pngsuite/s35n3p04.png", "testfiles/pngsuite/s36i3p04.png",
3041+
"testfiles/pngsuite/s36n3p04.png", "testfiles/pngsuite/s37i3p04.png",
3042+
"testfiles/pngsuite/s37n3p04.png", "testfiles/pngsuite/s38i3p04.png",
3043+
"testfiles/pngsuite/s38n3p04.png", "testfiles/pngsuite/s39i3p04.png",
3044+
"testfiles/pngsuite/s39n3p04.png", "testfiles/pngsuite/s40i3p04.png",
3045+
"testfiles/pngsuite/s40n3p04.png", "testfiles/pngsuite/tbbn0g04.png",
3046+
"testfiles/pngsuite/tbbn3p08.png", "testfiles/pngsuite/tbgn3p08.png",
3047+
"testfiles/pngsuite/tbrn2c08.png", "testfiles/pngsuite/tbwn3p08.png",
3048+
"testfiles/pngsuite/tbyn3p08.png", "testfiles/pngsuite/tm3n3p02.png",
3049+
"testfiles/pngsuite/tp0n0g08.png", "testfiles/pngsuite/tp0n2c08.png",
3050+
"testfiles/pngsuite/tp0n3p08.png", "testfiles/pngsuite/tp1n3p08.png"
3051+
};
3052+
static const char * const pngsuite_labels[80] =
3053+
{ // PngSuite test labels
3054+
"basi0g01", "basi0g02", "basi0g04", "basi0g08", "basi2c08", "basi3p01",
3055+
"basi3p02", "basi3p04", "basi3p08", "basi4a08", "basi6a08", "basn0g01",
3056+
"basn0g02", "basn0g04", "basn0g08", "basn2c08", "basn3p01", "basn3p02",
3057+
"basn3p04", "basn3p08", "basn4a08", "basn6a08", "exif2c08", "g03n2c08",
3058+
"g03n3p04", "g04n2c08", "g04n3p04", "g05n2c08", "g05n3p04", "g07n2c08",
3059+
"g07n3p04", "g10n2c08", "g10n3p04", "g25n2c08", "g25n3p04", "s02i3p01",
3060+
"s02n3p01", "s03i3p01", "s03n3p01", "s04i3p01", "s04n3p01", "s05i3p02",
3061+
"s05n3p02", "s06i3p02", "s06n3p02", "s07i3p02", "s07n3p02", "s08i3p02",
3062+
"s08n3p02", "s09i3p02", "s09n3p02", "s32i3p04", "s32n3p04", "s33i3p04",
3063+
"s33n3p04", "s34i3p04", "s34n3p04", "s35i3p04", "s35n3p04", "s36i3p04",
3064+
"s36n3p04", "s37i3p04", "s37n3p04", "s38i3p04", "s38n3p04", "s39i3p04",
3065+
"s39n3p04", "s40i3p04", "s40n3p04", "tbbn0g04", "tbbn3p08", "tbgn3p08",
3066+
"tbrn2c08", "tbwn3p08", "tbyn3p08", "tm3n3p02", "tp0n0g08", "tp0n2c08",
3067+
"tp0n3p08", "tp1n3p08"
3068+
};
3069+
#endif // HAVE_LIBPNG
30053070

30063071

30073072
// Import the PNG test images
@@ -3023,6 +3088,8 @@ write_png_test(pdfio_file_t *pdf, // I - PDF file
30233088
else
30243089
return (1);
30253090

3091+
#ifdef HAVE_LIBPNG
3092+
////// PDFio PNG image test page...
30263093
// Create the page dictionary, object, and stream...
30273094
fputs("pdfioDictCreate: ", stdout);
30283095
if ((dict = pdfioDictCreate(pdf)) != NULL)
@@ -3168,6 +3235,108 @@ write_png_test(pdfio_file_t *pdf, // I - PDF file
31683235
else
31693236
return (1);
31703237

3238+
////// PngSuite page
3239+
// Create the image objects...
3240+
for (i = 0; i < (sizeof(pngsuite_files) / sizeof(pngsuite_files[0])); i ++)
3241+
{
3242+
fprintf(stdout, "pdfioFileCreateImageObjFromFile(\"%s\"): ", pngsuite_files[i]);
3243+
if ((pngsuite[i] = pdfioFileCreateImageObjFromFile(pdf, pngsuite_files[i], false)) != NULL)
3244+
puts("PASS");
3245+
else
3246+
return (1);
3247+
}
3248+
3249+
// Create the page dictionary, object, and stream...
3250+
fputs("pdfioDictCreate: ", stdout);
3251+
if ((dict = pdfioDictCreate(pdf)) != NULL)
3252+
puts("PASS");
3253+
else
3254+
return (1);
3255+
3256+
for (i = 0; i < (sizeof(pngsuite_files) / sizeof(pngsuite_files[0])); i ++)
3257+
{
3258+
fprintf(stdout, "pdfioPageDictAddImage(\"%s\"): ", pngsuite_labels[i]);
3259+
snprintf(imgname, sizeof(imgname), "IM%u", (unsigned)(i + 1));
3260+
if (pdfioPageDictAddImage(dict, pdfioStringCreate(pdf, imgname), pngsuite[i]))
3261+
puts("PASS");
3262+
else
3263+
return (1);
3264+
}
3265+
3266+
fputs("pdfioPageDictAddFont(F1): ", stdout);
3267+
if (pdfioPageDictAddFont(dict, "F1", font))
3268+
puts("PASS");
3269+
else
3270+
return (1);
3271+
3272+
printf("pdfioFileCreatePage(%d): ", number + 1);
3273+
3274+
if ((st = pdfioFileCreatePage(pdf, dict)) != NULL)
3275+
puts("PASS");
3276+
else
3277+
return (1);
3278+
3279+
if (write_header_footer(st, "PngSuite Test Page", number + 1))
3280+
goto error;
3281+
3282+
// Show content...
3283+
fputs("pdfioContentSetTextFont(\"F1\", 9.0): ", stdout);
3284+
if (pdfioContentSetTextFont(st, "F1", 8.0))
3285+
puts("PASS");
3286+
else
3287+
goto error;
3288+
3289+
for (i = 0; i < (sizeof(pngsuite_files) / sizeof(pngsuite_files[0])); i ++)
3290+
{
3291+
double x = (i % 8) * 69.0 + 36; // X position
3292+
double y = 671 - (i / 8) * 64.0; // Y position
3293+
3294+
fputs("pdfioContentTextBegin(): ", stdout);
3295+
if (pdfioContentTextBegin(st))
3296+
puts("PASS");
3297+
else
3298+
goto error;
3299+
3300+
printf("pdfioContentTextMoveTo(%g, %g): ", x, y);
3301+
if (pdfioContentTextMoveTo(st, x, y))
3302+
puts("PASS");
3303+
else
3304+
goto error;
3305+
3306+
printf("pdfioContentTextShow(\"%s\"): ", pngsuite_labels[i]);
3307+
if (pdfioContentTextShow(st, false, pngsuite_labels[i]))
3308+
puts("PASS");
3309+
else
3310+
goto error;
3311+
3312+
fputs("pdfioContentTextEnd(): ", stdout);
3313+
if (pdfioContentTextEnd(st))
3314+
puts("PASS");
3315+
else
3316+
goto error;
3317+
}
3318+
3319+
for (i = 0; i < (sizeof(pngsuite_files) / sizeof(pngsuite_files[0])); i ++)
3320+
{
3321+
double x = (i % 8) * 69.0 + 36; // X position
3322+
double y = 671 - (i / 8) * 64.0; // Y position
3323+
3324+
snprintf(imgname, sizeof(imgname), "IM%u", (unsigned)(i + 1));
3325+
printf("pdfioContentDrawImage(\"%s\"): ", imgname);
3326+
if (pdfioContentDrawImage(st, imgname, x, y + 9, 32, 32))
3327+
puts("PASS");
3328+
else
3329+
goto error;
3330+
}
3331+
3332+
// Close the object and stream...
3333+
fputs("pdfioStreamClose: ", stdout);
3334+
if (pdfioStreamClose(st))
3335+
puts("PASS");
3336+
else
3337+
return (1);
3338+
#endif // HAVE_LIBPNG
3339+
31713340
return (0);
31723341

31733342
error:
@@ -3367,6 +3536,7 @@ write_unit_file(
33673536
*gray_jpg, // gray.jpg image
33683537
*helvetica, // Helvetica font
33693538
*page; // Page from test PDF file
3539+
int pagenum = 1; // Current page number
33703540
pdfio_dict_t *catalog; // Catalog dictionary
33713541

33723542

@@ -3506,10 +3676,14 @@ write_unit_file(
35063676
else
35073677
return (1);
35083678

3679+
pagenum ++;
3680+
35093681
// Write a page with a color image...
3510-
if (write_jpeg_test(outpdf, "Color JPEG Test", 2, helvetica, color_jpg))
3682+
if (write_jpeg_test(outpdf, "Color JPEG Test", pagenum, helvetica, color_jpg))
35113683
return (1);
35123684

3685+
pagenum ++;
3686+
35133687
// Copy the third page from the test PDF file...
35143688
fputs("pdfioFileGetPage(2): ", stdout);
35153689
if ((page = pdfioFileGetPage(inpdf, 2)) != NULL)
@@ -3523,39 +3697,60 @@ write_unit_file(
35233697
else
35243698
return (1);
35253699

3700+
pagenum ++;
3701+
35263702
// Write a page with a grayscale image...
3527-
if (write_jpeg_test(outpdf, "Grayscale JPEG Test", 4, helvetica, gray_jpg))
3703+
if (write_jpeg_test(outpdf, "Grayscale JPEG Test", pagenum, helvetica, gray_jpg))
35283704
return (1);
3705+
pagenum ++;
35293706

35303707
// Write a page with PNG images...
3531-
if (write_png_test(outpdf, 5, helvetica))
3708+
if (write_png_tests(outpdf, pagenum, helvetica))
35323709
return (1);
35333710

3711+
#ifdef HAVE_LIBPNG
3712+
pagenum += 2;
3713+
#else
3714+
pagenum ++;
3715+
#endif // HAVE_LIBPNG
3716+
35343717
// Write a page that tests multiple color spaces...
3535-
if (write_color_test(outpdf, 6, helvetica))
3718+
if (write_color_test(outpdf, pagenum, helvetica))
35363719
return (1);
35373720

3721+
pagenum ++;
3722+
35383723
// Write a page with test images...
35393724
*first_image = pdfioFileGetNumObjs(outpdf) + 1;
3540-
if (write_images_test(outpdf, 7, helvetica))
3725+
if (write_images_test(outpdf, pagenum, helvetica))
35413726
return (1);
35423727

3728+
pagenum ++;
3729+
35433730
// Write a page width alpha (soft masks)...
3544-
if (write_alpha_test(outpdf, 8, helvetica))
3731+
if (write_alpha_test(outpdf, pagenum, helvetica))
35453732
return (1);
35463733

3734+
pagenum ++;
3735+
35473736
// Test TrueType fonts...
3548-
if (write_font_test(outpdf, 9, helvetica, "testfiles/OpenSans-Regular.ttf", false))
3737+
if (write_font_test(outpdf, pagenum, helvetica, "testfiles/OpenSans-Regular.ttf", false))
35493738
return (1);
35503739

3551-
if (write_font_test(outpdf, 10, helvetica, "testfiles/OpenSans-Regular.ttf", true))
3740+
pagenum ++;
3741+
3742+
if (write_font_test(outpdf, pagenum, helvetica, "testfiles/OpenSans-Regular.ttf", true))
35523743
return (1);
35533744

3554-
if (write_font_test(outpdf, 11, helvetica, "testfiles/NotoSansJP-Regular.otf", true))
3745+
pagenum ++;
3746+
3747+
if (write_font_test(outpdf, pagenum, helvetica, "testfiles/NotoSansJP-Regular.otf", true))
35553748
return (1);
35563749

3750+
pagenum ++;
3751+
35573752
// Print this text file...
3558-
if (write_text_test(outpdf, 12, helvetica, "README.md"))
3753+
if (write_text_test(outpdf, pagenum, helvetica, "README.md"))
35593754
return (1);
35603755

35613756
fputs("pdfioFileGetNumPages: ", stdout);

0 commit comments

Comments
 (0)