png.c 4.5 KB

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