home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / sources / misc / 4188 < prev    next >
Encoding:
Text File  |  1992-12-14  |  57.3 KB  |  1,797 lines

  1. Newsgroups: comp.sources.misc
  2. Path: sparky!kent
  3. From: cristy@eplrx7.es.duPont.com (John Cristy)
  4. Subject:  v34i048:  imagemagick - X11 image processing and display v2.2, Part20/26
  5. Message-ID: <1992Dec15.035718.22788@sparky.imd.sterling.com>
  6. Followup-To: comp.sources.d
  7. X-Md4-Signature: 94c5d01e0c7dfba326e7e77563889187
  8. Sender: kent@sparky.imd.sterling.com (Kent Landfield)
  9. Organization: Sterling Software
  10. References: <csm-v34i028=imagemagick.141926@sparky.IMD.Sterling.COM>
  11. Date: Tue, 15 Dec 1992 03:57:18 GMT
  12. Approved: kent@sparky.imd.sterling.com
  13. Lines: 1782
  14.  
  15. Submitted-by: cristy@eplrx7.es.duPont.com (John Cristy)
  16. Posting-number: Volume 34, Issue 48
  17. Archive-name: imagemagick/part20
  18. Environment: UNIX, VMS, X11, SGI, DEC, Cray, Sun, Vax
  19.  
  20. #!/bin/sh
  21. # this is Part.20 (part 20 of a multipart archive)
  22. # do not concatenate these parts, unpack them in order with /bin/sh
  23. # file ImageMagick/montage.c continued
  24. #
  25. if test ! -r _shar_seq_.tmp; then
  26.     echo 'Please unpack part 1 first!'
  27.     exit 1
  28. fi
  29. (read Scheck
  30.  if test "$Scheck" != 20; then
  31.     echo Please unpack part "$Scheck" next!
  32.     exit 1
  33.  else
  34.     exit 0
  35.  fi
  36. ) < _shar_seq_.tmp || exit 1
  37. if test ! -f _shar_wnt_.tmp; then
  38.     echo 'x - still skipping ImageMagick/montage.c'
  39. else
  40. echo 'x - continuing file ImageMagick/montage.c'
  41. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/montage.c' &&
  42. X    if (display)
  43. X      {
  44. X        /*
  45. X          Copy tile label to the composite image.
  46. X        */
  47. X        (void) strcpy(annotate_info.text,image->label);
  48. X        annotate_info.width=XTextWidth(image_window.font_info,
  49. X          annotate_info.text,strlen(annotate_info.text));
  50. X        if (annotate_info.width > (tile_width+border_width*2))
  51. X          {
  52. X            /*
  53. X              Label is too wide-- shorten.
  54. X            */
  55. X            q=annotate_info.text+strlen(annotate_info.text);
  56. X            do
  57. X            {
  58. X              *--q='\0';
  59. X              if ((int) strlen(annotate_info.text) > 2)
  60. X                (void) strcpy(q-2,"...");
  61. X              annotate_info.width=XTextWidth(image_window.font_info,
  62. X                annotate_info.text,strlen(annotate_info.text));
  63. X            } while (annotate_info.width > (tile_width+border_width*2));
  64. X          }
  65. X        (void) sprintf(annotate_info.geometry,"%ux%u%+d%+d\0",
  66. X          annotate_info.width,annotate_info.height,x_offset+
  67. X          ((tile_width+border_width*2) >> 1)-(annotate_info.width >> 1),
  68. X          y_offset+y+(int) image->rows+tile_border_height+2);
  69. X        (void) XAnnotateImage(display,&image_window,&annotate_info,True,
  70. X          montage_image);
  71. X      }
  72. X    DestroyImage(image);
  73. X    if (((tile+1) % tiles_per_row) != 0)
  74. X      x_offset+=tile_width+(tile_border_width+border_width)*2;
  75. X    else
  76. X      {
  77. X        x_offset=tile_border_width;
  78. X        y_offset+=tile_height+(tile_border_height+border_width)*2+
  79. X          (annotate_info.height+4);
  80. X      }
  81. X  }
  82. X  (void) free((char *) annotate_info.text);
  83. X  if (display)
  84. X    {
  85. X      /*
  86. X        Free X resources.
  87. X      */
  88. X      XFreeFont(display,image_window.font_info);
  89. X      XFree((void *) image_window.visual_info);
  90. X    }
  91. X  return(montage_image);
  92. }
  93. X
  94. /*
  95. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  96. %                                                                             %
  97. %                                                                             %
  98. %                                                                             %
  99. %   U s a g e                                                                 %
  100. %                                                                             %
  101. %                                                                             %
  102. %                                                                             %
  103. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  104. %
  105. %  Function Usage displays the program command syntax.
  106. %
  107. %  The format of the Usage routine is:
  108. %
  109. %      Usage()
  110. %
  111. %
  112. */
  113. static void Usage()
  114. {
  115. X  char
  116. X    **p;
  117. X
  118. X  static char
  119. X    *options[]=
  120. X    {
  121. X      "-aspect_ratio         respect aspect ratio of the image",
  122. X      "-clip geometry        preferred size and location of the clipped image",
  123. X      "-colors value         preferred number of colors in the image",
  124. X      "-colorspace type      GRAY, RGB, XYZ, YIQ, or YUV",
  125. X      "-compose operator     composite operator",
  126. X      "-compress type        RunlengthEncoded or QEncoded",
  127. X      "-density geometry     vertical and horizonal density of the image",
  128. X      "-display server       query font from this X server",
  129. X      "-dither               apply Floyd/Steinberg error diffusion to image",
  130. X      "-gamma value          level of gamma correction",
  131. X      "-geometry geometry    preferred tile and border sizes",
  132. X      "-gravity direction    which direction to gravitate towards",
  133. X      "-monochrome           transform image to black and white",
  134. X      "-rotate degrees       apply Paeth rotation to the image",
  135. X      "-tiles_per_row value  number of image tiles per row",
  136. X      "-treedepth value      depth of the color classification tree",
  137. X      "-verbose              print detailed information about the image",
  138. X      (char *) NULL
  139. X    };
  140. X  (void) fprintf(stderr,
  141. X    "Usage: %s [-options ...] file [ [-options ...] file ...] file\n",
  142. X    application_name);
  143. X  (void) fprintf(stderr,"\nWhere options include: \n");
  144. X  for (p=options; *p != (char *) NULL; p++)
  145. X    (void) fprintf(stderr,"  %s\n",*p);
  146. X  (void) fprintf(stderr,
  147. X    "\nIn addition to those listed above, you can specify these standard X\n");
  148. X  (void) fprintf(stderr,
  149. X    "resources as command line options:  -background, -bordercolor,\n");
  150. X  (void) fprintf(stderr,
  151. X    "-borderwidth, -font, -foreground, or -title\n");
  152. X  (void) fprintf(stderr,
  153. X    "\nChange '-' to '+' in any option above to reverse its effect.  For\n");
  154. X  (void) fprintf(stderr,
  155. X    "example, specify +aspect_ratio to ignore the aspect ratio of an image.\n");
  156. X  (void) fprintf(stderr,
  157. X    "\nBy default, the image format of `file' is determined by its magic\n");
  158. X  (void) fprintf(stderr,
  159. X    "number.  To specify a particular image format, precede the filename\n");
  160. X  (void) fprintf(stderr,
  161. X    "with an image format name and a colon (i.e. mtv:image) or specify the\n");
  162. X  (void) fprintf(stderr,
  163. X    "image type as the filename suffix (i.e. image.mtv).  Specify 'file' as\n");
  164. X  (void) fprintf(stderr,"'-' for standard input or output.\n");
  165. X  exit(1);
  166. }
  167. X
  168. /*
  169. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  170. %                                                                             %
  171. %                                                                             %
  172. %                                                                             %
  173. %    M a i n                                                                  %
  174. %                                                                             %
  175. %                                                                             %
  176. %                                                                             %
  177. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  178. %
  179. %
  180. */
  181. int main(argc,argv)
  182. int
  183. X  argc;
  184. X
  185. char
  186. X  **argv;
  187. {
  188. X  AlienInfo
  189. X    alien_info;
  190. X
  191. X  char
  192. X    *clip_geometry,
  193. X    *option,
  194. X    *server_name,
  195. X    *write_filename;
  196. X
  197. X  Display
  198. X    *display;
  199. X
  200. X  double
  201. X    gamma;
  202. X
  203. X  Image
  204. X    **images,
  205. X    *montage_image;
  206. X
  207. X  int
  208. X    degrees,
  209. X    i,
  210. X    x;
  211. X
  212. X  time_t
  213. X    start_time;
  214. X
  215. X  unsigned int
  216. X    aspect_ratio,
  217. X    compose,
  218. X    compression,
  219. X    display_image,
  220. X    image_number,
  221. X    maximum_images,
  222. X    tiles_per_row,
  223. X    verbose;
  224. X
  225. X  XrmDatabase
  226. X    resource_database;
  227. X
  228. X  XResourceInfo
  229. X    resource_info;
  230. X
  231. X  /*
  232. X    Display usage profile if there are no command line arguments.
  233. X  */
  234. X  application_name=(*argv);
  235. X  if (argc < 3)
  236. X    Usage();
  237. X  /*
  238. X    Set defaults.
  239. X  */
  240. X  GetAlienInfo(&alien_info);
  241. X  aspect_ratio=True;
  242. X  clip_geometry=(char *) NULL;
  243. X  compose=ReplaceCompositeOp;
  244. X  compression=UndefinedCompression;
  245. X  degrees=0;
  246. X  display=(Display *) NULL;
  247. X  display_image=True;
  248. X  gamma=0.0;
  249. X  resource_database=(XrmDatabase) NULL;
  250. X  resource_info.border_width=0;
  251. X  resource_info.colorspace=RGBColorspace;
  252. X  resource_info.dither=False;
  253. X  resource_info.image_geometry=(char *) NULL;
  254. X  resource_info.monochrome=False;
  255. X  resource_info.number_colors=0;
  256. X  resource_info.tree_depth=0;
  257. X  server_name=(char *) NULL;
  258. X  tiles_per_row=0;
  259. X  verbose=False;
  260. X  maximum_images=2048;
  261. X  images=(Image **) malloc(maximum_images*sizeof(Image *));
  262. X  if (images == (Image **) NULL)
  263. X    Error("unable to montage images","memory allocation failed");
  264. X  /*
  265. X    Check for server name specified on the command line.
  266. X  */
  267. X  for (i=1; i < argc; i++)
  268. X  {
  269. X    /*
  270. X      Check command line for server name.
  271. X    */
  272. X    option=argv[i];
  273. X    if (((int) strlen(option) > 1) && ((*option == '-') || (*option == '+')))
  274. X      if (strncmp("display",option+1,3) == 0)
  275. X        {
  276. X          /*
  277. X            User specified server name.
  278. X          */
  279. X          display_image=(*option == '-');
  280. X          if (display_image)
  281. X            {
  282. X              i++;
  283. X              if (i == argc)
  284. X                Error("missing server name on -display",(char *) NULL);
  285. X              server_name=argv[i];
  286. X            }
  287. X          break;
  288. X        }
  289. X  }
  290. X  if (display_image)
  291. X    {
  292. X      char
  293. X        *resource_value;
  294. X
  295. X      XrmDatabase
  296. X        server_database;
  297. X
  298. X      /*
  299. X        Open X server connection.
  300. X      */
  301. X      display=XOpenDisplay(server_name);
  302. X      if (display == (Display *) NULL)
  303. X        Error("unable to connect to X server",XDisplayName(server_name));
  304. X      /*
  305. X        Set our forgiving error handler.
  306. X      */
  307. X      XSetErrorHandler(XError);
  308. X      /*
  309. X        Initialize resource database.
  310. X      */
  311. X      XrmInitialize();
  312. X      resource_database=XrmGetDatabase(display);
  313. X      resource_value=XResourceManagerString(display);
  314. X      if (resource_value == (char *) NULL)
  315. X        resource_value="";
  316. X      server_database=XrmGetStringDatabase(resource_value);
  317. X      XrmMergeDatabases(server_database,&resource_database);
  318. X      /*
  319. X        Get user defaults from X resource database.
  320. X      */
  321. X      XGetResourceInfo(resource_database,application_name,&resource_info);
  322. X      resource_value=XGetResource(resource_database,application_name,
  323. X        "aspect_ratio",(char *) NULL,"True");
  324. X      aspect_ratio=IsTrue(resource_value);
  325. X      clip_geometry=XGetResource(resource_database,application_name,
  326. X        "clipGeometry","ClipGeometry",(char *) NULL);
  327. X      resource_value=XGetResource(resource_database,application_name,
  328. X        "compression",(char *) NULL,"RunlengthEncoded");
  329. X      if (Latin1Compare("qencoded",resource_value) == 0)
  330. X        compression=QEncodedCompression;
  331. X      else
  332. X        compression=RunlengthEncodedCompression;
  333. X      resource_value=XGetResource(resource_database,application_name,"gamma",
  334. X        (char *) NULL,"0.0");
  335. X      gamma=atof(resource_value);
  336. X      resource_value=XGetResource(resource_database,application_name,"rotate",
  337. X        (char *) NULL,"0");
  338. X      degrees=atoi(resource_value);
  339. X      resource_value=XGetResource(resource_database,application_name,
  340. X        "tiles_per_row","TilesPerRow","0");
  341. X      tiles_per_row=atoi(resource_value);
  342. X      resource_value=XGetResource(resource_database,application_name,"verbose",
  343. X        (char *) NULL,"False");
  344. X      verbose=IsTrue(resource_value);
  345. X    }
  346. X  /*
  347. X    Composite image is the last item on the command line.
  348. X  */
  349. X  write_filename=argv[argc-1];
  350. X  if (access(write_filename,0) == 0)
  351. X    {
  352. X      char
  353. X        answer[2];
  354. X
  355. X      (void) fprintf(stderr,"Overwrite %s? ",write_filename);
  356. X      (void) gets(answer);
  357. X      if (!((*answer == 'y') || (*answer == 'Y')))
  358. X        exit(1);
  359. X    }
  360. X  /*
  361. X    Parse command line.
  362. X  */
  363. X  image_number=0;
  364. X  for (i=1; i < (argc-1); i++)
  365. X  {
  366. X    option=argv[i];
  367. X    if (((int) strlen(option) > 1) && ((*option == '-') || (*option == '+')))
  368. X      switch (*(option+1))
  369. X      {
  370. X        case 'a':
  371. X        {
  372. X          aspect_ratio=(*option == '-');
  373. X          break;
  374. X        }
  375. X        case 'b':
  376. X        {
  377. X          if (strncmp("background",option+1,5) == 0)
  378. X            {
  379. X              resource_info.background_color=(char *) NULL;
  380. X              if (*option == '-')
  381. X                {
  382. X                  i++;
  383. X                  if (i == argc)
  384. X                    Error("missing color on -background",(char *) NULL);
  385. X                  resource_info.background_color=argv[i];
  386. X                }
  387. X              break;
  388. X            }
  389. X          if (strncmp("bordercolor",option+1,7) == 0)
  390. X            {
  391. X              resource_info.border_color=(char *) NULL;
  392. X              if (*option == '-')
  393. X                {
  394. X                  i++;
  395. X                  if (i == argc)
  396. X                    Error("missing color on -bordercolor",(char *) NULL);
  397. X                  resource_info.border_color=argv[i];
  398. X                }
  399. X              break;
  400. X            }
  401. X          if (strncmp("borderwidth",option+1,7) == 0)
  402. X            {
  403. X              resource_info.border_width=0;
  404. X              if (*option == '-')
  405. X                {
  406. X                  i++;
  407. X                  if ((i == argc) || !sscanf(argv[i],"%d",&x))
  408. X                    Error("missing width on -borderwidth",(char *) NULL);
  409. X                  resource_info.border_width=atoi(argv[i]);
  410. X                }
  411. X              break;
  412. X            }
  413. X          Error("unrecognized option",option);
  414. X          break;
  415. X        }
  416. X        case 'c':
  417. X        {
  418. X          if (strncmp("clip",option+1,2) == 0)
  419. X            {
  420. X              clip_geometry=(char *) NULL;
  421. X              if (*option == '-')
  422. X                {
  423. X                  i++;
  424. X                  if (i == argc)
  425. X                    Error("missing geometry on -clip",(char *) NULL);
  426. X                  clip_geometry=argv[i];
  427. X                }
  428. X              break;
  429. X            }
  430. X          if (strncmp("colors",option+1,7) == 0)
  431. X            {
  432. X              resource_info.number_colors=0;
  433. X              if (*option == '-')
  434. X                {
  435. X                  i++;
  436. X                  if ((i == argc) || !sscanf(argv[i],"%d",&x))
  437. X                    Error("missing colors on -colors",(char *) NULL);
  438. X                  resource_info.number_colors=atoi(argv[i]);
  439. X                }
  440. X              break;
  441. X            }
  442. X          if (strncmp("colorspace",option+1,7) == 0)
  443. X            {
  444. X              resource_info.colorspace=RGBColorspace;
  445. X              if (*option == '-')
  446. X                {
  447. X                  i++;
  448. X                  if (i == argc)
  449. X                    Error("missing type on -colorspace",(char *) NULL);
  450. X                  option=argv[i];
  451. X                  resource_info.colorspace=UndefinedColorspace;
  452. X                  if (Latin1Compare("gray",option) == 0)
  453. X                    resource_info.colorspace=GRAYColorspace;
  454. X                  if (Latin1Compare("rgb",option) == 0)
  455. X                    resource_info.colorspace=RGBColorspace;
  456. X                  if (Latin1Compare("yiq",option) == 0)
  457. X                    resource_info.colorspace=YIQColorspace;
  458. X                  if (Latin1Compare("yuv",option) == 0)
  459. X                    resource_info.colorspace=YUVColorspace;
  460. X                  if (Latin1Compare("xyz",option) == 0)
  461. X                    resource_info.colorspace=XYZColorspace;
  462. X                  if (resource_info.colorspace == UndefinedColorspace)
  463. X                    Error("invalid colorspace type on -colorspace",option);
  464. X                }
  465. X              break;
  466. X            }
  467. X          if (strncmp("compress",option+1,5) == 0)
  468. X            {
  469. X              compression=NoCompression;
  470. X              if (*option == '-')
  471. X                {
  472. X                  i++;
  473. X                  if (i == argc)
  474. X                    Error("missing type on -compress",(char *) NULL);
  475. X                  option=argv[i];
  476. X                  if (Latin1Compare("runlengthencoded",option) == 0)
  477. X                    compression=RunlengthEncodedCompression;
  478. X                  else
  479. X                    if (Latin1Compare("qencoded",option) == 0)
  480. X                      compression=QEncodedCompression;
  481. X                    else
  482. X                      Error("invalid compression type on -compress",option);
  483. X                }
  484. X              break;
  485. X            }
  486. X          if (strncmp("compose",option+1,5) == 0)
  487. X            {
  488. X              compose=ReplaceCompositeOp;
  489. X              if (*option == '-')
  490. X                {
  491. X                  i++;
  492. X                  if (i == argc)
  493. X                    Error("missing type on -compose",(char *) NULL);
  494. X                  option=argv[i];
  495. X                  compose=UndefinedCompositeOp;
  496. X                  if (Latin1Compare("over",option) == 0)
  497. X                    compose=OverCompositeOp;
  498. X                  if (Latin1Compare("in",option) == 0)
  499. X                    compose=InCompositeOp;
  500. X                  if (Latin1Compare("out",option) == 0)
  501. X                    compose=OutCompositeOp;
  502. X                  if (Latin1Compare("atop",option) == 0)
  503. X                    compose=AtopCompositeOp;
  504. X                  if (Latin1Compare("xor",option) == 0)
  505. X                    compose=XorCompositeOp;
  506. X                  if (Latin1Compare("plus",option) == 0)
  507. X                    compose=PlusCompositeOp;
  508. X                  if (Latin1Compare("minus",option) == 0)
  509. X                    compose=MinusCompositeOp;
  510. X                  if (Latin1Compare("add",option) == 0)
  511. X                    compose=AddCompositeOp;
  512. X                  if (Latin1Compare("subtract",option) == 0)
  513. X                    compose=SubtractCompositeOp;
  514. X                  if (Latin1Compare("difference",option) == 0)
  515. X                    compose=DifferenceCompositeOp;
  516. X                  if (Latin1Compare("replace",option) == 0)
  517. X                    compose=ReplaceCompositeOp;
  518. X                  if (compose == UndefinedCompositeOp)
  519. X                    Error("invalid compose type on -compose",option);
  520. X                }
  521. X              break;
  522. X            }
  523. X          Error("unrecognized option",option);
  524. X          break;
  525. X        }
  526. X        case 'd':
  527. X        {
  528. X          if (strncmp("density",option+1,3) == 0)
  529. X            {
  530. X              alien_info.density=(char *) NULL;
  531. X              if (*option == '-')
  532. X                {
  533. X                  i++;
  534. X                  if (i == argc)
  535. X                    Error("missing density on -density",(char *) NULL);
  536. X                  alien_info.density=argv[i];
  537. X                }
  538. X              break;
  539. X            }
  540. X          if (strncmp("display",option+1,3) == 0)
  541. X            {
  542. X              server_name=(char *) NULL;
  543. X              if (*option == '-')
  544. X                {
  545. X                  i++;
  546. X                  if (i == argc)
  547. X                    Error("missing server name on -display",(char *) NULL);
  548. X                  server_name=argv[i];
  549. X                }
  550. X              alien_info.server_name=server_name;
  551. X              break;
  552. X            }
  553. X          if (strncmp("dither",option+1,3) == 0)
  554. X            {
  555. X              resource_info.dither=(*option == '-');
  556. X              break;
  557. X            }
  558. X          Error("unrecognized option",option);
  559. X          break;
  560. X        }
  561. X        case 'f':
  562. X        {
  563. X          if (strncmp("font",option+1,3) == 0)
  564. X            {
  565. X              resource_info.font=(char *) NULL;
  566. X              if (*option == '-')
  567. X                {
  568. X                  i++;
  569. X                  if (i == argc)
  570. X                    Error("missing font name on -font",(char *) NULL);
  571. X                  resource_info.font=argv[i];
  572. X                }
  573. X              alien_info.font=resource_info.font;
  574. X              break;
  575. X            }
  576. X          if (strncmp("foreground",option+1,3) == 0)
  577. X            {
  578. X              resource_info.foreground_color=(char *) NULL;
  579. X              if (*option == '-')
  580. X                {
  581. X                  i++;
  582. X                  if (i == argc)
  583. X                    Error("missing foreground on -foreground",(char *) NULL);
  584. X                  resource_info.foreground_color=argv[i];
  585. X                }
  586. X              break;
  587. X            }
  588. X          Error("unrecognized option",option);
  589. X          break;
  590. X        }
  591. X        case 'g':
  592. X        {
  593. X          if (strncmp("gamma",option+1,2) == 0)
  594. X            {
  595. X              gamma=0.0;
  596. X              if (*option == '-')
  597. X                {
  598. X                  i++;
  599. X                  if ((i == argc) || !sscanf(argv[i],"%f",(float *) &x))
  600. X                    Error("missing gamma on -gamma",(char *) NULL);
  601. X                  gamma=atof(argv[i]);
  602. X                }
  603. X              break;
  604. X            }
  605. X          if (strncmp("geometry",option+1,2) == 0)
  606. X            {
  607. X              resource_info.image_geometry=(char *) NULL;
  608. X              if (*option == '-')
  609. X                {
  610. X                  i++;
  611. X                  if (i == argc)
  612. X                    Error("missing geometry on -geometry",(char *) NULL);
  613. X                  resource_info.image_geometry=argv[i];
  614. X                }
  615. X              break;
  616. X            }
  617. X          if (strncmp("gravity",option+1,2) == 0)
  618. X            {
  619. X              resource_info.gravity=CenterGravity;
  620. X              if (*option == '-')
  621. X                {
  622. X                  i++;
  623. X                  if (i == argc)
  624. X                    Error("missing type on -gravity",(char *) NULL);
  625. X                  option=argv[i];
  626. X                  resource_info.gravity=(-1);
  627. X                  if (Latin1Compare("Forget",option) == 0)
  628. X                    resource_info.gravity=ForgetGravity;
  629. X                  if (Latin1Compare("NorthWest",option) == 0)
  630. X                    resource_info.gravity=NorthWestGravity;
  631. X                  if (Latin1Compare("North",option) == 0)
  632. X                    resource_info.gravity=NorthGravity;
  633. X                  if (Latin1Compare("NorthEast",option) == 0)
  634. X                    resource_info.gravity=NorthEastGravity;
  635. X                  if (Latin1Compare("West",option) == 0)
  636. X                    resource_info.gravity=WestGravity;
  637. X                  if (Latin1Compare("Center",option) == 0)
  638. X                    resource_info.gravity=CenterGravity;
  639. X                  if (Latin1Compare("East",option) == 0)
  640. X                    resource_info.gravity=EastGravity;
  641. X                  if (Latin1Compare("SouthWest",option) == 0)
  642. X                    resource_info.gravity=SouthWestGravity;
  643. X                  if (Latin1Compare("South",option) == 0)
  644. X                    resource_info.gravity=SouthGravity;
  645. X                  if (Latin1Compare("SouthEast",option) == 0)
  646. X                    resource_info.gravity=SouthEastGravity;
  647. X                  if (Latin1Compare("Static",option) == 0)
  648. X                    resource_info.gravity=StaticGravity;
  649. X                  if (resource_info.gravity == (-1))
  650. X                    Error("invalid gravity type on -gravity",option);
  651. X                }
  652. X              break;
  653. X            }
  654. X          Error("unrecognized option",option);
  655. X          break;
  656. X        }
  657. X        case 'h':
  658. X        {
  659. X          Usage();
  660. X          break;
  661. X        }
  662. X        case 'm':
  663. X        {
  664. X          resource_info.monochrome=(*option == '-');
  665. X          break;
  666. X        }
  667. X        case 'r':
  668. X        {
  669. X          degrees=0;
  670. X          if (*option == '-')
  671. X            {
  672. X              i++;
  673. X              if ((i == argc) || !sscanf(argv[i],"%d",&x))
  674. X                Error("missing degrees on -rotate",(char *) NULL);
  675. X              degrees=atoi(argv[i]);
  676. X            }
  677. X          break;
  678. X        }
  679. X        case 't':
  680. X        {
  681. X          if (strncmp("tiles_per_row",option+1,3) == 0)
  682. X            {
  683. X              tiles_per_row=0;
  684. X              if (*option == '-')
  685. X                {
  686. X                  i++;
  687. X                  if ((i == argc) || !sscanf(argv[i],"%d",&x))
  688. X                    Error("missing value on -tiles_per_row",(char *) NULL);
  689. X                  tiles_per_row=atoi(argv[i]);
  690. X                }
  691. X              break;
  692. X            }
  693. X          if (strncmp("title",option+1,3) == 0)
  694. X            {
  695. X              resource_info.title=(char *) NULL;
  696. X              if (*option == '-')
  697. X                {
  698. X                  i++;
  699. X                  if (i == argc)
  700. X                    Error("missing title on -title",(char *) NULL);
  701. X                  resource_info.title=argv[i];
  702. X                }
  703. X              break;
  704. X            }
  705. X          if (strncmp("treedepth",option+1,2) == 0)
  706. X            {
  707. X              resource_info.tree_depth=0;
  708. X              if (*option == '-')
  709. X                {
  710. X                  i++;
  711. X                  if ((i == argc) || !sscanf(argv[i],"%d",&x))
  712. X                    Error("missing depth on -treedepth",(char *) NULL);
  713. X                  resource_info.tree_depth=atoi(argv[i]);
  714. X                }
  715. X              break;
  716. X            }
  717. X          Error("unrecognized option",option);
  718. X          break;
  719. X        }
  720. X        case 'v':
  721. X        {
  722. X          verbose=(*option == '-');
  723. X          alien_info.verbose=verbose;
  724. X          break;
  725. X        }
  726. X        default:
  727. X        {
  728. X          Error("unrecognized option",option);
  729. X          break;
  730. X        }
  731. X      }
  732. X    else
  733. X      {
  734. X        char
  735. X          geometry[2048];
  736. X
  737. X        Image
  738. X          *image,
  739. X          info_image;
  740. X
  741. X        unsigned int
  742. X          scale_factor,
  743. X          tile_height,
  744. X          tile_width;
  745. X
  746. X        /*
  747. X          Option is a file name: begin by reading image from specified file.
  748. X        */
  749. X        start_time=time((time_t *) 0);
  750. X        (void) strcpy(alien_info.filename,option);
  751. X        image=ReadAlienImage(&alien_info);
  752. X        if (image == (Image *) NULL)
  753. X          if (*option == '-')
  754. X            break;
  755. X          else
  756. X            continue;
  757. X        do
  758. X        {
  759. X          if (image->scene == 0)
  760. X            image->scene=image_number;
  761. X          info_image=(*image);
  762. X          /*
  763. X            Allocate and initialize image label.
  764. X          */
  765. X          image->label=(char *)
  766. X            malloc(((strlen(image->filename)+2048)*sizeof(char)));
  767. X          if (image->label == (char *) NULL)
  768. X            Error("unable to montage images","memory allocation failed");
  769. X          (void) sprintf(image->label,"%s %ux%u\0",image->filename,
  770. X            image->columns,image->rows);
  771. X          /*
  772. X            Tile size maintains the aspect ratio of the image.
  773. X          */
  774. X          tile_width=256;
  775. X          tile_height=256;
  776. X          if (resource_info.image_geometry != (char *) NULL)
  777. X            {
  778. X              int
  779. X                y;
  780. X
  781. X              XParseGeometry(resource_info.image_geometry,&x,&y,&tile_width,
  782. X                &tile_height);
  783. X              if ((tile_width == 0) || (tile_height == 0))
  784. X                Error("invalid image geometry",resource_info.image_geometry);
  785. X            }
  786. X          if (aspect_ratio)
  787. X            {
  788. X              /*
  789. X                Respect aspect ratio of the image.
  790. X              */
  791. X              scale_factor=UpShift(tile_width)/image->columns;
  792. X              if (scale_factor > (UpShift(tile_height)/image->rows))
  793. X                scale_factor=UpShift(tile_height)/image->rows;
  794. X              tile_width=DownShift(image->columns*scale_factor);
  795. X              tile_height=DownShift(image->rows*scale_factor);
  796. X            }
  797. X          (void) sprintf(geometry,"%ux%u\0",tile_width,tile_height);
  798. X          TransformImage(&image,clip_geometry,geometry,(char *) NULL);
  799. X          if ((degrees % 360) != 0)
  800. X            {
  801. X              Image
  802. X                *rotated_image;
  803. X
  804. X              /*
  805. X                Rotate image.
  806. X              */
  807. X              rotated_image=RotateImage(image,(double) degrees,False);
  808. X              if (rotated_image != (Image *) NULL)
  809. X                {
  810. X                  DestroyImage(image);
  811. X                  image=rotated_image;
  812. X                }
  813. X            }
  814. X          if (gamma > 0.0)
  815. X            GammaImage(image,gamma);
  816. X          if (verbose)
  817. X            {
  818. X              /*
  819. X                Display detailed info about the image.
  820. X              */
  821. X              (void) fprintf(stderr,"[%u] %s",
  822. X                image->scene == 0 ? image_number : image->scene,
  823. X                image->filename);
  824. X              (void) fprintf(stderr," %ux%u",info_image.columns,
  825. X                info_image.rows);
  826. X              if ((info_image.columns != image->columns) ||
  827. X                  (info_image.rows != image->rows))
  828. X                (void) fprintf(stderr,"=>%ux%u",image->columns,image->rows);
  829. X              if (image->class == DirectClass)
  830. X                (void) fprintf(stderr," DirectClass");
  831. X              else
  832. X                (void) fprintf(stderr," PseudoClass %uc",image->colors);
  833. X              (void) fprintf(stderr," %s\n",image->magick);
  834. X            }
  835. X          /*
  836. X            Pack image data to conserve memory (memory <=> speed).
  837. X          */
  838. X          (void) PackImage(image);
  839. X          (void) free((char *) image->pixels);
  840. X          image->pixels=(RunlengthPacket *) NULL;
  841. X          if (image_number == maximum_images)
  842. X            {
  843. X              /*
  844. X                Increase size of images array.
  845. X              */
  846. X              maximum_images<<=1;
  847. X              images=(Image **)
  848. X                realloc((char *) images,maximum_images*sizeof(Image *));
  849. X              if (images == (Image **) NULL)
  850. X                Error("unable to montage images","memory allocation failed");
  851. X            }
  852. X          images[image_number++]=image;
  853. X          image=image->next;
  854. X        } while (image != (Image *) NULL);
  855. X      }
  856. X    }
  857. X  if (image_number == 0)
  858. X    Error("missing an image file name",(char *) NULL);
  859. X  /*
  860. X    Create composite image.
  861. X  */
  862. X  montage_image=MontageImage(display,&resource_info,compose,tiles_per_row,
  863. X    images,image_number);
  864. X  if (resource_info.colorspace == GRAYColorspace)
  865. X    QuantizeImage(montage_image,256,8,resource_info.dither,GRAYColorspace,True);
  866. X  if (resource_info.monochrome)
  867. X    QuantizeImage(montage_image,2,8,resource_info.dither,GRAYColorspace,True);
  868. X  if (resource_info.number_colors > 0)
  869. X    QuantizeImage(montage_image,resource_info.number_colors,
  870. X      resource_info.tree_depth,resource_info.dither,resource_info.colorspace,
  871. X      True);
  872. X  if (compression != UndefinedCompression)
  873. X    montage_image->compression=compression;
  874. X  strcpy(montage_image->filename,write_filename);
  875. X  (void) WriteAlienImage(montage_image);
  876. X  if (verbose)
  877. X    {
  878. X      /*
  879. X        Display detailed info about the image.
  880. X      */
  881. X      if (montage_image->class == DirectClass)
  882. X        montage_image->colors=NumberColors(montage_image);
  883. X      (void) fprintf(stderr,"[%u] %s %ux%u",montage_image->scene,
  884. X        montage_image->filename,montage_image->columns,montage_image->rows);
  885. X      if (montage_image->class == DirectClass)
  886. X        (void) fprintf(stderr," DirectClass ");
  887. X      else
  888. X        (void) fprintf(stderr," PseudoClass ");
  889. X      (void) fprintf(stderr,"%uc %s %lds\n",montage_image->colors,
  890. X        montage_image->magick,time((time_t *) 0)-start_time+1);
  891. X    }
  892. X  DestroyImage(montage_image);
  893. X  (void) free((char *) images);
  894. X  if (display != (Display *) NULL)
  895. X    XCloseDisplay(display);
  896. X  return(False);
  897. }
  898. SHAR_EOF
  899. echo 'File ImageMagick/montage.c is complete' &&
  900. chmod 0644 ImageMagick/montage.c ||
  901. echo 'restore of ImageMagick/montage.c failed'
  902. Wc_c="`wc -c < 'ImageMagick/montage.c'`"
  903. test 49738 -eq "$Wc_c" ||
  904.     echo 'ImageMagick/montage.c: original size 49738, current size' "$Wc_c"
  905. rm -f _shar_wnt_.tmp
  906. fi
  907. # ============= ImageMagick/alien.c ==============
  908. if test -f 'ImageMagick/alien.c' -a X"$1" != X"-c"; then
  909.     echo 'x - skipping ImageMagick/alien.c (File already exists)'
  910.     rm -f _shar_wnt_.tmp
  911. else
  912. > _shar_wnt_.tmp
  913. echo 'x - extracting ImageMagick/alien.c (Text)'
  914. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/alien.c' &&
  915. /*
  916. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  917. %                                                                             %
  918. %                                                                             %
  919. %                                                                             %
  920. %                        AAA   L      IIIII EEEEE  N   N                      %
  921. %                       A   A  L        I   E      NN  N                      %
  922. %                       AAAAA  L        I   EEE    N N N                      %
  923. %                       A   A  L        I   E      N  NN                      %
  924. %                       A   A  LLLLL  IIIII EEEEE  N   N                      %
  925. %                                                                             %
  926. %                                                                             %
  927. %                  Utility Routines to Read Alien Image Formats               %
  928. %                                                                             %
  929. %                                                                             %
  930. %                                                                             %
  931. %                             Software Design                                 %
  932. %                               John Cristy                                   %
  933. %                              January 1992                                   %
  934. %                                                                             %
  935. %                                                                             %
  936. %  Copyright 1992 E. I. du Pont de Nemours & Company                          %
  937. %                                                                             %
  938. %  Permission to use, copy, modify, distribute, and sell this software and    %
  939. %  its documentation for any purpose is hereby granted without fee,           %
  940. %  provided that the above Copyright notice appear in all copies and that     %
  941. %  both that Copyright notice and this permission notice appear in            %
  942. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  943. %  & Company not be used in advertising or publicity pertaining to            %
  944. %  distribution of the software without specific, written prior               %
  945. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  946. %  about the suitability of this software for any purpose.  It is provided    %
  947. %  "as is" without express or implied warranty.                               %
  948. %                                                                             %
  949. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  950. %  to this software, including all implied warranties of merchantability      %
  951. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  952. %  liable for any special, indirect or consequential damages or any           %
  953. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  954. %  in an action of contract, negligence or other tortious action, arising     %
  955. %  out of or in connection with the use or performance of this software.      %
  956. %                                                                             %
  957. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  958. %
  959. %  Functions in this library convert to and from `alien' image formats to the
  960. %  MIFF image format.
  961. %
  962. %
  963. */
  964. X
  965. /*
  966. X  Include declarations.
  967. */
  968. #include "display.h"
  969. #include "image.h"
  970. #include "alien.h"
  971. #include "compress.h"
  972. #include "X.h"
  973. X
  974. /*
  975. X  Global declarations.
  976. */
  977. static char
  978. X  *AlienTypes[]=
  979. X  {
  980. X    "AVS",
  981. X    "CMYK",
  982. X    "FAX",
  983. X    "GIF",
  984. X    "GRAY",
  985. X    "JPEG",
  986. X    "JPG",
  987. X    "MIFF",
  988. X    "MTV",
  989. X    "PNM",
  990. X    "PS",
  991. X    "RGB",
  992. X    "RLE",
  993. X    "SUN",
  994. X    "TEXT",
  995. X    "TIFF",
  996. X    "VICAR",
  997. X    "YUV",
  998. X    "X",
  999. X    "XC",
  1000. X    "XBM",
  1001. X    "XWD",
  1002. X    (char *) NULL,
  1003. X  };
  1004. X
  1005. /*
  1006. X  External declarations.
  1007. */
  1008. extern char
  1009. X  *application_name;
  1010. X
  1011. /*
  1012. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1013. %                                                                             %
  1014. %                                                                             %
  1015. %                                                                             %
  1016. %  M S B F i r s t O r d e r L o n g                                          %
  1017. %                                                                             %
  1018. %                                                                             %
  1019. %                                                                             %
  1020. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1021. %
  1022. %  Function MSBFirstOrderLong converts a least-significant byte first buffer
  1023. %  of long integers to most-significant byte first.
  1024. %
  1025. %  The format of the MSBFirstOrderLong routine is:
  1026. %
  1027. %       MSBFirstOrderLong(p,length);
  1028. %
  1029. %  A description of each parameter follows.
  1030. %
  1031. %   o  p:  Specifies a pointer to a buffer of integers.
  1032. %
  1033. %   o  length:  Specifies the length of the buffer.
  1034. %
  1035. %
  1036. */
  1037. static void MSBFirstOrderLong(p,length)
  1038. register char
  1039. X  *p;
  1040. X
  1041. register unsigned
  1042. X  length;
  1043. {
  1044. X  register char
  1045. X    c,
  1046. X    *q,
  1047. X    *sp;
  1048. X
  1049. X  q=p+length;
  1050. X  while (p < q)
  1051. X  {
  1052. X    sp=p+3;
  1053. X    c=(*sp);
  1054. X    *sp=(*p);
  1055. X    *p++=c;
  1056. X    sp=p+1;
  1057. X    c=(*sp);
  1058. X    *sp=(*p);
  1059. X    *p++=c;
  1060. X    p+=2;
  1061. X  }
  1062. }
  1063. X
  1064. /*
  1065. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1066. %                                                                             %
  1067. %                                                                             %
  1068. %                                                                             %
  1069. %  M S B F i r s t O r d e r S h o r t                                        %
  1070. %                                                                             %
  1071. %                                                                             %
  1072. %                                                                             %
  1073. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1074. %
  1075. %  Function MSBFirstOrderShort converts a least-significant byte first buffer
  1076. %  of short integers to most-significant byte first.
  1077. %
  1078. %  The format of the MSBFirstOrderShort routine is:
  1079. %
  1080. %       MSBFirstOrderLongShort(p,length);
  1081. %
  1082. %  A description of each parameter follows.
  1083. %
  1084. %   o  p:  Specifies a pointer to a buffer of integers.
  1085. %
  1086. %   o  length:  Specifies the length of the buffer.
  1087. %
  1088. %
  1089. */
  1090. static void MSBFirstOrderShort(p,length)
  1091. register char
  1092. X  *p;
  1093. X
  1094. register unsigned
  1095. X  length;
  1096. {
  1097. X  register char
  1098. X    c,
  1099. X    *q;
  1100. X
  1101. X  q=p+length;
  1102. X  while (p < q)
  1103. X  {
  1104. X    c=(*p);
  1105. X    *p=(*(p+1));
  1106. X    p++;
  1107. X    *p++=c;
  1108. X  }
  1109. }
  1110. X
  1111. /*
  1112. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1113. %                                                                             %
  1114. %                                                                             %
  1115. %                                                                             %
  1116. %  R e a d A V S I m a g e                                                    %
  1117. %                                                                             %
  1118. %                                                                             %
  1119. %                                                                             %
  1120. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1121. %
  1122. %  Function ReadAVSImage reads an image file and returns it.  It allocates the
  1123. %  memory necessary for the new Image structure and returns a pointer to the
  1124. %  new image.
  1125. %
  1126. %  The format of the ReadAVSImage routine is:
  1127. %
  1128. %      image=ReadAVSImage(alien_info)
  1129. %
  1130. %  A description of each parameter follows:
  1131. %
  1132. %    o image:  Function ReadAVSImage returns a pointer to the image after
  1133. %      reading. A null image is returned if there is a a memory shortage or if
  1134. %      the image cannot be read.
  1135. %
  1136. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  1137. %
  1138. %
  1139. */
  1140. static Image *ReadAVSImage(alien_info)
  1141. AlienInfo
  1142. X  *alien_info;
  1143. {
  1144. X  typedef struct _Rasterfile
  1145. X  {
  1146. X    int
  1147. X      width,
  1148. X      height;
  1149. X  } Rasterfile;
  1150. X
  1151. X  Image
  1152. X    *image;
  1153. X
  1154. X  Rasterfile
  1155. X    avs_header;
  1156. X
  1157. X  register int
  1158. X    i;
  1159. X
  1160. X  register unsigned char
  1161. X    *p;
  1162. X
  1163. X  register RunlengthPacket
  1164. X    *q;
  1165. X
  1166. X  unsigned char
  1167. X    *avs_pixels;
  1168. X
  1169. X  unsigned int
  1170. X    status;
  1171. X
  1172. X  /*
  1173. X    Allocate image structure.
  1174. X  */
  1175. X  image=AllocateImage("AVS");
  1176. X  if (image == (Image *) NULL)
  1177. X    return((Image *) NULL);
  1178. X  /*
  1179. X    Open image file.
  1180. X  */
  1181. X  (void) strcpy(image->filename,alien_info->filename);
  1182. X  OpenImage(image,"r");
  1183. X  if (image->file == (FILE *) NULL)
  1184. X    {
  1185. X      Warning("unable to open file",image->filename);
  1186. X      DestroyImage(image);
  1187. X      return((Image *) NULL);
  1188. X    }
  1189. X  /*
  1190. X    Read AVS image.
  1191. X  */
  1192. X  status=ReadData((char *) &avs_header,1,sizeof(Rasterfile),image->file);
  1193. X  if (status == False)
  1194. X    {
  1195. X      Warning("not a AVS image file",(char *) NULL);
  1196. X      DestroyImage(image);
  1197. X      return((Image *) NULL);
  1198. X    }
  1199. X  do
  1200. X  {
  1201. X    avs_pixels=(unsigned char *)
  1202. X      malloc(4*avs_header.width*avs_header.height*sizeof(unsigned char));
  1203. X    if (avs_pixels == (unsigned char *) NULL)
  1204. X      {
  1205. X        Warning("memory allocation error",(char *) NULL);
  1206. X        DestroyImages(image);
  1207. X        return((Image *) NULL);
  1208. X      }
  1209. X    (void) ReadData((char *) avs_pixels,sizeof(unsigned char),
  1210. X      avs_header.width*avs_header.height*4,image->file);
  1211. X    /*
  1212. X      Create image.
  1213. X    */
  1214. X    image->alpha=True;
  1215. X    image->columns=avs_header.width;
  1216. X    image->rows=avs_header.height;
  1217. X    image->packets=image->columns*image->rows;
  1218. X    image->pixels=(RunlengthPacket *)
  1219. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1220. X    image->comments=(char *)
  1221. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  1222. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  1223. X        (image->comments == (char *) NULL))
  1224. X      {
  1225. X        Warning("memory allocation error",(char *) NULL);
  1226. X        DestroyImages(image);
  1227. X        return((Image *) NULL);
  1228. X      }
  1229. X    (void) sprintf(image->comments,"\n  Imported from AVS raster image:  %s\n",
  1230. X      image->filename);
  1231. X    /*
  1232. X      Convert AVS raster image to runlength-encoded packets.
  1233. X    */
  1234. X    p=avs_pixels;
  1235. X    q=image->pixels;
  1236. X    for (i=0; i < (image->columns*image->rows); i++)
  1237. X    {
  1238. X      q->index=(unsigned char) (*p++);
  1239. X      q->red=(*p++);
  1240. X      q->green=(*p++);
  1241. X      q->blue=(*p++);
  1242. X      q->length=0;
  1243. X      q++;
  1244. X    }
  1245. X    (void) free((char *) avs_pixels);
  1246. X    status=ReadData((char *) &avs_header,1,sizeof(Rasterfile),image->file);
  1247. X    if (status == True)
  1248. X      {
  1249. X        /*
  1250. X          Allocate image structure.
  1251. X        */
  1252. X        image->next=AllocateImage("AVS");
  1253. X        if (image->next == (Image *) NULL)
  1254. X          {
  1255. X            DestroyImages(image);
  1256. X            return((Image *) NULL);
  1257. X          }
  1258. X        image->next->file=image->file;
  1259. X        (void) sprintf(image->next->filename,"%s.%u\0",alien_info->filename,
  1260. X          image->scene+1);
  1261. X        image->next->scene=image->scene+1;
  1262. X        image->next->last=image;
  1263. X        image=image->next;
  1264. X      }
  1265. X  } while (status == True);
  1266. X  while (image->last != (Image *) NULL)
  1267. X    image=image->last;
  1268. X  CloseImage(image);
  1269. X  return(image);
  1270. }
  1271. X
  1272. /*
  1273. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1274. %                                                                             %
  1275. %                                                                             %
  1276. %                                                                             %
  1277. %  R e a d C M Y K I m a g e                                                  %
  1278. %                                                                             %
  1279. %                                                                             %
  1280. %                                                                             %
  1281. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1282. %
  1283. %  Function ReadCMYKImage reads an image file and returns it.  It allocates the
  1284. %  memory necessary for the new Image structure and returns a pointer to the
  1285. %  new image.
  1286. %
  1287. %  The format of the ReadCMYKImage routine is:
  1288. %
  1289. %      image=ReadCMYKImage(alien_info)
  1290. %
  1291. %  A description of each parameter follows:
  1292. %
  1293. %    o image:  Function ReadCMYKImage returns a pointer to the image after
  1294. %      reading.  A null image is returned if there is a a memory shortage or
  1295. %      if the image cannot be read.
  1296. %
  1297. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  1298. %
  1299. %
  1300. */
  1301. static Image *ReadCMYKImage(alien_info)
  1302. AlienInfo
  1303. X  *alien_info;
  1304. {
  1305. X  Image
  1306. X    *image;
  1307. X
  1308. X  int
  1309. X    x,
  1310. X    y;
  1311. X
  1312. X  register int
  1313. X    i;
  1314. X
  1315. X  register RunlengthPacket
  1316. X    *q;
  1317. X
  1318. X  register unsigned char
  1319. X    *p;
  1320. X
  1321. X  unsigned char
  1322. X    black,
  1323. X    *cmyk_pixels,
  1324. X    cyan,
  1325. X    magenta,
  1326. X    yellow;
  1327. X
  1328. X  unsigned int
  1329. X    height,
  1330. X    width;
  1331. X
  1332. X  /*
  1333. X    Allocate image structure.
  1334. X  */
  1335. X  image=AllocateImage("CMYK");
  1336. X  if (image == (Image *) NULL)
  1337. X    return((Image *) NULL);
  1338. X  /*
  1339. X    Open image file.
  1340. X  */
  1341. X  (void) strcpy(image->filename,alien_info->filename);
  1342. X  OpenImage(image,"r");
  1343. X  if (image->file == (FILE *) NULL)
  1344. X    {
  1345. X      Warning("unable to open file",image->filename);
  1346. X      DestroyImage(image);
  1347. X      return((Image *) NULL);
  1348. X    }
  1349. X  /*
  1350. X    Create image.
  1351. X  */
  1352. X  width=512;
  1353. X  height=512;
  1354. X  if (alien_info->geometry != (char *) NULL)
  1355. X    (void) XParseGeometry(alien_info->geometry,&x,&y,&width,&height);
  1356. X  image->columns=width;
  1357. X  image->rows=height;
  1358. X  image->packets=image->columns*image->rows;
  1359. X  cmyk_pixels=(unsigned char *)
  1360. X    malloc((unsigned int) image->packets*4*sizeof(unsigned char));
  1361. X  image->pixels=(RunlengthPacket *)
  1362. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1363. X  if ((cmyk_pixels == (unsigned char *) NULL) ||
  1364. X      (image->pixels == (RunlengthPacket *) NULL))
  1365. X    {
  1366. X      Warning("unable to open file",image->filename);
  1367. X      DestroyImage(image);
  1368. X      return((Image *) NULL);
  1369. X    }
  1370. X  /*
  1371. X    Convert raster image to runlength-encoded packets.
  1372. X  */
  1373. X  (void) ReadData((char *) cmyk_pixels,4,(int) (image->columns*image->rows),
  1374. X    image->file);
  1375. X  p=cmyk_pixels;
  1376. X  q=image->pixels;
  1377. X  for (i=0; i < (image->columns*image->rows); i++)
  1378. X  {
  1379. X    cyan=(*p++);
  1380. X    magenta=(*p++);
  1381. X    yellow=(*p++);
  1382. X    black=(*p++);
  1383. X    if ((unsigned int) (cyan+black) > MaxRGB)
  1384. X      q->red=0;
  1385. X    else
  1386. X      q->red=MaxRGB-(cyan+black);
  1387. X    if ((unsigned int) (magenta+black) > MaxRGB)
  1388. X      q->green=0;
  1389. X    else
  1390. X      q->green=MaxRGB-(magenta+black);
  1391. X    if ((unsigned int) (yellow+black) > MaxRGB)
  1392. X      q->blue=0;
  1393. X    else
  1394. X      q->blue=MaxRGB-(yellow+black);
  1395. X    q->index=0;
  1396. X    q->length=0;
  1397. X    q++;
  1398. X  }
  1399. X  (void) free((char *) cmyk_pixels);
  1400. X  CloseImage(image);
  1401. X  return(image);
  1402. }
  1403. X
  1404. /*
  1405. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1406. %                                                                             %
  1407. %                                                                             %
  1408. %                                                                             %
  1409. %  R e a d G I F I m a g e                                                    %
  1410. %                                                                             %
  1411. %                                                                             %
  1412. %                                                                             %
  1413. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1414. %
  1415. %  Function ReadGIFImage reads an image file and returns it.  It allocates the
  1416. %  memory necessary for the new Image structure and returns a pointer to the
  1417. %  new image.
  1418. %
  1419. %  The format of the ReadGIFImage routine is:
  1420. %
  1421. %      image=ReadGIFImage(alien_info)
  1422. %
  1423. %  A description of each parameter follows:
  1424. %
  1425. %    o image:  Function ReadGIFImage returns a pointer to the image after
  1426. %      reading.  A null image is returned if there is a a memory shortage or
  1427. %      an error occurs.
  1428. %
  1429. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  1430. %
  1431. %
  1432. */
  1433. static Image *ReadGIFImage(alien_info)
  1434. AlienInfo
  1435. X  *alien_info;
  1436. {
  1437. #define BitSet(byte,bit)  (((byte) & (bit)) == (bit))
  1438. #define LSBFirstOrder(x,y)  (((y) << 8) | (x))
  1439. X
  1440. X  Image
  1441. X    *image;
  1442. X
  1443. X  int
  1444. X    pass,
  1445. X    status,
  1446. X    x,
  1447. X    y;
  1448. X
  1449. X  register int
  1450. X    i;
  1451. X
  1452. X  unsigned char
  1453. X    c,
  1454. X    *colormap,
  1455. X    header[2048],
  1456. X    magick[12];
  1457. X
  1458. X  unsigned int
  1459. X    image_count,
  1460. X    interlace,
  1461. X    local_colormap;
  1462. X
  1463. X  /*
  1464. X    Allocate image structure.
  1465. X  */
  1466. X  image=AllocateImage("GIF");
  1467. X  if (image == (Image *) NULL)
  1468. X    return((Image *) NULL);
  1469. X  /*
  1470. X    Open image file.
  1471. X  */
  1472. X  (void) strcpy(image->filename,alien_info->filename);
  1473. X  OpenImage(image,"r");
  1474. X  if (image->file == (FILE *) NULL)
  1475. X    {
  1476. X      Warning("unable to open file",image->filename);
  1477. X      DestroyImage(image);
  1478. X      return((Image *) NULL);
  1479. X    }
  1480. X  /*
  1481. X    Determine if this is a GIF file.
  1482. X  */
  1483. X  status=ReadData((char *) magick,1,6,image->file);
  1484. X  if ((status == False) || ((strncmp((char *) magick,"GIF87",5) != 0) &&
  1485. X      (strncmp((char *) magick,"GIF89",5) != 0)))
  1486. X    {
  1487. X      Warning("not a GIF image file",(char *) NULL);
  1488. X      DestroyImage(image);
  1489. X      return((Image *) NULL);
  1490. X    }
  1491. X  /*
  1492. X    Read the screen descriptor.
  1493. X  */
  1494. X  status=ReadData((char *) header,1,7,image->file);
  1495. X  if (status == False)
  1496. X    {
  1497. X      Warning("failed to read screen descriptor",(char *) NULL );
  1498. X      DestroyImage(image);
  1499. X      return((Image *) NULL);
  1500. X    }
  1501. X  image->colors=1 << ((header[4] & 0x07)+1);
  1502. X  if (BitSet(header[4],0x80))
  1503. X    {
  1504. X      unsigned char
  1505. X        *p;
  1506. X
  1507. X      /*
  1508. X        Read global colormap.
  1509. X      */
  1510. X      image->class=PseudoClass;
  1511. X      colormap=(unsigned char *) malloc(3*image->colors*sizeof(unsigned char));
  1512. X      image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  1513. X      if ((colormap == (unsigned char *) NULL) ||
  1514. X          (image->colormap == (ColorPacket *) NULL))
  1515. X        {
  1516. X          Warning("unable to read image colormap","memory allocation failed");
  1517. X          DestroyImage(image);
  1518. X          return((Image *) NULL);
  1519. X        }
  1520. X      (void) ReadData((char *) colormap,1,(int) image->colors*3,image->file);
  1521. X      p=colormap;
  1522. X      for (i=0; i < image->colors; i++)
  1523. X      {
  1524. X        image->colormap[i].red=(*p++);
  1525. X        image->colormap[i].green=(*p++);
  1526. X        image->colormap[i].blue=(*p++);
  1527. X      }
  1528. X      (void) free((char *) colormap);
  1529. X    }
  1530. X  image_count=0;
  1531. X  while (1)
  1532. X  {
  1533. X    status=ReadData((char *) &c,1,1,image->file);
  1534. X    if (status == False)
  1535. X      break;
  1536. X    if (c == ';')
  1537. X      break;  /* terminator */
  1538. X    if (c == '!')
  1539. X      {
  1540. X        /*
  1541. X          GIF Extension block.
  1542. X        */
  1543. X        status=ReadData((char *) &c,1,1,image->file);
  1544. X        if (status == False)
  1545. X          {
  1546. X            Warning("unable to read extention block",(char *) NULL);
  1547. X            DestroyImage(image);
  1548. X            return((Image *) NULL);
  1549. X          }
  1550. X        if (c != 0xfe)
  1551. X          while (ReadDataBlock((char *) header,image->file) != 0);
  1552. X        else
  1553. X          while (ReadDataBlock((char *) header,image->file) != 0)
  1554. X          {
  1555. X            /*
  1556. X              Comment extension block.
  1557. X            */
  1558. X            if (image->comments != (char *) NULL)
  1559. X              image->comments=(char *) realloc((char *) image->comments,
  1560. X                (unsigned int) (strlen(image->comments)+
  1561. X                strlen((char *) header)+1));
  1562. X            else
  1563. X              {
  1564. X                image->comments=(char *)
  1565. X                  malloc((strlen((char *) header)+1)*sizeof(char));
  1566. X                *image->comments='\0';
  1567. X              }
  1568. X            if (image->comments == (char *) NULL)
  1569. X              {
  1570. X                Warning("memory allocation error",(char *) NULL);
  1571. X                DestroyImage(image);
  1572. X                return((Image *) NULL);
  1573. X              }
  1574. X            (void) strcat(image->comments,(char *) header);
  1575. X          }
  1576. X      }
  1577. X    if (c != ',')
  1578. X      continue;
  1579. X    /*
  1580. X      Read image attributes.
  1581. X    */
  1582. X    if (image_count > 0)
  1583. X      {
  1584. X        /*
  1585. X          Allocate image structure.
  1586. X        */
  1587. X        CompressColormap(image);
  1588. X        image->next=AllocateImage("GIF");
  1589. X        if (image->next == (Image *) NULL)
  1590. X          {
  1591. X            DestroyImages(image);
  1592. X            return((Image *) NULL);
  1593. X          }
  1594. X        image->next->file=image->file;
  1595. X        (void) sprintf(image->next->filename,"%s.%u\0",alien_info->filename,
  1596. X          image->scene+1);
  1597. X        image->next->scene=image->scene+1;
  1598. X        image->next->last=image;
  1599. X        image=image->next;
  1600. X      }
  1601. X    image_count++;
  1602. X    status=ReadData((char *) header,1,9,image->file);
  1603. X    if (status == False)
  1604. X      {
  1605. X        Warning("unable to read left/top/width/height",(char *) NULL);
  1606. X        DestroyImage(image);
  1607. X        return((Image *) NULL);
  1608. X      }
  1609. X    interlace=BitSet(header[8],0x40);
  1610. X    local_colormap=BitSet(header[8],0x80);
  1611. X    /*
  1612. X      Allocate image.
  1613. X    */
  1614. X    image->columns=LSBFirstOrder(header[4],header[5]);
  1615. X    image->rows=LSBFirstOrder(header[6],header[7]);
  1616. X    image->packets=image->columns*image->rows;
  1617. X    if (image->pixels != (RunlengthPacket *) NULL)
  1618. X      (void) free((char *) image->pixels);
  1619. X    image->pixels=(RunlengthPacket *)
  1620. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1621. X    if (image->pixels == (RunlengthPacket *) NULL)
  1622. X      {
  1623. X        Warning("memory allocation error",(char *) NULL);
  1624. X        DestroyImage(image);
  1625. X        return((Image *) NULL);
  1626. X      }
  1627. X    if (local_colormap)
  1628. X      {
  1629. X        unsigned char
  1630. X          *p;
  1631. X
  1632. X        /*
  1633. X          Read local colormap.
  1634. X        */
  1635. X        image->class=PseudoClass;
  1636. X        image->colors=1 << ((header[8] & 0x07)+1);
  1637. X        colormap=(unsigned char *)
  1638. X          malloc(3*image->colors*sizeof(unsigned char));
  1639. X        if (image->colormap != (ColorPacket *) NULL)
  1640. X          (void) free((char *) image->colormap);
  1641. X        image->colormap=(ColorPacket *)
  1642. X          malloc(image->colors*sizeof(ColorPacket));
  1643. X        if ((colormap == (unsigned char *) NULL) ||
  1644. X            (image->colormap == (ColorPacket *) NULL))
  1645. X          {
  1646. X            Warning("unable to read image","memory allocation failed");
  1647. X            DestroyImage(image);
  1648. X            return((Image *) NULL);
  1649. X          }
  1650. X        (void) ReadData((char *) colormap,1,(int) image->colors*3,image->file);
  1651. X        p=colormap;
  1652. X        for (i=0; i < image->colors; i++)
  1653. X        {
  1654. X          image->colormap[i].red=(*p++);
  1655. X          image->colormap[i].green=(*p++);
  1656. X          image->colormap[i].blue=(*p++);
  1657. X        }
  1658. X        (void) free((char *) colormap);
  1659. X      }
  1660. X    /*
  1661. X      Decode image.
  1662. X    */
  1663. X    status=LZWDecodeImage(image);
  1664. X    if (status == False)
  1665. X      {
  1666. X        Warning("unable to read image data",(char *) NULL);
  1667. X        DestroyImage(image);
  1668. X        return((Image *) NULL);
  1669. X      }
  1670. X    if (interlace)
  1671. X      {
  1672. X        Image
  1673. X          *interlaced_image;
  1674. X
  1675. X        register RunlengthPacket
  1676. X          *p,
  1677. X          *q;
  1678. X
  1679. X        static int
  1680. X          interlace_rate[4] = { 8, 8, 4, 2 },
  1681. X          interlace_start[4] = { 0, 4, 2, 1 };
  1682. X
  1683. X        /*
  1684. X          Interlace image.
  1685. X        */
  1686. X        interlaced_image=image;
  1687. X        image=CopyImage(interlaced_image,interlaced_image->columns,
  1688. X          interlaced_image->rows,False);
  1689. X        if (image == (Image *) NULL)
  1690. X          {
  1691. X            Warning("unable to read image","memory allocation failed");
  1692. X            DestroyImage(interlaced_image);
  1693. X            return((Image *) NULL);
  1694. X          }
  1695. X        p=interlaced_image->pixels;
  1696. X        q=image->pixels;
  1697. X        for (pass=0; pass < 4; pass++)
  1698. X        {
  1699. X          y=interlace_start[pass];
  1700. X          while (y < image->rows)
  1701. X          {
  1702. X            q=image->pixels+(y*image->columns);
  1703. X            for (x=0; x < image->columns; x++)
  1704. X              *q++=(*p++);
  1705. X            y+=interlace_rate[pass];
  1706. X          }
  1707. X        }
  1708. X        DestroyImage(interlaced_image);
  1709. X      }
  1710. X  }
  1711. X  CompressColormap(image);
  1712. X  while (image->last != (Image *) NULL)
  1713. X    image=image->last;
  1714. X  CloseImage(image);
  1715. X  return(image);
  1716. }
  1717. X
  1718. /*
  1719. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1720. %                                                                             %
  1721. %                                                                             %
  1722. %                                                                             %
  1723. %  R e a d G R A Y I m a g e                                                  %
  1724. %                                                                             %
  1725. %                                                                             %
  1726. %                                                                             %
  1727. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1728. %
  1729. %  Function ReadGRAYImage reads an image file and returns it.  It allocates the
  1730. %  memory necessary for the new Image structure and returns a pointer to the
  1731. %  new image.
  1732. %
  1733. %  The format of the ReadGRAYImage routine is:
  1734. %
  1735. %      image=ReadGRAYImage(alien_info)
  1736. %
  1737. %  A description of each parameter follows:
  1738. %
  1739. %    o image:  Function ReadGRAYImage returns a pointer to the image after
  1740. %      reading.  A null image is returned if there is a a memory shortage or
  1741. %      if the image cannot be read.
  1742. %
  1743. %    o alien_info: Specifies a pointer to an AlienInfo structure.
  1744. %
  1745. %
  1746. */
  1747. static Image *ReadGRAYImage(alien_info)
  1748. AlienInfo
  1749. X  *alien_info;
  1750. {
  1751. X  Image
  1752. X    *image;
  1753. X
  1754. X  int
  1755. X    x,
  1756. X    y;
  1757. X
  1758. X  register int
  1759. X    i;
  1760. X
  1761. X  register RunlengthPacket
  1762. X    *q;
  1763. X
  1764. X  register unsigned char
  1765. X    index,
  1766. X    *p;
  1767. X
  1768. X  unsigned char
  1769. X    *gray_pixels;
  1770. X
  1771. X  unsigned int
  1772. X    height,
  1773. X    width;
  1774. X
  1775. X  /*
  1776. X    Allocate image structure.
  1777. X  */
  1778. X  image=AllocateImage("GRAY");
  1779. X  if (image == (Image *) NULL)
  1780. X    return((Image *) NULL);
  1781. X  /*
  1782. X    Open image file.
  1783. X  */
  1784. X  (void) strcpy(image->filename,alien_info->filename);
  1785. X  OpenImage(image,"r");
  1786. X  if (image->file == (FILE *) NULL)
  1787. X    {
  1788. X      Warning("unable to open file",image->filename);
  1789. SHAR_EOF
  1790. true || echo 'restore of ImageMagick/alien.c failed'
  1791. fi
  1792. echo 'End of  part 20'
  1793. echo 'File ImageMagick/alien.c is continued in part 21'
  1794. echo 21 > _shar_seq_.tmp
  1795. exit 0
  1796. exit 0 # Just in case...
  1797.