#include <qdir.h> #include <qfile.h> #include <qimage.h> #include <iostream> int thumb_w; int thumb_h; int across; int spacing; int quality; bool padthumbs; QColor padbgcol; bool notext; QString imgattrib; QString link; int rcount; bool intable; bool padbgcolset; int lineno; bool savesuccess; QFile outfile; int help(); int version(); void output( QString out ); void process_directive( QString dir ); void process_text( QString text ); void process_file( QString file ); int main( int argc, char** argv ) { argc--; argv++; QString inf; if ( argc ) inf = argv[0]; else inf = "webthumb.txt"; if ( inf == "-h" ) return help(); if ( inf == "-V" ) return version(); QFile infile( inf ); if ( !infile.open( IO_ReadOnly ) ) { fprintf( stderr, "Unable to open %s\n", inf ); return 1; } lineno = 0; savesuccess = true; QDir dir; dir.mkdir( "thumbs" ); //Default settings. thumb_w = 200; thumb_h = 200; across = 0; spacing = 5; quality = -1; padthumbs = false; padbgcol = QColor( "white" ); padbgcolset = false; notext = true; imgattrib = QString::null; link = QString::null; rcount = 0; intable = false; QString line; while ( infile.readLine( line, 2048 ) != -1 ) { lineno++; if ( line.endsWith( "\n" ) ) { line = line.left( line.length() - 1 ); } if ( line.startsWith( "\\" ) ) { process_directive( line.right( line.length() - 1 ) ); continue; } if ( line.startsWith( "#" ) ) { process_text( line.right( line.length() - 1 ) ); continue; } process_file( line ); } if ( intable ) { intable = false; if ( rcount ) { rcount = 0; output( "</tr>" ); } output( "</table>" ); } if ( !savesuccess ) fprintf( stderr, "Some thumbnails could not be saved to disk.\n" ); return 0; } void output( QString out ) { if ( outfile.name().length() ) { const char* bytes = out.ascii(); while ( *bytes ) { outfile.putch( *bytes ); bytes++; } #ifdef WIN32 outfile.putch( '\r' ); #endif outfile.putch( '\n' ); return; } printf( out.ascii() ); printf( "\n" ); } void process_directive( QString dir ) { //chop newline dir.truncate( dir.length() - 1 ); if ( dir.startsWith( "output=", false ) ) { outfile.setName( dir.right( dir.length() - 7 ).stripWhiteSpace() ); if ( !outfile.open( IO_WriteOnly ) ) { fprintf( stderr, "Unable to open %s for write.\n", outfile.name().ascii() ); outfile.setName( "" ); }; return; } if ( dir.startsWith( "width=", false ) ) { thumb_w = dir.right( dir.length() - 6 ).toInt(); if ( thumb_w <= 0 ) { fprintf( stderr, "invalid width - resetting to 200\n" ); thumb_w = 200; } return; } if ( dir.startsWith( "height=", false ) ) { thumb_h = dir.right( dir.length() - 7 ).toInt(); if ( thumb_h <= 0 ) { fprintf( stderr, "invalid height - resetting to 200\n" ); thumb_h = 200; } return; } if ( dir.startsWith( "columns=", false ) ) { across = dir.right( dir.length() - 8 ).toInt(); return; } if ( dir.startsWith( "spacing=", false ) ) { spacing = dir.right( dir.length() - 8 ).toInt(); return; } if ( dir.startsWith( "padthumb", false ) ) { padthumbs = true; return; } if ( dir.startsWith( "padbgcol=", false ) ) { padbgcol = QColor( dir.right( dir.length() - 9 ) ); padbgcolset = true; return; } if ( dir.startsWith( "quality=", false ) ) { quality = dir.right( dir.length() - 8 ).toInt(); return; } if ( dir.startsWith( "captions", false ) ) { notext = false; if ( !across ) across = 4; return; } if ( dir.startsWith( "imgattrib=", false ) ) { imgattrib = dir.right( dir.length() - 10 ); return; } if ( dir.startsWith( "link=", false ) ) { link = dir.right( dir.length() - 5 ); return; } if ( dir.startsWith( "url=", false ) ) { link = dir.right( dir.length() - 4 ); return; } fprintf( stderr, "Invalid configuration variable \"%s\" at line %d\n", dir.ascii(), lineno ); } void process_text( QString text ) { if ( intable ) { intable = false; if ( rcount ) { rcount = 0; output( "</tr>" ); } output( "</table>" ); } output( text ); } QImage padthumb( const QImage& thumb ); void process_file( QString file ) { if ( file.length() <= 5 ) return; if ( !intable && across ) { intable = true; rcount = 0; output( "<table cellspacing=" + QString::number( spacing ) + " cellpadding=0>" ); } if ( !rcount && across ) { output( "<tr align=center>" ); } file = file.simplifyWhiteSpace(); QString img_name = file.section( ' ', 0, 0 ); QString img_desc = file.section( ' ', 1 ); QImage img_data( img_name ); QImage thumb_data = img_data.smoothScale( thumb_w, thumb_h, QImage::ScaleMin ); if ( padthumbs ) thumb_data = padthumb( thumb_data ); QString thumb_name = img_name; thumb_name = thumb_name.replace( '.', '_' ).replace( '/', '_') + "_thumb.jpg"; thumb_name = thumb_name.prepend( "thumbs/" ); savesuccess &= thumb_data.save( thumb_name, "JPEG", quality ); //open cell and link QString cell; if ( intable ) cell += "<td>"; cell += "<a href=\""; if ( link.isNull() ) { cell += img_name; } else { cell += link; link = QString::null; } cell += "\">"; //img tag cell += "<img src=\"" + thumb_name + "\""; cell += " " + imgattrib + " "; cell += "title=\"" + img_desc.replace('"', """) + "\">"; //description if ( !notext ) cell += "<br>" + img_desc; //close link and cell cell += "</a>"; if ( intable ) cell += "</td>"; output( cell ); if ( !intable ) return; rcount++; if ( rcount >= across ) { output( "</tr>" ); rcount = 0; } } QImage padthumb( const QImage& thumb ) { QImage pad( thumb_w, thumb_h, 32 ); if ( padbgcolset ) pad.fill( padbgcol.rgb() ); int sx = ( thumb_w - thumb.width() ) / 2; int sy = ( thumb_h - thumb.height() ) / 2; bitBlt( &pad, sx, sy, &thumb ); double r, g, b; QRgb rgb; if ( sx && !padbgcolset ) { QImage hpad( sx + 1, thumb_h, 32 ); r = g = b = 0; for ( int y = 0 ; y < thumb.height() ; y++ ) { rgb = thumb.pixel( 0, y ); r += qRed( rgb ); g += qGreen( rgb ); b += qBlue( rgb ); } r /= thumb.height(); g /= thumb.height(); b /= thumb.height(); hpad.fill( qRgb( r, g, b ) ); bitBlt( &pad, 0, 0, &hpad, 0, 0, sx, thumb_h ); r = g = b = 0; for ( int y = 0 ; y < thumb.height() ; y++ ) { rgb = thumb.pixel( thumb.width() - 1, y ); r += qRed( rgb ); g += qGreen( rgb ); b += qBlue( rgb ); } r /= thumb.height(); g /= thumb.height(); b /= thumb.height(); hpad.fill( qRgb( r, g, b ) ); bitBlt( &pad, sx + thumb.width(), 0, &hpad, 0, 0, sx + 1, thumb_h ); } if ( sy && !padbgcolset ) { QImage vpad( thumb_w, sy + 1, 32 ); r = g = b = 0; for ( int x = 0 ; x < thumb.width() ; x++ ) { rgb = thumb.pixel( x, 0 ); r += qRed( rgb ); g += qGreen( rgb ); b += qBlue( rgb ); } r /= thumb.width(); g /= thumb.width(); b /= thumb.width(); vpad.fill( qRgb( r, g, b ) ); bitBlt( &pad, 0, 0, &vpad, 0, 0, thumb_w, sy ); r = g = b = 0; for ( int x = 0 ; x < thumb.width() ; x++ ) { rgb = thumb.pixel( x, thumb.height() - 1 ); r += qRed( rgb ); g += qGreen( rgb ); b += qBlue( rgb ); } r /= thumb.width(); g /= thumb.width(); b /= thumb.width(); vpad.fill( qRgb( r, g, b ) ); bitBlt( &pad, 0, sy + thumb.height(), &vpad , 0, 0, thumb_w, sy + 1); } return pad; } int help() { printf( "\n" ); printf( "Usage: webthumb [input file]\n" ); printf( "Input file defaults to webthumb.txt.\n" ); printf( "Syntax for input file:\n" ); printf( "--------------------------\n" ); printf( "IMAGE Image Caption : thumbnail IMAGE with caption 'Image Caption'\n" ); printf( "--------------------------\n" ); printf( "#<insert html here> : pass html straight through to output file\n" ); printf( "--------------------------\n" ); printf( "\\variable=value : set configuration variable\n" ); printf( " output=FILE (specifies HTML file to write to, default stdout)\n" ); printf( " height=NUM (specifies max height of thumbnails, default 200)\n" ); printf( " width=NUM (specifies max width of thumbnails, default 200)\n" ); printf( " columns=NUM (specifies number of columns in table, default off)\n" ); printf( " spacing=NUM (specifies space in pixels between thumbnails)\n" ); printf( " quality=NUM (specifies thumbnail quality on a scale of 0-100)\n" ); printf( " padthumbs (pads all thumbnails to max height and width)\n" ); printf( " padbgcol=C (uses HTML color C as background for padded thumbnails\n" ); printf( " imgattrib=A (inserts string A into image tags, i.e. <img A ...>\n" ); printf( " link=URL (redirects next link to URL instead of source image)\n" ); printf( " captions (places captions under each image [requires columns])\n" ); return 0; } int version() { QString change = "$Change: 5984 $"; change.remove( "$Change: " ); change.remove( " $" ); QString date = "$Date: 2007/07/17 $"; date.remove( "$Date: " ); date.remove( " $" ); printf( "\n" ); printf( "WebThumb build %s (%s)\n", change.ascii(), date.ascii() ); printf( "A LeadToGold.com concoction\n" ); printf( "\n" ); return 0; }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#12 | 5984 | Sam Stafford |
Fix a couple of bugs and made "titlesonly" the default; setting "captions" overrides it. The new captions setting also forces "columns" to a non-zero value, since the linebreaks in the captions break the non-table mode. |
||
#11 | 5970 | Sam Stafford |
New table-less output mode for Webthumb. Does all the same thumbnailing and padding but generates a big wrapping row instead of a table. This is the new default; if you want a set number of columns henceforth you'll have to use \columns explicitly. |
||
#10 | 5687 | Sam Stafford |
New "\link=" feature. Webthumb also now handles images in subdirectories; the thumbnails all still go into the same "thumbs" folder in the current directory, flattened by having slashes converted into underscores. |
||
#9 | 5463 | Sam Stafford | Correct for potential rounding problem. | ||
#8 | 5461 | Sam Stafford |
A whole mess of new configuration variables, mostly to do with spacing out thumbnails in more visually pleasing fashions. |
||
#7 | 5460 | Sam Stafford |
Add new "imgattrib" variable, to allow handy things like changing the border style on thumbnail images. Also added a little error checking on variables (invalid variables now produce console warnings), and an ifdef to make WebThumb output native line endings. |
||
#6 | 5458 | Sam Stafford | Change version string to remove RCS keyword cruft and include a date. | ||
#5 | 5457 | Sam Stafford |
Add "titlesonly" synonym (since I'm always forgetting which it is), and horizontally center images where appropriate. (Also converted VS projects to latest version.) |
||
#4 | 4996 | Sam Stafford | Put quotes around "href" fields as well. | ||
#3 | 4994 | Sam Stafford |
Make WebThumb more HTML spec compliant (Firefox gets fussy about the lack of quotes), add a new option ("titleonly"), add a -V so I can check versions easily. |
||
#2 | 4933 | Sam Stafford | Changed WebThumb build to CONSOLE, added -h usage information. | ||
#1 | 4845 | Sam Stafford |
WebThumb, a quick 'n dirty little tool for generating thumbnail web pages. |