home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / imagemagic / part14 < prev    next >
Internet Message Format  |  1991-05-23  |  75KB

  1. Path: uunet!uunet!cs.utexas.edu!sun-barr!newstop!exodus!dupont.com!cristy
  2. From: cristy@dupont.com
  3. Newsgroups: comp.sources.x
  4. Subject: v13i030: ImageMagick - Graphics display programs, Part14/21
  5. Message-ID: <13944@exodus.Eng.Sun.COM>
  6. Date: 24 May 91 03:19:36 GMT
  7. References: <csx-13i017:imagemagic@uunet.UU.NET>
  8. Sender: news@exodus.Eng.Sun.COM
  9. Lines: 2136
  10. Approved: argv@sun.com
  11.  
  12. Submitted-by: cristy@dupont.com
  13. Posting-number: Volume 13, Issue 30
  14. Archive-name: imagemagic/part14
  15.  
  16. #!/bin/sh
  17. # this is img.14 (part 14 of ImageMagick)
  18. # do not concatenate these parts, unpack them in order with /bin/sh
  19. # file ImageMagick/filters/Imakefile continued
  20. #
  21. if test ! -r _shar_seq_.tmp; then
  22.     echo 'Please unpack part 1 first!'
  23.     exit 1
  24. fi
  25. (read Scheck
  26.  if test "$Scheck" != 14; then
  27.     echo Please unpack part "$Scheck" next!
  28.     exit 1
  29.  else
  30.     exit 0
  31.  fi
  32. ) < _shar_seq_.tmp || exit 1
  33. if test ! -f _shar_wnt_.tmp; then
  34.     echo 'x - still skipping ImageMagick/filters/Imakefile'
  35. else
  36. echo 'x - continuing file ImageMagick/filters/Imakefile'
  37. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/filters/Imakefile' &&
  38. EXTRA_LIBRARIES= -lX11 -lm
  39. PPM_LIBRARIES= /usr/local/lib/libppm.a /usr/local/lib/libpgm.a  \
  40. X  /usr/local/lib/libpbm.a
  41. TIFF_LIBRARIES= /usr/local/lib/libtiff.a
  42. X
  43. AVStoMIFFSources= AVStoMIFF.c
  44. AVStoMIFFObjects= AVStoMIFF.o image.o compress.o
  45. GIFtoMIFFSources= GIFtoMIFF.c
  46. GIFtoMIFFObjects= GIFtoMIFF.o image.o compress.o
  47. GRAYtoMIFFSources= GRAYtoMIFF.c
  48. GRAYtoMIFFObjects= GRAYtoMIFF.o image.o compress.o
  49. MIFFtoAVSSources= MIFFtoAVS.c
  50. MIFFtoAVSObjects= MIFFtoAVS.o image.o compress.o
  51. MIFFtoGIFSources= MIFFtoGIF.c
  52. MIFFtoGIFObjects= MIFFtoGIF.o image.o kolb.o quantize.o compress.o
  53. MIFFtoGRAYSources= MIFFtoGRAY.c
  54. MIFFtoGRAYObjects= MIFFtoGRAY.o image.o kolb.o quantize.o compress.o
  55. MIFFtoPPMSources= MIFFtoPPM.c
  56. MIFFtoPPMObjects= MIFFtoPPM.o image.o compress.o
  57. MIFFtoRGBSources= MIFFtoRGB.c
  58. MIFFtoRGBObjects= MIFFtoRGB.o image.o compress.o
  59. MIFFtoSUNSources= MIFFtoSUN.c
  60. MIFFtoSUNObjects= MIFFtoSUN.o image.o compress.o
  61. MIFFtoTIFFSources= MIFFtoTIFF.c
  62. MIFFtoTIFFObjects= MIFFtoTIFF.o image.o compress.o
  63. MIFFtoXBMSources= MIFFtoXBM.c
  64. MIFFtoXBMObjects= MIFFtoXBM.o image.o kolb.o quantize.o compress.o
  65. MIFFtoXWDSources= MIFFtoXWD.c
  66. MIFFtoXWDObjects= MIFFtoXWD.o image.o compress.o
  67. MTVtoMIFFSources= MTVtoMIFF.c
  68. MTVtoMIFFObjects= MTVtoMIFF.o image.o compress.o
  69. SUNtoMIFFSources= SUNtoMIFF.c
  70. SUNtoMIFFObjects= SUNtoMIFF.o image.o compress.o
  71. RGBtoMIFFSources= RGBtoMIFF.c
  72. RGBtoMIFFObjects= RGBtoMIFF.o image.o compress.o
  73. PPMtoMIFFSources= PPMtoMIFF.c
  74. PPMtoMIFFObjects= PPMtoMIFF.o image.o kolb.o quantize.o colors.o compress.o
  75. TIFFtoMIFFSources= TIFFtoMIFF.c
  76. TIFFtoMIFFObjects= TIFFtoMIFF.o image.o kolb.o quantize.o compress.o
  77. XXBMtoMIFFSources= XBMtoMIFF.c
  78. XXBMtoMIFFObjects= XBMtoMIFF.o image.o compress.o
  79. XXWDtoMIFFSources= XWDtoMIFF.c
  80. XXWDtoMIFFObjects= XWDtoMIFF.o image.o compress.o
  81. X
  82. PROGRAMS= AVStoMIFF MIFFtoAVS GIFtoMIFF MIFFtoGIF GRAYtoMIFF MIFFtoGRAY  \
  83. X  MTVtoMIFF RGBtoMIFF MIFFtoRGB SUNtoMIFF MIFFtoSUN XBMtoMIFF MIFFtoXBM  \
  84. X  XWDtoMIFF MIFFtoXWD PPMtoMIFF MIFFtoPPM TIFFtoMIFF MIFFtoTIFF
  85. X
  86. AllTarget($(PROGRAMS))
  87. X
  88. NormalProgramTarget(AVStoMIFF,$(AVStoMIFFObjects), , , )
  89. InstallProgram(AVStoMIFF,$(BINDIR))
  90. NormalProgramTarget(MIFFtoAVS,$(MIFFtoAVSObjects), , , )
  91. InstallProgram(MIFFtoAVS,$(BINDIR))
  92. NormalProgramTarget(GIFtoMIFF,$(GIFtoMIFFObjects), , , )
  93. InstallProgram(GIFtoMIFF,$(BINDIR))
  94. NormalProgramTarget(MIFFtoGIF,$(MIFFtoGIFObjects), , , )
  95. InstallProgram(MIFFtoGIF,$(BINDIR))
  96. NormalProgramTarget(GRAYtoMIFF,$(GRAYtoMIFFObjects), , , )
  97. InstallProgram(GRAYtoMIFF,$(BINDIR))
  98. NormalProgramTarget(MIFFtoGRAY,$(MIFFtoGRAYObjects), , , )
  99. InstallProgram(MIFFtoGRAY,$(BINDIR))
  100. NormalProgramTarget(MTVtoMIFF,$(MTVtoMIFFObjects), , , )
  101. InstallProgram(MTVtoMIFF,$(BINDIR))
  102. NormalProgramTarget(RGBtoMIFF,$(RGBtoMIFFObjects), , , )
  103. InstallProgram(RGBtoMIFF,$(BINDIR))
  104. NormalProgramTarget(MIFFtoRGB,$(MIFFtoRGBObjects), , , )
  105. InstallProgram(MIFFtoRGB,$(BINDIR))
  106. NormalProgramTarget(SUNtoMIFF,$(SUNtoMIFFObjects), , , )
  107. InstallProgram(SUNtoMIFF,$(BINDIR))
  108. NormalProgramTarget(MIFFtoSUN,$(MIFFtoSUNObjects), , , )
  109. InstallProgram(MIFFtoSUN,$(BINDIR))
  110. NormalProgramTarget(XBMtoMIFF,$(XBMtoMIFFObjects), , , )
  111. InstallProgram(XBMtoMIFF,$(BINDIR))
  112. NormalProgramTarget(MIFFtoXBM,$(MIFFtoXBMObjects), , , )
  113. InstallProgram(MIFFtoXBM,$(BINDIR))
  114. NormalProgramTarget(XWDtoMIFF,$(XWDtoMIFFObjects), , , )
  115. InstallProgram(XWDtoMIFF,$(BINDIR))
  116. NormalProgramTarget(MIFFtoXWD,$(MIFFtoXWDObjects), , , )
  117. InstallProgram(MIFFtoXWD,$(BINDIR))
  118. NormalProgramTarget(PPMtoMIFF,$(PPMtoMIFFObjects), ,$(PPM_LIBRARIES), )
  119. InstallProgram(PPMtoMIFF,$(BINDIR))
  120. NormalProgramTarget(MIFFtoPPM,$(MIFFtoPPMObjects), ,$(PPM_LIBRARIES), )
  121. InstallProgram(MIFFtoPPM,$(BINDIR))
  122. NormalProgramTarget(TIFFtoMIFF,$(TIFFtoMIFFObjects), ,$(TIFF_LIBRARIES), )
  123. InstallProgram(TIFFtoMIFF,$(BINDIR))
  124. NormalProgramTarget(MIFFtoTIFF,$(MIFFtoTIFFObjects), ,$(TIFF_LIBRARIES), )
  125. InstallProgram(MIFFtoTIFF,$(BINDIR))
  126. DependTarget()
  127. X
  128. image.o:
  129. X    $(CC) $(CFLAGS) -c ../image.c
  130. X
  131. kolb.o:
  132. X    $(CC) $(CFLAGS) -c ../kolb.c
  133. X
  134. quantize.o:
  135. X    $(CC) $(CFLAGS) -c ../quantize.c
  136. X
  137. colors.o:
  138. X    $(CC) $(CFLAGS) -c ../colors.c
  139. X
  140. compress.o:
  141. X    $(CC) $(CFLAGS) -c ../compress.c
  142. SHAR_EOF
  143. echo 'File ImageMagick/filters/Imakefile is complete' &&
  144. chmod 0755 ImageMagick/filters/Imakefile ||
  145. echo 'restore of ImageMagick/filters/Imakefile failed'
  146. Wc_c="`wc -c < 'ImageMagick/filters/Imakefile'`"
  147. test 4132 -eq "$Wc_c" ||
  148.     echo 'ImageMagick/filters/Imakefile: original size 4132, current size' "$Wc_c"
  149. rm -f _shar_wnt_.tmp
  150. fi
  151. # ============= ImageMagick/filters/MIFFtoXBM.c ==============
  152. if test -f 'ImageMagick/filters/MIFFtoXBM.c' -a X"$1" != X"-c"; then
  153.     echo 'x - skipping ImageMagick/filters/MIFFtoXBM.c (File already exists)'
  154.     rm -f _shar_wnt_.tmp
  155. else
  156. > _shar_wnt_.tmp
  157. echo 'x - extracting ImageMagick/filters/MIFFtoXBM.c (Text)'
  158. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/MIFFtoXBM.c' &&
  159. /*
  160. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  161. %                                                                             %
  162. %                                                                             %
  163. %                                                                             %
  164. %                 EEEEE  X   X  PPPP    OOO   RRRR    TTTTT                   %
  165. %                 E       X X   P   P  O   O  R   R     T                     %
  166. %                 EEE      X    PPPP   O   O  RRRR      T                     %
  167. %                 E       X X   P      O   O  R R       T                     %
  168. %                 EEEEE  X   X  P       OOO   R  R      T                     %
  169. %                                                                             %
  170. %                                                                             %
  171. %                   Export MIFF image to a X bitmap format.                   %
  172. %                                                                             %
  173. %                                                                             %
  174. %                                                                             %
  175. %                           Software Design                                   %
  176. %                             John Cristy                                     %
  177. %                            January  1991                                    %
  178. %                                                                             %
  179. %                                                                             %
  180. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  181. %                                                                             %
  182. %  Permission to use, copy, modify, distribute, and sell this software and    %
  183. %  its documentation for any purpose is hereby granted without fee,           %
  184. %  provided that the above Copyright notice appear in all copies and that     %
  185. %  both that Copyright notice and this permission notice appear in            %
  186. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  187. %  & Company not be used in advertising or publicity pertaining to            %
  188. %  distribution of the software without specific, written prior               %
  189. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  190. %  about the suitability of this software for any purpose.  It is provided    %
  191. %  "as is" without express or implied warranty.                               %
  192. %                                                                             %
  193. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  194. %  to this software, including all implied warranties of merchantability      %
  195. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  196. %  liable for any special, indirect or consequential damages or any           %
  197. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  198. %  in an action of contract, negligence or other tortious action, arising     %
  199. %  out of or in connection with the use or performance of this software.      %
  200. %                                                                             %
  201. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  202. %
  203. %  Command syntax:
  204. %
  205. %  export image.miff image.xbm
  206. %
  207. %  Specify 'image.miff' as '-' for standard input.  
  208. %  Specify 'image.xbm' as '-' for standard output.
  209. %
  210. %
  211. */
  212. X
  213. #include <string.h>
  214. #include "display.h"
  215. #include "image.h"
  216. X
  217. /*
  218. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  219. %                                                                             %
  220. %                                                                             %
  221. %                                                                             %
  222. %   E r r o r                                                                 %
  223. %                                                                             %
  224. %                                                                             %
  225. %                                                                             %
  226. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  227. %
  228. %  Function Error displays an error message and then terminates the program.
  229. %
  230. %  The format of the Error routine is:
  231. %
  232. %      Error(message,qualifier)
  233. %
  234. %  A description of each parameter follows:
  235. %
  236. %    o message:  Specifies the message to display before terminating the
  237. %      program.
  238. %
  239. %    o qualifier:  Specifies any qualifier to the message.
  240. %
  241. %
  242. */
  243. void Error(message,qualifier)
  244. char
  245. X  *message,
  246. X  *qualifier;
  247. {
  248. X  (void) fprintf(stderr,"%s: %s",application_name,message); 
  249. X  if (qualifier != (char *) NULL)
  250. X    (void) fprintf(stderr," %s",qualifier);
  251. X  (void) fprintf(stderr,".\n");
  252. X  exit(1);
  253. }
  254. X
  255. /*
  256. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  257. %                                                                             %
  258. %                                                                             %
  259. %                                                                             %
  260. %   U s a g e                                                                 %
  261. %                                                                             %
  262. %                                                                             %
  263. %                                                                             %
  264. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  265. %
  266. %  Procedure Usage displays the program usage;
  267. %
  268. %  The format of the Usage routine is:
  269. %
  270. %      Usage(message)
  271. %
  272. %  A description of each parameter follows:
  273. %
  274. %    message:  Specifies a specific message to display to the user.
  275. %
  276. */
  277. static void Usage(message)
  278. char
  279. X  *message;
  280. {
  281. X  if (message != (char *) NULL)
  282. X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
  283. X  (void) fprintf(stderr,"Usage: %s image.miff image.xbm\n\n",application_name);
  284. X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard input.\n");
  285. X  (void) fprintf(stderr,"Specify 'image.xbm' as '-' for standard output.\n");
  286. X  exit(1);
  287. }
  288. X
  289. /*
  290. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  291. %                                                                             %
  292. %                                                                             %
  293. %                                                                             %
  294. %   W r i t e X B M I m a g e                                                 %
  295. %                                                                             %
  296. %                                                                             %
  297. %                                                                             %
  298. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  299. %
  300. %  Procedure WriteXBMImage writes an image to a file on disk in X
  301. %  bitmap format.
  302. %
  303. %  The format of the WriteXBMImage routine is:
  304. %
  305. %      WriteXBMImage(image)
  306. %
  307. %  A description of each parameter follows.
  308. %
  309. %    o image:  A pointer to a Image structure.
  310. %
  311. %
  312. */
  313. static void WriteXBMImage(image)
  314. Image
  315. X  *image;
  316. {
  317. X  char
  318. X    name[256];
  319. X
  320. X  register int
  321. X    i,
  322. X    j,
  323. X    x;
  324. X
  325. X  register char
  326. X    *q;
  327. X
  328. X  register RunlengthPacket
  329. X    *p;
  330. X
  331. X  register unsigned char
  332. X    bit,
  333. X    byte;
  334. X
  335. X  register unsigned short
  336. X    foreground_pixel;
  337. X
  338. X  unsigned int
  339. X    count;
  340. X
  341. X  /*
  342. X    Open output image file.
  343. X  */
  344. X  if (*image->filename == '-')
  345. X    image->file=stdout;
  346. X  else
  347. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  348. X      image->file=fopen(image->filename,"w");
  349. X    else
  350. X      {
  351. X        char
  352. X          command[256];
  353. X
  354. X        /*
  355. X          Image file is compressed-- uncompress it.
  356. X        */
  357. X        (void) sprintf(command,"compress -c > %s",image->filename);
  358. X        image->file=(FILE *) popen(command,"w");
  359. X      }
  360. X  if (image->file == (FILE *) NULL)
  361. X    Error("unable to open file",image->filename);
  362. X  /*
  363. X    Write X bitmap header.
  364. X  */
  365. X  (void) strcpy(name,image->filename);
  366. X  q=strchr(name,'.');
  367. X  if (q)
  368. X    *q=(char) NULL;
  369. X  (void) fprintf(image->file,"#define %s_width %d\n",name,image->columns);
  370. X  (void) fprintf(image->file,"#define %s_height %d\n",name,image->rows);
  371. X  (void) fprintf(image->file,"static char %s_bits[] = {\n",name);
  372. X  (void) fprintf(image->file," ");
  373. X  /*
  374. X    Convert MIFF to monochrome.
  375. X  */
  376. X  (void) GrayImage(image);
  377. X  QuantizeImage(image,2,0,True,True);
  378. X  /*
  379. X    Convert MIFF to X bitmap pixels.
  380. X  */
  381. X  p=image->pixels;
  382. X  foreground_pixel=(Intensity(image->colormap[0]) > 
  383. X    Intensity(image->colormap[1]) ? 0 : 1);
  384. X  bit=0;
  385. X  byte=0;
  386. X  count=0;
  387. X  x=0;
  388. X  (void) fprintf(image->file," ");
  389. X  for (i=0; i < image->packets; i++)
  390. X  {
  391. X    for (j=0; j <= p->length; j++)
  392. X    {
  393. X      byte>>=1;
  394. X      if (p->index == foreground_pixel)
  395. X        byte|=0x80;
  396. X      bit++;
  397. X      if (bit == 8)
  398. X        {
  399. X          /*
  400. X            Write a bitmap byte to the image file.
  401. X          */
  402. X          (void) fprintf(image->file,"0x%02x, ",~byte & 0xff);
  403. X          count++;
  404. X          if (count == 12)
  405. X            {
  406. X              (void) fprintf(image->file,"\n  ");
  407. X              count=0;
  408. X            };
  409. X          bit=0;
  410. X          byte=0;
  411. X        }
  412. X      x++;
  413. X      if (x == image->columns)
  414. X        {
  415. X          if (bit > 0)
  416. X            {
  417. X              /*
  418. X                Write a bitmap byte to the image file.
  419. X              */
  420. X              byte>>=(8-bit);
  421. X              (void) fprintf(image->file,"0x%02x, ",~byte & 0xff);
  422. X              count++;
  423. X              if (count == 12)
  424. X                {
  425. X                  (void) fprintf(image->file,"\n  ");
  426. X                  count=0;
  427. X                };
  428. X              bit=0;
  429. X              byte=0;
  430. X            };
  431. X          x=0;
  432. X        }
  433. X    }
  434. X    p++;
  435. X  }
  436. X  fprintf(image->file,"};\n");
  437. X  if (image->file != stdin)
  438. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  439. X      (void) fclose(image->file);
  440. X    else
  441. X      (void) pclose(image->file);
  442. }
  443. X
  444. /*
  445. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  446. %                                                                             %
  447. %                                                                             %
  448. %                                                                             %
  449. %    M a i n                                                                  %
  450. %                                                                             %
  451. %                                                                             %
  452. %                                                                             %
  453. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  454. %
  455. %
  456. */
  457. int main(argc,argv)
  458. int
  459. X  argc;
  460. X
  461. char
  462. X  *argv[];
  463. {
  464. X  Image
  465. X    *image;
  466. X
  467. X  application_name=argv[0];
  468. X  if (argc < 3)
  469. X    Usage((char *) NULL);
  470. X  image=ReadImage(argv[1]);
  471. X  if (image == (Image *) NULL)
  472. X    exit(1);
  473. X  (void) strcpy(image->filename,argv[2]);
  474. X  WriteXBMImage(image);
  475. X  (void) fprintf(stderr,"%s=> %s  %dx%d\n",argv[1],argv[2],image->columns,
  476. X    image->rows);
  477. X  return(False);
  478. }
  479. SHAR_EOF
  480. chmod 0755 ImageMagick/filters/MIFFtoXBM.c ||
  481. echo 'restore of ImageMagick/filters/MIFFtoXBM.c failed'
  482. Wc_c="`wc -c < 'ImageMagick/filters/MIFFtoXBM.c'`"
  483. test 11243 -eq "$Wc_c" ||
  484.     echo 'ImageMagick/filters/MIFFtoXBM.c: original size 11243, current size' "$Wc_c"
  485. rm -f _shar_wnt_.tmp
  486. fi
  487. # ============= ImageMagick/filters/XBMtoMIFF.c ==============
  488. if test -f 'ImageMagick/filters/XBMtoMIFF.c' -a X"$1" != X"-c"; then
  489.     echo 'x - skipping ImageMagick/filters/XBMtoMIFF.c (File already exists)'
  490.     rm -f _shar_wnt_.tmp
  491. else
  492. > _shar_wnt_.tmp
  493. echo 'x - extracting ImageMagick/filters/XBMtoMIFF.c (Text)'
  494. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/XBMtoMIFF.c' &&
  495. /*
  496. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  497. %                                                                             %
  498. %                                                                             %
  499. %                                                                             %
  500. %                 IIIII  M   M  PPPP    OOO   RRRR    TTTTT                   %
  501. %                   I    MM MM  P   P  O   O  R   R     T                     %
  502. %                   I    M M M  PPPP   O   O  RRRR      T                     %
  503. %                   I    M   M  P      O   O  R R       T                     %
  504. %                 IIIII  M   M  P       OOO   R  R      T                     % 
  505. %                                                                             %
  506. %                                                                             %
  507. %                  Import X bitmap image to a MIFF format.                    %
  508. %                                                                             %
  509. %                                                                             %
  510. %                                                                             %
  511. %                           Software Design                                   %
  512. %                             John Cristy                                     %
  513. %                            January  1991                                    %
  514. %                                                                             %
  515. %                                                                             %
  516. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  517. %                                                                             %
  518. %  Permission to use, copy, modify, distribute, and sell this software and    %
  519. %  its documentation for any purpose is hereby granted without fee,           %
  520. %  provided that the above Copyright notice appear in all copies and that     %
  521. %  both that Copyright notice and this permission notice appear in            %
  522. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  523. %  & Company not be used in advertising or publicity pertaining to            %
  524. %  distribution of the software without specific, written prior               %
  525. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  526. %  about the suitability of this software for any purpose.  It is provided    %
  527. %  "as is" without express or implied warranty.                               %
  528. %                                                                             %
  529. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  530. %  to this software, including all implied warranties of merchantability      %
  531. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  532. %  liable for any special, indirect or consequential damages or any           %
  533. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  534. %  in an action of contract, negligence or other tortious action, arising     %
  535. %  out of or in connection with the use or performance of this software.      %
  536. %                                                                             %
  537. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  538. %  Command syntax:
  539. %
  540. %  import [-scene #] image.xbm image.miff
  541. %  
  542. %  Specify 'image.xbm' as '-' for standard input.  
  543. %  Specify 'image.miff' as '-' for standard output.
  544. %  
  545. %
  546. */
  547. X
  548. #include <string.h>
  549. #include "display.h"
  550. #include "image.h"
  551. X
  552. /*
  553. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  554. %                                                                             %
  555. %                                                                             %
  556. %                                                                             %
  557. %   E r r o r                                                                 %
  558. %                                                                             %
  559. %                                                                             %
  560. %                                                                             %
  561. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  562. %
  563. %  Function Error displays an error message and then terminates the program.
  564. %
  565. %  The format of the Error routine is:
  566. %
  567. %      Error(message,qualifier)
  568. %
  569. %  A description of each parameter follows:
  570. %
  571. %    o message:  Specifies the message to display before terminating the
  572. %      program.
  573. %
  574. %    o qualifier:  Specifies any qualifier to the message.
  575. %
  576. %
  577. */
  578. void Error(message,qualifier)
  579. char
  580. X  *message,
  581. X  *qualifier;
  582. {
  583. X  (void) fprintf(stderr,"%s: %s",application_name,message); 
  584. X  if (qualifier != (char *) NULL)
  585. X    (void) fprintf(stderr," %s",qualifier);
  586. X  (void) fprintf(stderr,".\n");
  587. X  exit(1);
  588. }
  589. X
  590. /*
  591. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  592. %                                                                             %
  593. %                                                                             %
  594. %                                                                             %
  595. %  R e a d X B M I m a g e                                                    %
  596. %                                                                             %
  597. %                                                                             %
  598. %                                                                             %
  599. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  600. %
  601. %  Function ReadImage reads an image file and returns it.  It allocates the 
  602. %  memory necessary for the new Image structure and returns a pointer to the 
  603. %  new image.
  604. %
  605. %  The format of the ReadImage routine is:
  606. %
  607. %      image=ReadImage(filename)
  608. %
  609. %  A description of each parameter follows:
  610. %
  611. %    o image:  Function ReadImage returns a pointer to the image after 
  612. %      reading.  A null image is returned if there is a a memory shortage or 
  613. %      if the image cannot be read.
  614. %
  615. %    o filename:  Specifies the name of the image to read.
  616. %
  617. %
  618. */
  619. static Image *ReadXBMImage(filename)
  620. char
  621. X  *filename;
  622. {
  623. #define MaxLineLength  256
  624. X
  625. X  char
  626. X    data[MaxLineLength];
  627. X
  628. X  Image
  629. X    *image;
  630. X
  631. X  int
  632. X    byte;
  633. X
  634. X  register int
  635. X    x,
  636. X    y;
  637. X
  638. X  register RunlengthPacket
  639. X    *p;
  640. X
  641. X  register unsigned char
  642. X    bit,
  643. X    blue,
  644. X    green,
  645. X    red;
  646. X
  647. X  register unsigned short
  648. X    index;
  649. X
  650. X  unsigned int
  651. X    value;
  652. X
  653. X  /*
  654. X    Allocate image structure.
  655. X  */
  656. X  image=(Image *) malloc(sizeof(Image));
  657. X  if (image == (Image *) NULL)
  658. X    Error("memory allocation error",(char *) NULL);
  659. X  /*
  660. X    Initialize Image structure.
  661. X  */
  662. X  image->id=UnknownId;
  663. X  image->class=PseudoClass;
  664. X  image->compression=RunlengthEncodedCompression;
  665. X  image->columns=0;
  666. X  image->rows=0;
  667. X  image->packets=0;
  668. X  image->colors=0;
  669. X  image->scene=0;
  670. X  image->colormap=(ColorPacket *) NULL;
  671. X  image->pixels=(RunlengthPacket *) NULL;
  672. X  image->comments=(char *) NULL;
  673. X  /*
  674. X    Open image file.
  675. X  */
  676. X  (void) strcpy(image->filename,filename);
  677. X  if (*image->filename == '-')
  678. X    image->file=stdin;
  679. X  else
  680. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  681. X      image->file=fopen(image->filename,"r");
  682. X    else
  683. X      {
  684. X        char
  685. X          command[256];
  686. X
  687. X        /*
  688. X          Image file is compressed-- uncompress it.
  689. X        */
  690. X        (void) sprintf(command,"uncompress -c %s",image->filename);
  691. X        image->file=(FILE *) popen(command,"r");
  692. X      }
  693. X  if (image->file == (FILE *) NULL)
  694. X    Error("unable to open file",image->filename);
  695. X  /*
  696. X    Read X bitmap header.
  697. X  */
  698. X  while (!feof(image->file))
  699. X  {
  700. X    (void) fgets(data,MaxLineLength,image->file);
  701. X    if (sscanf(data,"#define %*32[a-zA-Z0-9]_width %d",&value) == 1)
  702. X      image->columns=value;
  703. X    if (sscanf(data,"#define %*32[a-zA-Z0-9]_height %d",&value) == 1)
  704. X      image->rows=value;
  705. X    if (sscanf(data,"%*[^#] char"))
  706. X      break;
  707. X  }
  708. X  if ((image->columns == 0) || (image->rows == 0))
  709. X    Error("XBM file is not in the correct format",image->filename);
  710. X  /*
  711. X    Create image.
  712. X  */
  713. X  image->pixels=(RunlengthPacket *) 
  714. X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
  715. X  if (image->pixels == (RunlengthPacket *) NULL)
  716. X    Error("memory allocation error",(char *) NULL);
  717. X  /*
  718. X    Create colormap.
  719. X  */
  720. X  image->colors=2;
  721. X  image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  722. X  if (image->colormap == (ColorPacket *) NULL)
  723. X    Error("memory allocation error",(char *) NULL);
  724. X  image->colormap[0].red=0;
  725. X  image->colormap[0].green=0;
  726. X  image->colormap[0].blue=0;
  727. X  image->colormap[1].red=255;
  728. X  image->colormap[1].green=255;
  729. X  image->colormap[1].blue=255;
  730. X  /*
  731. X    Initial image comment.
  732. X  */
  733. X  image->comments=(char *) malloc((unsigned int) strlen(image->filename)+256);
  734. X  if (image->comments == (char *) NULL)
  735. X    Error("unable to allocate memory",(char *) NULL);
  736. X  (void) sprintf(image->comments,"\n  Imported from X11 bitmap file:  %s\n\0",
  737. X    image->filename);
  738. X  /*
  739. X    Convert X bitmap image to runlength-encoded packets.
  740. X  */
  741. X  image->packets=0;
  742. X  p=image->pixels;
  743. X  p->length=MaxRunlength;
  744. X  for (y=0; y < image->rows; y++)
  745. X  {
  746. X    bit=0;
  747. X    for (x=0; x < image->columns; x++)
  748. X    {
  749. X      if (bit == 0)
  750. X        (void) fscanf(image->file,"%i,",&byte);
  751. X      index=(byte & 0x01) ? 0 : 1;
  752. X      byte>>=1;
  753. X      red=image->colormap[index].red;
  754. X      green=image->colormap[index].green;
  755. X      blue=image->colormap[index].blue;
  756. X      if ((red == p->red) && (green == p->green) && (blue == p->blue) &&
  757. X          (p->length < MaxRunlength))
  758. X        p->length++;
  759. X      else
  760. X        {
  761. X          if (image->packets > 0)
  762. X            p++;
  763. X          image->packets++;
  764. X          p->red=red;
  765. X          p->green=green;
  766. X          p->blue=blue;
  767. X          p->index=index;
  768. X          p->length=0;
  769. X        }
  770. X      bit++;
  771. X      if (bit == 8)
  772. X        bit=0;
  773. X    }
  774. X  }
  775. X  if (image->file != stdin)
  776. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  777. X      (void) fclose(image->file);
  778. X    else
  779. X      (void) pclose(image->file);
  780. X  if (image->packets > ((image->columns*image->rows*3) >> 2))
  781. X    image->compression=NoCompression;
  782. X  image->pixels=(RunlengthPacket *)
  783. X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
  784. X  return(image);
  785. }
  786. X
  787. /*
  788. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  789. %                                                                             %
  790. %                                                                             %
  791. %                                                                             %
  792. %   U s a g e                                                                 %
  793. %                                                                             %
  794. %                                                                             %
  795. %                                                                             %
  796. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  797. %
  798. %  Function Usage displays the program usage;
  799. %
  800. %  The format of the Usage routine is:
  801. %
  802. %      Usage(message)
  803. %
  804. %  A description of each parameter follows:
  805. %
  806. %    message:  Specifies a specific message to display to the user.
  807. %
  808. */
  809. static void Usage(message)
  810. char
  811. X  *message;
  812. {
  813. X  if (message != (char *) NULL)
  814. X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
  815. X  (void) fprintf(stderr,
  816. X    "Usage: %s [-scene] width height image.xbm image.miff\n",application_name);
  817. X  (void) fprintf(stderr,"\nSpecify 'image.xbm' as '-' for standard input.\n");
  818. X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard output.\n");
  819. X  exit(1);
  820. }
  821. X
  822. /*
  823. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  824. %                                                                             %
  825. %                                                                             %
  826. %                                                                             %
  827. %  M a i n                                                                    %
  828. %                                                                             %
  829. %                                                                             %
  830. %                                                                             %
  831. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  832. %
  833. %
  834. */
  835. int main(argc,argv)
  836. int
  837. X  argc;
  838. X   
  839. char
  840. X  *argv[];
  841. {
  842. X  char
  843. X    filename[256];
  844. X
  845. X  Image
  846. X    *image;
  847. X
  848. X  int
  849. X    i;
  850. X
  851. X  unsigned int
  852. X    scene;
  853. X
  854. X  /*
  855. X    Initialize program variables.
  856. X  */
  857. X  application_name=argv[0];
  858. X  i=1;
  859. X  scene=0;
  860. X  if (argc < 3)
  861. X    Usage((char *) NULL);
  862. X  /*
  863. X    Read image and convert to MIFF format.
  864. X  */
  865. X  if (strncmp(argv[i],"-scene",2) == 0)
  866. X    {
  867. X      i++;
  868. X      scene=atoi(argv[i++]);
  869. X    }
  870. X  (void) strcpy(filename,argv[i++]);
  871. X  image=ReadXBMImage(filename);
  872. X  if (image == (Image *) NULL)
  873. X    exit(1);
  874. X  (void) strcpy(image->filename,argv[i++]);
  875. X  image->scene=scene;
  876. X  (void) WriteImage(image);
  877. X  (void) fprintf(stderr,"%s => %s  %dx%d\n",filename,image->filename,
  878. X    image->columns,image->rows);
  879. X  DestroyImage(image);
  880. X  return(False);
  881. }
  882. SHAR_EOF
  883. chmod 0755 ImageMagick/filters/XBMtoMIFF.c ||
  884. echo 'restore of ImageMagick/filters/XBMtoMIFF.c failed'
  885. Wc_c="`wc -c < 'ImageMagick/filters/XBMtoMIFF.c'`"
  886. test 13275 -eq "$Wc_c" ||
  887.     echo 'ImageMagick/filters/XBMtoMIFF.c: original size 13275, current size' "$Wc_c"
  888. rm -f _shar_wnt_.tmp
  889. fi
  890. # ============= ImageMagick/filters/PPMtoMIFF.c ==============
  891. if test -f 'ImageMagick/filters/PPMtoMIFF.c' -a X"$1" != X"-c"; then
  892.     echo 'x - skipping ImageMagick/filters/PPMtoMIFF.c (File already exists)'
  893.     rm -f _shar_wnt_.tmp
  894. else
  895. > _shar_wnt_.tmp
  896. echo 'x - extracting ImageMagick/filters/PPMtoMIFF.c (Text)'
  897. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/PPMtoMIFF.c' &&
  898. /*
  899. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  900. %                                                                             %
  901. %                                                                             %
  902. %                                                                             %
  903. %                 IIIII  M   M  PPPP    OOO   RRRR    TTTTT                   %
  904. %                   I    MM MM  P   P  O   O  R   R     T                     %
  905. %                   I    M M M  PPPP   O   O  RRRR      T                     %
  906. %                   I    M   M  P      O   O  R R       T                     %
  907. %                 IIIII  M   M  P       OOO   R  R      T                     % 
  908. %                                                                             %
  909. %                                                                             %
  910. %                 Import PPM raster image to a MIFF format.                   %
  911. %                                                                             %
  912. %                                                                             %
  913. %                                                                             %
  914. %                           Software Design                                   %
  915. %                             John Cristy                                     %
  916. %                            January  1991                                    %
  917. %                                                                             %
  918. %                                                                             %
  919. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  920. %                                                                             %
  921. %  Permission to use, copy, modify, distribute, and sell this software and    %
  922. %  its documentation for any purpose is hereby granted without fee,           %
  923. %  provided that the above Copyright notice appear in all copies and that     %
  924. %  both that Copyright notice and this permission notice appear in            %
  925. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  926. %  & Company not be used in advertising or publicity pertaining to            %
  927. %  distribution of the software without specific, written prior               %
  928. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  929. %  about the suitability of this software for any purpose.  It is provided    %
  930. %  "as is" without express or implied warranty.                               %
  931. %                                                                             %
  932. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  933. %  to this software, including all implied warranties of merchantability      %
  934. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  935. %  liable for any special, indirect or consequential damages or any           %
  936. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  937. %  in an action of contract, negligence or other tortious action, arising     %
  938. %  out of or in connection with the use or performance of this software.      %
  939. %                                                                             %
  940. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  941. %  Command syntax:
  942. %
  943. %  import [-scene #] image.ppm image.miff
  944. %  
  945. %  Specify 'image.ppm' as '-' for standard input.  
  946. %  Specify 'image.miff' as '-' for standard output.
  947. %  
  948. %
  949. */
  950. X
  951. #include "display.h"
  952. #include "image.h"
  953. #include "ppm.h"
  954. X
  955. /*
  956. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  957. %                                                                             %
  958. %                                                                             %
  959. %                                                                             %
  960. %   E r r o r                                                                 %
  961. %                                                                             %
  962. %                                                                             %
  963. %                                                                             %
  964. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  965. %
  966. %  Function Error displays an error message and then terminates the program.
  967. %
  968. %  The format of the Error routine is:
  969. %
  970. %      Error(message,qualifier)
  971. %
  972. %  A description of each parameter follows:
  973. %
  974. %    o message:  Specifies the message to display before terminating the
  975. %      program.
  976. %
  977. %    o qualifier:  Specifies any qualifier to the message.
  978. %
  979. %
  980. */
  981. void Error(message,qualifier)
  982. char
  983. X  *message,
  984. X  *qualifier;
  985. {
  986. X  (void) fprintf(stderr,"%s: %s",application_name,message); 
  987. X  if (qualifier != (char *) NULL)
  988. X    (void) fprintf(stderr," %s",qualifier);
  989. X  (void) fprintf(stderr,".\n");
  990. X  exit(1);
  991. }
  992. X
  993. /*
  994. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  995. %                                                                             %
  996. %                                                                             %
  997. %                                                                             %
  998. %  R e a d P P M I m a g e                                                    %
  999. %                                                                             %
  1000. %                                                                             %
  1001. %                                                                             %
  1002. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1003. %
  1004. %  Function ReadPPMImage reads an image file and returns it.  It allocates 
  1005. %  the memory necessary for the new Image structure and returns a pointer to 
  1006. %  the new image.
  1007. %
  1008. %  The format of the ReadPPMImage routine is:
  1009. %
  1010. %      image=ReadPPMImage(filename)
  1011. %
  1012. %  A description of each parameter follows:
  1013. %
  1014. %    o image:  Function ReadPPMImage returns a pointer to the image after 
  1015. %      reading.  A null image is returned if there is a a memory shortage or 
  1016. %      if the image cannot be read.
  1017. %
  1018. %    o filename:  Specifies the name of the image to read.
  1019. %
  1020. %
  1021. */
  1022. static Image *ReadPPMImage(filename)
  1023. char
  1024. X  *filename;
  1025. {
  1026. X  Image
  1027. X    *image;
  1028. X
  1029. X  int
  1030. X    format;
  1031. X
  1032. X  pixel
  1033. X    **pixels;
  1034. X
  1035. X  pixval
  1036. X    max_value;
  1037. X
  1038. X  register int
  1039. X    i;
  1040. X
  1041. X  register pixel
  1042. X    *p;
  1043. X
  1044. X  register RunlengthPacket
  1045. X    *q;
  1046. X
  1047. X  unsigned char
  1048. X    blue,
  1049. X    green,
  1050. X    red;
  1051. X
  1052. X  /*
  1053. X    Allocate image structure.
  1054. X  */
  1055. X  image=(Image *) malloc(sizeof(Image));
  1056. X  if (image == (Image *) NULL)
  1057. X    Error("memory allocation error",(char *) NULL);
  1058. X  /*
  1059. X    Initialize Image structure.
  1060. X  */
  1061. X  image->id=UnknownId;
  1062. X  image->class=DirectClass;
  1063. X  image->compression=RunlengthEncodedCompression;
  1064. X  image->columns=0;
  1065. X  image->rows=0;
  1066. X  image->packets=0;
  1067. X  image->colors=0;
  1068. X  image->scene=0;
  1069. X  image->colormap=(ColorPacket *) NULL;
  1070. X  image->pixels=(RunlengthPacket *) NULL;
  1071. X  image->comments=(char *) NULL;
  1072. X  /*
  1073. X    Open image file.
  1074. X  */
  1075. X  (void) strcpy(image->filename,filename);
  1076. X  if (*image->filename == '-')
  1077. X    image->file=stdin;
  1078. X  else
  1079. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  1080. X      image->file=fopen(image->filename,"r");
  1081. X    else
  1082. X      {
  1083. X        char
  1084. X          command[256];
  1085. X
  1086. X        /*
  1087. X          Image file is compressed-- uncompress it.
  1088. X        */
  1089. X        (void) sprintf(command,"uncompress -c %s",image->filename);
  1090. X        image->file=(FILE *) popen(command,"r");
  1091. X      }
  1092. X  if (image->file == (FILE *) NULL)
  1093. X    Error("unable to open file",image->filename);
  1094. X  /*
  1095. X    Read PPM image.
  1096. X  */
  1097. X  pixels=ppm_readppm(image->file,&image->columns,&image->rows,&max_value);
  1098. X  /*
  1099. X    Create image.
  1100. X  */
  1101. X  image->comments=(char *) malloc((unsigned int) (strlen(image->filename)+256));
  1102. X  if (image->comments == (char *) NULL)
  1103. X    Error("memory allocation error",(char *) NULL);
  1104. X  (void) sprintf(image->comments,
  1105. X    "\n  Imported from PPM raster image:  %s\n",image->filename);
  1106. X  image->pixels=(RunlengthPacket *) 
  1107. X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
  1108. X  if (image->pixels == (RunlengthPacket *) NULL)
  1109. X    Error("memory allocation error",(char *) NULL);
  1110. X  /*
  1111. X    Convert PPM raster image to runlength-encoded packets.
  1112. X  */
  1113. X  p=(*pixels);
  1114. X  image->packets=0;
  1115. X  q=image->pixels;
  1116. X  q->length=MaxRunlength;
  1117. X  for (i=0; i < (image->columns*image->rows); i++)
  1118. X  {
  1119. X    red=(unsigned char) ((unsigned long) PPM_GETR(*p)*MaxRgb/max_value);
  1120. X    green=(unsigned char) ((unsigned long) PPM_GETG(*p)*MaxRgb/max_value);
  1121. X    blue=(unsigned char) ((unsigned long) PPM_GETB(*p)*MaxRgb/max_value);
  1122. X    if ((red == q->red) && (green == q->green) && (blue == q->blue) &&
  1123. X        (q->length < MaxRunlength))
  1124. X      q->length++;
  1125. X    else
  1126. X      {
  1127. X        if (image->packets > 0)
  1128. X          q++;
  1129. X        image->packets++;
  1130. X        q->red=red;
  1131. X        q->green=green;
  1132. X        q->blue=blue;
  1133. X        q->index=0;
  1134. X        q->length=0;
  1135. X      }
  1136. X    p++;
  1137. X  }
  1138. X  ppm_freearray(pixels,image->rows);
  1139. X  if (image->file != stdin)
  1140. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  1141. X      (void) fclose(image->file);
  1142. X    else
  1143. X      (void) pclose(image->file);
  1144. X  image->colors=NumberColors(image);
  1145. X  if (image->colors <= 256)
  1146. X    QuantizeImage(image,image->colors,0,False,True);
  1147. X  if (image->packets > ((image->columns*image->rows*3) >> 2))
  1148. X    image->compression=NoCompression;
  1149. X  image->pixels=(RunlengthPacket *)
  1150. X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
  1151. X  return(image);
  1152. }
  1153. X
  1154. /*
  1155. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1156. %                                                                             %
  1157. %                                                                             %
  1158. %                                                                             %
  1159. %   U s a g e                                                                 %
  1160. %                                                                             %
  1161. %                                                                             %
  1162. %                                                                             %
  1163. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1164. %
  1165. %  Procedure Usage displays the program usage;
  1166. %
  1167. %  The format of the Usage routine is:
  1168. %
  1169. %      Usage(message)
  1170. %
  1171. %  A description of each parameter follows:
  1172. %
  1173. %    message:  Specifies a specific message to display to the user.
  1174. %
  1175. */
  1176. static void Usage(message)
  1177. char
  1178. X  *message;
  1179. {
  1180. X  if (message != (char *) NULL)
  1181. X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
  1182. X  (void) fprintf(stderr,"Usage: %s [-scene #] image.ppm image.miff\n",
  1183. X    application_name);
  1184. X  (void) fprintf(stderr,"\nSpecify 'image.ppm' as '-' for standard input.\n");
  1185. X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard output.\n");
  1186. X  exit(1);
  1187. }
  1188. X
  1189. /*
  1190. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1191. %                                                                             %
  1192. %                                                                             %
  1193. %                                                                             %
  1194. %  M a i n                                                                    %
  1195. %                                                                             %
  1196. %                                                                             %
  1197. %                                                                             %
  1198. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1199. %
  1200. %
  1201. */
  1202. int main(argc,argv)
  1203. int
  1204. X  argc;
  1205. X   
  1206. char
  1207. X  *argv[];
  1208. {
  1209. X  char
  1210. X    filename[256];
  1211. X
  1212. X  Image
  1213. X    *image;
  1214. X
  1215. X  int
  1216. X    i;
  1217. X
  1218. X  unsigned int
  1219. X    scene;
  1220. X
  1221. X  /*
  1222. X    Initialize program variables.
  1223. X  */
  1224. X  application_name=argv[0];
  1225. X  i=1;
  1226. X  scene=0;
  1227. X  if (argc < 3)
  1228. X    Usage((char *) NULL);
  1229. X  /*
  1230. X    Read image and convert to MIFF format.
  1231. X  */
  1232. X  if (strncmp(argv[i],"-scene",2) == 0)
  1233. X    {
  1234. X      i++;
  1235. X      scene=atoi(argv[i++]);
  1236. X    }
  1237. X  (void) strcpy(filename,argv[i++]);
  1238. X  image=ReadPPMImage(filename);
  1239. X  if (image == (Image *) NULL)
  1240. X    exit(1);
  1241. X  (void) strcpy(image->filename,argv[i++]);
  1242. X  image->scene=scene;
  1243. X  (void) WriteImage(image);
  1244. X  (void) fprintf(stderr,"%s => %s  %dx%d\n",filename,image->filename,
  1245. X    image->columns,image->rows);
  1246. X  DestroyImage(image);
  1247. X  return(False);
  1248. }
  1249. SHAR_EOF
  1250. chmod 0755 ImageMagick/filters/PPMtoMIFF.c ||
  1251. echo 'restore of ImageMagick/filters/PPMtoMIFF.c failed'
  1252. Wc_c="`wc -c < 'ImageMagick/filters/PPMtoMIFF.c'`"
  1253. test 12416 -eq "$Wc_c" ||
  1254.     echo 'ImageMagick/filters/PPMtoMIFF.c: original size 12416, current size' "$Wc_c"
  1255. rm -f _shar_wnt_.tmp
  1256. fi
  1257. # ============= ImageMagick/filters/TIFFtoSUN.c ==============
  1258. if test -f 'ImageMagick/filters/TIFFtoSUN.c' -a X"$1" != X"-c"; then
  1259.     echo 'x - skipping ImageMagick/filters/TIFFtoSUN.c (File already exists)'
  1260.     rm -f _shar_wnt_.tmp
  1261. else
  1262. > _shar_wnt_.tmp
  1263. echo 'x - extracting ImageMagick/filters/TIFFtoSUN.c (Text)'
  1264. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/filters/TIFFtoSUN.c' &&
  1265. /*
  1266. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1267. %                                                                             %
  1268. %                                                                             %
  1269. %                                                                             %
  1270. %                 IIIII  M   M  PPPP    OOO   RRRR    TTTTT                   %
  1271. %                   I    MM MM  P   P  O   O  R   R     T                     %
  1272. %                   I    M M M  PPPP   O   O  RRRR      T                     %
  1273. %                   I    M   M  P      O   O  R R       T                     %
  1274. %                 IIIII  M   M  P       OOO   R  R      T                     % 
  1275. %                                                                             %
  1276. %                                                                             %
  1277. %                    Import TIFF image to a MIFF format.                      %
  1278. %                                                                             %
  1279. %                                                                             %
  1280. %                                                                             %
  1281. %                           Software Design                                   %
  1282. %                             John Cristy                                     %
  1283. %                            January  1991                                    %
  1284. %                                                                             %
  1285. %                                                                             %
  1286. %  Copyright 1991 E. I. Dupont de Nemours & Company                           %
  1287. %                                                                             %
  1288. %  Permission to use, copy, modify, distribute, and sell this software and    %
  1289. %  its documentation for any purpose is hereby granted without fee,           %
  1290. %  provided that the above Copyright notice appear in all copies and that     %
  1291. %  both that Copyright notice and this permission notice appear in            %
  1292. %  supporting documentation, and that the name of E. I. Dupont de Nemours     %
  1293. %  & Company not be used in advertising or publicity pertaining to            %
  1294. %  distribution of the software without specific, written prior               %
  1295. %  permission.  E. I. Dupont de Nemours & Company makes no representations    %
  1296. %  about the suitability of this software for any purpose.  It is provided    %
  1297. %  "as is" without express or implied warranty.                               %
  1298. %                                                                             %
  1299. %  E. I. Dupont de Nemours & Company disclaims all warranties with regard     %
  1300. %  to this software, including all implied warranties of merchantability      %
  1301. %  and fitness, in no event shall E. I. Dupont de Nemours & Company be        %
  1302. %  liable for any special, indirect or consequential damages or any           %
  1303. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  1304. %  in an action of contract, negligence or other tortious action, arising     %
  1305. %  out of or in connection with the use or performance of this software.      %
  1306. %                                                                             %
  1307. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1308. %  Command syntax:
  1309. %
  1310. %  import [-scene #] image.tiff image.miff
  1311. %  
  1312. %  Specify 'image.tiff' as '-' for standard input.  
  1313. %  Specify 'image.miff' as '-' for standard output.
  1314. %
  1315. */
  1316. X
  1317. #include <string.h>
  1318. #include "display.h"
  1319. #include "image.h"
  1320. X
  1321. /*
  1322. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1323. %                                                                             %
  1324. %                                                                             %
  1325. %                                                                             %
  1326. %   E r r o r                                                                 %
  1327. %                                                                             %
  1328. %                                                                             %
  1329. %                                                                             %
  1330. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1331. %
  1332. %  Function Error displays an error message and then terminates the program.
  1333. %
  1334. %  The format of the Error routine is:
  1335. %
  1336. %      Error(message,qualifier)
  1337. %
  1338. %  A description of each parameter follows:
  1339. %
  1340. %    o message:  Specifies the message to display before terminating the
  1341. %      program.
  1342. %
  1343. %    o qualifier:  Specifies any qualifier to the message.
  1344. %
  1345. %
  1346. */
  1347. void Error(message,qualifier)
  1348. char
  1349. X  *message,
  1350. X  *qualifier;
  1351. {
  1352. X  (void) fprintf(stderr,"%s: %s",application_name,message); 
  1353. X  if (qualifier != (char *) NULL)
  1354. X    (void) fprintf(stderr," %s",qualifier);
  1355. X  (void) fprintf(stderr,".\n");
  1356. X  exit(1);
  1357. }
  1358. X
  1359. /*
  1360. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1361. %                                                                             %
  1362. %                                                                             %
  1363. %                                                                             %
  1364. %  M S B F i r s t O r d e r                                                  %
  1365. %                                                                             %
  1366. %                                                                             %
  1367. %                                                                             %
  1368. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1369. %
  1370. %  Function MSBFirstOrder converts a least-significant byte first buffer of
  1371. %  integers to most-significant byte first.
  1372. %
  1373. %  The format of the MSBFirstOrder routine is:
  1374. %
  1375. %       MSBFirstOrder(p,length);
  1376. %
  1377. %  A description of each parameter follows.
  1378. %
  1379. %   o  p:  Specifies a pointer to a buffer of integers.
  1380. %
  1381. %   o  length:  Specifies the length of the buffer.
  1382. %
  1383. %
  1384. */
  1385. MSBFirstOrder(p,length)
  1386. register char 
  1387. X  *p;
  1388. X
  1389. register unsigned 
  1390. X  length;
  1391. {
  1392. X  register char 
  1393. X    c,
  1394. X    *q,
  1395. X    *sp;
  1396. X
  1397. X  q=p+length;
  1398. X  while (p < q) 
  1399. X  {
  1400. X    sp=p+3;
  1401. X    c=(*sp);
  1402. X    *sp=(*p);
  1403. X    *p++=c;
  1404. X    sp=p+1;
  1405. X    c=(*sp);
  1406. X    *sp=(*p);
  1407. X    *p++=c;
  1408. X    p+=2;
  1409. X  }
  1410. }
  1411. X
  1412. /*
  1413. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1414. %                                                                             %
  1415. %                                                                             %
  1416. %                                                                             %
  1417. %  R e a d T i f f I m a g e                                                  %
  1418. %                                                                             %
  1419. %                                                                             %
  1420. %                                                                             %
  1421. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1422. %
  1423. %  Function ReadTiffImage reads an image file and returns it.  It allocates the 
  1424. %  memory necessary for the new Image structure and returns a pointer to the 
  1425. %  new image.
  1426. %
  1427. %  The format of the ReadTiffImage routine is:
  1428. %
  1429. %      image=ReadTiffImage(filename)
  1430. %
  1431. %  A description of each parameter follows:
  1432. %
  1433. %    o image:  Function ReadTiffImage returns a pointer to the image after 
  1434. %      reading.  A null image is returned if there is a a memory shortage or 
  1435. %      if the image cannot be read.
  1436. %
  1437. %    o filename:  Specifies the name of the image to read.
  1438. %
  1439. %    o  verbose:  A value greater than zero prints detailed information about 
  1440. %       the image.
  1441. %
  1442. %
  1443. */
  1444. static Image *ReadTiffImage(filename,verbose)
  1445. char
  1446. X  *filename;
  1447. X
  1448. unsigned int
  1449. X  verbose;
  1450. {
  1451. #include <tiff.h>
  1452. #include <tiffio.h>
  1453. X
  1454. X  Image
  1455. X    *image;
  1456. X
  1457. X  register int
  1458. X    i;
  1459. X
  1460. X  register RunlengthPacket
  1461. X    *q;
  1462. X
  1463. X  register unsigned char
  1464. X    *p;
  1465. X
  1466. X  register unsigned int 
  1467. X    x,
  1468. X    y;
  1469. X
  1470. X  register unsigned short
  1471. X    index;
  1472. X
  1473. X  TIFF
  1474. X    *file;
  1475. X
  1476. X  unsigned char
  1477. X    blue,
  1478. X    green,
  1479. X    red,
  1480. X    *scanline;
  1481. X
  1482. X  unsigned short
  1483. X    bits_per_sample,
  1484. X    fill_order,
  1485. X    image_length,
  1486. X    image_width,
  1487. X    photometric,
  1488. X    planar_configuration,
  1489. X    samples_per_pixel;
  1490. X
  1491. X  /*
  1492. X    Allocate image structure.
  1493. X  */
  1494. X  image=(Image *) malloc(sizeof(Image));
  1495. X  if (image == (Image *) NULL)
  1496. X    Error("unable to allocate memory",(char *) NULL);
  1497. X  /*
  1498. X    Initialize Image structure.
  1499. X  */
  1500. X  image->id=UnknownId;
  1501. X  image->class=DirectClass;
  1502. X  image->compression=RunlengthEncodedCompression;
  1503. X  image->columns=0;
  1504. X  image->rows=0;
  1505. X  image->packets=0;
  1506. X  image->colors=0;
  1507. X  image->scene=0;
  1508. X  image->colormap=(ColorPacket *) NULL;
  1509. X  image->pixels=(RunlengthPacket *) NULL;
  1510. X  image->comments=(char *) NULL;
  1511. X  /*
  1512. X    Open TIFF image file.
  1513. X  */
  1514. X  (void) strcpy(image->filename,filename);
  1515. X  file=TIFFOpen(image->filename,"r"); 
  1516. X  if (file == (TIFF *) NULL)
  1517. X    exit(1);
  1518. X  if (verbose)
  1519. X    TIFFPrintDirectory(file,stderr,False,False,False);
  1520. X  /*
  1521. X    Verify this image is acceptable.
  1522. X  */
  1523. X  if (!TIFFGetField(file,TIFFTAG_BITSPERSAMPLE,&bits_per_sample))
  1524. X    bits_per_sample=1;
  1525. X  if (!TIFFGetField(file,TIFFTAG_PHOTOMETRIC,&photometric))
  1526. X    photometric=PHOTOMETRIC_PALETTE;
  1527. X  if ((bits_per_sample > 8) && (photometric != PHOTOMETRIC_RGB))
  1528. X    Error("image is more than 8-bits per sample",(char *) NULL);
  1529. X  if (!TIFFGetField(file,TIFFTAG_SAMPLESPERPIXEL,&samples_per_pixel))
  1530. X    samples_per_pixel=1;
  1531. X  if ((samples_per_pixel != 1) && (samples_per_pixel != 3) &&
  1532. X      (samples_per_pixel != 4))
  1533. X    Error("samples per pixel must be 1, 3, or 4",(char *) NULL);
  1534. X  /*
  1535. X    Initialize image attributes.
  1536. X  */
  1537. X  if (!TIFFGetField(file,TIFFTAG_PHOTOMETRIC,&photometric))
  1538. X    photometric=PHOTOMETRIC_PALETTE;
  1539. X  TIFFGetField(file,TIFFTAG_IMAGEWIDTH,&image_width);
  1540. X  TIFFGetField(file,TIFFTAG_IMAGELENGTH,&image_length);
  1541. X  image->comments=(char *) malloc((unsigned int) (strlen(image->filename)+256));
  1542. X  if (image->comments == (char *) NULL)
  1543. X    Error("unable to allocate memory",(char *) NULL);
  1544. X  (void) sprintf(image->comments,"\n  Imported from TIFF image %s.\n\0",
  1545. X    image->filename);
  1546. X  image->class=(photometric == PHOTOMETRIC_RGB ? DirectClass : PseudoClass);
  1547. X  image->columns=image_width;
  1548. X  image->rows=image_length;
  1549. X  image->colors=1 << bits_per_sample;
  1550. X  if (image->class == PseudoClass)
  1551. X    {
  1552. X      /*
  1553. X        Prepare for pseudo-color image.
  1554. X      */
  1555. X      image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  1556. X      if (image->colormap == (ColorPacket *) NULL)
  1557. X        Error("unable to allocate memory",(char *) NULL);
  1558. X      /*
  1559. X        Create colormap.
  1560. X      */
  1561. X      switch (photometric)
  1562. X      {
  1563. X        case PHOTOMETRIC_MINISBLACK:
  1564. X        {
  1565. X          for (i=0; i < image->colors; i++)
  1566. X          {
  1567. X            image->colormap[i].red=(255*i)/(image->colors-1);
  1568. X            image->colormap[i].green=(255*i)/(image->colors-1);
  1569. X            image->colormap[i].blue=(255*i)/(image->colors-1);
  1570. X          }
  1571. X          break;
  1572. X        }
  1573. X        case PHOTOMETRIC_MINISWHITE:
  1574. X        {
  1575. X          for (i=0; i < image->colors; i++)
  1576. X          {
  1577. X            image->colormap[i].red=255-(255*i)/(image->colors-1);
  1578. X            image->colormap[i].green=255-(255*i)/(image->colors-1);
  1579. X            image->colormap[i].blue=255-(255*i)/(image->colors-1);
  1580. X          }
  1581. X          break;
  1582. X        }
  1583. X        case PHOTOMETRIC_PALETTE:
  1584. X        {
  1585. X          unsigned short
  1586. X            *blue_colormap,
  1587. X            *green_colormap,
  1588. X            *red_colormap;
  1589. X
  1590. X          TIFFGetField(file,TIFFTAG_COLORMAP,&red_colormap,&green_colormap,
  1591. X            &blue_colormap);
  1592. X          for (i=0; i < image->colors; i++)
  1593. X          {
  1594. X            image->colormap[i].red=red_colormap[i] >> 8;
  1595. X            image->colormap[i].green=green_colormap[i] >> 8;
  1596. X            image->colormap[i].blue=blue_colormap[i] >> 8;
  1597. X          }
  1598. X          break;
  1599. X        }
  1600. X        default:
  1601. X          break;
  1602. X      }
  1603. X    }
  1604. X  /*
  1605. X    Allocate memory for the image and scanline buffer.
  1606. X  */
  1607. X  image->pixels=(RunlengthPacket *) 
  1608. X    malloc(image->columns*image->rows*sizeof(RunlengthPacket));
  1609. X  TIFFGetField(file,TIFFTAG_PLANARCONFIG,&planar_configuration);
  1610. X  if (planar_configuration == PLANARCONFIG_CONTIG)
  1611. X    scanline=(unsigned char *) malloc((unsigned int) TIFFScanlineSize(file));
  1612. X  else
  1613. X    scanline=(unsigned char *) 
  1614. X      malloc((unsigned int) samples_per_pixel*TIFFScanlineSize(file));
  1615. X  if ((image->pixels == (RunlengthPacket *) NULL) ||
  1616. X      (scanline == (unsigned char *) NULL))
  1617. X    Error("unable to allocate memory",(char *) NULL);
  1618. X  /*
  1619. X    Read the TIFF image and write to image pixel buffer.
  1620. X  */
  1621. X  if (!TIFFGetField(file,TIFFTAG_FILLORDER,&fill_order))
  1622. X    fill_order=FILLORDER_MSB2LSB;
  1623. X  image->packets=0;
  1624. X  q=image->pixels;
  1625. X  q->length=MaxRunlength;
  1626. X  for (y=0; y < image->rows; y++)
  1627. X  {
  1628. X    p=scanline;
  1629. X    if (TIFFReadScanline(file,p,y,0) < 0)
  1630. X      Error("unable to read TIFF scanline",(char *) NULL);
  1631. X    if (planar_configuration != PLANARCONFIG_CONTIG)
  1632. X      for (i=1; i < samples_per_pixel; i++)
  1633. X        if (TIFFReadScanline(file,p+i*TIFFScanlineSize(file),y,i) < 0)
  1634. X          Error("unable to read TIFF scanline",(char *) NULL);
  1635. X    switch (photometric)
  1636. X    {
  1637. X      case PHOTOMETRIC_RGB:
  1638. X      {
  1639. X        switch (bits_per_sample)
  1640. X        {
  1641. X          case 8:
  1642. X          default:
  1643. X          {
  1644. X            /*
  1645. X              Assumes Max Sample Value is 255.
  1646. X            */
  1647. X            for (x=0; x < image->columns; x++)
  1648. X            {
  1649. X              if (planar_configuration == PLANARCONFIG_CONTIG)
  1650. X                {
  1651. X                  red=(*p++);
  1652. X                  green=(*p++);
  1653. X                  blue=(*p++);
  1654. X                }
  1655. X              else
  1656. X                {
  1657. X                  red=(*p);
  1658. X                  green=(*(p+TIFFScanlineSize(file)));
  1659. X                  blue=(*(p+2*TIFFScanlineSize(file)));
  1660. X                  p++;
  1661. X                }
  1662. X              if ((red == q->red) && (green == q->green) && 
  1663. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1664. X                q->length++;
  1665. X              else
  1666. X                {
  1667. X                  if (image->packets > 0)
  1668. X                    q++;
  1669. X                  image->packets++;
  1670. X                  q->red=red;
  1671. X                  q->green=green;
  1672. X                  q->blue=blue;
  1673. X                  q->index=0;
  1674. X                  q->length=0;
  1675. X                }
  1676. X              if (samples_per_pixel == 4)
  1677. X                p++;
  1678. X            }
  1679. X            break;
  1680. X          }
  1681. X          case 16:
  1682. X          {
  1683. X            register unsigned short
  1684. X              *p;
  1685. X
  1686. X            /*
  1687. X              Assumes Max Sample Value is 2047.
  1688. X            */
  1689. X            p=(unsigned short *) scanline;
  1690. X            for (x=0; x < image->columns; x++)
  1691. X            {
  1692. X              if (planar_configuration == PLANARCONFIG_CONTIG)
  1693. X                {
  1694. X                  red=(*p++ >> 3);
  1695. X                  green=(*p++ >> 3);
  1696. X                  blue=(*p++ >> 3);
  1697. X                }
  1698. X              else
  1699. X                {
  1700. X                  red=(*p >> 3);
  1701. X                  green=(*(p+TIFFScanlineSize(file)) >> 3);
  1702. X                  blue=(*(p+2*TIFFScanlineSize(file)) >> 3);
  1703. X                  p++;
  1704. X                }
  1705. X              if ((red == q->red) && (green == q->green) && 
  1706. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1707. X                q->length++;
  1708. X              else
  1709. X                {
  1710. X                  if (image->packets > 0)
  1711. X                    q++;
  1712. X                  image->packets++;
  1713. X                  q->red=red;
  1714. X                  q->green=green;
  1715. X                  q->blue=blue;
  1716. X                  q->index=0;
  1717. X                  q->length=0;
  1718. X                }
  1719. X              if (samples_per_pixel == 4)
  1720. X                p++;
  1721. X            }
  1722. X            break;
  1723. X          }
  1724. X        }
  1725. X        break;
  1726. X      }
  1727. X      case PHOTOMETRIC_MINISBLACK:
  1728. X      case PHOTOMETRIC_MINISWHITE:
  1729. X      {
  1730. X        switch (bits_per_sample)
  1731. X        {
  1732. X          case 1:
  1733. X          {
  1734. X            register int
  1735. X              bit;
  1736. X
  1737. X            for (x=0; x < image->columns; x+=8)
  1738. X            {
  1739. X              for (bit=7; bit >= 0; bit--)
  1740. X              {
  1741. X                index=((*p) & (0x01 << bit) ? 0x01 : 0x00);
  1742. X                red=image->colormap[index].red;
  1743. X                green=image->colormap[index].green;
  1744. X                blue=image->colormap[index].blue;
  1745. X                if ((red == q->red) && (green == q->green) &&
  1746. X                    (blue == q->blue) && (q->length < MaxRunlength))
  1747. X                  q->length++;
  1748. X                else
  1749. X                  {
  1750. X                    if (image->packets > 0)
  1751. X                      q++;
  1752. X                    image->packets++;
  1753. X                    q->red=red;
  1754. X                    q->green=green;
  1755. X                    q->blue=blue;
  1756. X                    q->index=index;
  1757. X                    q->length=0;
  1758. X                  }
  1759. X              }
  1760. X              p++;
  1761. X            }
  1762. X            if ((image->columns % 8) != 0)
  1763. X              {
  1764. X                for (bit=7; bit >= (8-(image->columns % 8)); bit--)
  1765. X                {
  1766. X                  index=((*p) & (0x01 << bit) ? 0x00 : 0x01);
  1767. X                  red=image->colormap[index].red;
  1768. X                  green=image->colormap[index].green;
  1769. X                  blue=image->colormap[index].blue;
  1770. X                  if ((red == q->red) && (green == q->green) &&
  1771. X                      (blue == q->blue) && (q->length < MaxRunlength))
  1772. X                    q->length++;
  1773. X                  else
  1774. X                    {
  1775. X                      if (image->packets > 0)
  1776. X                        q++;
  1777. X                      image->packets++;
  1778. X                      q->red=red;
  1779. X                      q->green=green;
  1780. X                      q->blue=blue;
  1781. X                      q->index=index;
  1782. X                      q->length=0;
  1783. X                    }
  1784. X                }
  1785. X                p++;
  1786. X              }
  1787. X            break;
  1788. X          }
  1789. X          case 2:
  1790. X          {
  1791. X            for (x=0; x < image->columns; x+=4)
  1792. X            {
  1793. X              index=(*p >> 6) & 3;
  1794. X              red=image->colormap[index].red;
  1795. X              green=image->colormap[index].green;
  1796. X              blue=image->colormap[index].blue;
  1797. X              if ((red == q->red) && (green == q->green) &&
  1798. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1799. X                q->length++;
  1800. X              else
  1801. X                {
  1802. X                  if (image->packets > 0)
  1803. X                    q++;
  1804. X                  image->packets++;
  1805. X                  q->red=red;
  1806. X                  q->green=green;
  1807. X                  q->blue=blue;
  1808. X                  q->index=index;
  1809. X                  q->length=0;
  1810. X                }
  1811. X              index=(*p >> 4) & 3;
  1812. X              red=image->colormap[index].red;
  1813. X              green=image->colormap[index].green;
  1814. X              blue=image->colormap[index].blue;
  1815. X              if ((red == q->red) && (green == q->green) &&
  1816. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1817. X                q->length++;
  1818. X              else
  1819. X                {
  1820. X                  if (image->packets > 0)
  1821. X                    q++;
  1822. X                  image->packets++;
  1823. X                  q->red=red;
  1824. X                  q->green=green;
  1825. X                  q->blue=blue;
  1826. X                  q->index=index;
  1827. X                  q->length=0;
  1828. X                }
  1829. X              index=(*p >> 2) & 3;
  1830. X              red=image->colormap[index].red;
  1831. X              green=image->colormap[index].green;
  1832. X              blue=image->colormap[index].blue;
  1833. X              if ((red == q->red) && (green == q->green) &&
  1834. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1835. X                q->length++;
  1836. X              else
  1837. X                {
  1838. X                  if (image->packets > 0)
  1839. X                    q++;
  1840. X                  image->packets++;
  1841. X                  q->red=red;
  1842. X                  q->green=green;
  1843. X                  q->blue=blue;
  1844. X                  q->index=index;
  1845. X                  q->length=0;
  1846. X                }
  1847. X              index=(*p) & 3;
  1848. X              red=image->colormap[index].red;
  1849. X              green=image->colormap[index].green;
  1850. X              blue=image->colormap[index].blue;
  1851. X              if ((red == q->red) && (green == q->green) &&
  1852. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1853. X                q->length++;
  1854. X              else
  1855. X                {
  1856. X                  if (image->packets > 0)
  1857. X                    q++;
  1858. X                  image->packets++;
  1859. X                  q->red=red;
  1860. X                  q->green=green;
  1861. X                  q->blue=blue;
  1862. X                  q->index=index;
  1863. X                  q->length=0;
  1864. X                }
  1865. X              p++;
  1866. X            }
  1867. X            break;
  1868. X          }
  1869. X          case 4:
  1870. X          {
  1871. X            for (x=0; x < image->columns; x+=2)
  1872. X            {
  1873. X              index=(*p >> 4);
  1874. X              red=image->colormap[index].red;
  1875. X              green=image->colormap[index].green;
  1876. X              blue=image->colormap[index].blue;
  1877. X              if ((red == q->red) && (green == q->green) &&
  1878. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1879. X                q->length++;
  1880. X              else
  1881. X                {
  1882. X                  if (image->packets > 0)
  1883. X                    q++;
  1884. X                  image->packets++;
  1885. X                  q->red=red;
  1886. X                  q->green=green;
  1887. X                  q->blue=blue;
  1888. X                  q->index=index;
  1889. X                  q->length=0;
  1890. X                }
  1891. X              index=(*p) & 0xf;
  1892. X              red=image->colormap[index].red;
  1893. X              green=image->colormap[index].green;
  1894. X              blue=image->colormap[index].blue;
  1895. X              if ((red == q->red) && (green == q->green) &&
  1896. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1897. X                q->length++;
  1898. X              else
  1899. X                {
  1900. X                  if (image->packets > 0)
  1901. X                    q++;
  1902. X                  image->packets++;
  1903. X                  q->red=red;
  1904. X                  q->green=green;
  1905. X                  q->blue=blue;
  1906. X                  q->index=index;
  1907. X                  q->length=0;
  1908. X                }
  1909. X              p++;
  1910. X            }
  1911. X            break;
  1912. X          }
  1913. X          case 8:
  1914. X          {
  1915. X            for (x=0; x < image->columns; x++)
  1916. X            {
  1917. X              index=(*p);
  1918. X              red=image->colormap[index].red;
  1919. X              green=image->colormap[index].green;
  1920. X              blue=image->colormap[index].blue;
  1921. X              if ((red == q->red) && (green == q->green) &&
  1922. X                  (blue == q->blue) && (q->length < MaxRunlength))
  1923. X                q->length++;
  1924. X              else
  1925. X                {
  1926. X                  if (image->packets > 0)
  1927. X                    q++;
  1928. X                  image->packets++;
  1929. X                  q->red=red;
  1930. X                  q->green=green;
  1931. X                  q->blue=blue;
  1932. X                  q->index=index;
  1933. X                  q->length=0;
  1934. X                }
  1935. X              p++;
  1936. X            }
  1937. X            break;
  1938. X          }
  1939. X          default:
  1940. X            break;
  1941. X        }
  1942. X        break;
  1943. X      }
  1944. X      case PHOTOMETRIC_PALETTE:
  1945. X      {
  1946. X        for (x=0; x < image->columns; x++)
  1947. X        {
  1948. X          index=(*p++);
  1949. X          red=image->colormap[index].red;
  1950. X          green=image->colormap[index].green;
  1951. X          blue=image->colormap[index].blue;
  1952. X          if ((red == q->red) && (green == q->green) && (blue == q->blue) && 
  1953. X              (q->length < MaxRunlength))
  1954. X            q->length++;
  1955. X          else
  1956. X            {
  1957. X              if (image->packets > 0)
  1958. X                q++;
  1959. X              image->packets++;
  1960. X              q->red=red;
  1961. X              q->green=green;
  1962. X              q->blue=blue;
  1963. X              q->index=index;
  1964. X              q->length=0;
  1965. X            }
  1966. X        }
  1967. X        break;
  1968. X      }
  1969. X      default:
  1970. X        break;
  1971. X    }
  1972. X  }
  1973. X  free((char *) scanline);
  1974. X  TIFFClose(file);
  1975. X  if (image->class == PseudoClass)
  1976. X    QuantizeImage(image,image->colors,0,False,True);
  1977. X  if (image->packets > ((image->columns*image->rows*3) >> 2))
  1978. X    image->compression=NoCompression;
  1979. X  image->pixels=(RunlengthPacket *)
  1980. X    realloc((char *) image->pixels,image->packets*sizeof(RunlengthPacket));
  1981. X  return(image);
  1982. }
  1983. X
  1984. /*
  1985. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1986. %                                                                             %
  1987. %                                                                             %
  1988. %                                                                             %
  1989. %   U s a g e                                                                 %
  1990. %                                                                             %
  1991. %                                                                             %
  1992. %                                                                             %
  1993. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1994. %
  1995. %  Procedure Usage displays the program usage;
  1996. %
  1997. %  The format of the Usage routine is:
  1998. %
  1999. %      Usage(message)
  2000. %
  2001. %  A description of each parameter follows:
  2002. %
  2003. %    program_name:  Specifies the name of this program.
  2004. %
  2005. %    message:  Specifies a specific message to display to the user.
  2006. %
  2007. */
  2008. static void Usage(message)
  2009. char
  2010. X  *message;
  2011. {
  2012. X  if (message != (char *) NULL)
  2013. X    (void) fprintf(stderr,"Can't continue, %s\n\n",message);
  2014. X  (void) fprintf(stderr,"Usage: %s image.tiff image.miff\n\n",application_name);
  2015. X  (void) fprintf(stderr,"Specify 'image.tiff' as '-' for standard input.\n");
  2016. X  (void) fprintf(stderr,"Specify 'image.miff' as '-' for standard output.\n");
  2017. X  exit(1);
  2018. }
  2019. X
  2020. /*
  2021. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2022. %                                                                             %
  2023. %                                                                             %
  2024. %                                                                             %
  2025. %   W r i t e R a s t e r I m a g e                                           %
  2026. %                                                                             %
  2027. %                                                                             %
  2028. %                                                                             %
  2029. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2030. %
  2031. %  Procedure WriteRasterImage writes an image to a file on disk in SUN 
  2032. %  rasterfile format.
  2033. %
  2034. %  The format of the WriteRasterImage routine is:
  2035. %
  2036. %      WriteRasterImage(image)
  2037. %
  2038. %  A description of each parameter follows.
  2039. %
  2040. %    o image:  A pointer to a Image structure.
  2041. %
  2042. %
  2043. */
  2044. static void WriteRasterImage(image)
  2045. Image
  2046. X  *image;
  2047. {
  2048. #define RT_STANDARD  1
  2049. #define RMT_NONE  0
  2050. #define RMT_EQUAL_RGB  1
  2051. X
  2052. X  typedef struct Raster
  2053. X  {
  2054. X    int
  2055. X      magic,
  2056. X      width,
  2057. X      height,
  2058. X      depth,
  2059. X      length,
  2060. X      type,
  2061. X      maptype,
  2062. X      maplength;
  2063. X  } Raster;
  2064. X
  2065. X  Raster
  2066. X    sun_header;
  2067. X
  2068. X  register int
  2069. X    i,
  2070. X    j,
  2071. X    x;
  2072. X
  2073. X  register RunlengthPacket
  2074. X    *p;
  2075. X
  2076. X  register unsigned char
  2077. X    *q;
  2078. X
  2079. X  unsigned char
  2080. X    *sun_pixels;
  2081. X
  2082. X  unsigned long
  2083. X    lsb_first;
  2084. X
  2085. X  /*
  2086. X    Open output image file.
  2087. X  */
  2088. X  if (*image->filename == '-')
  2089. X    image->file=stdout;
  2090. X  else
  2091. X    if (strcmp(image->filename+strlen(image->filename)-2,".Z") != 0)
  2092. X      image->file=fopen(image->filename,"w");
  2093. X    else
  2094. X      {
  2095. X        char
  2096. X          command[256];
  2097. X
  2098. X        /*
  2099. X          Image file is compressed-- uncompress it.
  2100. X        */
  2101. X        (void) sprintf(command,"compress -c > %s",image->filename);
  2102. X        image->file=(FILE *) popen(command,"w");
  2103. X      }
  2104. X  if (image->file == (FILE *) NULL)
  2105. X    Error("unable to open file",image->filename);
  2106. X  /*
  2107. X    Initialize raster file sun_header.
  2108. X  */
  2109. X  sun_header.magic=0x59a66a95;
  2110. X  sun_header.width=image->columns;
  2111. X  sun_header.height=image->rows;
  2112. X  sun_header.depth=(image->class == DirectClass ? 24 : 8);
  2113. X  sun_header.length=image->columns*image->rows*(sun_header.depth >> 3)+
  2114. X    (image->columns % 2 ? image->rows : 0);
  2115. X  sun_header.type=RT_STANDARD;
  2116. X  sun_header.maptype=(image->class == DirectClass ? RMT_NONE : RMT_EQUAL_RGB);
  2117. X  sun_header.maplength=(image->class == DirectClass ? 0 : image->colors*3);
  2118. X  /*
  2119. X    Ensure the sun_header byte-order is most-significant byte first.
  2120. X  */
  2121. X  lsb_first=1;
  2122. X  if ((*(char *) &lsb_first) == 0)
  2123. X    (void) fwrite((char *) &sun_header,sizeof(sun_header),1,image->file);
  2124. X  else
  2125. X    {
  2126. X      Raster
  2127. X        reverse_header;
  2128. X
  2129. X      reverse_header=sun_header;
  2130. X      MSBFirstOrder((char *) &reverse_header,sizeof(reverse_header));
  2131. X      (void) fwrite((char *) &reverse_header,sizeof(reverse_header),1,
  2132. SHAR_EOF
  2133. true || echo 'restore of ImageMagick/filters/TIFFtoSUN.c failed'
  2134. fi
  2135. echo 'End of ImageMagick part 14'
  2136. echo 'File ImageMagick/filters/TIFFtoSUN.c is continued in part 15'
  2137. echo 15 > _shar_seq_.tmp
  2138. exit 0
  2139. --
  2140. Dan Heller
  2141. O'Reilly && Associates       Z-Code Software    Comp-sources-x:
  2142. Senior Writer                President          comp-sources-x@uunet.uu.net
  2143. argv@ora.com                 argv@zipcode.com
  2144.