mirror of
https://github.com/michaelrsweet/pdfio.git
synced 2025-02-20 19:02:50 +01:00
Add decode testing to testpdfio
This commit is contained in:
parent
db8be28ff6
commit
01ca711ec8
@ -448,6 +448,8 @@ _pdfioStreamOpen(pdfio_obj_t *obj, // I - Object
|
|||||||
st->flate.next_in = (Bytef *)st->cbuffer;
|
st->flate.next_in = (Bytef *)st->cbuffer;
|
||||||
st->flate.avail_in = (uInt)_pdfioFileRead(st->pdf, st->cbuffer, sizeof(st->cbuffer));
|
st->flate.avail_in = (uInt)_pdfioFileRead(st->pdf, st->cbuffer, sizeof(st->cbuffer));
|
||||||
|
|
||||||
|
PDFIO_DEBUG("_pdfioStreamOpen: avail_in=%u\n", st->flate.avail_in);
|
||||||
|
|
||||||
if (inflateInit(&(st->flate)) != Z_OK)
|
if (inflateInit(&(st->flate)) != Z_OK)
|
||||||
{
|
{
|
||||||
_pdfioFileError(st->pdf, "Unable to start Flate filter.");
|
_pdfioFileError(st->pdf, "Unable to start Flate filter.");
|
||||||
|
143
testpdfio.c
143
testpdfio.c
@ -6,6 +6,12 @@
|
|||||||
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
// Licensed under Apache License v2.0. See the file "LICENSE" for more
|
||||||
// information.
|
// information.
|
||||||
//
|
//
|
||||||
|
// Usage:
|
||||||
|
//
|
||||||
|
// ./testpdfio
|
||||||
|
//
|
||||||
|
// ./testpdfio FILENAME [OBJECT-NUMBER] [FILENAME [OBJECT-NUMBER]] ...
|
||||||
|
//
|
||||||
|
|
||||||
//
|
//
|
||||||
// Include necessary headers...
|
// Include necessary headers...
|
||||||
@ -20,7 +26,7 @@
|
|||||||
// Local functions...
|
// Local functions...
|
||||||
//
|
//
|
||||||
|
|
||||||
static int do_test_file(const char *filename);
|
static int do_test_file(const char *filename, int objnum);
|
||||||
static int do_unit_tests(void);
|
static int do_unit_tests(void);
|
||||||
static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label);
|
static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, double w, double h, const char *label);
|
||||||
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
|
static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error);
|
||||||
@ -52,7 +58,15 @@ main(int argc, // I - Number of command-line arguments
|
|||||||
|
|
||||||
for (i = 1; i < argc; i ++)
|
for (i = 1; i < argc; i ++)
|
||||||
{
|
{
|
||||||
if (do_test_file(argv[i]))
|
if ((i + 1) < argc && isdigit(argv[i + 1][0] & 255))
|
||||||
|
{
|
||||||
|
// filename.pdf object-number
|
||||||
|
if (do_test_file(argv[i], atoi(argv[i + 1])))
|
||||||
|
return (1);
|
||||||
|
|
||||||
|
i ++;
|
||||||
|
}
|
||||||
|
else if (do_test_file(argv[i], 0))
|
||||||
return (1);
|
return (1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -70,7 +84,8 @@ main(int argc, // I - Number of command-line arguments
|
|||||||
//
|
//
|
||||||
|
|
||||||
static int // O - Exit status
|
static int // O - Exit status
|
||||||
do_test_file(const char *filename) // I - PDF filename
|
do_test_file(const char *filename, // I - PDF filename
|
||||||
|
int objnum) // I - Object number to dump, if any
|
||||||
{
|
{
|
||||||
bool error = false; // Have we shown an error yet?
|
bool error = false; // Have we shown an error yet?
|
||||||
pdfio_file_t *pdf; // PDF file
|
pdfio_file_t *pdf; // PDF file
|
||||||
@ -82,59 +97,95 @@ do_test_file(const char *filename) // I - PDF filename
|
|||||||
|
|
||||||
|
|
||||||
// Try opening the file...
|
// Try opening the file...
|
||||||
printf("pdfioFileOpen(\"%s\", ...): ", filename);
|
if (!objnum)
|
||||||
|
printf("pdfioFileOpen(\"%s\", ...): ", filename);
|
||||||
|
|
||||||
if ((pdf = pdfioFileOpen(filename, (pdfio_error_cb_t)error_cb, &error)) != NULL)
|
if ((pdf = pdfioFileOpen(filename, (pdfio_error_cb_t)error_cb, &error)) != NULL)
|
||||||
{
|
{
|
||||||
puts("PASS");
|
if (objnum)
|
||||||
|
|
||||||
// Show basic stats...
|
|
||||||
num_objs = pdfioFileGetNumObjs(pdf);
|
|
||||||
num_pages = pdfioFileGetNumPages(pdf);
|
|
||||||
|
|
||||||
printf(" PDF %s, %d pages, %d objects.\n", pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs);
|
|
||||||
|
|
||||||
// Show a summary of each page...
|
|
||||||
for (n = 0; n < num_pages; n ++)
|
|
||||||
{
|
{
|
||||||
if ((obj = pdfioFileGetPage(pdf, n)) == NULL)
|
const char *filter; // Stream filter
|
||||||
|
pdfio_stream_t *st; // Stream
|
||||||
|
char buffer[8192]; // Read buffer
|
||||||
|
ssize_t bytes; // Bytes read
|
||||||
|
|
||||||
|
if ((obj = pdfioFileFindObj(pdf, (size_t)objnum)) == NULL)
|
||||||
{
|
{
|
||||||
printf("%s: Unable to get page #%d.\n", filename, (int)n + 1);
|
puts("Not found.");
|
||||||
|
return (1);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if ((dict = pdfioObjGetDict(obj)) == NULL)
|
||||||
{
|
{
|
||||||
pdfio_rect_t media_box; // MediaBox value
|
puts("Not a stream.");
|
||||||
|
return (1);
|
||||||
memset(&media_box, 0, sizeof(media_box));
|
|
||||||
dict = pdfioObjGetDict(obj);
|
|
||||||
|
|
||||||
if (!pdfioDictGetRect(dict, "MediaBox", &media_box))
|
|
||||||
{
|
|
||||||
if ((obj = pdfioDictGetObj(dict, "Parent")) != NULL)
|
|
||||||
{
|
|
||||||
dict = pdfioObjGetDict(obj);
|
|
||||||
pdfioDictGetRect(dict, "MediaBox", &media_box);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
filter = pdfioDictGetName(dict, "Filter");
|
||||||
|
|
||||||
|
if ((st = pdfioObjOpenStream(obj, (filter && !strcmp(filter, "FlateDecode")) ? PDFIO_FILTER_FLATE : PDFIO_FILTER_NONE)) == NULL)
|
||||||
|
return (1);
|
||||||
|
|
||||||
|
while ((bytes = pdfioStreamRead(st, buffer, sizeof(buffer))) > 0)
|
||||||
|
fwrite(buffer, 1, (size_t)bytes, stdout);
|
||||||
|
|
||||||
|
pdfioStreamClose(st);
|
||||||
|
|
||||||
|
return (0);
|
||||||
}
|
}
|
||||||
|
else
|
||||||
// Show the associated value with each object...
|
|
||||||
for (n = 0; n < num_objs; n ++)
|
|
||||||
{
|
{
|
||||||
if ((obj = pdfioFileGetObj(pdf, n)) == NULL)
|
puts("PASS");
|
||||||
{
|
|
||||||
printf(" Unable to get object #%d.\n", (int)n);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
dict = pdfioObjGetDict(obj);
|
|
||||||
|
|
||||||
printf(" %u %u obj dict=%p(%lu pairs)\n", (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL);
|
// Show basic stats...
|
||||||
fputs(" ", stdout);
|
num_objs = pdfioFileGetNumObjs(pdf);
|
||||||
_pdfioValueDebug(&obj->value, stdout);
|
num_pages = pdfioFileGetNumPages(pdf);
|
||||||
putchar('\n');
|
|
||||||
|
printf(" PDF %s, %d pages, %d objects.\n", pdfioFileGetVersion(pdf), (int)num_pages, (int)num_objs);
|
||||||
|
|
||||||
|
// Show a summary of each page...
|
||||||
|
for (n = 0; n < num_pages; n ++)
|
||||||
|
{
|
||||||
|
if ((obj = pdfioFileGetPage(pdf, n)) == NULL)
|
||||||
|
{
|
||||||
|
printf("%s: Unable to get page #%d.\n", filename, (int)n + 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pdfio_rect_t media_box; // MediaBox value
|
||||||
|
|
||||||
|
memset(&media_box, 0, sizeof(media_box));
|
||||||
|
dict = pdfioObjGetDict(obj);
|
||||||
|
|
||||||
|
if (!pdfioDictGetRect(dict, "MediaBox", &media_box))
|
||||||
|
{
|
||||||
|
if ((obj = pdfioDictGetObj(dict, "Parent")) != NULL)
|
||||||
|
{
|
||||||
|
dict = pdfioObjGetDict(obj);
|
||||||
|
pdfioDictGetRect(dict, "MediaBox", &media_box);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
printf(" Page #%d is %gx%g.\n", (int)n + 1, media_box.x2, media_box.y2);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show the associated value with each object...
|
||||||
|
for (n = 0; n < num_objs; n ++)
|
||||||
|
{
|
||||||
|
if ((obj = pdfioFileGetObj(pdf, n)) == NULL)
|
||||||
|
{
|
||||||
|
printf(" Unable to get object #%d.\n", (int)n);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
dict = pdfioObjGetDict(obj);
|
||||||
|
|
||||||
|
printf(" %u %u obj dict=%p(%lu pairs)\n", (unsigned)pdfioObjGetNumber(obj), (unsigned)pdfioObjGetGeneration(obj), dict, dict ? (unsigned long)dict->num_pairs : 0UL);
|
||||||
|
fputs(" ", stdout);
|
||||||
|
_pdfioValueDebug(&obj->value, stdout);
|
||||||
|
putchar('\n');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user