home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / netpbma.zip / ppm / ppmtopgm.c < prev    next >
C/C++ Source or Header  |  1993-10-04  |  7KB  |  172 lines

  1. /* ppmtopgm.c - convert a portable pixmap to a portable graymap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "ppm.h"
  14. #include "pgm.h"
  15.  
  16. int
  17. main( argc, argv )
  18.     int argc;
  19.     char* argv[];
  20.     {
  21.     FILE* ifp;
  22.     pixel* pixelrow;
  23.     register pixel* pP;
  24.     gray* grayrow;
  25.     register gray* gP;
  26.     int rows, cols, format, row;
  27.     register int col;
  28.     pixval maxval;
  29.  
  30.     /* Lookup tables for fast RGB -> luminance calculation. */
  31.     static int times77[256] = {
  32.         0,    77,   154,   231,   308,   385,   462,   539,
  33.       616,   693,   770,   847,   924,  1001,  1078,  1155,
  34.      1232,  1309,  1386,  1463,  1540,  1617,  1694,  1771,
  35.      1848,  1925,  2002,  2079,  2156,  2233,  2310,  2387,
  36.      2464,  2541,  2618,  2695,  2772,  2849,  2926,  3003,
  37.      3080,  3157,  3234,  3311,  3388,  3465,  3542,  3619,
  38.      3696,  3773,  3850,  3927,  4004,  4081,  4158,  4235,
  39.      4312,  4389,  4466,  4543,  4620,  4697,  4774,  4851,
  40.      4928,  5005,  5082,  5159,  5236,  5313,  5390,  5467,
  41.      5544,  5621,  5698,  5775,  5852,  5929,  6006,  6083,
  42.      6160,  6237,  6314,  6391,  6468,  6545,  6622,  6699,
  43.      6776,  6853,  6930,  7007,  7084,  7161,  7238,  7315,
  44.      7392,  7469,  7546,  7623,  7700,  7777,  7854,  7931,
  45.      8008,  8085,  8162,  8239,  8316,  8393,  8470,  8547,
  46.      8624,  8701,  8778,  8855,  8932,  9009,  9086,  9163,
  47.      9240,  9317,  9394,  9471,  9548,  9625,  9702,  9779,
  48.      9856,  9933, 10010, 10087, 10164, 10241, 10318, 10395,
  49.     10472, 10549, 10626, 10703, 10780, 10857, 10934, 11011,
  50.     11088, 11165, 11242, 11319, 11396, 11473, 11550, 11627,
  51.     11704, 11781, 11858, 11935, 12012, 12089, 12166, 12243,
  52.     12320, 12397, 12474, 12551, 12628, 12705, 12782, 12859,
  53.     12936, 13013, 13090, 13167, 13244, 13321, 13398, 13475,
  54.     13552, 13629, 13706, 13783, 13860, 13937, 14014, 14091,
  55.     14168, 14245, 14322, 14399, 14476, 14553, 14630, 14707,
  56.     14784, 14861, 14938, 15015, 15092, 15169, 15246, 15323,
  57.     15400, 15477, 15554, 15631, 15708, 15785, 15862, 15939,
  58.     16016, 16093, 16170, 16247, 16324, 16401, 16478, 16555,
  59.     16632, 16709, 16786, 16863, 16940, 17017, 17094, 17171,
  60.     17248, 17325, 17402, 17479, 17556, 17633, 17710, 17787,
  61.     17864, 17941, 18018, 18095, 18172, 18249, 18326, 18403,
  62.     18480, 18557, 18634, 18711, 18788, 18865, 18942, 19019,
  63.     19096, 19173, 19250, 19327, 19404, 19481, 19558, 19635 };
  64.     static int times150[256] = {
  65.         0,   150,   300,   450,   600,   750,   900,  1050,
  66.      1200,  1350,  1500,  1650,  1800,  1950,  2100,  2250,
  67.      2400,  2550,  2700,  2850,  3000,  3150,  3300,  3450,
  68.      3600,  3750,  3900,  4050,  4200,  4350,  4500,  4650,
  69.      4800,  4950,  5100,  5250,  5400,  5550,  5700,  5850,
  70.      6000,  6150,  6300,  6450,  6600,  6750,  6900,  7050,
  71.      7200,  7350,  7500,  7650,  7800,  7950,  8100,  8250,
  72.      8400,  8550,  8700,  8850,  9000,  9150,  9300,  9450,
  73.      9600,  9750,  9900, 10050, 10200, 10350, 10500, 10650,
  74.     10800, 10950, 11100, 11250, 11400, 11550, 11700, 11850,
  75.     12000, 12150, 12300, 12450, 12600, 12750, 12900, 13050,
  76.     13200, 13350, 13500, 13650, 13800, 13950, 14100, 14250,
  77.     14400, 14550, 14700, 14850, 15000, 15150, 15300, 15450,
  78.     15600, 15750, 15900, 16050, 16200, 16350, 16500, 16650,
  79.     16800, 16950, 17100, 17250, 17400, 17550, 17700, 17850,
  80.     18000, 18150, 18300, 18450, 18600, 18750, 18900, 19050,
  81.     19200, 19350, 19500, 19650, 19800, 19950, 20100, 20250,
  82.     20400, 20550, 20700, 20850, 21000, 21150, 21300, 21450,
  83.     21600, 21750, 21900, 22050, 22200, 22350, 22500, 22650,
  84.     22800, 22950, 23100, 23250, 23400, 23550, 23700, 23850,
  85.     24000, 24150, 24300, 24450, 24600, 24750, 24900, 25050,
  86.     25200, 25350, 25500, 25650, 25800, 25950, 26100, 26250,
  87.     26400, 26550, 26700, 26850, 27000, 27150, 27300, 27450,
  88.     27600, 27750, 27900, 28050, 28200, 28350, 28500, 28650,
  89.     28800, 28950, 29100, 29250, 29400, 29550, 29700, 29850,
  90.     30000, 30150, 30300, 30450, 30600, 30750, 30900, 31050,
  91.     31200, 31350, 31500, 31650, 31800, 31950, 32100, 32250,
  92.     32400, 32550, 32700, 32850, 33000, 33150, 33300, 33450,
  93.     33600, 33750, 33900, 34050, 34200, 34350, 34500, 34650,
  94.     34800, 34950, 35100, 35250, 35400, 35550, 35700, 35850,
  95.     36000, 36150, 36300, 36450, 36600, 36750, 36900, 37050,
  96.     37200, 37350, 37500, 37650, 37800, 37950, 38100, 38250 };
  97.     static int times29[256] = {
  98.         0,    29,    58,    87,   116,   145,   174,   203,
  99.       232,   261,   290,   319,   348,   377,   406,   435,
  100.       464,   493,   522,   551,   580,   609,   638,   667,
  101.       696,   725,   754,   783,   812,   841,   870,   899,
  102.       928,   957,   986,  1015,  1044,  1073,  1102,  1131,
  103.      1160,  1189,  1218,  1247,  1276,  1305,  1334,  1363,
  104.      1392,  1421,  1450,  1479,  1508,  1537,  1566,  1595,
  105.      1624,  1653,  1682,  1711,  1740,  1769,  1798,  1827,
  106.      1856,  1885,  1914,  1943,  1972,  2001,  2030,  2059,
  107.      2088,  2117,  2146,  2175,  2204,  2233,  2262,  2291,
  108.      2320,  2349,  2378,  2407,  2436,  2465,  2494,  2523,
  109.      2552,  2581,  2610,  2639,  2668,  2697,  2726,  2755,
  110.      2784,  2813,  2842,  2871,  2900,  2929,  2958,  2987,
  111.      3016,  3045,  3074,  3103,  3132,  3161,  3190,  3219,
  112.      3248,  3277,  3306,  3335,  3364,  3393,  3422,  3451,
  113.      3480,  3509,  3538,  3567,  3596,  3625,  3654,  3683,
  114.      3712,  3741,  3770,  3799,  3828,  3857,  3886,  3915,
  115.      3944,  3973,  4002,  4031,  4060,  4089,  4118,  4147,
  116.      4176,  4205,  4234,  4263,  4292,  4321,  4350,  4379,
  117.      4408,  4437,  4466,  4495,  4524,  4553,  4582,  4611,
  118.      4640,  4669,  4698,  4727,  4756,  4785,  4814,  4843,
  119.      4872,  4901,  4930,  4959,  4988,  5017,  5046,  5075,
  120.      5104,  5133,  5162,  5191,  5220,  5249,  5278,  5307,
  121.      5336,  5365,  5394,  5423,  5452,  5481,  5510,  5539,
  122.      5568,  5597,  5626,  5655,  5684,  5713,  5742,  5771,
  123.      5800,  5829,  5858,  5887,  5916,  5945,  5974,  6003,
  124.      6032,  6061,  6090,  6119,  6148,  6177,  6206,  6235,
  125.      6264,  6293,  6322,  6351,  6380,  6409,  6438,  6467,
  126.      6496,  6525,  6554,  6583,  6612,  6641,  6670,  6699,
  127.      6728,  6757,  6786,  6815,  6844,  6873,  6902,  6931,
  128.      6960,  6989,  7018,  7047,  7076,  7105,  7134,  7163,
  129.      7192,  7221,  7250,  7279,  7308,  7337,  7366,  7395 };
  130.  
  131.  
  132.     ppm_init( &argc, argv );
  133.  
  134.     if ( argc > 2 )
  135.     pm_usage( "[ppmfile]" );
  136.  
  137.     if ( argc == 2 )
  138.     ifp = pm_openr( argv[1] );
  139.     else
  140.     ifp = stdin;
  141.  
  142.     ppm_readppminit( ifp, &cols, &rows, &maxval, &format );
  143.     pgm_writepgminit( stdout, cols, rows, maxval, 0 );
  144.     pixelrow = ppm_allocrow( cols );
  145.     grayrow = pgm_allocrow( cols );
  146.  
  147.     for ( row = 0; row < rows; ++row )
  148.     {
  149.     ppm_readppmrow( ifp, pixelrow, cols, maxval, format );
  150.     if ( maxval <= 255 )
  151.         /* Use fast approximation to 0.299 r + 0.587 g + 0.114 b. */
  152.         for ( col = 0, pP = pixelrow, gP = grayrow;
  153.           col < cols;
  154.           ++col, ++pP, ++gP )
  155.         *gP = (gray)
  156.             ( ( times77[PPM_GETR( *pP )] + times150[PPM_GETG( *pP )] +
  157.                 times29[PPM_GETB( *pP )] ) >> 8 );
  158.     else
  159.         /* Can't use fast approximation, so fall back on floats. */
  160.         for ( col = 0, pP = pixelrow, gP = grayrow;
  161.           col < cols;
  162.           ++col, ++pP, ++gP )
  163.         *gP = (gray) ( PPM_LUMIN( *pP ) + 0.5 );
  164.     pgm_writepgmrow( stdout, grayrow, cols, maxval, 0 );
  165.     }
  166.  
  167.     pm_close( ifp );
  168.     pm_close( stdout );
  169.  
  170.     exit( 0 );
  171.     }
  172.