iconv.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 <draw.h>
  12. #include <memdraw.h>
  13. void
  14. usage(void)
  15. {
  16. fprint(2, "usage: iconv [-u] [-c chanstr] [file]\n");
  17. exits("usage");
  18. }
  19. void
  20. writeuncompressed(int fd, Memimage *m)
  21. {
  22. char chanstr[32];
  23. int bpl, y, j;
  24. uint8_t *buf;
  25. if(chantostr(chanstr, m->chan) == nil)
  26. sysfatal("can't convert channel descriptor: %r");
  27. fprint(fd, "%11s %11d %11d %11d %11d ",
  28. chanstr, m->r.min.x, m->r.min.y, m->r.max.x, m->r.max.y);
  29. bpl = bytesperline(m->r, m->depth);
  30. buf = malloc(bpl);
  31. if(buf == nil)
  32. sysfatal("malloc failed: %r");
  33. for(y=m->r.min.y; y<m->r.max.y; y++){
  34. j = unloadmemimage(m, Rect(m->r.min.x, y, m->r.max.x, y+1), buf, bpl);
  35. if(j != bpl)
  36. sysfatal("image unload failed: %r");
  37. if(write(fd, buf, bpl) != bpl)
  38. sysfatal("write failed: %r");
  39. }
  40. free(buf);
  41. }
  42. void
  43. main(int argc, char *argv[])
  44. {
  45. char *tostr, *file;
  46. int fd, uncompressed;
  47. uint32_t tochan;
  48. Memimage *m, *n;
  49. tostr = nil;
  50. uncompressed = 0;
  51. ARGBEGIN{
  52. case 'c':
  53. tostr = EARGF(usage());
  54. break;
  55. case 'u':
  56. uncompressed = 1;
  57. break;
  58. default:
  59. usage();
  60. }ARGEND
  61. memimageinit();
  62. file = "<stdin>";
  63. m = nil;
  64. switch(argc){
  65. case 0:
  66. m = readmemimage(0);
  67. break;
  68. case 1:
  69. file = argv[0];
  70. fd = open(file, OREAD);
  71. if(fd < 0)
  72. sysfatal("can't open %s: %r", file);
  73. m = readmemimage(fd);
  74. close(fd);
  75. break;
  76. default:
  77. usage();
  78. }
  79. if(m == nil)
  80. sysfatal("can't read %s: %r", file);
  81. if(tostr == nil)
  82. tochan = m->chan;
  83. else{
  84. tochan = strtochan(tostr);
  85. if(tochan == 0)
  86. sysfatal("bad channel descriptor '%s'", tostr);
  87. }
  88. n = allocmemimage(m->r, tochan);
  89. if(n == nil)
  90. sysfatal("can't allocate new image: %r");
  91. memimagedraw(n, n->r, m, m->r.min, nil, ZP, S);
  92. if(uncompressed)
  93. writeuncompressed(1, n);
  94. else
  95. writememimage(1, n);
  96. exits(nil);
  97. }