topng.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include <ctype.h>
  14. #include <bio.h>
  15. #include <flate.h>
  16. #include "imagefile.h"
  17. void
  18. usage(void)
  19. {
  20. fprint(2, "usage: topng [-c 'comment'] [-g 'gamma'] [file]\n");
  21. exits("usage");
  22. }
  23. void
  24. main(int argc, char *argv[])
  25. {
  26. Biobuf bout;
  27. Memimage *i;
  28. int fd;
  29. char *err, *filename;
  30. ImageInfo II;
  31. ARGBEGIN{
  32. case 'c':
  33. II.comment = ARGF();
  34. if(II.comment == nil)
  35. usage();
  36. II.fields_set |= II_COMMENT;
  37. break;
  38. case 'g':
  39. II.gamma = atof(ARGF());
  40. if(II.gamma == 0.)
  41. usage();
  42. II.fields_set |= II_GAMMA;
  43. break;
  44. case 't':
  45. break;
  46. default:
  47. usage();
  48. }ARGEND
  49. if(Binit(&bout, 1, OWRITE) < 0)
  50. sysfatal("Binit failed: %r");
  51. memimageinit();
  52. if(argc == 0){
  53. fd = 0;
  54. filename = "<stdin>";
  55. }else{
  56. fd = open(argv[0], OREAD);
  57. if(fd < 0)
  58. sysfatal("can't open %s: %r", argv[0]);
  59. filename = argv[0];
  60. }
  61. i = readmemimage(fd);
  62. if(i == nil)
  63. sysfatal("can't readimage %s: %r", filename);
  64. close(fd);
  65. err = memwritepng(&bout, i, &II);
  66. freememimage(i);
  67. if(err != nil)
  68. fprint(2, "topng: %s\n", err);
  69. exits(err);
  70. }