png.c 5.5 KB

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