home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Graphics / Viewers / aa_m68k_Intel_Only / ToyViewer1.2 / Source / ToyWinPPM.m < prev    next >
Encoding:
Text File  |  1995-11-12  |  1.8 KB  |  87 lines

  1. #import <appkit/Application.h>
  2. #import <appkit/appkit.h>
  3. #import <sys/file.h>
  4. #import <stdio.h>
  5. #import <stdlib.h>
  6. #import <string.h>
  7. #import "ToyWinPPM.h"
  8. #import "ppm.h"
  9. #import "strfunc.h"
  10.  
  11.  
  12. static FILE *openPipe(const char **list)
  13. {
  14.     int pfd[2];
  15.  
  16.     if (list == NULL || access(list[0], X_OK) < 0)
  17.         return NULL;    /* not executable */
  18.     (void)pipe(pfd);
  19.     if (fork() == 0) { /* child process */
  20.         (void)close(1);
  21.         dup(pfd[1]);
  22.         (void)close(pfd[0]);
  23.         (void)close(pfd[1]);
  24.         execv(list[0], &list[1]);
  25.         exit(1);    /* Error */
  26.     }
  27.     (void)close(pfd[1]);
  28.     return fdopen(pfd[0], "r");
  29. }
  30.  
  31.  
  32. @implementation ToyWinPPM
  33.  
  34. - (int)drawToyWin:(const char *)fileName Type:(int)type Num:(int)num
  35. {
  36.     FILE *fp = NULL;
  37.     commonInfo *cinf = NULL;
  38.     int err = 0;
  39.     const char *ext = NULL;
  40.     unsigned char *map[5];
  41.  
  42.     if (type == Type_ppm)
  43.         fp = fopen(fileName, "r");
  44.     else {
  45.         fp = openPipe(execList);
  46.         ext = (extension && *extension) ? extension : execList[1];
  47.         free((void *)execList);
  48.         execList = NULL;
  49.     }
  50.     if (fp == NULL)
  51.         return Err_OPEN;
  52.  
  53.     if ((cinf = loadPpmHeader(fp, &err)) == NULL) {
  54.         (void)fclose(fp);
  55.         return err;
  56.     }
  57.  
  58.     [self initLocateWindow:fileName
  59.         Width:cinf->width Height:cinf->height Num:num];
  60.  
  61. /* Bitmap data of planes in 'map[]' is one block of memory area.
  62.    map[0] is beginning of the area, and map[1] = map[0] + (size of plane),
  63.    and so on. But, if the image is monochrome, map[1] is NULL.
  64.    The area of map[0] and (commonInfo *)cinf are kept in an object of
  65.    ToyView, and freed by it.
  66. */
  67.     err = ppmGetImage(fp, cinf, map, ext);
  68.     (void)fclose(fp);
  69.     if (err) /* •ä„ö⁄˛⁄ë */
  70.         errAlert(fileName, err);
  71.  
  72.     if ([self drawView:map info: cinf] == nil)
  73.         return -1;
  74.  
  75.     return 0;
  76. }
  77.  
  78. - setExecList: (char **)list ext: (const char *)type
  79.     /* list is free-ed by this obj */
  80. {
  81.     execList = list;
  82.     extension = type;
  83.     return self;
  84. }
  85.  
  86. @end
  87.