diff --git a/pdfio-object.c b/pdfio-object.c index 5ab05e6..d044f06 100644 --- a/pdfio-object.c +++ b/pdfio-object.c @@ -299,6 +299,23 @@ pdfioObjGetNumber(pdfio_obj_t *obj) // I - Object } +// +// 'pdfioObjGetSubtype()' - Get an object's subtype. +// + +const char * // O - Object subtype +pdfioObjGetSubtype(pdfio_obj_t *obj) // I - Object +{ + pdfio_dict_t *dict; // Object dictionary + + + if ((dict = pdfioObjGetDict(obj)) == NULL) + return (NULL); + else + return (pdfioDictGetName(dict, "Subtype")); +} + + // // 'pdfioObjGetType()' - Get an object's type. // diff --git a/pdfio.h b/pdfio.h index e6271b7..dad63e7 100644 --- a/pdfio.h +++ b/pdfio.h @@ -169,6 +169,7 @@ extern pdfio_dict_t *pdfioObjGetDict(pdfio_obj_t *obj) PDFIO_PUBLIC; extern unsigned short pdfioObjGetGeneration(pdfio_obj_t *obj) PDFIO_PUBLIC; extern size_t pdfioObjGetLength(pdfio_obj_t *obj) PDFIO_PUBLIC; extern size_t pdfioObjGetNumber(pdfio_obj_t *obj) PDFIO_PUBLIC; +extern const char *pdfioObjGetSubtype(pdfio_obj_t *obj) PDFIO_PUBLIC; extern const char *pdfioObjGetType(pdfio_obj_t *obj) PDFIO_PUBLIC; extern pdfio_stream_t *pdfioObjOpenStream(pdfio_obj_t *obj, bool decode) PDFIO_PUBLIC; diff --git a/testpdfio.c b/testpdfio.c index 470f5a1..e3330a8 100644 --- a/testpdfio.c +++ b/testpdfio.c @@ -26,6 +26,7 @@ static int draw_image(pdfio_stream_t *st, const char *name, double x, double y, static bool error_cb(pdfio_file_t *pdf, const char *message, bool *error); static ssize_t token_consume_cb(const char **s, size_t bytes); static ssize_t token_peek_cb(const char **s, char *buffer, size_t bytes); +static int verify_image(pdfio_file_t *pdf, size_t number); static int write_color_patch(pdfio_stream_t *st, bool device); static int write_color_test(pdfio_file_t *pdf, int number, pdfio_obj_t *font); static pdfio_obj_t *write_image_object(pdfio_file_t *pdf, _pdfio_predictor_t predictor); @@ -166,6 +167,8 @@ do_unit_tests(void) *gray_jpg, // gray.jpg image *helvetica, // Helvetica font *page; // Page from test PDF file + size_t first_image, // First image object + num_pages; // Number of pages written static const char *complex_dict = // Complex dictionary value "< 0) + { + printf("PASS (%lu)\n", (unsigned long)num_pages); + } + else + { + puts("FAIL"); + return (1); + } + // Close the new PDF file... - fputs("pdfioFileClose(\"testpdfio-out.pdf\": ", stdout); + fputs("pdfioFileClose(\"testpdfio-out.pdf\"): ", stdout); if (pdfioFileClose(outpdf)) puts("PASS"); else return (1); + // Open the new PDF file to read it... + fputs("pdfioFileOpen(\"testpdfio-out.pdf\", ...): ", stdout); + if ((pdf = pdfioFileOpen("testpdfio-out.pdf", (pdfio_error_cb_t)error_cb, &error)) != NULL) + puts("PASS"); + else + return (1); + + // Verify the number of pages is the same... + fputs("pdfioFileGetNumPages: ", stdout); + if (num_pages == pdfioFileGetNumPages(pdf)) + { + puts("PASS"); + } + else + { + printf("FAIL (%lu != %lu)\n", (unsigned long)num_pages, (unsigned long)pdfioFileGetNumPages(pdf)); + return (1); + } + + // Verify the images + for (i = 0; i < 7; i ++) + verify_image(pdf, (size_t)(first_image + i)); + + // Close the new PDF file... + fputs("pdfioFileClose(\"testpdfio-out.pdf\"): ", stdout); + if (pdfioFileClose(pdf)) + puts("PASS"); + else + return (1); + return (0); } @@ -418,6 +463,114 @@ token_peek_cb(const char **s, // IO - Test string } +// +// 'verify_image()' - Verify an image object. +// + +static int // O - 1 on failure, 0 on success +verify_image(pdfio_file_t *pdf, // I - PDF file + size_t number) // I - Object number +{ + pdfio_obj_t *obj; // Image object + const char *type, // Object type + *subtype; // Object subtype + double width, // Width of object + height; // Height of object + pdfio_stream_t *st; // Stream + int x, y; // Coordinates in image + unsigned char buffer[768], // Expected data + *bufptr, // Pointer into buffer + line[768]; // Line from file + ssize_t bytes; // Bytes read from stream + + + printf("pdfioFileFindObject(%lu): ", (unsigned long)number); + if ((obj = pdfioFileFindObject(pdf, number)) != NULL) + puts("PASS"); + else + return (1); + + fputs("pdfioObjGetType: ", stdout); + if ((type = pdfioObjGetType(obj)) != NULL && !strcmp(type, "XObject")) + { + puts("PASS"); + } + else + { + printf("FAIL (got %s, expected XObject)\n", type); + return (1); + } + + fputs("pdfioObjGetSubtype: ", stdout); + if ((subtype = pdfioObjGetSubtype(obj)) != NULL && !strcmp(subtype, "Image")) + { + puts("PASS"); + } + else + { + printf("FAIL (got %s, expected Image)\n", subtype); + return (1); + } + + fputs("pdfioImageGetWidth: ", stdout); + if ((width = pdfioImageGetWidth(obj)) == 256.0) + { + puts("PASS"); + } + else + { + printf("FAIL (got %g, expected 256)\n", width); + return (1); + } + + fputs("pdfioImageGetHeight: ", stdout); + if ((height = pdfioImageGetHeight(obj)) == 256.0) + { + puts("PASS"); + } + else + { + printf("FAIL (got %g, expected 256)\n", height); + return (1); + } + + // Open the image stream, read the image, and verify it matches expectations... + fputs("pdfioObjOpenStream: ", stdout); + if ((st = pdfioObjOpenStream(obj, PDFIO_FILTER_FLATE)) != NULL) + puts("PASS"); + else + return (1); + + for (y = 0; y < 256; y ++) + { + for (x = 0, bufptr = buffer; x < 256; x ++, bufptr += 3) + { + bufptr[0] = y; + bufptr[1] = y + x; + bufptr[2] = y - x; + } + + if ((bytes = pdfioStreamRead(st, line, sizeof(line))) != (ssize_t)sizeof(line)) + { + printf("pdfioStreamRead: FAIL (got %d for line %d, expected 768)\n", y, (int)bytes); + pdfioStreamClose(st); + return (1); + } + + if (memcmp(buffer, line, sizeof(buffer))) + { + printf("pdfioStreamRead: FAIL (line %d doesn't match expectations)\n", y); + pdfioStreamClose(st); + return (1); + } + } + + pdfioStreamClose(st); + + return (0); +} + + // // 'write_color_patch()' - Write a color patch... //