PageInfo *analyse_page (PopplerDocument *doc, guint page_num) { PopplerPage *page; PageInfo *info; GdkPixbuf *image; double width_points, height_points; int width, height; gboolean *white_rows, *white_cols; page = poppler_document_get_page (doc, page_num); if (!page) { g_error ("Couldn't open page %d of document", page_num); } /* There are 72 points in an inch. So width and height should be * multiplied by settings.dpi / 72.0 */ poppler_page_get_size (page, &width_points, &height_points); width = (int) ((width_points * settings.dpi / 72.0) + 0.5); height = (int) ((height_points * settings.dpi / 72.0) + 0.5); image = gdk_pixbuf_new(GDK_COLORSPACE_RGB, TRUE, 8, width, height); if (!image) { g_error ("Couldn't create an image (size %d x %d) for page %d", width, height, page_num); } poppler_page_render_to_pixbuf (page, 0, 0, width, height, settings.dpi / 72.0, 0, image); g_object_unref (page); find_white (image, &white_rows, &white_cols); g_object_unref (image); guint firstrow, lastrow, hunkscount; HunkData* hunks = find_hunks (white_rows, height, &firstrow, &lastrow, &hunkscount); info = g_new (PageInfo, 1); info->bbox.x = first_zero (white_cols, width); info->bbox.width = last_zero (white_cols, width) - info->bbox.x; if (info->bbox.width <= 0) { g_error ("Empty page (%d)? Couldn't find a nonwhite column.", page_num); } info->bbox.y = firstrow; info->bbox.height = lastrow - firstrow; info->num_hunks = hunkscount; info->hunks = hunks; g_free (white_rows); g_free (white_cols); return info; }
END_TEST START_TEST (test_util_find) { char ws[] = " "; char ts[] = " abc def"; char *tsnwf = ts + 1; char *tsnw = tsnwf + 4; const char *got; /* XXX: This should really be using a loop test */ got=skip_white(ws); fail_unless(got == ws+sizeof(ws)-1, "exp: %p :'%s'; got: %p: '%s'", ws+sizeof(ws)-1, ws+sizeof(ws)-1, got, got); got=skip_white(ts); fail_unless(got == tsnwf, "exp: %p :'%s'; got: %p: '%s'", tsnwf, tsnwf, got, got); got=skip_white(tsnwf); fail_unless(got == tsnwf, "exp: %p :'%s'; got: %p: '%s'", tsnwf, tsnwf, got, got); got=skip_white(tsnw); fail_unless(got == tsnw, "exp: %p :'%s'; got: %p: '%s'", tsnw, tsnw, got, got); got=find_white(ts); fail_unless(got == ts, "exp: %p :'%s'; got: %p: '%s'", ts, ts, got, got); got=find_white(tsnwf); fail_unless(got == tsnw - 1, "exp: %p :'%s'; got: %p: '%s'", tsnw-1, tsnw-1, got, got); got=find_white(tsnw); fail_unless(got == tsnw+strlen(tsnw), "exp: %p :'%s'; got: %p: '%s'", tsnw+strlen(tsnw), tsnw+strlen(tsnw), got, got); got=find_charn(ts, 'a', sizeof(ts)); fail_unless(got == tsnwf, "exp: %p :'%s'; got: %p: '%s'", tsnwf, tsnwf, got, got); got=find_charn(ts, 'z', sizeof(ts) + 10); fail_unless(got == NULL, "exp: %p :'%s'; got: %p: '%s'", NULL, NULL, got, got); got=find_charn(ts, 'a', 1); fail_unless(got == NULL, "exp: %p :'%s'; got: %p: '%s'", NULL, NULL, got, got); }