Personal warehouse management system
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
1.6KB

  1. package main
  2. import (
  3. "image"
  4. _ "image/gif"
  5. _ "image/jpeg"
  6. _ "image/png"
  7. "flag"
  8. "fmt"
  9. "log"
  10. "os"
  11. "janouch.name/sklad/imgutil"
  12. "janouch.name/sklad/ql"
  13. )
  14. var scale = flag.Int("scale", 1, "integer upscaling")
  15. var rotate = flag.Bool("rotate", false, "print sideways")
  16. func main() {
  17. flag.Usage = func() {
  18. fmt.Fprintf(os.Stderr, "Usage: %s IMAGE\n", os.Args[0])
  19. flag.PrintDefaults()
  20. }
  21. flag.Parse()
  22. if flag.NArg() != 1 {
  23. flag.Usage()
  24. os.Exit(1)
  25. }
  26. // Open the picture.
  27. f, err := os.Open(flag.Arg(0))
  28. if err != nil {
  29. log.Fatalln(err)
  30. }
  31. defer f.Close()
  32. // Load and eventually transform the picture.
  33. img, _, err := image.Decode(f)
  34. if err != nil {
  35. log.Fatalln(err)
  36. }
  37. if *scale > 1 {
  38. img = &imgutil.Scale{Image: img, Scale: *scale}
  39. }
  40. if *rotate {
  41. img = &imgutil.LeftRotate{Image: img}
  42. }
  43. // Open and initialize the printer.
  44. p, err := ql.Open()
  45. if err != nil {
  46. log.Fatalln(err)
  47. }
  48. if p == nil {
  49. log.Fatalln("no suitable printer found")
  50. }
  51. if err := p.Initialize(); err != nil {
  52. log.Fatalln(err)
  53. }
  54. if err := p.UpdateStatus(); err != nil {
  55. log.Fatalln(err)
  56. }
  57. // Check the picture against the media in the printer.
  58. mi := ql.GetMediaInfo(
  59. p.LastStatus.MediaWidthMM(),
  60. p.LastStatus.MediaLengthMM(),
  61. )
  62. if mi == nil {
  63. log.Fatalln("unknown media")
  64. }
  65. bounds := img.Bounds()
  66. dx, dy := bounds.Dx(), bounds.Dy()
  67. if dx > mi.PrintAreaPins {
  68. log.Fatalln("the image is too wide,", dx, ">", mi.PrintAreaPins, "pt")
  69. }
  70. if dy > mi.PrintAreaLength && mi.PrintAreaLength != 0 {
  71. log.Fatalln("the image is too high,", dy, ">", mi.PrintAreaLength, "pt")
  72. }
  73. if err := p.Print(img); err != nil {
  74. log.Fatalln(err)
  75. }
  76. }