tga.c 4.5 KB

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