ppm.c 4.5 KB

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