ppm.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <draw.h>
  5. #include <event.h>
  6. #include "imagefile.h"
  7. int cflag = 0;
  8. int dflag = 0;
  9. int eflag = 0;
  10. int nineflag = 0;
  11. int threeflag = 0;
  12. int output = 0;
  13. ulong outchan = CMAP8;
  14. int defaultcolor = 1;
  15. Image *image;
  16. enum{
  17. Border = 2,
  18. Edge = 5
  19. };
  20. char *show(int, char*);
  21. void
  22. eresized(int new)
  23. {
  24. Rectangle r;
  25. if(new && getwindow(display, Refnone) < 0){
  26. fprint(2, "ppm: can't reattach to window\n");
  27. exits("resize");
  28. }
  29. if(image == nil)
  30. return;
  31. r = insetrect(screen->clipr, Edge+Border);
  32. r.max.x = r.min.x+Dx(image->r);
  33. r.max.y = r.min.y+Dy(image->r);
  34. border(screen, r, -Border, nil, ZP);
  35. drawop(screen, r, image, nil, image->r.min, S);
  36. flushimage(display, 1);
  37. }
  38. void
  39. main(int argc, char *argv[])
  40. {
  41. int fd, i;
  42. char *err;
  43. ARGBEGIN{
  44. case '3': /* produce encoded, compressed, three-color bitmap file; no display by default */
  45. threeflag++;
  46. /* fall through */
  47. case 't': /* produce encoded, compressed, true-color bitmap file; no display by default */
  48. cflag++;
  49. dflag++;
  50. output++;
  51. defaultcolor = 0;
  52. outchan = RGB24;
  53. break;
  54. case 'c': /* produce encoded, compressed, bitmap file; no display by default */
  55. cflag++;
  56. dflag++;
  57. output++;
  58. if(defaultcolor)
  59. outchan = CMAP8;
  60. break;
  61. case 'd': /* suppress display of image */
  62. dflag++;
  63. break;
  64. case 'e': /* disable floyd-steinberg error diffusion */
  65. eflag++;
  66. break;
  67. case 'k': /* force black and white */
  68. defaultcolor = 0;
  69. outchan = GREY8;
  70. break;
  71. case 'v': /* force RGBV */
  72. defaultcolor = 0;
  73. outchan = CMAP8;
  74. break;
  75. case '9': /* produce plan 9, uncompressed, bitmap file; no display by default */
  76. nineflag++;
  77. dflag++;
  78. output++;
  79. if(defaultcolor)
  80. outchan = CMAP8;
  81. break;
  82. default:
  83. fprint(2, "usage: ppm -39cdektv [file.ppm ...]\n");
  84. exits("usage");
  85. }ARGEND;
  86. err = nil;
  87. if(argc == 0)
  88. err = show(0, "<stdin>");
  89. else{
  90. for(i=0; i<argc; i++){
  91. fd = open(argv[i], OREAD);
  92. if(fd < 0){
  93. fprint(2, "ppm: can't open %s: %r\n", argv[i]);
  94. err = "open";
  95. }else{
  96. err = show(fd, argv[i]);
  97. close(fd);
  98. }
  99. if((nineflag || cflag) && argc>1 && err==nil){
  100. fprint(2, "ppm: exiting after one file\n");
  101. break;
  102. }
  103. }
  104. }
  105. exits(err);
  106. }
  107. int
  108. init(void)
  109. {
  110. static int inited;
  111. if(inited == 0){
  112. if(initdraw(0, 0, 0) < 0){
  113. fprint(2, "ppm: initdraw failed: %r");
  114. return -1;
  115. }
  116. einit(Ekeyboard|Emouse);
  117. inited++;
  118. }
  119. return 1;
  120. }
  121. char*
  122. show(int fd, char *name)
  123. {
  124. Rawimage **array, *r, *c;
  125. Image *i;
  126. int j, ch;
  127. char buf[32];
  128. array = readpixmap(fd, CRGB);
  129. if(array == nil || array[0]==nil){
  130. fprint(2, "ppm: decode %s failed: %r\n", name);
  131. return "decode";
  132. }
  133. if(!dflag){
  134. if(init() < 0)
  135. return "initdraw";
  136. if(defaultcolor && screen->depth>8)
  137. outchan = RGB24;
  138. }
  139. r = array[0];
  140. if(outchan == CMAP8)
  141. c = torgbv(r, !eflag);
  142. else{
  143. if(outchan==GREY8 || (r->chandesc==CY && threeflag==0))
  144. c = totruecolor(r, CY);
  145. else
  146. c = totruecolor(r, CRGB24);
  147. }
  148. if(c == nil){
  149. fprint(2, "ppm: converting %s to local format failed: %r\n", name);
  150. return "torgbv";
  151. }
  152. if(!dflag){
  153. if(r->chandesc == CY)
  154. outchan = GREY8;
  155. i = allocimage(display, c->r, outchan, 0, 0);
  156. if(i == nil){
  157. fprint(2, "ppm: allocimage %s failed: %r\n", name);
  158. return "allocimage";
  159. }
  160. if(loadimage(i, i->r, c->chans[0], c->chanlen) < 0){
  161. fprint(2, "ppm: loadimage %s failed: %r\n", name);
  162. return "loadimage";
  163. }
  164. image = i;
  165. eresized(0);
  166. if((ch=ekbd())=='q' || ch==0x7F || ch==0x04)
  167. exits(nil);
  168. draw(screen, screen->clipr, display->white, nil, ZP);
  169. image = nil;
  170. freeimage(i);
  171. }
  172. if(nineflag){
  173. if(r->chandesc == CY)
  174. outchan = GREY8;
  175. chantostr(buf, outchan);
  176. print("%11s %11d %11d %11d %11d ", buf,
  177. c->r.min.x, c->r.min.y, c->r.max.x, c->r.max.y);
  178. if(write(1, c->chans[0], c->chanlen) != c->chanlen){
  179. fprint(2, "ppm: %s: write error %r\n", name);
  180. return "write";
  181. }
  182. }else if(cflag){
  183. if(writerawimage(1, c) < 0){
  184. fprint(2, "ppm: %s: write error: %r\n", name);
  185. return "write";
  186. }
  187. }
  188. for(j=0; j<r->nchans; j++)
  189. free(r->chans[j]);
  190. free(r->cmap);
  191. free(r);
  192. free(array);
  193. if(c){
  194. free(c->chans[0]);
  195. free(c);
  196. }
  197. return nil;
  198. }