CyBook Gen3 PDF thumbnail images
Saturday, 26 January 2008 22:16

One drawback of the CyBook's PDF support is the missing metadata. For example, it simply does not display preview images of PDF files in the browser.

However, when looking around a bit on the SD card, some files with the extension .t2b seem to be the right candidates. It turns out that they are 4-bit greyscale images of the size 144x96.

Here is a listing of my pdftot2b:

  1. #!/usr/bin/perl
  2.  
  3. use GD;
  4.  
  5. my $palette = $0;
  6. $palette =~ s/[^\/]+$/palette.gif/;
  7.  
  8. while ( my $file = shift @ARGV )
  9. {
  10. open IN, "pdftops -f 1 -l 1 -paper match $file - |
  11. convert -resize 96x144 -type TrueColor PS:- JPG:- |
  12. djpeg -pnm -map $palette -dither ordered |
  13. pnmtopng 2>/dev/null |";
  14. my $in = join( '', <IN> );
  15. close IN;
  16.  
  17. $image = GD::Image->new( $in );
  18.  
  19. $k = 3;
  20. @px = ( );
  21.  
  22. $left = int( ( 96 - $image->width ) / 2 );
  23. $right = 96 - $image->width - $left;
  24. $top = int( ( 144 - $image->height ) / 2 );
  25. $bottom = 144 - $image->height - $top;
  26.  
  27. $file =~ s/\.pdf$/_6090.t2b/;
  28. open OUT, "> $file";
  29.  
  30. for ( $i = - $top; $i < 144 - $top; $i ++ )
  31. {
  32. for ( $j = - $left; $j < 96 - $left; $j ++ )
  33. {
  34. $px[$k --] = ( ( $i >= 0 and $i < $image->height and $j >= 0 and $j < $image->width )
  35. ? $image->getPixel( $j, $i )
  36. : 3 );
  37. if ( $k < 0 )
  38. {
  39. # this line can be here because 4 divides 96
  40. print OUT chr( $px[0] + 4 * $px[1] + 16 * $px[2] + 64 * $px[3] );
  41. $k = 3;
  42. }
  43. }
  44. }
  45.  
  46. close OUT;
  47. }
Listing: pdftot2b

The purpose of the file palette.ppm is to give the dithering algorithm the codes of the four colours we want to use. I started with four equally spaced grey codes (0x00, 0x55, 0xaa, 0xff). It should lie in the same directory as the script file.

The conversion performed in the lines 10–13 could of course be done by convert alone. However, the result of the dithering by means of djpeg was slightly more satisfactory.

update (2008-01-29): Of course, the whole script can be used to generate thumbnails from any format that is supported by ImageMagick by adapting lines 10–13 and 26 appropriately. Here is a modified version of the script: t2b.