home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / comp / sys / amiga / graphics / 7355 < prev    next >
Encoding:
Text File  |  1992-11-11  |  7.3 KB  |  214 lines

  1. Newsgroups: comp.sys.amiga.graphics
  2. Path: sparky!uunet!utcsri!torn!csd.unb.ca!morgan.ucs.mun.ca!cs.mun.ca!garfield.cs.mun.ca!byron
  3. From: byron@garfield.cs.mun.ca (Mr. Subliminal)
  4. Subject: pbmtoascii converter
  5. Message-ID: <1992Nov10.191534.26380@cs.mun.ca>
  6. Sender: usenet@cs.mun.ca (NNTP server account)
  7. Organization: CS Dept., Memorial University of Newfoundland
  8. Distribution: na
  9. Date: Tue, 10 Nov 1992 19:15:34 GMT
  10. Lines: 202
  11.  
  12. Here is the code for a pbmtoascii converter that does more along the lines of
  13. what I want. Just compile it and run it with the name of a pbm file and
  14. redirect the output to a file.  I use it on unix, I didn't try it on the amiga
  15. so some playing around with it may be necessary to get it to work on the amiga
  16. end.  One idea you might try is reading in different fonts, I am using the
  17. topaz font, characters 32-126. It could use some speeding up as well.
  18.  
  19. Example: (I uploaded a deluxe paint 640x200 2 color ilbm and converted it to 
  20.       pbm format with pbmplus then ran it through my program here.)
  21.                                                                __
  22.                                                                `0q_
  23.   _                                                             `LL
  24.   #^,                                              p         __  5F
  25.   `_7LqL__                    __   *n, ,_          "   __mg_jND j0L
  26.    ^F`0FXD*u       QP"^       #5L   #`L P    ___g_ j# J5F 9qEJ@" "
  27.       D  E ]L ___  `L___      JLQ   ]WF   j*pP3F #_AD" @
  28.      d  jF JLjFQF   """"#   g  B@J#n#F jLJ"J" !  "" 
  29.         "  Abf 0    _  jFj__D_p0_/" GW^!"  
  30.                     P^^" ""~   ~                                                
  31.  
  32.  Byron...
  33.  ___________________________________________________________________________
  34. |     ///    "Byron Montgomerie"  Internet: byron@odie.cs.mun.ca       ///  |
  35. | \\\///      "Does anybody here remember Vera Lynn?               \\\///   |
  36. |  \///        How she said that we would meet again,               \///    |
  37. |  Amiga       some sunny day..." -- Pink Floyd --                 Amiga    |
  38.  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  39. -----cut here----
  40. #include <stdio.h>
  41.  
  42. #define MAXBITMAP 100000
  43.  
  44. int convert (char *buffer);
  45. int percentage (char *v1, char *v2);
  46.  
  47. int main (int argc, char **argv)
  48. {
  49.     char *inputfile, bitmap[MAXBITMAP], magicstr[2], buffer[8];
  50.     int file, bytes, character, i, width, height,
  51.     bwidth, bheight, row, col, datastart, enddata;
  52.  
  53.     if (argc < 2) {
  54.         printf("Usage: %s pgmfile\n", argv[0]);
  55.         return 0;
  56.     }
  57.  
  58.     inputfile = argv[1];
  59.  
  60.     if (!(file = open(inputfile, "r")))
  61.     {
  62.         printf("Can't open file.\n");
  63.         return 0;
  64.     };
  65.  
  66. /*    printf("Reading %s...\n\n", inputfile); */
  67.     bytes = read(file, bitmap, MAXBITMAP);
  68.  
  69.     sscanf(bitmap, "%s %d %d %n", magicstr, &width, &height,
  70.         &datastart);
  71.  
  72.     if (strcasecmp(magicstr, "P4"))
  73.     {
  74.         printf("Wrong file type, must be binary pgm file.\n");
  75.         return 0;
  76.     }
  77.  
  78.     bwidth = (width / 8) + ((width % 8) != 0);
  79.     bheight = (height / 8) + ((height % 8) != 0);
  80.  
  81.     enddata = datastart + bwidth * bheight;
  82.     for (i = datastart + bwidth * height; i < enddata; i++)
  83.         bitmap[i] = '\0';
  84.  
  85. /*    printf("Magic:%s, Width:%d Height:%d Size:%d\n\n", magicstr,
  86.         width, height, bytes-datastart);
  87.         */
  88.  
  89.     for (row = 0; row < bheight; row++) {
  90.         for (col = 0; col < bwidth; col++) {
  91.             for (i=0; i<8; i++)
  92.                 buffer[i] = bitmap[datastart + col + bwidth * (i+row*8)] & 255;
  93.             character = convert(buffer);
  94.             printf("%c", character);
  95.         }
  96.         printf("\n");
  97.     }
  98.  
  99. /*    printf("Ok.\n");*/
  100.     close(file);
  101. }
  102.  
  103. int convert (char *buffer)
  104. {
  105.  
  106.     /* The font data: */
  107.     static long font[768]=
  108.     {
  109.         /* Row 0: */
  110.         0x0018,0x6C6C,0x1800,0x3818,0x0C30,0x0000,0x0000,0x0003,
  111.         0x7C18,0x7C7C,0x1CFE,0x38FE,0x7C7C,0x0000,0x0C00,0x303C,
  112.         0x7C18,0xFC3C,0xF8FE,0xFE3C,0xC63C,0x06C6,0xC082,0xC638,
  113.         0xFC38,0xFC7C,0x7EC6,0xC3C6,0xC3C3,0xFE3C,0xC03C,0x1000,
  114.         0x1800,0xC000,0x0600,0x1C00,0xC018,0x06C0,0x3800,0x0000,
  115.         0x0000,0x0000,0x1000,0x0000,0x0000,0x000E,0x1870,0x72CC,
  116.         /* Row 1: */
  117.         0x003C,0x6C6C,0x7EC6,0x6C18,0x1818,0x6618,0x0000,0x0006,
  118.         0xC638,0xC6C6,0x3CC0,0x6006,0xC6C6,0x1818,0x1800,0x1866,
  119.         0xC63C,0xC666,0xCCC0,0xC066,0xC618,0x06C6,0xC0C6,0xE66C,
  120.         0xC66C,0xC6C6,0x18C6,0xC3C6,0x66C3,0x0630,0x600C,0x3800,
  121.         0x1800,0xC000,0x0600,0x3600,0xC000,0x00C0,0x1800,0x0000,
  122.         0x0000,0x0000,0x3000,0x0000,0x0000,0x0018,0x1818,0x9C33,
  123.         /* Row 2: */
  124.         0x003C,0x00FE,0xC0CC,0x6830,0x300C,0x3C18,0x0000,0x000C,
  125.         0xCE18,0x0606,0x6CFC,0xC006,0xC6C6,0x1818,0x307E,0x0C06,
  126.         0xDE3C,0xC6C0,0xC6C0,0xC0C0,0xC618,0x06CC,0xC0EE,0xF6C6,
  127.         0xC6C6,0xC6E0,0x18C6,0x66C6,0x3C66,0x0C30,0x300C,0x6C00,
  128.         0x0C7C,0xDC7C,0x767C,0x3076,0xDC18,0x06C6,0x18CC,0xFC7C,
  129.         0xFC7E,0xDC7E,0xFEC6,0xC6C6,0xC6C6,0xFC18,0x1818,0x00CC,
  130.         /* Row 3: */
  131.         0x0018,0x006C,0x7C18,0x7600,0x300C,0xFF7E,0x007E,0x0018,
  132.         0xDE18,0x1C3C,0xCC06,0xFC0C,0x7C7E,0x0000,0x6000,0x060C,
  133.         0xDE66,0xFCC0,0xC6F8,0xF8C6,0xFE18,0x06F8,0xC0FE,0xDEC6,
  134.         0xFCC6,0xFC38,0x18C6,0x66D6,0x183C,0x1830,0x180C,0xC600,
  135.         0x0006,0xE6C6,0xCEC6,0xFCCC,0xE618,0x06CC,0x18FE,0xC6C6,
  136.         0xC6C6,0xE6C0,0x30C6,0xC6D6,0x6CC6,0x1870,0x180E,0x0033,
  137.         /* Row 4: */
  138.         0x0018,0x00FE,0x0630,0xDC00,0x300C,0x3C18,0x0000,0x0030,
  139.         0xF618,0x7006,0xFE06,0xC618,0xC606,0x0000,0x3000,0x0C18,
  140.         0xDE7E,0xC6C0,0xC6C0,0xC0C6,0xC618,0xC6CC,0xC0D6,0xCEC6,
  141.         0xC0C6,0xD80E,0x18C6,0x3CFE,0x3C18,0x3030,0x0C0C,0x0000,
  142.         0x007E,0xC6C0,0xC6FE,0x30CC,0xC618,0x06F8,0x18D6,0xC6C6,
  143.         0xC6C6,0xC07C,0x30C6,0xC6D6,0x38C6,0x3018,0x1818,0x00CC,
  144.         /* Row 5: */
  145.         0x0000,0x006C,0xFC66,0xCC00,0x1818,0x6618,0x1800,0x1860,
  146.         0xC618,0xC0C6,0x0CC6,0xC630,0xC60C,0x1818,0x187E,0x1800,
  147.         0xC0C3,0xC666,0xCCC0,0xC066,0xC618,0xC6C6,0xC0C6,0xC66C,
  148.         0xC06C,0xCCC6,0x18C6,0x3CEE,0x6618,0x6030,0x060C,0x0000,
  149.         0x00C6,0xC6C6,0xC6C0,0x3078,0xC618,0x06CC,0x18C6,0xC6C6,
  150.         0xC6C6,0xC006,0x34C6,0x6C6C,0x6C7C,0x6018,0x1818,0x0033,
  151.         /* Row 6: */
  152.         0x0018,0x006C,0x18C6,0x7600,0x0C30,0x0000,0x1800,0x18C0,
  153.         0x7C3C,0xFE7C,0x0C7C,0x7C30,0x7C38,0x1818,0x0C00,0x3018,
  154.         0x78C3,0xFC3C,0xF8FE,0xC03E,0xC63C,0x7CC6,0xFEC6,0xC638,
  155.         0xC03C,0xC67C,0x187E,0x18C6,0xC318,0xFE3C,0x033C,0x0000,
  156.         0x007E,0xFC7C,0x7E7C,0x30C6,0xC618,0xC6C6,0x18C6,0xC67C,
  157.         0xFC7E,0xC0FC,0x187E,0x386C,0xC618,0xFE0E,0x1870,0x00CC,
  158.         /* Row 7: */
  159.         0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x3000,0x0000,
  160.         0x0000,0x0000,0x0000,0x0000,0x0000,0x0030,0x0000,0x0000,
  161.         0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,
  162.         0x0006,0x0000,0x0000,0x0000,0x0000,0x0000,0x0000,0x00FE,
  163.         0x0000,0x0000,0x0000,0x007C,0x0000,0x7C00,0x0000,0x0000,
  164.         0xC007,0x0000,0x0000,0x0000,0x00F0,0x0000,0x0000,0x0033
  165.     };
  166.  
  167.     char character[8];
  168.     int row, col, c=' ', match, omatch=0;
  169.     if (buffer == "\0\0\0\0\0\0\0\0")
  170.         return ' ';
  171.  
  172.     for (col = 0; col <= 47; col++) {
  173.         for (row = 0; row < 8; row++)
  174.             character[row] = font[col + row * 48] / 256;
  175.  
  176.         match = percentage(character, buffer);
  177.         if (match > omatch) {
  178.             c = 32 + col*2;
  179.             omatch = match;
  180.         };
  181.  
  182.         for (row = 0; row < 8; row++)
  183.             character[row] = font[col + row * 48] % 256;
  184.  
  185.         match = percentage(character, buffer);
  186.         if (match > omatch && col != 47) {
  187.             c = 33 + col*2;
  188.             omatch = match;
  189.         };
  190.  
  191.     };
  192.         return c;
  193. }
  194.  
  195. int percentage (char *v1, char *v2)
  196. {
  197.     int byte, num1, num2, hits=0;
  198.     int bits[8]={128,64,32,16,8,4,2,1}, bit, slice;
  199.  
  200.     for (byte = 0; byte < 8; byte++) {
  201.         for (bit = 0; bit < 8; bit++) {
  202.             num1 = v1[byte];
  203.             num2 = v2[byte];
  204.             slice = bits[bit];
  205.             num1 = num1 & slice;
  206.             num2 = num2 & slice;
  207.             hits = hits + (num1 == num2);
  208.         };
  209.     };
  210.  
  211.     return hits;
  212. }
  213.  
  214.