Utah Raster Toolkit  9999-git
URT Development version (post-3.1b)
gettaac.c
Go to the documentation of this file.
1 #ifndef lint
2 static char sccsid[] = "@(#)gettaac.c 1.2 7/5/90 Copyright (c) 1989, 1990\
3 by Southwest Research Institute, San Antonio, Texas";
4 #endif
5 
6 /*
7 Copyright (c) 1989, 1990 Southwest Research Institute
8 All rights reserved.
9 
10 Redistribution and use in source and binary forms are permitted
11 provided that the above copyright notice and this paragraph are
12 duplicated in all such forms and that any documentation,
13 advertising materials, and other materials related to such
14 distribution and use acknowledge that the software was developed
15 by Southwest Research Institute. The name of Southwest Research
16 Institute may not be used to endorse or promote products derived
17 from this software without specific prior written permission.
18 THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
19 IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
20 WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
21 */
22 
23 /*
24  * Send bug fixes and improvements to: ksp@maxwell.nde.swri.edu
25  */
26 
27 #include <errno.h>
28 #include <stdio.h>
29 #include <strings.h>
30 #include <signal.h>
31 #include <suntool/sunview.h>
32 #include <suntool/panel.h>
33 #include <suntool/canvas.h>
34 #include <sunwindow/win_cursor.h>
35 #include <sunwindow/notify.h>
36 #include <sys/types.h>
37 #include <sys/dir.h>
38 #include <sys/file.h>
39 #include <pwd.h>
40 #include "rle.h"
41 #include <sys/stat.h>
42 #include <taac1/taio.h>
43 
44 #define MAX_FILE_NAME 1024
45 #define FONT "/usr/lib/fonts/fixedwidthfonts/serif.r.14"
46 #define NCMAP 256
47 #define X_PIXELS 512 /* number of x pixels */
48 #define Y_PIXELS 480 /* number of y pixels */
49 
51 static Canvas canvas;
52 static Pixwin *pw;
53 static Cursor cursor;
54 
55 
56 /* control panel */
57 static Panel panel;
58 static void init_panel();
61 
62 /* panel notify procs */
63 static void doexit();
64 static void read_file();
65 static void format_select();
66 static void make_new_name();
67 
68 /* utilities */
69 static void init_color_map();
70 static void bw_init_colors();
71 static void init_canvas();
72 
73 /* useful stuff */
74 static struct pixfont *pixfont;
75 static unsigned int *scan_line_buffer;
76 static char *my_name;
77 
78 static short icon_image[] = {
79 #include "gettaac.icon"
80 };
81 
82 DEFINE_ICON_FROM_IMAGE(gettaac_icon, icon_image);
83 
84 static unsigned char red[NCMAP];
85 static unsigned char green[NCMAP];
86 static unsigned char blue[NCMAP];
87 
88 #define RGB 0
89 #define GRAY 1
90 
91 static int display_mode = RGB;
92 
93 static void read_file_rgb();
94 static void read_file_gray();
95 static void read_file_bw();
96 
97 /* taac stuff */
99 static int erase[X_PIXELS];
100 static Notify_value taac_interposer();
101 
103 int argc;
104 char **argv;
105 {
106 
107  register int i;
108  struct pixrect *screen;
109 
110  my_name = cmd_name( argv );
111 
112  pixfont = pf_open(FONT);
113 
114  if (pixfont == NULL) {
115  fprintf(stderr, "Can't open the font %s\n", FONT);
116  exit(1);
117  }
118 
119  /* open the taac */
120  if((tahandle = ta_open(0))==NULL){
121  fprintf(stderr, "ta_open failed.\n");
122  exit(1);
123  }
124 
125  if (ta_init(tahandle) == TA_FAILURE) {
126  fprintf(stderr, "error initializing taac\n");
127  exit(1);
128  }
129 
130  ta_set_video(tahandle,TA_VIDEO_MIXEDTAAC,TA_SYNC_EXTERNAL);
131 
132 
133 
134  base_frame = window_create(0, FRAME,
135  FRAME_ICON, &gettaac_icon,
136  FRAME_ARGC_PTR_ARGV, &argc, argv,
137  0);
138 
139  init_panel(base_frame);
140 
141 
142  canvas = window_create(base_frame, CANVAS,
143  WIN_WIDTH, X_PIXELS,
144  WIN_HEIGHT, Y_PIXELS,
145  CANVAS_AUTO_SHRINK, FALSE,
146  0);
147 
148  window_fit(base_frame);
149 
150  ta_taac_canvas(tahandle, base_frame, canvas);
151 
152  window_main_loop(base_frame);
153 
154  ta_close(tahandle);
155 
156 }
157 
158 static void
159 init_panel(base_frame)
160 Frame base_frame;
161 {
162 
163  panel = window_create(base_frame, PANEL,
164  WIN_ROWS, 2,
165  PANEL_LABEL_BOLD, TRUE,
166  0);
167 
168  panel_create_item(panel, PANEL_BUTTON,
169  PANEL_ITEM_X, ATTR_COL(1),
170  PANEL_ITEM_Y, ATTR_ROW(0) + 4,
171  PANEL_LABEL_IMAGE, panel_button_image(panel, "EXIT", 4, 0),
172  PANEL_NOTIFY_PROC, doexit,
173  0);
174 
175  panel_create_item(panel, PANEL_BUTTON,
176  PANEL_ITEM_X, ATTR_COL(8),
177  PANEL_ITEM_Y, ATTR_ROW(0) + 4,
178  PANEL_LABEL_IMAGE, panel_button_image(panel, "LOAD", 4, 0),
179  PANEL_NOTIFY_PROC, read_file,
180  0);
181 
182  panel_create_item(panel, PANEL_CHOICE,
183  PANEL_ITEM_X, ATTR_COL(15),
184  PANEL_ITEM_Y, ATTR_ROW(0) + 4,
185  PANEL_CHOICE_STRINGS, "RGB", "Gray", 0,
186  PANEL_NOTIFY_PROC, format_select,
187  0);
188 
189  gamma_item = panel_create_item(panel, PANEL_TEXT,
190  PANEL_ITEM_X, ATTR_COL(30),
191  PANEL_ITEM_Y, ATTR_ROW(0) + 8,
192  PANEL_VALUE_DISPLAY_LENGTH, 4,
193  PANEL_LABEL_STRING, "Gamma:",
194  PANEL_VALUE, "1.6",
195  0);
196 
197  file_name = panel_create_item(panel, PANEL_TEXT,
198  PANEL_ITEM_X, ATTR_COL(42),
199  PANEL_ITEM_Y, ATTR_ROW(0) + 8,
200  PANEL_LABEL_STRING, "File:",
201  PANEL_VALUE_STORED_LENGTH, MAX_FILE_NAME,
202  PANEL_NOTIFY_PROC, make_new_name,
203  PANEL_VALUE_STORED_LENGTH, MAX_FILE_NAME,
204  PANEL_NOTIFY_STRING, "\033\t\r",
205  PANEL_VALUE_DISPLAY_LENGTH, 14,
206  0);
207 
208  window_set(panel, PANEL_CARET_ITEM, file_name, 0);
209 }
210 
211 
212 
213 /* utilites */
214 
215 static void
216 doexit()
217 {
218 
219  /* close the taac */
220  ta_close(tahandle);
221 
222  exit(0);
223 }
224 
225 static void
226 read_file()
227 {
228 
229  char *filename;
230  char full_path[MAX_FILE_NAME];
231  FILE *fp;
232  rle_hdr hdr;
233 
234  hdr = *rle_hdr_init( (rle_hdr *)NULL );
235 
236  filename = (char *)panel_get_value(file_name);
237 
238  rle_names( &hdr, my_name, filename );
239 
240  if (*filename == '\0') {
241  fp = stdin;
242  } else {
243  get_full_path(filename, full_path);
244  if ((fp = fopen(full_path, "r")) == NULL) {
245  fprintf(stderr, "Can't open file ->%s<-\n", full_path);
246  return;
247  }
248  }
249 
250  hdr.rle_file = fp;
251  rle_get_setup_ok( &hdr, NULL, NULL );
252 
253  if (display_mode == RGB)
254  read_file_rgb(&hdr);
255  else if (display_mode == GRAY)
256  read_file_gray(&hdr);
257  else
258  read_file_rgb(&hdr);
259 
260  fclose (hdr.rle_file);
261 }
262 
263 static void
264 read_file_rgb(hdr)
265 rle_hdr *hdr;
266 {
267 
268  unsigned char *scan[3];
269  register unsigned char *r, *g, *b;
270  register int x, y;
271  unsigned char *rmap, *gmap, *bmap;
272  rle_pixel **in_cmap;
273  double gamma;
274  int x_size, y_size;
275  int i;
276 
277  RLE_CLR_BIT(*hdr, RLE_ALPHA);
278 
279  x_size = (hdr->xmax - hdr->xmin + 1);
280  y_size = (hdr->ymax - hdr->ymin + 1);
281 
282  /* get memory for the scan line data */
283  for (i = 0; i < 3; i++)
284  scan[i] = (unsigned char *) malloc(x_size);
285 
286  hdr->xmax -= hdr->xmin;
287  hdr->xmin = 0;
288 
289  if (sscanf((char *)panel_get_value(gamma_item), "%lf", &gamma) != 1)
290  gamma = 1.0;
291 
292  in_cmap = buildmap( hdr, 3, 1.0 / gamma, 1.0 );
293 
294  rmap = &(in_cmap[0][0]);
295  gmap = &(in_cmap[1][0]);
296  bmap = &(in_cmap[2][0]);
297 
298 
299  /* get memory for the output buffer */
300  scan_line_buffer = (unsigned int *) malloc(x_size * sizeof(unsigned int));
301 
302 
303  for ( i = 0; i < Y_PIXELS; i++)
304  ta_write2d(tahandle, erase, X_PIXELS, 1, 0, i);
305 
306  while ( (y = rle_getrow( hdr, scan )) <= hdr->ymax ) {
307  for(x = 0, r = scan[0], g = scan[1], b = scan[2];
308  x < x_size; x++, r++, g++, b++) {
309  scan_line_buffer[x] = rmap[*r] + (gmap[*g] << 8) + (bmap[*b] << 16);
310  }
311  /* XXX min ? */
312  ta_write2d(tahandle, scan_line_buffer, min(x_size, 1024), 1, 0, y_size - y);
313  }
314 
315  /* be neat and free memory */
316  free(scan_line_buffer);
317  for (i = 0; i < 3; i++)
318  free(scan[i]);
319 }
320 
321 static void
322 read_file_gray(hdr)
323 rle_hdr *hdr;
324 {
325 
326  unsigned char *scan[3];
327  register unsigned char *r, *g, *b;
328  register int x, value;
329  unsigned char *map;
330  rle_pixel **in_cmap;
331  double gamma;
332  int x_size, y_size, y;
333  int i;
334 
335 
336  RLE_CLR_BIT(*hdr, RLE_ALPHA);
337 
338  x_size = (hdr->xmax - hdr->xmin + 1);
339  y_size = (hdr->ymax - hdr->ymin + 1);
340 
341  /* get memory for the scan line data */
342  for (i = 0; i < 3; i++)
343  scan[i] = (unsigned char *) malloc(x_size);
344 
345  hdr->xmax -= hdr->xmin;
346  hdr->xmin = 0;
347 
348  if (sscanf((char *)panel_get_value(gamma_item), "%lf", &gamma) != 1)
349  gamma = 1.0;
350 
351  in_cmap = buildmap( hdr, 1, 1.0 / gamma, 1.0 );
352 
353  map = &(in_cmap[0][0]);
354 
355  /* get memory for the output buffer */
356  scan_line_buffer = (unsigned int *) malloc(x_size * sizeof(unsigned int));
357 
358  for ( i = 0; i < Y_PIXELS; i++)
359  ta_write2d(tahandle, erase, X_PIXELS, 1, 0, i);
360 
361  while ( (y = rle_getrow( hdr, scan )) <= hdr->ymax ) {
362  for(x = 0, r = scan[0], g = scan[1], b = scan[2];
363  x < x_size; x++, r++, g++, b++) {
364  value = (35*(*r)+55*(*g)+10*(*b)) / 100;
365  value = map[value];
366  scan_line_buffer[x] = value + (value << 8) + (value << 16);
367  }
368  /* XXX min ? */
369  ta_write2d(tahandle, scan_line_buffer, min(x_size, 1024), 1, 0, y_size - y);
370 
371  }
372 
373  /* be neat and free memory */
374  free(scan_line_buffer);
375  for (i = 0; i < 3; i++)
376  free(scan[i]);
377 }
378 
379 static void
380 format_select(item, value, event)
381 Panel_item item;
382 int value;
383 Event *event;
384 {
385  if (value == 0)
386  display_mode = RGB;
387  else if (value == 1)
388  display_mode = GRAY;
389  else
390  display_mode = RGB;
391 }
392 
393 
394 
395 /*
396  * the next three functions are from the sunview "touchup" editor
397  *
398  * I have made a few changes to make the code work with gettaac
399  * please forward bugs to me. -ksp
400  */
401 
402 /**************************************************************************
403  Touchup a bitmap graphics editor for the Sun Workstation running SunView
404  Copyright (c) 1988 by Raymond Kreisel
405  1/22/88 @ Suny Stony Brook
406 
407  This program may be redistributed without fee as long as this copyright
408  notice is intact.
409 
410 ==> PLEASE send comments and bug reports to one of the following addresses:
411 
412  Ray Kreisel
413  CS Dept., SUNY at Stony Brook, Stony Brook NY 11794
414 
415  UUCP: {allegra, philabs, pyramid, research}!sbcs!rayk
416  ARPA-Internet: rayk@sbcs.sunysb.edu
417  CSnet: rayk@suny-sb
418  (If nobody is home at any of the above addresses try:
419  S72QKRE@TOWSONVX.BITNET )
420 
421  "If I get home before daylight, I just might get some sleep tonight...."
422 
423 **************************************************************************/
424 /**************************************************************************
425 
426 /*
427  * Let's do file completion on what we have in the file name prompt
428  */
429 static void
430 make_new_name(item, event)
431 Panel_item item;
432 Event *event;
433 {
434  static filename[MAX_FILE_NAME];
435 
436  {
437  strcpy(filename,(char*)panel_get_value(file_name));
438  if (complete(filename))
439  window_bell(panel);
440  panel_set(file_name,PANEL_VALUE,filename,0);
441  }
442 
443 }
444 
445 
446 /* This function, written by Marc J Newberger,
447  * will do both login name completion and file name completion, DAM fast.
448  * That means as fast as the csh does it.
449  */
450 int complete(template)
451 char *template;
452 {
453 
454  char dirName[255];
455  char *prefix;
456  int pref_len;
457  char *suffix;
458  char *p, *q;
459  char first;
460  char nonUnique;
461  char twiddleUserCompletion;
462 
463  struct direct *nameEntry;
464  DIR *dirChan;
465  struct passwd *pwdEntry;
466 
467  /*
468  * First do a little parsing of the input. Separate the
469  * prefix template from the directory if there is one.
470  */
471  twiddleUserCompletion= 0;
472  prefix= template+strlen(template);
473  while (*(--prefix) != '/' && prefix >= template);
474 
475  /*
476  * See if a directory was specified:
477  */
478  if (prefix < template) {
479  /*
480  * No /'s, could be either a username completion or
481  * a completion in the current directory.
482  */
483  if (template[0] == '~') {
484  prefix++;
485  twiddleUserCompletion= 1;
486  }
487  else {
488  strcpy(dirName, ".");
489  }
490  }
491  else if (prefix == template) {
492  /*
493  * Special case !! The directory excluding the trailing
494  * '/' is zero length. It's the root:
495  */
496  strcpy(dirName, "/");
497  }
498  else {
499  /*
500  * We're completing a file in a directory.
501  * The directory may be lead by a ~<username> abbreviation.
502  * If that's the case expand it.
503  */
504  if (template[0] == '~') {
505  /*
506  * We need to do twiddle directory expansion.
507  * See if it's our directory:
508  */
509  if (template[1] == '/') {
510  strcpy(dirName, getenv("HOME"));
511  if ( &template[1] != prefix )
512  {
513  p= dirName+strlen(dirName);
514  q= &template[1];
515  while (q < prefix) {
516  *p= *q;
517  p++, q++;
518  }
519  *p= 0;
520  }
521  }
522  else {
523  /*
524  * It's someone else's. Let our fingers
525  * do the walking. (Why the fuck do they call it
526  * the "yellow pages" anyway. They're white pages
527  * dammit ! If they were YELLOW pages, we could
528  * say ypmatch "Automobile, Dealers, Retail", and
529  * things like that !).
530  */
531  for (p= dirName, q= &template[1];
532  (*p= *q) != '/';
533  p++, q++);
534  *p= 0;
535  if (!(pwdEntry= getpwnam(dirName))) {
536  return errno;
537  }
538  strcpy(dirName, pwdEntry->pw_dir);
539  p= dirName+strlen(dirName);
540  while (q < prefix) {
541  *p= *q;
542  p++, q++;
543  }
544  *p= 0;
545  }
546  }
547  else {
548  /*
549  * It's a vanilla directory. Strip it out.
550  */
551  strncpy(dirName, template, prefix-template);
552  dirName[prefix-template]= 0;
553  }
554  }
555  /*
556  * Bump prefix past the '/'.
557  */
558  prefix++;
559 
560  /*
561  * Get the prefix length and a pointer to the end of the
562  * prefix.
563  */
564  pref_len= strlen(prefix);
565  suffix= template + strlen(template);
566 
567  /*
568  * See whether we're doing filename or username completion:
569  */
570  if (!twiddleUserCompletion) {
571 
572  /*
573  * It's filename completion. Read through the directory:
574  */
575  if ((dirChan= opendir(dirName)) == 0) {
576  return errno;
577  }
578 
579  first= 1;
580  nonUnique= 0;
581  for (;;) {
582  if (!(nameEntry= readdir(dirChan))) {
583  break;
584  }
585  if (!strncmp(prefix, nameEntry->d_name, pref_len)) {
586  /*
587  * We have a file that matches the template.
588  * If it's the first one, we fill the completion
589  * suffix with it. Otherwise we scan and pare down
590  * the suffix.
591  */
592  if (first) {
593  first= 0 ;
594  strcpy(suffix, nameEntry->d_name+pref_len);
595  }
596  else {
597  nonUnique= 1;
598  p= suffix;
599  q= nameEntry->d_name+pref_len;
600  while (*p == *q) {
601  ++p; ++q;
602  }
603  *p= 0;
604 
605  /*
606  * A little optimization: If p == suffix, we
607  * were unable to do any extension of the name.
608  * We might as well quit here.
609  */
610  if (p == suffix) {
611  break;
612  }
613  }
614  }
615  }
616 
617  closedir(dirChan);
618  }
619  else {
620  /*
621  * Do ~Username completion. Start by resetting the passwd file.
622  */
623  setpwent();
624 
625  first= 1;
626  nonUnique= 0;
627  for (;;) {
628  if (!(pwdEntry= getpwent())) {
629  break;
630  }
631  if (!strncmp(prefix, pwdEntry->pw_name, pref_len)) {
632  /*
633  * We have a user that matches the template.
634  * If it's the first one, we fill the completion
635  * suffix with it. Otherwise we scan and pare down
636  * the suffix.
637  */
638  if (first) {
639  first= 0 ;
640  strcpy(suffix, pwdEntry->pw_name+pref_len);
641  }
642  else {
643  p= suffix;
644  q= pwdEntry->pw_name+pref_len;
645  while (*p == *q) {
646  ++p; ++q;
647  }
648 
649  /*
650  * Here there is a possibility of seeing the
651  * same username twice. For this reason, we
652  * only set nonUnique to 1 if we're shortening
653  * the suffix. This means that the new name is
654  * distinct from any name we've seen.
655  */
656  if (*p) {
657  nonUnique= 1;
658  *p= 0;
659  }
660 
661  /*
662  * A little optimization: If p == suffix, we
663  * were unable to do any extension of the name.
664  * We might as well quit here.
665  */
666  if (p == suffix) {
667  break;
668  }
669  }
670  }
671  }
672  }
673 
674  /*
675  * If nothing matched, return a -1, if there was non-uniqueness
676  * return -2.
677  */
678  if (first) {
679  return -1;
680  }
681  else if (nonUnique) {
682  return -2;
683  }
684  else {
685  return 0;
686  }
687 
688 }
689 
690 /*
691  * Take a filename with a ~ character at the begining and return
692  * the full path name to that file
693  */
694 get_full_path(template,full_path)
695 char template[];
696 char full_path[];
697 {
698  char *p, *q;
699  struct passwd *pwdEntry;
700  /*
701  * We're completing a file in a directory.
702  * The directory may be lead by a ~<username> abbreviation.
703  * If that's the case expand it.
704  */
705  if (template[0] == '~') {
706  /*
707  * We need to do twiddle directory expansion.
708  * See if it's our directory:
709  */
710  if (template[1] == '/') {
711  strcpy(full_path, getenv("HOME"));
712  strcat(full_path,&template[1]);
713  }
714  else {
715 
716  /*
717  * It's someone else's. Let our fingers
718  * do the walking. (Why the fuck do they call it
719  * the "yellow pages" anyway. They're white pages
720  * dammit ! If they were YELLOW pages, we could
721  * say ypmatch "Automobile, Dealers, Retail", and
722  * things like that !).
723  */
724  for (p= full_path, q= &template[1];
725  (*p= *q) != '/';
726  p++, q++);
727  *p= 0;
728  if (!(pwdEntry= getpwnam(full_path))) {
729  return errno;
730  }
731  strcpy(full_path, pwdEntry->pw_dir);
732  strcat(full_path,q);
733  }
734  }
735  else
736  strcpy(full_path,template);
737 }
738 
739 /*
740  * end of code from touchup
741  */
int xmin
Definition: rle.h:100
static Panel_item gamma_item
Definition: gettaac.c:60
static Canvas canvas
Definition: gettaac.c:51
char * cmd_name(char **argv)
Definition: cmd_name.c:31
void main(int argc, char **argv)
Definition: aliastorle.c:121
TA_HANDLE * tahandle
Definition: gettaac.c:98
#define RGB
Definition: gettaac.c:88
int rle_getrow(rle_hdr *the_hdr, scanline)
Definition: rle_getrow.c:333
static Frame base_frame
Definition: gettaac.c:50
int ymin
Definition: rle.h:100
static Cursor cursor
Definition: gettaac.c:53
static Pixwin * pw
Definition: gettaac.c:52
#define Y_PIXELS
Definition: gettaac.c:48
#define GRAY
Definition: gettaac.c:89
static int display_mode
Definition: gettaac.c:91
rle_pixel ** buildmap(rle_hdr *the_hdr, int minmap, double orig_gamma, double new_gamma)
Definition: buildmap.c:56
static unsigned char green[256]
Definition: gettaac.c:85
#define X_PIXELS
Definition: gettaac.c:47
int xmax
Definition: rle.h:100
gpr_ $event_t event
Definition: getap.c:105
static char sccsid[]
Definition: gettaac.c:2
static int erase[512]
Definition: gettaac.c:99
static unsigned int * scan_line_buffer
Definition: gettaac.c:75
static unsigned char blue[256]
Definition: gettaac.c:86
#define MAX_FILE_NAME
Definition: gettaac.c:44
void rle_get_setup_ok(rle_hdr *the_hdr, const char *prog_name, const char *file_name)
Definition: rle_getrow.c:254
static Panel_item file_name
Definition: gettaac.c:59
#define RLE_CLR_BIT(glob, bit)
Definition: rle.h:124
int ymax
Definition: rle.h:100
unsigned char rle_pixel
Definition: rle.h:56
#define RLE_ALPHA
Definition: rle.h:65
#define NCMAP
Definition: gettaac.c:46
static Panel panel
Definition: gettaac.c:57
static char * my_name
Definition: gettaac.c:76
#define FONT
Definition: gettaac.c:45
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)
Definition: rle_hdr.c:267
FILE * rle_file
Definition: rle.h:114
static short icon_image[]
Definition: gettaac.c:78
static unsigned char red[256]
Definition: gettaac.c:84