File: setpbm.c

package info (click to toggle)
libnjb 2.2.5-4.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,528 kB
  • ctags: 1,071
  • sloc: ansic: 11,860; sh: 8,611; makefile: 139
file content (149 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "common.h"
/* For isspace() */
#include <ctype.h>

static void usage (void)
{
  fprintf(stderr, "usage: setpbm [ -D debuglvl ] <path>\n");
  exit(1);
}

static void skip_whitespaces (FILE *f) {
  int c;

  while (!feof (f)) {
    c = fgetc (f);
    if (!isspace (c)) {
      if (c == '#') { // Skip comment
	while (!feof (f) && (c = fgetc (f)) != '\n')
	  ;
      }
      else {
	ungetc (c, f);
	break;
      }
    }
  }
}


static int verify_pbm (FILE *f, int x, int y) {
  u_int16_t magic;
  unsigned int width, height;
  int c;

  if (fread (&magic, 1, 2, f) < 2) {
    return -1;
  }

  if (magic != 0x3450) {
    return -1;
  }

  skip_whitespaces(f);
  width = 0;
  fscanf (f, "%u", &width);
  if (width != x) {
    return -1;
  }
  skip_whitespaces (f);
  fscanf (f, "%u", &height);
  if (height != y) {
    return -1;
  }
  c = fgetc (f);
  if (!isspace (c)) {
    return -1;
  }
  return 0;
}

int main(int argc, char **argv)
{
  njb_t njbs[NJB_MAX_DEVICES], *njb;
  int n, opt, debug;
  extern int optind;
  extern char *optarg;
  char *path;
  int x, y, bytes;
  
  debug = 0;
  while ( (opt = getopt(argc, argv, "D:")) != -1 ) {
    switch (opt) {
    case 'D':
      debug= atoi(optarg);
      break;
    default:
      usage();
    }
  }

  argc -= optind;
  argv += optind;
  
  if ( argc != 1 ) usage();
  
  if ( debug ) NJB_Set_Debug(debug);

  path= argv[0];
  
  printf("Uploading image:\n");
  printf("Filename:     %s\n", path);

  if (NJB_Discover(njbs, 0, &n) == -1) {
    fprintf(stderr, "could not locate any jukeboxes\n");
    return 1;
  }
  
  if ( n == 0 ) {
    fprintf(stderr, "no NJB devices found\n");
    return 0;
  } 
  
  njb = njbs;

  if ( NJB_Open(njb) == -1 ) {
    NJB_Error_Dump(njb,stderr);
    return 1;
  }

  NJB_Capture(njb);

  /* Get the bitmap dimensions */
  if ( NJB_Get_Bitmap_Dimensions(njb, &x, &y, &bytes) == -1 ) {
    printf("This device does not support setting the bitmap.");
    NJB_Error_Dump(njb, stderr);
  } else {
      FILE *f;
      unsigned char data[1088];

      if ( (f = fopen(path, "rb")) == NULL) {
	printf("Could not open bitmap file.\n");
	exit(-1);
      }
      
      if (verify_pbm(f, x, y) == -1) {
	printf("Bad bitmap dimensions or bad file.\n");
	fclose (f);
	exit(-1);
      }

      if ( fread (data, 1, 1088, f) < 1088 ) {
	printf("Could not read in bitmap file.\n");
	fclose (f);
	exit(-1);
      }

      fclose (f);

      if ( NJB_Set_Bitmap(njb, data) == -1 ) {
	NJB_Error_Dump(njb, stderr);
      }
  }
  
  NJB_Release(njb);
  
  NJB_Close (njb);

  return 0;
}