home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume34 / imagemagick / part20 < prev    next >
Encoding:
Text File  |  1992-12-14  |  57.1 KB  |  1,791 lines

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