libnjb: setpbm.c

- Main Page
- Modules
- Data Structures
- Files
- Directories
- Examples

# setpbm.c

    00001 #include "common.h"
    00002 /* For isspace() */
    00003 #include <ctype.h>
    00004
    00005 static void usage (void)
    00006 {
    00007   fprintf(stderr, "usage: setpbm [ -D debuglvl ] <path>\n");
    00008   exit(1);
    00009 }
    00010
    00011 static void skip_whitespaces (FILE *f) {
    00012   int c;
    00013
    00014   while (!feof (f)) {
    00015     c = fgetc (f);
    00016     if (!isspace (c)) {
    00017       if (c == '#') { // Skip comment
    00018         while (!feof (f) && (c = fgetc (f)) != '\n')
    00019           ;
    00020       }
    00021       else {
    00022         ungetc (c, f);
    00023         break;
    00024       }
    00025     }
    00026   }
    00027 }
    00028
    00029
    00030 static int verify_pbm (FILE *f, int x, int y) {
    00031   u_int16_t magic;
    00032   unsigned int width, height;
    00033   int c;
    00034
    00035   if (fread (&magic, 1, 2, f) < 2) {
    00036     return -1;
    00037   }
    00038
    00039   if (magic != 0x3450) {
    00040     return -1;
    00041   }
    00042
    00043   skip_whitespaces(f);
    00044   width = 0;
    00045   fscanf (f, "%u", &width);
    00046   if (width != x) {
    00047     return -1;
    00048   }
    00049   skip_whitespaces (f);
    00050   fscanf (f, "%u", &height);
    00051   if (height != y) {
    00052     return -1;
    00053   }
    00054   c = fgetc (f);
    00055   if (!isspace (c)) {
    00056     return -1;
    00057   }
    00058   return 0;
    00059 }
    00060
    00061 int main(int argc, char **argv)
    00062 {
    00063   njb_t njbs[NJB_MAX_DEVICES], *njb;
    00064   int n, opt, debug;
    00065   extern int optind;
    00066   extern char *optarg;
    00067   char *path;
    00068   int x, y, bytes;
    00069
    00070   debug = 0;
    00071   while ( (opt = getopt(argc, argv, "D:")) != -1 ) {
    00072     switch (opt) {
    00073     case 'D':
    00074       debug= atoi(optarg);
    00075       break;
    00076     default:
    00077       usage();
    00078     }
    00079   }
    00080
    00081   argc -= optind;
    00082   argv += optind;
    00083
    00084   if ( argc != 1 ) usage();
    00085
    00086   if ( debug ) NJB_Set_Debug(debug);
    00087
    00088   path= argv[0];
    00089
    00090   printf("Uploading image:\n");
    00091   printf("Filename:     %s\n", path);
    00092
    00093   if (NJB_Discover(njbs, 0, &n) == -1) {
    00094     fprintf(stderr, "could not locate any jukeboxes\n");
    00095     return 1;
    00096   }
    00097
    00098   if ( n == 0 ) {
    00099     fprintf(stderr, "no NJB devices found\n");
    00100     return 0;
    00101   }
    00102
    00103   njb = njbs;
    00104
    00105   if ( NJB_Open(njb) == -1 ) {
    00106     NJB_Error_Dump(njb,stderr);
    00107     return 1;
    00108   }
    00109
    00110   NJB_Capture(njb);
    00111
    00112   /* Get the bitmap dimensions */
    00113   if ( NJB_Get_Bitmap_Dimensions(njb, &x, &y, &bytes) == -1 ) {
    00114     printf("This device does not support setting the bitmap.");
    00115     NJB_Error_Dump(njb, stderr);
    00116   } else {
    00117       FILE *f;
    00118       unsigned char data[1088];
    00119
    00120       if ( (f = fopen(path, "rb")) == NULL) {
    00121         printf("Could not open bitmap file.\n");
    00122         exit(-1);
    00123       }
    00124
    00125       if (verify_pbm(f, x, y) == -1) {
    00126         printf("Bad bitmap dimensions or bad file.\n");
    00127         fclose (f);
    00128         exit(-1);
    00129       }
    00130
    00131       if ( fread (data, 1, 1088, f) < 1088 ) {
    00132         printf("Could not read in bitmap file.\n");
    00133         fclose (f);
    00134         exit(-1);
    00135       }
    00136
    00137       fclose (f);
    00138
    00139       if ( NJB_Set_Bitmap(njb, data) == -1 ) {
    00140         NJB_Error_Dump(njb, stderr);
    00141       }
    00142   }
    00143
    00144   NJB_Release(njb);
    00145
    00146   NJB_Close (njb);
    00147
    00148   return 0;
    00149 }