You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. #include <getopt.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. static int palette[8][3] = {
  5. /* black, red, green, yellow, */
  6. { 0, 0, 0},{255, 0, 0},{ 0,255, 0},{255,255, 0},
  7. { 0, 0,255},{255, 0,255},{ 0,255,255},{255,255,255}};
  8. /* blue, magenta, cyan, white */
  9. static struct trans {
  10. struct trans *next;
  11. int idx,r,g,b;
  12. } *trans = NULL;
  13. static int skipcomment(FILE *fp)
  14. {
  15. int ch;
  16. for (;;) {
  17. ch = getc(fp);
  18. if (ch != '#')
  19. return(ch);
  20. while (ch != '\n' && ch != EOF)
  21. ch = getc(fp); }
  22. }
  23. static int readentry(FILE *fp,int format,int depth)
  24. {
  25. int ch,i = 0;
  26. if (format == '3') {
  27. while ((ch = getc(fp)) == ' ' || ch == '\t' || ch == '\r' || ch == '\n');
  28. if (ch < '0' || ch > '9') {
  29. error:
  30. fprintf(stderr,"Format error in input file\n");
  31. exit(1); }
  32. for (; ch >= '0' && ch <= '9'; ch = getc(fp))
  33. i = 10*i + ch - '0'; }
  34. else {
  35. if ((i = getc(fp)) > depth || i < 0)
  36. goto error; }
  37. return((i*256)/(depth+1));
  38. }
  39. static void packpixel(char *data,int c)
  40. {
  41. int i = 0, n = 0;
  42. while (c--) {
  43. i = (i << 3) | (*data++ & 0x7);
  44. if ((n += 3) >= 8)
  45. putchar((i >> (n -= 8)) & 0xFF); }
  46. if (n)
  47. putchar(i << (8 - n));
  48. return;
  49. }
  50. static int dg(int i)
  51. {
  52. int d;
  53. for (d = 0; i; d++, i /= 10);
  54. return(d);
  55. }
  56. static char *i2s(char *buf,int i)
  57. {
  58. /* if (!i)
  59. *buf = '\000';
  60. else*/
  61. sprintf(buf,"%d",i);
  62. return(buf);
  63. }
  64. static void flushdata(int x,int y,int c,char *data)
  65. {
  66. char b1[10],b2[10],b3[10],b4[10];
  67. int i,j,rle,v;
  68. for (i = j = v = 0; i < c; ) {
  69. for (rle = 0; i+rle < c && data[i] == data[i+rle]; rle++);
  70. if (rle > (i != j ? (v ? 4 : 6) : 0) +
  71. ((v || (i != j)) ? 4+dg(rle)+dg(data[i])
  72. : 6+dg(x+i)+dg(y)+dg(rle)+dg(data[i]))) {
  73. if (i != j) {
  74. if (v)
  75. printf("[%s-",i2s(b1,i-j));
  76. else
  77. printf("[%s;%s;%s-",i2s(b1,x+j),i2s(b2,y),i2s(b3,i-j));
  78. packpixel(data+j,i-j); }
  79. if (v++ || (i != j))
  80. printf("[%s;%s+",i2s(b1,rle),i2s(b2,data[i]));
  81. else
  82. printf("[%s;%s;%s;%s+",i2s(b1,x+i),i2s(b2,y),
  83. i2s(b3,rle),i2s(b4,data[i]));
  84. j = i += rle; }
  85. else
  86. i++; }
  87. if (j != c) {
  88. if (v)
  89. printf("[%s-",i2s(b1,c-j));
  90. else
  91. printf("[%s;%s;%s-",i2s(b1,x+j),i2s(b2,y),i2s(b3,c-j));
  92. packpixel(data+j,c-j); }
  93. return;
  94. }
  95. int main(int argc,char *argv[])
  96. {
  97. extern int optind;
  98. extern char *optarg;
  99. FILE *infile = NULL;
  100. int ch,i,j,dist,idx;
  101. int format,width,height,depth;
  102. int bg = 0,bgred = 0,bggreen = 0,bgblue = 0;
  103. int xoffset = 0,yoffset = 0;
  104. int w,h,r,g,b,c;
  105. struct trans *tp;
  106. char *buf;
  107. while ((i = getopt(argc,argv,"b:t:x:y:")) >= 0) switch(i) {
  108. case 'b':
  109. bg++;
  110. for (i = bgred = 0; optarg[i] >= '0' && optarg[i] <= '9';
  111. bgred = 10*bgred + optarg[i++] - '0');
  112. if (optarg[i++] != '/')
  113. goto usage;
  114. for (bggreen = 0; optarg[i] >= '0' && optarg[i] <= '9';
  115. bggreen = 10*bggreen + optarg[i++] - '0');
  116. if (optarg[i++] != '/')
  117. goto usage;
  118. for (bgblue = 0; optarg[i] >= '0' && optarg[i] <= '9';
  119. bgblue = 10*bgblue + optarg[i++] - '0');
  120. if (optarg[i])
  121. goto usage;
  122. break;
  123. case 't':
  124. if ((tp = malloc(sizeof(struct trans))) == NULL)
  125. goto usage;
  126. for (i = tp->r = 0; optarg[i] >= '0' && optarg[i] <= '9';
  127. tp->r = 10*tp->r + optarg[i++] - '0');
  128. if (optarg[i++] != '/')
  129. goto usage;
  130. for (tp->g = 0; optarg[i] >= '0' && optarg[i] <= '9';
  131. tp->g = 10*tp->g + optarg[i++] - '0');
  132. if (optarg[i++] != '/')
  133. goto usage;
  134. for (tp->b = 0; optarg[i] >= '0' && optarg[i] <= '9';
  135. tp->b = 10*tp->b + optarg[i++] - '0');
  136. if (optarg[i++] != ':')
  137. goto usage;
  138. if (optarg[i] == '-') {
  139. j = -1; i++; }
  140. else j = 1;
  141. for (tp->idx = 0; optarg[i] >= '0' && optarg[i] <= '9';
  142. tp->idx = 10*tp->idx + optarg[i++] - '0');
  143. tp->idx *= j;
  144. if (tp->idx < -1 || tp->idx >= 8)
  145. goto usage;
  146. if (optarg[i])
  147. goto usage;
  148. tp->next = trans;
  149. trans = tp;
  150. break;
  151. case 'x':
  152. for (i = xoffset = 0; optarg[i] >= '0' && optarg[i] <= '9';
  153. xoffset = 10*xoffset + optarg[i++] - '0');
  154. if (optarg[i])
  155. goto usage;
  156. break;
  157. case 'y':
  158. for (i = yoffset = 0; optarg[i] >= '0' && optarg[i] <= '9';
  159. yoffset = 10*yoffset + optarg[i++] - '0');
  160. if (optarg[i])
  161. goto usage;
  162. break;
  163. default:
  164. usage:
  165. fprintf(stderr,"Usage: %s [-b r/g/b] [-t r/g/b:idx] "
  166. "[-x offset] [-y offset] [ppmfile]\n",argv[0]);
  167. exit(1); }
  168. if (argc-optind == 0)
  169. infile = stdin;
  170. else if (argc-optind == 1)
  171. infile = fopen(argv[optind],"r");
  172. if (!infile)
  173. goto usage;
  174. if ((ch = skipcomment(infile)) != 'P' ||
  175. ((format = getc(infile)) != '3' && format != '6') ||
  176. ((ch = getc(infile)) != '\n' && ch != '\r' && getc(infile) != '\n'))
  177. goto usage;
  178. for (width = 0; (ch = skipcomment(infile)) >= '0' && ch <= '9';
  179. width = 10*width + ch - '0');
  180. while (ch == ' ') ch = getc(infile);
  181. for (height = 0; ch >= '0' && ch <= '9'; ch = getc(infile))
  182. height = 10*height + ch - '0';
  183. if (ch != '\n' && ch != '\r' && getc(infile) != '\n')
  184. goto usage;
  185. for (depth = 0; (ch = skipcomment(infile)) >= '0' && ch <= '9';
  186. depth = 10*depth + ch - '0');
  187. if (ch != '\n' && ch != '\r' && getc(infile) != '\n')
  188. goto usage;
  189. if (!width || !height || !depth /* || depth > 255 */)
  190. goto usage;
  191. if ((buf = malloc(width)) == NULL)
  192. goto usage;
  193. for (h = 0; h < height; h++) {
  194. for (w = c = 0; w < width; w++) {
  195. r = readentry(infile,format,depth);
  196. g = readentry(infile,format,depth);
  197. b = readentry(infile,format,depth);
  198. idx = 255;
  199. if (bg && bgred == r &&
  200. bggreen == g && bgblue == b)
  201. idx = -1;
  202. else for (tp = trans; tp; tp = tp->next)
  203. if (tp->r == r && tp->g == g && tp->b == b) {
  204. idx = tp->idx;
  205. break; }
  206. if (idx == 255)
  207. for (idx = -1, dist = 3*255*255, i = 8; i--;)
  208. if ((j = (r-palette[i][0])*(r-palette[i][0]) +
  209. (g-palette[i][1])*(g-palette[i][1]) +
  210. (b-palette[i][2])*(b-palette[i][2])) < dist) {
  211. dist = j; idx = i; }
  212. if (idx >= 0)
  213. buf[c++] = idx;
  214. else if (c) {
  215. flushdata(w-c+xoffset,h+yoffset,c,buf);
  216. c = 0; } }
  217. if (c)
  218. flushdata(w-c+xoffset,h+yoffset,c,buf); }
  219. exit(0);
  220. }