home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / x / volume20 / imagemgc / part14 < prev    next >
Encoding:
Text File  |  1993-07-13  |  50.6 KB  |  1,808 lines

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i070:  imagemagic - X11 image processing and display, Part14/38
  4. Message-ID: <1993Jul14.175620.1519@sparky.sterling.com>
  5. X-Md4-Signature: 841f44e0314446e48345fdf5ed933449
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 17:56:20 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 70
  13. Archive-name: imagemagic/part14
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.14 (part 14 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/decode.c continued
  21. #
  22. if test ! -r _shar_seq_.tmp; then
  23.     echo 'Please unpack part 1 first!'
  24.     exit 1
  25. fi
  26. (read Scheck
  27.  if test "$Scheck" != 14; then
  28.     echo Please unpack part "$Scheck" next!
  29.     exit 1
  30.  else
  31.     exit 0
  32.  fi
  33. ) < _shar_seq_.tmp || exit 1
  34. if test ! -f _shar_wnt_.tmp; then
  35.     echo 'x - still skipping ImageMagick/decode.c'
  36. else
  37. echo 'x - continuing file ImageMagick/decode.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/decode.c' &&
  39. X    */
  40. X    bytes_per_line=((image->columns*bmp_header.bit_count+31)/32)*4;
  41. X    switch (bmp_header.bit_count)
  42. X    {
  43. X      case 1:
  44. X      {
  45. X        /*
  46. X          Convert bitmap scanline to runlength-encoded color packets.
  47. X        */
  48. X        for (y=image->rows-1; y >= 0; y--)
  49. X        {
  50. X          p=bmp_pixels+(image->rows-y-1)*bytes_per_line;
  51. X          q=image->pixels+(y*image->columns);
  52. X          for (x=0; x < (image->columns-7); x+=8)
  53. X          {
  54. X            for (bit=0; bit < 8; bit++)
  55. X            {
  56. X              q->index=((*p) & (0x80 >> bit) ? 0x00 : 0x01);
  57. X              q->length=0;
  58. X              q++;
  59. X            }
  60. X            p++;
  61. X          }
  62. X          if ((image->columns % 8) != 0)
  63. X            {
  64. X              for (bit=0; bit < (8-(image->columns % 8)); bit++)
  65. X              {
  66. X                q->index=((*p) & (0x80 >> bit) ? 0x00 : 0x01);
  67. X                q->length=0;
  68. X                q++;
  69. X              }
  70. X              p++;
  71. X            }
  72. X        }
  73. X        SyncImage(image);
  74. X        break;
  75. X      }
  76. X      case 4:
  77. X      {
  78. X        /*
  79. X          Convert PseudoColor scanline to runlength-encoded color packets.
  80. X        */
  81. X        for (y=image->rows-1; y >= 0; y--)
  82. X        {
  83. X          p=bmp_pixels+(image->rows-y-1)*bytes_per_line;
  84. X          q=image->pixels+(y*image->columns);
  85. X          for (x=0; x < (image->columns-1); x+=2)
  86. X          {
  87. X            q->index=(*p >> 4) & 0xf;
  88. X            q->length=0;
  89. X            q++;
  90. X            q->index=(*p) & 0xf;
  91. X            q->length=0;
  92. X            p++;
  93. X            q++;
  94. X          }
  95. X          if ((image->columns % 2) != 0)
  96. X            {
  97. X              q->index=(*p >> 4) & 0xf;
  98. X              q->length=0;
  99. X              q++;
  100. X              p++;
  101. X            }
  102. X        }
  103. X        SyncImage(image);
  104. X        CompressColormap(image);
  105. X        break;
  106. X      }
  107. X      case 8:
  108. X      {
  109. X        /*
  110. X          Convert PseudoColor scanline to runlength-encoded color packets.
  111. X        */
  112. X        for (y=image->rows-1; y >= 0; y--)
  113. X        {
  114. X          p=bmp_pixels+(image->rows-y-1)*bytes_per_line;
  115. X          q=image->pixels+(y*image->columns);
  116. X          for (x=0; x < image->columns; x++)
  117. X          {
  118. X            q->index=(*p++);
  119. X            q->length=0;
  120. X            q++;
  121. X          }
  122. X        }
  123. X        SyncImage(image);
  124. X        CompressColormap(image);
  125. X        break;
  126. X      }
  127. X      case 24:
  128. X      {
  129. X        /*
  130. X          Convert DirectColor scanline to runlength-encoded color packets.
  131. X        */
  132. X        for (y=image->rows-1; y >= 0; y--)
  133. X        {
  134. X          p=bmp_pixels+(image->rows-y-1)*bytes_per_line;
  135. X          q=image->pixels+(y*image->columns);
  136. X          for (x=0; x < image->columns; x++)
  137. X          {
  138. X            q->index=(unsigned short) (image->alpha ? (*p++) : 0);
  139. X            q->blue=(*p++);
  140. X            q->green=(*p++);
  141. X            q->red=(*p++);
  142. X            q->length=0;
  143. X            q++;
  144. X          }
  145. X        }
  146. X        break;
  147. X      }
  148. X      default:
  149. X      {
  150. X        Warning("not a BMP image file",(char *) NULL);
  151. X        DestroyImages(image);
  152. X        return((Image *) NULL);
  153. X      }
  154. X    }
  155. X    (void) free((char *) bmp_pixels);
  156. X    /*
  157. X      Proceed to next image.
  158. X    */
  159. X    status=ReadData((char *) magick,1,2,image->file);
  160. X    if ((status == True) && (strncmp((char *) magick,"BM",2) == 0))
  161. X      {
  162. X        /*
  163. X          Allocate image structure.
  164. X        */
  165. X        image->next=AllocateImage("BMP");
  166. X        if (image->next == (Image *) NULL)
  167. X          {
  168. X            DestroyImages(image);
  169. X            return((Image *) NULL);
  170. X          }
  171. X        image->next->file=image->file;
  172. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  173. X          image->scene+1);
  174. X        image->next->scene=image->scene+1;
  175. X        image->next->previous=image;
  176. X        image=image->next;
  177. X      }
  178. X  } while ((status == True) && (strncmp((char *) magick,"BM",2) == 0));
  179. X  while (image->previous != (Image *) NULL)
  180. X    image=image->previous;
  181. X  CloseImage(image);
  182. X  return(image);
  183. }
  184. X
  185. /*
  186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  187. %                                                                             %
  188. %                                                                             %
  189. %                                                                             %
  190. %  R e a d C M Y K I m a g e                                                  %
  191. %                                                                             %
  192. %                                                                             %
  193. %                                                                             %
  194. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  195. %
  196. %  Function ReadCMYKImage reads an image of raw cyan, magenta, yellow, and
  197. %  black bytes and returns it.  It allocates the memory necessary for the new
  198. %  Image structure and returns a pointer to the new image.
  199. %
  200. %  The format of the ReadCMYKImage routine is:
  201. %
  202. %      image=ReadCMYKImage(image_info)
  203. %
  204. %  A description of each parameter follows:
  205. %
  206. %    o image:  Function ReadCMYKImage returns a pointer to the image after
  207. %      reading.  A null image is returned if there is a a memory shortage or
  208. %      if the image cannot be read.
  209. %
  210. %    o image_info: Specifies a pointer to an ImageInfo structure.
  211. %
  212. %
  213. */
  214. static Image *ReadCMYKImage(image_info)
  215. ImageInfo
  216. X  *image_info;
  217. {
  218. X  Image
  219. X    *image;
  220. X
  221. X  int
  222. X    x,
  223. X    y;
  224. X
  225. X  register int
  226. X    i;
  227. X
  228. X  register RunlengthPacket
  229. X    *q;
  230. X
  231. X  register unsigned char
  232. X    *p;
  233. X
  234. X  unsigned char
  235. X    black,
  236. X    *cmyk_pixels,
  237. X    cyan,
  238. X    magenta,
  239. X    yellow;
  240. X
  241. X  unsigned int
  242. X    height,
  243. X    width;
  244. X
  245. X  /*
  246. X    Allocate image structure.
  247. X  */
  248. X  image=AllocateImage("CMYK");
  249. X  if (image == (Image *) NULL)
  250. X    return((Image *) NULL);
  251. X  /*
  252. X    Open image file.
  253. X  */
  254. X  (void) strcpy(image->filename,image_info->filename);
  255. X  OpenImage(image,"r");
  256. X  if (image->file == (FILE *) NULL)
  257. X    {
  258. X      Warning("unable to open file",image->filename);
  259. X      DestroyImage(image);
  260. X      return((Image *) NULL);
  261. X    }
  262. X  /*
  263. X    Create image.
  264. X  */
  265. X  width=512;
  266. X  height=512;
  267. X  if (image_info->geometry != (char *) NULL)
  268. X    (void) XParseGeometry(image_info->geometry,&x,&y,&width,&height);
  269. X  image->columns=width;
  270. X  image->rows=height;
  271. X  image->packets=image->columns*image->rows;
  272. X  cmyk_pixels=(unsigned char *)
  273. X    malloc((unsigned int) image->packets*4*sizeof(unsigned char));
  274. X  image->pixels=(RunlengthPacket *)
  275. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  276. X  if ((cmyk_pixels == (unsigned char *) NULL) ||
  277. X      (image->pixels == (RunlengthPacket *) NULL))
  278. X    {
  279. X      Warning("unable to open file",image->filename);
  280. X      DestroyImage(image);
  281. X      return((Image *) NULL);
  282. X    }
  283. X  /*
  284. X    Convert raster image to runlength-encoded packets.
  285. X  */
  286. X  (void) ReadData((char *) cmyk_pixels,4,(int) (image->columns*image->rows),
  287. X    image->file);
  288. X  p=cmyk_pixels;
  289. X  switch (image_info->interlace)
  290. X  {
  291. X    case NoneInterlace:
  292. X    default:
  293. X    {
  294. X      /*
  295. X        No interlacing:  CMYKCMYKCMYKCMYKCMYKCMYK...
  296. X      */
  297. X      q=image->pixels;
  298. X      for (i=0; i < (image->columns*image->rows); i++)
  299. X      {
  300. X        q->red=(*p++);
  301. X        q->green=(*p++);
  302. X        q->blue=(*p++);
  303. X        q->index=(*p++);
  304. X        q->length=0;
  305. X        q++;
  306. X      }
  307. X      break;
  308. X    }
  309. X    case LineInterlace:
  310. X    {
  311. X      /*
  312. X        Line interlacing:  CCC...MMM...YYY...KKK...CCC...MMM...YYY...KKK...
  313. X      */
  314. X      for (y=0; y < image->rows; y++)
  315. X      {
  316. X        q=image->pixels+y*image->columns;
  317. X        for (x=0; x < image->columns; x++)
  318. X        {
  319. X          q->red=(*p++);
  320. X          q->length=0;
  321. X          q++;
  322. X        }
  323. X        q=image->pixels+y*image->columns;
  324. X        for (x=0; x < image->columns; x++)
  325. X        {
  326. X          q->green=(*p++);
  327. X          q++;
  328. X        }
  329. X        q=image->pixels+y*image->columns;
  330. X        for (x=0; x < image->columns; x++)
  331. X        {
  332. X          q->blue=(*p++);
  333. X          q++;
  334. X        }
  335. X        q=image->pixels+y*image->columns;
  336. X        for (x=0; x < image->columns; x++)
  337. X        {
  338. X          q->index=(*p++);
  339. X          q++;
  340. X        }
  341. X      }
  342. X      break;
  343. X    }
  344. X    case PlaneInterlace:
  345. X    {
  346. X      /*
  347. X        Plane interlacing:  CCCCCC...MMMMMM...YYYYYY...KKKKKK...
  348. X      */
  349. X      q=image->pixels;
  350. X      for (i=0; i < (image->columns*image->rows); i++)
  351. X      {
  352. X        q->red=(*p++);
  353. X        q->length=0;
  354. X        q++;
  355. X      }
  356. X      q=image->pixels;
  357. X      for (i=0; i < (image->columns*image->rows); i++)
  358. X      {
  359. X        q->green=(*p++);
  360. X        q++;
  361. X      }
  362. X      q=image->pixels;
  363. X      for (i=0; i < (image->columns*image->rows); i++)
  364. X      {
  365. X        q->blue=(*p++);
  366. X        q++;
  367. X      }
  368. X      q=image->pixels;
  369. X      for (i=0; i < (image->columns*image->rows); i++)
  370. X      {
  371. X        q->index=(*p++);
  372. X        q++;
  373. X      }
  374. X      break;
  375. X    }
  376. X  }
  377. X  /*
  378. X    Transform image to CMYK.
  379. X  */
  380. X  q=image->pixels;
  381. X  for (i=0; i < (image->columns*image->rows); i++)
  382. X  {
  383. X    cyan=q->red;
  384. X    magenta=q->green;
  385. X    yellow=q->blue;
  386. X    black=q->index;
  387. X    if ((unsigned int) (cyan+black) > MaxRGB)
  388. X      q->red=0;
  389. X    else
  390. X      q->red=MaxRGB-(cyan+black);
  391. X    if ((unsigned int) (magenta+black) > MaxRGB)
  392. X      q->green=0;
  393. X    else
  394. X      q->green=MaxRGB-(magenta+black);
  395. X    if ((unsigned int) (yellow+black) > MaxRGB)
  396. X      q->blue=0;
  397. X    else
  398. X      q->blue=MaxRGB-(yellow+black);
  399. X    q->index=0;
  400. X    q->length=0;
  401. X    q++;
  402. X  }
  403. X  (void) free((char *) cmyk_pixels);
  404. X  CloseImage(image);
  405. X  return(image);
  406. }
  407. X
  408. /*
  409. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  410. %                                                                             %
  411. %                                                                             %
  412. %                                                                             %
  413. %  R e a d F A X I m a g e                                                    %
  414. %                                                                             %
  415. %                                                                             %
  416. %                                                                             %
  417. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  418. %
  419. %  Function ReadFAXImage reads a Group 3 FAX image file and returns it.  It
  420. %  allocates the memory necessary for the new Image structure and returns a
  421. %  pointer to the new image.
  422. %
  423. %  The format of the ReadFAXImage routine is:
  424. %
  425. %      image=ReadFAXImage(image_info)
  426. %
  427. %  A description of each parameter follows:
  428. %
  429. %    o image:  Function ReadFAXImage returns a pointer to the image after
  430. %      reading.  A null image is returned if there is a a memory shortage or
  431. %      if the image cannot be read.
  432. %
  433. %    o image_info: Specifies a pointer to an ImageInfo structure.
  434. %
  435. %
  436. */
  437. static Image *ReadFAXImage(image_info)
  438. ImageInfo
  439. X  *image_info;
  440. {
  441. X  Image
  442. X    *image;
  443. X
  444. X  Warning("Cannot read FAX images",image_info->filename);
  445. X  image=ReadMIFFImage(image_info);
  446. X  return(image);
  447. }
  448. X
  449. /*
  450. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  451. %                                                                             %
  452. %                                                                             %
  453. %                                                                             %
  454. %  R e a d G I F I m a g e                                                    %
  455. %                                                                             %
  456. %                                                                             %
  457. %                                                                             %
  458. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  459. %
  460. %  Function ReadGIFImage reads a Compuserve Graphics image file and returns it.
  461. %  It allocates the memory necessary for the new Image structure and returns a
  462. %  pointer to the new image.
  463. %
  464. %  The format of the ReadGIFImage routine is:
  465. %
  466. %      image=ReadGIFImage(image_info)
  467. %
  468. %  A description of each parameter follows:
  469. %
  470. %    o image:  Function ReadGIFImage returns a pointer to the image after
  471. %      reading.  A null image is returned if there is a a memory shortage or
  472. %      an error occurs.
  473. %
  474. %    o image_info: Specifies a pointer to an ImageInfo structure.
  475. %
  476. %
  477. */
  478. static Image *ReadGIFImage(image_info)
  479. ImageInfo
  480. X  *image_info;
  481. {
  482. #define BitSet(byte,bit)  (((byte) & (bit)) == (bit))
  483. #define LSBFirstOrder(x,y)  (((y) << 8) | (x))
  484. X
  485. X  Image
  486. X    *image;
  487. X
  488. X  int
  489. X    pass,
  490. X    status,
  491. X    x,
  492. X    y;
  493. X
  494. X  register int
  495. X    i;
  496. X
  497. X  register unsigned char
  498. X    *p;
  499. X
  500. X  unsigned char
  501. X    c,
  502. X    *global_colormap,
  503. X    header[2048],
  504. X    magick[12];
  505. X
  506. X  unsigned int
  507. X    global_colors,
  508. X    image_count,
  509. X    interlace,
  510. X    local_colormap;
  511. X
  512. X  /*
  513. X    Allocate image structure.
  514. X  */
  515. X  image=AllocateImage("GIF");
  516. X  if (image == (Image *) NULL)
  517. X    return((Image *) NULL);
  518. X  /*
  519. X    Open image file.
  520. X  */
  521. X  (void) strcpy(image->filename,image_info->filename);
  522. X  OpenImage(image,"r");
  523. X  if (image->file == (FILE *) NULL)
  524. X    {
  525. X      Warning("unable to open file",image->filename);
  526. X      DestroyImage(image);
  527. X      return((Image *) NULL);
  528. X    }
  529. X  /*
  530. X    Determine if this is a GIF file.
  531. X  */
  532. X  status=ReadData((char *) magick,1,6,image->file);
  533. X  if ((status == False) || ((strncmp((char *) magick,"GIF87",5) != 0) &&
  534. X      (strncmp((char *) magick,"GIF89",5) != 0)))
  535. X    {
  536. X      Warning("not a GIF image file",(char *) NULL);
  537. X      DestroyImage(image);
  538. X      return((Image *) NULL);
  539. X    }
  540. X  /*
  541. X    Read the screen descriptor.
  542. X  */
  543. X  status=ReadData((char *) header,1,7,image->file);
  544. X  if (status == False)
  545. X    {
  546. X      Warning("failed to read screen descriptor",(char *) NULL );
  547. X      DestroyImage(image);
  548. X      return((Image *) NULL);
  549. X    }
  550. X  global_colors=0;
  551. X  global_colormap=(unsigned char *) NULL;
  552. X  if (BitSet(header[4],0x80))
  553. X    {
  554. X      /*
  555. X        Read global colormap.
  556. X      */
  557. X      global_colors=1 << ((header[4] & 0x07)+1);
  558. X      global_colormap=(unsigned char *)
  559. X        malloc(3*global_colors*sizeof(unsigned char));
  560. X      if (global_colormap == (unsigned char *) NULL)
  561. X        {
  562. X          Warning("unable to read image colormap","memory allocation failed");
  563. X          DestroyImage(image);
  564. X          return((Image *) NULL);
  565. X        }
  566. X      (void) ReadData((char *) global_colormap,1,(int) (3*global_colors),
  567. X        image->file);
  568. X    }
  569. X  image_count=0;
  570. X  for ( ; ; )
  571. X  {
  572. X    status=ReadData((char *) &c,1,1,image->file);
  573. X    if (status == False)
  574. X      break;
  575. X    if (c == ';')
  576. X      break;  /* terminator */
  577. X    if (c == '!')
  578. X      {
  579. X        /*
  580. X          GIF Extension block.
  581. X        */
  582. X        status=ReadData((char *) &c,1,1,image->file);
  583. X        if (status == False)
  584. X          {
  585. X            Warning("unable to read extention block",(char *) NULL);
  586. X            DestroyImages(image);
  587. X            return((Image *) NULL);
  588. X          }
  589. X        if (c != 0xfe)
  590. X          while (ReadDataBlock((char *) header,image->file) != 0);
  591. X        else
  592. X          while (ReadDataBlock((char *) header,image->file) != 0)
  593. X          {
  594. X            /*
  595. X              Comment extension block.
  596. X            */
  597. X            if (image->comments != (char *) NULL)
  598. X              image->comments=(char *) realloc((char *) image->comments,
  599. X                (unsigned int) (strlen(image->comments)+
  600. X                strlen((char *) header)+1));
  601. X            else
  602. X              {
  603. X                image->comments=(char *)
  604. X                  malloc((strlen((char *) header)+1)*sizeof(char));
  605. X                *image->comments='\0';
  606. X              }
  607. X            if (image->comments == (char *) NULL)
  608. X              {
  609. X                Warning("memory allocation error",(char *) NULL);
  610. X                DestroyImages(image);
  611. X                return((Image *) NULL);
  612. X              }
  613. X            (void) strcat(image->comments,(char *) header);
  614. X          }
  615. X      }
  616. X    if (c != ',')
  617. X      continue;
  618. X    /*
  619. X      Read image attributes.
  620. X    */
  621. X    if (image_count != 0)
  622. X      {
  623. X        /*
  624. X          Allocate image structure.
  625. X        */
  626. X        CompressColormap(image);
  627. X        image->next=AllocateImage("GIF");
  628. X        if (image->next == (Image *) NULL)
  629. X          {
  630. X            DestroyImages(image);
  631. X            return((Image *) NULL);
  632. X          }
  633. X        image->next->file=image->file;
  634. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  635. X          image->scene+1);
  636. X        image->next->scene=image->scene+1;
  637. X        image->next->previous=image;
  638. X        image=image->next;
  639. X      }
  640. X    image_count++;
  641. X    status=ReadData((char *) header,1,9,image->file);
  642. X    if (status == False)
  643. X      {
  644. X        Warning("unable to read left/top/width/height",(char *) NULL);
  645. X        DestroyImages(image);
  646. X        return((Image *) NULL);
  647. X      }
  648. X    interlace=BitSet(header[8],0x40);
  649. X    local_colormap=BitSet(header[8],0x80);
  650. X    /*
  651. X      Allocate image.
  652. X    */
  653. X    image->columns=LSBFirstOrder(header[4],header[5]);
  654. X    image->rows=LSBFirstOrder(header[6],header[7]);
  655. X    image->packets=image->columns*image->rows;
  656. X    if (image->pixels != (RunlengthPacket *) NULL)
  657. X      (void) free((char *) image->pixels);
  658. X    image->pixels=(RunlengthPacket *)
  659. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  660. X    if (image->pixels == (RunlengthPacket *) NULL)
  661. X      {
  662. X        Warning("unable to read image","memory allocation failed");
  663. X        DestroyImages(image);
  664. X        return((Image *) NULL);
  665. X      }
  666. X    /*
  667. X      Inititialize colormap.
  668. X    */
  669. X    image->class=PseudoClass;
  670. X    image->colors=!local_colormap ? global_colors : 1 << ((header[8] & 0x07)+1);
  671. X    image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  672. X    if (image->colormap == (ColorPacket *) NULL)
  673. X      {
  674. X        Warning("unable to read image","memory allocation failed");
  675. X        DestroyImages(image);
  676. X        return((Image *) NULL);
  677. X      }
  678. X    if (!local_colormap)
  679. X      {
  680. X        /*
  681. X          Use global colormap.
  682. X        */
  683. X        p=global_colormap;
  684. X        for (i=0; i < image->colors; i++)
  685. X        {
  686. X          image->colormap[i].red=(*p++);
  687. X          image->colormap[i].green=(*p++);
  688. X          image->colormap[i].blue=(*p++);
  689. X        }
  690. X      }
  691. X    else
  692. X      {
  693. X        unsigned char
  694. X          *colormap;
  695. X
  696. X        /*
  697. X          Read local colormap.
  698. X        */
  699. X        colormap=(unsigned char *)
  700. X          malloc(3*image->colors*sizeof(unsigned char));
  701. X        if (colormap == (unsigned char *) NULL)
  702. X          {
  703. X            Warning("unable to read local colormap","memory allocation failed");
  704. X            DestroyImages(image);
  705. X            return((Image *) NULL);
  706. X          }
  707. X        (void) ReadData((char *) colormap,1,(int) image->colors*3,image->file);
  708. X        p=colormap;
  709. X        for (i=0; i < image->colors; i++)
  710. X        {
  711. X          image->colormap[i].red=(*p++);
  712. X          image->colormap[i].green=(*p++);
  713. X          image->colormap[i].blue=(*p++);
  714. X        }
  715. X        (void) free((char *) colormap);
  716. X      }
  717. X    /*
  718. X      Decode image.
  719. X    */
  720. X    status=LZWDecodeImage(image);
  721. X    if (status == False)
  722. X      {
  723. X        Warning("unable to read image data",(char *) NULL);
  724. X        DestroyImages(image);
  725. X        return((Image *) NULL);
  726. X      }
  727. X    if (interlace)
  728. X      {
  729. X        Image
  730. X          *interlaced_image;
  731. X
  732. X        register RunlengthPacket
  733. X          *p,
  734. X          *q;
  735. X
  736. X        static int
  737. X          interlace_rate[4] = { 8, 8, 4, 2 },
  738. X          interlace_start[4] = { 0, 4, 2, 1 };
  739. X
  740. X        /*
  741. X          Interlace image.
  742. X        */
  743. X        interlaced_image=image;
  744. X        image=CopyImage(interlaced_image,interlaced_image->columns,
  745. X          interlaced_image->rows,False);
  746. X        if (image == (Image *) NULL)
  747. X          {
  748. X            Warning("unable to read image","memory allocation failed");
  749. X            DestroyImages(interlaced_image);
  750. X            return((Image *) NULL);
  751. X          }
  752. X        p=interlaced_image->pixels;
  753. X        q=image->pixels;
  754. X        for (pass=0; pass < 4; pass++)
  755. X        {
  756. X          y=interlace_start[pass];
  757. X          while (y < image->rows)
  758. X          {
  759. X            q=image->pixels+(y*image->columns);
  760. X            for (x=0; x < image->columns; x++)
  761. X            {
  762. X              *q=(*p);
  763. X              p++;
  764. X              q++;
  765. X            }
  766. X            y+=interlace_rate[pass];
  767. X          }
  768. X        }
  769. X        DestroyImages(interlaced_image);
  770. X      }
  771. X  }
  772. X  (void) free((char *) global_colormap);
  773. X  CompressColormap(image);
  774. X  while (image->previous != (Image *) NULL)
  775. X    image=image->previous;
  776. X  CloseImage(image);
  777. X  return(image);
  778. }
  779. X
  780. /*
  781. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  782. %                                                                             %
  783. %                                                                             %
  784. %                                                                             %
  785. %  R e a d G R A Y I m a g e                                                  %
  786. %                                                                             %
  787. %                                                                             %
  788. %                                                                             %
  789. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  790. %
  791. %  Function ReadGRAYImage reads an image of raw grayscale bytes and returns it.
  792. %  It allocates the memory necessary for the new Image structure and returns a
  793. %  pointer to the new image.
  794. %
  795. %  The format of the ReadGRAYImage routine is:
  796. %
  797. %      image=ReadGRAYImage(image_info)
  798. %
  799. %  A description of each parameter follows:
  800. %
  801. %    o image:  Function ReadGRAYImage returns a pointer to the image after
  802. %      reading.  A null image is returned if there is a a memory shortage or
  803. %      if the image cannot be read.
  804. %
  805. %    o image_info: Specifies a pointer to an ImageInfo structure.
  806. %
  807. %
  808. */
  809. static Image *ReadGRAYImage(image_info)
  810. ImageInfo
  811. X  *image_info;
  812. {
  813. X  Image
  814. X    *image;
  815. X
  816. X  int
  817. X    x,
  818. X    y;
  819. X
  820. X  register int
  821. X    i;
  822. X
  823. X  register RunlengthPacket
  824. X    *q;
  825. X
  826. X  register unsigned char
  827. X    index,
  828. X    *p;
  829. X
  830. X  unsigned char
  831. X    *gray_pixels;
  832. X
  833. X  unsigned int
  834. X    height,
  835. X    width;
  836. X
  837. X  /*
  838. X    Allocate image structure.
  839. X  */
  840. X  image=AllocateImage("GRAY");
  841. X  if (image == (Image *) NULL)
  842. X    return((Image *) NULL);
  843. X  /*
  844. X    Open image file.
  845. X  */
  846. X  (void) strcpy(image->filename,image_info->filename);
  847. X  OpenImage(image,"r");
  848. X  if (image->file == (FILE *) NULL)
  849. X    {
  850. X      Warning("unable to open file",image->filename);
  851. X      DestroyImage(image);
  852. X      return((Image *) NULL);
  853. X    }
  854. X  /*
  855. X    Create linear colormap.
  856. X  */
  857. X  image->class=PseudoClass;
  858. X  image->colors=256;
  859. X  image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  860. X  if (image->colormap == (ColorPacket *) NULL)
  861. X    {
  862. X      Warning("memory allocation error",(char *) NULL);
  863. X      DestroyImage(image);
  864. X      return((Image *) NULL);
  865. X    }
  866. X  for (i=0; i < image->colors; i++)
  867. X  {
  868. X    image->colormap[i].red=(unsigned char) i;
  869. X    image->colormap[i].green=(unsigned char) i;
  870. X    image->colormap[i].blue=(unsigned char) i;
  871. X  }
  872. X  /*
  873. X    Create image.
  874. X  */
  875. X  width=512;
  876. X  height=512;
  877. X  if (image_info->geometry != (char *) NULL)
  878. X    (void) XParseGeometry(image_info->geometry,&x,&y,&width,&height);
  879. X  image->columns=width;
  880. X  image->rows=height;
  881. X  image->packets=image->columns*image->rows;
  882. X  gray_pixels=(unsigned char *)
  883. X    malloc((unsigned int) image->packets*sizeof(unsigned char));
  884. X  image->pixels=(RunlengthPacket *)
  885. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  886. X  if ((gray_pixels == (unsigned char *) NULL) ||
  887. X      (image->pixels == (RunlengthPacket *) NULL))
  888. X    {
  889. X      Warning("memory allocation error",(char *) NULL);
  890. X      DestroyImage(image);
  891. X      return((Image *) NULL);
  892. X    }
  893. X  /*
  894. X    Convert raster image to runlength-encoded packets.
  895. X  */
  896. X  (void) ReadData((char *) gray_pixels,1,(int) (image->columns*image->rows),
  897. X    image->file);
  898. X  p=gray_pixels;
  899. X  q=image->pixels;
  900. X  for (i=0; i < (image->columns*image->rows); i++)
  901. X  {
  902. X    index=(*p++);
  903. X    q->red=index;
  904. X    q->green=index;
  905. X    q->blue=index;
  906. X    q->index=(unsigned short) index;
  907. X    q->length=0;
  908. X    q++;
  909. X  }
  910. X  (void) free((char *) gray_pixels);
  911. X  CompressColormap(image);
  912. X  CloseImage(image);
  913. X  return(image);
  914. }
  915. X
  916. /*
  917. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  918. %                                                                             %
  919. %                                                                             %
  920. %                                                                             %
  921. %  R e a d H I S T O G R A M I m a g e                                        %
  922. %                                                                             %
  923. %                                                                             %
  924. %                                                                             %
  925. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  926. %
  927. %  Function ReadHISTOGRAMImage reads a HISTOGRAM image file and returns it.  It
  928. %  allocates the memory necessary for the new Image structure and returns a
  929. %  pointer to the new image.
  930. %
  931. %  The format of the ReadHISTOGRAMImage routine is:
  932. %
  933. %      image=ReadHISTOGRAMImage(image_info)
  934. %
  935. %  A description of each parameter follows:
  936. %
  937. %    o image:  Function ReadHISTOGRAMImage returns a pointer to the image after
  938. %      reading.  A null image is returned if there is a a memory shortage or
  939. %      if the image cannot be read.
  940. %
  941. %    o image_info: Specifies a pointer to an ImageInfo structure.
  942. %
  943. %
  944. */
  945. static Image *ReadHISTOGRAMImage(image_info)
  946. ImageInfo
  947. X  *image_info;
  948. {
  949. X  Image
  950. X    *image;
  951. X
  952. X  Warning("Cannot read HISTOGRAM images",image_info->filename);
  953. X  image=ReadMIFFImage(image_info);
  954. X  return(image);
  955. }
  956. X
  957. /*
  958. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  959. %                                                                             %
  960. %                                                                             %
  961. %                                                                             %
  962. %  R e a d I R I S I m a g e                                                  %
  963. %                                                                             %
  964. %                                                                             %
  965. %                                                                             %
  966. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  967. %
  968. %  Function ReadIRISImage reads a SGI RGB image file and returns it.  It
  969. %  allocates the memory necessary for the new Image structure and returns a
  970. %  pointer to the new image.
  971. %
  972. %  The format of the ReadIRISImage routine is:
  973. %
  974. %      image=ReadIRISImage(image_info)
  975. %
  976. %  A description of each parameter follows:
  977. %
  978. %    o image:  Function ReadIRISImage returns a pointer to the image after
  979. %      reading.  A null image is returned if there is a a memory shortage or
  980. %      if the image cannot be read.
  981. %
  982. %    o image_info: Specifies a pointer to an ImageInfo structure.
  983. %
  984. %
  985. */
  986. X
  987. static void IRISDecode(packets,pixels)
  988. unsigned char
  989. X  *packets,
  990. X  *pixels;
  991. {
  992. X  unsigned char
  993. X    count,
  994. X    pixel;
  995. X
  996. X  for ( ; ;)
  997. X  {
  998. X    pixel=(*packets++);
  999. X    count=pixel & 0x7f;
  1000. X    if (count == 0)
  1001. X      break;
  1002. X    if (pixel & 0x80)
  1003. X      for ( ; count != 0; count--)
  1004. X      {
  1005. X        *pixels=(*packets++);
  1006. X        pixels+=4;
  1007. X      }
  1008. X    else
  1009. X      {
  1010. X        pixel=(*packets++);
  1011. X        for ( ; count != 0; count--)
  1012. X        {
  1013. X          *pixels=pixel;
  1014. X          pixels+=4;
  1015. X        }
  1016. X      }
  1017. X  }
  1018. }
  1019. X
  1020. static Image *ReadIRISImage(image_info)
  1021. ImageInfo
  1022. X  *image_info;
  1023. {
  1024. X  typedef struct _IRISHeader
  1025. X  {
  1026. X    unsigned short
  1027. X      magic,
  1028. X      type,
  1029. X      dimension,
  1030. X      columns,
  1031. X      rows,
  1032. X      depth;
  1033. X
  1034. X    unsigned long
  1035. X      minimum_value,
  1036. X      maximum_value;
  1037. X
  1038. X    unsigned char
  1039. X      filler[492];
  1040. X  } IRISHeader;
  1041. X
  1042. X  Image
  1043. X    *image;
  1044. X
  1045. X  IRISHeader
  1046. X    iris_header;
  1047. X
  1048. X  register int
  1049. X    i,
  1050. X    x,
  1051. X    y,
  1052. X    z;
  1053. X
  1054. X  register RunlengthPacket
  1055. X    *q;
  1056. X
  1057. X  register unsigned char
  1058. X    *p;
  1059. X
  1060. X  unsigned char
  1061. X    *iris_pixels;
  1062. X
  1063. X  /*
  1064. X    Allocate image structure.
  1065. X  */
  1066. X  image=AllocateImage("IRIS");
  1067. X  if (image == (Image *) NULL)
  1068. X    return((Image *) NULL);
  1069. X  /*
  1070. X    Open image file.
  1071. X  */
  1072. X  (void) strcpy(image->filename,image_info->filename);
  1073. X  OpenImage(image,"r");
  1074. X  if (image->file == (FILE *) NULL)
  1075. X    {
  1076. X      Warning("unable to open file",image->filename);
  1077. X      DestroyImage(image);
  1078. X      return((Image *) NULL);
  1079. X    }
  1080. X  /*
  1081. X    Read IRIS raster header.
  1082. X  */
  1083. X  iris_header.magic=MSBFirstReadShort(image->file);
  1084. X  do
  1085. X  {
  1086. X    /*
  1087. X      Verify IRIS identifier.
  1088. X    */
  1089. X    if (iris_header.magic != 0x01DA)
  1090. X      {
  1091. X        Warning("not a IRIS RGB image,",image->filename);
  1092. X        DestroyImages(image);
  1093. X        return((Image *) NULL);
  1094. X      }
  1095. X    iris_header.type=MSBFirstReadShort(image->file);
  1096. X    if (((iris_header.type) & 0x00ff) != 1)
  1097. X      {
  1098. X        Warning("image must have 1 byte per pixel channel,",image->filename);
  1099. X        DestroyImages(image);
  1100. X        return((Image *) NULL);
  1101. X      }
  1102. X    iris_header.dimension=MSBFirstReadShort(image->file);
  1103. X    iris_header.columns=MSBFirstReadShort(image->file);
  1104. X    iris_header.rows=MSBFirstReadShort(image->file);
  1105. X    iris_header.depth=MSBFirstReadShort(image->file);
  1106. X    iris_header.minimum_value=MSBFirstReadLong(image->file);
  1107. X    iris_header.maximum_value=MSBFirstReadLong(image->file);
  1108. X    (void) ReadData((char *) iris_header.filler,1,sizeof(iris_header.filler),
  1109. X      image->file);
  1110. X    /*
  1111. X      Allocate IRIS pixels.
  1112. X    */
  1113. X    iris_pixels=(unsigned char *)
  1114. X      malloc(4*iris_header.columns*iris_header.rows*sizeof(unsigned char));
  1115. X    if (iris_pixels == (unsigned char *) NULL)
  1116. X      {
  1117. X        Warning("memory allocation error",image->filename);
  1118. X        DestroyImages(image);
  1119. X        return((Image *) NULL);
  1120. X      }
  1121. X    if (((iris_header.type) & 0xff00) != 0x0100)
  1122. X      {
  1123. X        unsigned char
  1124. X          *scanline;
  1125. X
  1126. X        /*
  1127. X          Read standard image format.
  1128. X        */
  1129. X        scanline=(unsigned char *)
  1130. X          malloc(iris_header.columns*sizeof(unsigned char));
  1131. X        if (scanline == (unsigned char *) NULL)
  1132. X          {
  1133. X            Warning("memory allocation error",image->filename);
  1134. X            DestroyImages(image);
  1135. X            return((Image *) NULL);
  1136. X          }
  1137. X        for (z=0; z < (int) iris_header.depth; z++)
  1138. X        {
  1139. X          p=iris_pixels+z;
  1140. X          for (y=0; y < (int) iris_header.rows; y++)
  1141. X          {
  1142. X            (void) ReadData((char *) scanline,1,(int) iris_header.columns,
  1143. X              image->file);
  1144. X            for (x=0; x < (int) iris_header.columns; x++)
  1145. X            {
  1146. X              *p=scanline[x];
  1147. X              p+=4;
  1148. X            }
  1149. X          }
  1150. X        }
  1151. X        (void) free(scanline);
  1152. X      }
  1153. X    else
  1154. X      {
  1155. X        unsigned char
  1156. X          *packets;
  1157. X
  1158. X        unsigned int
  1159. X          data_order;
  1160. X
  1161. X        unsigned long
  1162. X          offset,
  1163. X          *offsets,
  1164. X          *runlength;
  1165. X
  1166. X        /*
  1167. X          Read runlength-encoded image format.
  1168. X        */
  1169. X        offsets=(unsigned long *)
  1170. X          malloc(iris_header.rows*iris_header.depth*sizeof(unsigned long));
  1171. X        packets=(unsigned char *)
  1172. X          malloc((2*iris_header.columns+10)*sizeof(unsigned char));
  1173. X        runlength=(unsigned long *)
  1174. X          malloc(iris_header.rows*iris_header.depth*sizeof(unsigned long));
  1175. X        if ((offsets == (unsigned long *) NULL) ||
  1176. X            (packets == (unsigned char *) NULL) ||
  1177. X            (runlength == (unsigned long *) NULL))
  1178. X          {
  1179. X            Warning("memory allocation error",image->filename);
  1180. X            DestroyImages(image);
  1181. X            return((Image *) NULL);
  1182. X          }
  1183. X        (void) ReadData((char *) offsets,sizeof(unsigned long),
  1184. X          (int) (iris_header.rows*iris_header.depth),image->file);
  1185. X        (void) ReadData((char *) runlength,sizeof(unsigned long),
  1186. X          (int) (iris_header.rows*iris_header.depth),image->file);
  1187. X        /*
  1188. X          Check data order.
  1189. X        */
  1190. X        offset=0;
  1191. X        data_order=0;
  1192. X        for (y=0; ((y < (int) iris_header.rows) && !data_order); y++)
  1193. X          for (z=0; ((z < (int) iris_header.depth) && !data_order); z++)
  1194. X          {
  1195. X            if (offsets[y+z*iris_header.rows] < offset)
  1196. X              data_order=1;
  1197. X            offset=offsets[y+z*iris_header.rows];
  1198. X          }
  1199. X        offset=512+
  1200. X          2*(iris_header.rows*iris_header.depth)*sizeof(unsigned long);
  1201. X        if (data_order == 1)
  1202. X          {
  1203. X            for (z=0; z < (int) iris_header.depth; z++)
  1204. X            {
  1205. X              p=iris_pixels;
  1206. X              for (y=0; y < (int) iris_header.rows; y++)
  1207. X              {
  1208. X                if (offset != offsets[y+z*iris_header.rows])
  1209. X                  {
  1210. X                    offset=offsets[y+z*iris_header.rows];
  1211. X                    (void) fseek(image->file,offset,0);
  1212. X                  }
  1213. X                (void) ReadData((char *) packets,1,
  1214. X                  (int) runlength[y+z*iris_header.rows],image->file);
  1215. X                offset+=runlength[y+z*iris_header.rows];
  1216. X                IRISDecode(packets,p+z);
  1217. X                p+=(iris_header.columns*4);
  1218. X              }
  1219. X            }
  1220. X          }
  1221. X        else
  1222. X          {
  1223. X            p=iris_pixels;
  1224. X            for (y=0; y < (int) iris_header.rows; y++)
  1225. X            {
  1226. X              for (z=0; z < (int) iris_header.depth; z++)
  1227. X              {
  1228. X                if (offset != offsets[y+z*iris_header.rows])
  1229. X                  {
  1230. X                    offset=offsets[y+z*iris_header.rows];
  1231. X                    (void) fseek(image->file,offset,0);
  1232. X                  }
  1233. X                (void) ReadData((char *) packets,1,
  1234. X                  (int) runlength[y+z*iris_header.rows],image->file);
  1235. X                offset+=runlength[y+z*iris_header.rows];
  1236. X                IRISDecode(packets,p+z);
  1237. X              }
  1238. X              p+=(iris_header.columns*4);
  1239. X            }
  1240. X          }
  1241. X        (void) free(runlength);
  1242. X        (void) free(packets);
  1243. X        (void) free(offsets);
  1244. X      }
  1245. X    /*
  1246. X      Create image.
  1247. X    */
  1248. X    image->alpha=iris_header.depth == 4;
  1249. X    image->columns=iris_header.columns;
  1250. X    image->rows=iris_header.rows;
  1251. X    image->packets=image->columns*image->rows;
  1252. X    image->pixels=(RunlengthPacket *)
  1253. X      malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  1254. X    image->comments=(char *)
  1255. X      malloc((strlen(image->filename)+2048)*sizeof(char));
  1256. X    if ((image->pixels == (RunlengthPacket *) NULL) ||
  1257. X        (image->comments == (char *) NULL))
  1258. X      {
  1259. X        Warning("memory allocation error",(char *) NULL);
  1260. X        DestroyImages(image);
  1261. X        return((Image *) NULL);
  1262. X      }
  1263. X    (void) sprintf(image->comments,"\n  Imported from IRIS raster image:  %s\n",
  1264. X      image->filename);
  1265. X    /*
  1266. X      Convert IRIS raster image to runlength-encoded packets.
  1267. X    */
  1268. X    q=image->pixels;
  1269. X    if (iris_header.depth >= 3)
  1270. X      {
  1271. X        /*
  1272. X          Convert IRIS image to DirectClass runlength-encoded packets.
  1273. X        */
  1274. X        for (y=0; y < image->rows; y++)
  1275. X        {
  1276. X          p=iris_pixels+((image->rows-1)-y)*(image->columns*4);
  1277. X          for (x=0; x < image->columns; x++)
  1278. X          {
  1279. X            q->red=(*p);
  1280. X            q->green=(*(p+1));
  1281. X            q->blue=(*(p+2));
  1282. X            q->index=(*(p+3));
  1283. X            q->length=0;
  1284. X            p+=4;
  1285. X            q++;
  1286. X          }
  1287. X        }
  1288. X      }
  1289. X    else
  1290. X      {
  1291. X        unsigned short
  1292. X          index;
  1293. X
  1294. X        /*
  1295. X          Create grayscale map.
  1296. X        */
  1297. X        image->class=PseudoClass;
  1298. X        image->colors=256;
  1299. X        image->colormap=(ColorPacket *)
  1300. X          malloc(image->colors*sizeof(ColorPacket));
  1301. X        if (image->colormap == (ColorPacket *) NULL)
  1302. X          {
  1303. X            Warning("unable to read image","memory allocation failed");
  1304. X            DestroyImage(image);
  1305. X            return((Image *) NULL);
  1306. X          }
  1307. X        for (i=0; i < image->colors; i++)
  1308. X        {
  1309. X          image->colormap[i].red=(unsigned char) i;
  1310. X          image->colormap[i].green=(unsigned char) i;
  1311. X          image->colormap[i].blue=(unsigned char) i;
  1312. X        }
  1313. X        /*
  1314. X          Convert IRIS image to PseudoClass runlength-encoded packets.
  1315. X        */
  1316. X        for (y=0; y < image->rows; y++)
  1317. X        {
  1318. X          p=iris_pixels+((image->rows-1)-y)*(image->columns*4);
  1319. X          for (x=0; x < image->columns; x++)
  1320. X          {
  1321. X            index=(unsigned short) (*p);
  1322. X            q->red=image->colormap[index].red;
  1323. X            q->green=image->colormap[index].green;
  1324. X            q->blue=image->colormap[index].blue;
  1325. X            q->index=index;
  1326. X            q->length=0;
  1327. X            p+=4;
  1328. X            q++;
  1329. X          }
  1330. X        }
  1331. X      }
  1332. X    (void) free((char *) iris_pixels);
  1333. X    /*
  1334. X      Proceed to next image.
  1335. X    */
  1336. X    iris_header.magic=MSBFirstReadShort(image->file);
  1337. X    if (iris_header.magic == 0x01DA)
  1338. X      {
  1339. X        /*
  1340. X          Allocate image structure.
  1341. X        */
  1342. X        image->next=AllocateImage("IRIS");
  1343. X        if (image->next == (Image *) NULL)
  1344. X          {
  1345. X            DestroyImages(image);
  1346. X            return((Image *) NULL);
  1347. X          }
  1348. X        image->next->file=image->file;
  1349. X        (void) sprintf(image->next->filename,"%s.%u",image_info->filename,
  1350. X          image->scene+1);
  1351. X        image->next->scene=image->scene+1;
  1352. X        image->next->previous=image;
  1353. X        image=image->next;
  1354. X      }
  1355. X  } while (iris_header.magic == 0x01DA);
  1356. X  while (image->previous != (Image *) NULL)
  1357. X    image=image->previous;
  1358. X  CloseImage(image);
  1359. X  return(image);
  1360. }
  1361. X
  1362. #ifdef HasJPEG
  1363. #undef FREAD
  1364. #undef FWRITE
  1365. #undef const
  1366. #include "jinclude.h"
  1367. static Image
  1368. X  *jpeg_image;
  1369. X
  1370. /*
  1371. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1372. %                                                                             %
  1373. %                                                                             %
  1374. %                                                                             %
  1375. %  R e a d J P E G I m a g e                                                  %
  1376. %                                                                             %
  1377. %                                                                             %
  1378. %                                                                             %
  1379. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1380. %
  1381. %  Function ReadJPEGImage reads a JPEG image file and returns it.  It allocates
  1382. %  the memory necessary for the new Image structure and returns a pointer to
  1383. %  the new image.
  1384. %
  1385. %  The format of the ReadJPEGImage routine is:
  1386. %
  1387. %      image=ReadJPEGImage(image_info)
  1388. %
  1389. %  A description of each parameter follows:
  1390. %
  1391. %    o image:  Function ReadJPEGImage returns a pointer to the image after
  1392. %      reading.  A null image is returned if there is a a memory shortage or
  1393. %      if the image cannot be read.
  1394. %
  1395. %    o filename:  Specifies the name of the jpeg_image to read.
  1396. %
  1397. %
  1398. */
  1399. X
  1400. static void MIFFInitializeImage(jpeg_info)
  1401. decompress_info_ptr
  1402. X  jpeg_info;
  1403. {
  1404. X  /*
  1405. X    Create jpeg_image.
  1406. X  */
  1407. X  jpeg_image->columns=jpeg_info->image_width;
  1408. X  jpeg_image->rows=jpeg_info->image_height;
  1409. X  jpeg_image->packets=jpeg_image->columns*jpeg_image->rows;
  1410. X  jpeg_image->pixels=(RunlengthPacket *)
  1411. X    malloc(jpeg_image->packets*sizeof(RunlengthPacket));
  1412. X  jpeg_image->comments=(char *)
  1413. X    malloc((strlen(jpeg_image->filename)+2048)*sizeof(char));
  1414. X  if ((jpeg_image->pixels == (RunlengthPacket *) NULL) ||
  1415. X      (jpeg_image->comments == (char *) NULL))
  1416. X    {
  1417. X      Warning("memory allocation error",(char *) NULL);
  1418. X      exit(1);
  1419. X    }
  1420. X  (void) sprintf(jpeg_image->comments,
  1421. X    "\n  Imported from JFIF JPEG image:  %s\n",jpeg_image->filename);
  1422. X  jpeg_image->packet=jpeg_image->pixels;
  1423. }
  1424. X
  1425. static void MIFFOutputTermMethod(jpeg_info)
  1426. decompress_info_ptr
  1427. X  jpeg_info;
  1428. {
  1429. }
  1430. X
  1431. static void MIFFWriteGRAY(jpeg_info,number_rows,pixel_data)
  1432. decompress_info_ptr
  1433. X  jpeg_info;
  1434. X
  1435. int
  1436. X  number_rows;
  1437. X
  1438. JSAMPIMAGE
  1439. X  pixel_data;
  1440. {
  1441. X  register int
  1442. X    column,
  1443. X    row;
  1444. X
  1445. X  register JSAMPROW
  1446. X    gray;
  1447. X
  1448. X  register RunlengthPacket
  1449. X    *q;
  1450. X
  1451. X  /*
  1452. X    Transfer grayscale JPEG pixel data to MIFF pixels.
  1453. X  */
  1454. X  q=jpeg_image->packet;
  1455. X  for (row=0; row < number_rows; row++)
  1456. X  {
  1457. X    gray=pixel_data[0][row];
  1458. X    for (column=jpeg_info->image_width; column > 0; column--)
  1459. X    {
  1460. X      q->red=GETJSAMPLE(*gray);
  1461. X      q->green=GETJSAMPLE(*gray);
  1462. X      q->blue=GETJSAMPLE(*gray);
  1463. X      q->index=(unsigned short) GETJSAMPLE(*gray);
  1464. X      q->length=0;
  1465. X      q++;
  1466. X      gray++;
  1467. X    }
  1468. X  }
  1469. X  jpeg_image->packet=q;
  1470. }
  1471. X
  1472. static void MIFFWriteRGB(jpeg_info,number_rows,pixel_data)
  1473. decompress_info_ptr
  1474. X  jpeg_info;
  1475. X
  1476. int
  1477. X  number_rows;
  1478. X
  1479. JSAMPIMAGE
  1480. X  pixel_data;
  1481. {
  1482. X  register int
  1483. X    column,
  1484. X    row;
  1485. X
  1486. X  register JSAMPROW
  1487. X    blue,
  1488. X    green,
  1489. X    red;
  1490. X
  1491. X  register RunlengthPacket
  1492. X    *q;
  1493. X
  1494. X  /*
  1495. X    Transfer JPEG pixel data to MIFF pixels.
  1496. X  */
  1497. X  q=jpeg_image->packet;
  1498. X  for (row=0; row < number_rows; row++)
  1499. X  {
  1500. X    red=pixel_data[0][row];
  1501. X    green=pixel_data[1][row];
  1502. X    blue=pixel_data[2][row];
  1503. X    for (column=jpeg_info->image_width; column > 0; column--)
  1504. X    {
  1505. X      q->red=GETJSAMPLE(*red++);
  1506. X      q->green=GETJSAMPLE(*green++);
  1507. X      q->blue=GETJSAMPLE(*blue++);
  1508. X      q->index=0;
  1509. X      q->length=0;
  1510. X      q++;
  1511. X    }
  1512. X  }
  1513. X  jpeg_image->packet=q;
  1514. }
  1515. X
  1516. static void MIFFSelectMethod(jpeg_info)
  1517. decompress_info_ptr
  1518. X  jpeg_info;
  1519. {
  1520. X  jpeg_info->methods->put_pixel_rows=MIFFWriteRGB;
  1521. X  jpeg_info->out_color_space=CS_RGB;
  1522. X  if (jpeg_info->jpeg_color_space == CS_GRAYSCALE)
  1523. X    {
  1524. X      jpeg_info->out_color_space=CS_GRAYSCALE;
  1525. X      jpeg_info->methods->put_pixel_rows=MIFFWriteGRAY;
  1526. X    }
  1527. X  jpeg_info->data_precision=8;
  1528. }
  1529. X
  1530. static Image *ReadJPEGImage(image_info)
  1531. ImageInfo
  1532. X  *image_info;
  1533. {
  1534. X  struct Decompress_info_struct
  1535. X    jpeg_info;
  1536. X
  1537. X  struct Decompress_methods_struct
  1538. X    jpeg_methods;
  1539. X
  1540. X  struct External_methods_struct
  1541. X    external_methods;
  1542. X
  1543. X  /*
  1544. X    Allocate jpeg_image structure.
  1545. X  */
  1546. X  jpeg_image=AllocateImage("JPEG");
  1547. X  if (jpeg_image == (Image *) NULL)
  1548. X    return((Image *) NULL);
  1549. X  /*
  1550. X    Open image file.
  1551. X  */
  1552. X  (void) strcpy(jpeg_image->filename,image_info->filename);
  1553. X  OpenImage(jpeg_image,"r");
  1554. X  if (jpeg_image->file == (FILE *) NULL)
  1555. X    {
  1556. X      Warning("unable to open file",jpeg_image->filename);
  1557. X      DestroyImage(jpeg_image);
  1558. X      return((Image *) NULL);
  1559. X    }
  1560. X  /*
  1561. X    Initialize the JPEG system-dependent methods.
  1562. X  */
  1563. X  jpeg_info.methods=(&jpeg_methods);
  1564. X  jpeg_info.emethods=(&external_methods);
  1565. X  jselerror(&external_methods);
  1566. X  jselmemmgr(&external_methods);
  1567. X  jpeg_info.methods->output_init=MIFFInitializeImage;
  1568. X  jpeg_info.methods->output_term=MIFFOutputTermMethod;
  1569. X  jpeg_methods.d_ui_method_selection=MIFFSelectMethod;
  1570. X  j_d_defaults(&jpeg_info,True);
  1571. X  /*
  1572. X    Read a JFIF JPEG file.
  1573. X  */
  1574. X  jpeg_info.input_file=jpeg_image->file;
  1575. X  jpeg_info.output_file=(FILE *) NULL;
  1576. X  jselrjfif(&jpeg_info);
  1577. X  jpeg_decompress(&jpeg_info);
  1578. X  if (jpeg_info.jpeg_color_space == CS_GRAYSCALE)
  1579. X    {
  1580. X      register int
  1581. X        i;
  1582. X
  1583. X      /*
  1584. X        Initialize grayscale colormap.
  1585. X      */
  1586. X      jpeg_image->class=PseudoClass;
  1587. X      jpeg_image->colors=256;
  1588. X      jpeg_image->colormap=(ColorPacket *)
  1589. X        malloc(jpeg_image->colors*sizeof(ColorPacket));
  1590. X      if (jpeg_image->colormap == (ColorPacket *) NULL)
  1591. X        {
  1592. X          Warning("unable to create image colormap","memory allocation failed");
  1593. X          DestroyImage(jpeg_image);
  1594. X          return((Image *) NULL);
  1595. X        }
  1596. X      for (i=0; i < jpeg_image->colors; i++)
  1597. X      {
  1598. X        jpeg_image->colormap[i].red=(unsigned short) i;
  1599. X        jpeg_image->colormap[i].green=(unsigned short) i;
  1600. X        jpeg_image->colormap[i].blue=(unsigned short) i;
  1601. X      }
  1602. X    }
  1603. X  CloseImage(jpeg_image);
  1604. X  return(jpeg_image);
  1605. }
  1606. #else
  1607. static Image *ReadJPEGImage(image_info)
  1608. ImageInfo
  1609. X  *image_info;
  1610. {
  1611. X  Warning("JPEG library is not available",image_info->filename);
  1612. X  return(ReadMIFFImage(image_info));
  1613. }
  1614. #endif
  1615. X
  1616. /*
  1617. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1618. %                                                                             %
  1619. %                                                                             %
  1620. %                                                                             %
  1621. %   R e a d M I F F I m a g e                                                 %
  1622. %                                                                             %
  1623. %                                                                             %
  1624. %                                                                             %
  1625. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1626. %
  1627. %  Function ReadMIFFImage reads a MIFF image file and returns it.  It
  1628. %  allocates the memory necessary for the new Image structure and returns a
  1629. %  pointer to the new image.
  1630. %
  1631. %  The format of the ReadMIFFImage routine is:
  1632. %
  1633. %      image=ReadMIFFImage(filename)
  1634. %
  1635. %  A description of each parameter follows:
  1636. %
  1637. %    o image: Function ReadMIFFImage returns a pointer to the image after
  1638. %      reading.  A null image is returned if there is a a memory shortage or
  1639. %      if the image cannot be read.
  1640. %
  1641. %    o image_info: Specifies a pointer to an ImageInfo structure.
  1642. %
  1643. %
  1644. */
  1645. static Image *ReadMIFFImage(image_info)
  1646. ImageInfo
  1647. X  *image_info;
  1648. {
  1649. #define MaxKeywordLength  2048
  1650. X
  1651. X  char
  1652. X    keyword[MaxKeywordLength],
  1653. X    value[MaxKeywordLength];
  1654. X
  1655. X  Image
  1656. X    *image;
  1657. X
  1658. X  register int
  1659. X    c,
  1660. X    i;
  1661. X
  1662. X  register unsigned char
  1663. X    *p;
  1664. X
  1665. X  unsigned int
  1666. X    max_characters,
  1667. X    packet_size,
  1668. X    status;
  1669. X
  1670. X  unsigned long
  1671. X    count,
  1672. X    packets;
  1673. X
  1674. X  /*
  1675. X    Allocate image structure.
  1676. X  */
  1677. X  image=AllocateImage("MIFF");
  1678. X  if (image == (Image *) NULL)
  1679. X    return((Image *) NULL);
  1680. X  /*
  1681. X    Open image file.
  1682. X  */
  1683. X  (void) strcpy(image->filename,image_info->filename);
  1684. X  OpenImage(image,"r");
  1685. X  if (image->file == (FILE *) NULL)
  1686. X    {
  1687. X      Warning("unable to open file",image->filename);
  1688. X      DestroyImage(image);
  1689. X      return((Image *) NULL);
  1690. X    }
  1691. X  /*
  1692. X    Decode image header;  header terminates one character beyond a ':'.
  1693. X  */
  1694. X  c=fgetc(image->file);
  1695. X  if (c == EOF)
  1696. X    {
  1697. X      DestroyImage(image);
  1698. X      return((Image *) NULL);
  1699. X    }
  1700. X  do
  1701. X  {
  1702. X    /*
  1703. X      Decode image header;  header terminates one character beyond a ':'.
  1704. X    */
  1705. X    image->compression=NoCompression;
  1706. X    while (isgraph(c) && (c != ':'))
  1707. X    {
  1708. X      register char
  1709. X        *p;
  1710. X
  1711. X      if (c == '{')
  1712. X        {
  1713. X          /*
  1714. X            Comment.
  1715. X          */
  1716. X          max_characters=2048;
  1717. X          image->comments=(char *) malloc(max_characters*sizeof(char));
  1718. X          if (image->comments == (char *) NULL)
  1719. X            {
  1720. X              Warning("unable to read image","memory allocation failed");
  1721. X              DestroyImages(image);
  1722. X              return((Image *) NULL);
  1723. X            }
  1724. X          p=image->comments;
  1725. X          *p='\0';
  1726. X          c=fgetc(image->file);
  1727. X          while ((isgraph(c) || isspace(c)) && (c != '}'))
  1728. X          {
  1729. X            if (p >= (image->comments+max_characters-1))
  1730. X              {
  1731. X                /*
  1732. X                  Allocate more memory for the comment.
  1733. X                */
  1734. X                max_characters<<=1;
  1735. X                image->comments=(char *)
  1736. X                  realloc((char *) image->comments,max_characters);
  1737. X                if (image->comments == (char *) NULL)
  1738. X                  {
  1739. X                    Warning("unable to read image","memory allocation failed");
  1740. X                    DestroyImages(image);
  1741. X                    return((Image *) NULL);
  1742. X                  }
  1743. X                p=image->comments+strlen(image->comments);
  1744. X              }
  1745. X            *p++=(unsigned char) c;
  1746. X            c=fgetc(image->file);
  1747. X          }
  1748. X          *p='\0';
  1749. X          c=fgetc(image->file);
  1750. X        }
  1751. X      else
  1752. X        if (isalnum(c))
  1753. X          {
  1754. X            /*
  1755. X              Determine a keyword and its value.
  1756. X            */
  1757. X            p=keyword;
  1758. X            do
  1759. X            {
  1760. X              if ((p-keyword) < (MaxKeywordLength-1))
  1761. X                *p++=(char) c;
  1762. X              c=fgetc(image->file);
  1763. X            } while (isalnum(c));
  1764. X            *p='\0';
  1765. X            while (isspace(c) || (c == '='))
  1766. X              c=fgetc(image->file);
  1767. X            p=value;
  1768. X            while (!isspace(c))
  1769. X            {
  1770. X              if ((p-value) < (MaxKeywordLength-1))
  1771. X                *p++=(char) c;
  1772. X              c=fgetc(image->file);
  1773. X            }
  1774. X            *p='\0';
  1775. X            /*
  1776. X              Assign a value to the specified keyword.
  1777. X            */
  1778. X            if (strcmp(keyword,"alpha") == 0)
  1779. X              if ((strcmp(value,"True") == 0) || (strcmp(value,"true") == 0))
  1780. X                image->alpha=True;
  1781. X              else
  1782. X                image->class=False;
  1783. X            if (strcmp(keyword,"class") == 0)
  1784. X              if (strcmp(value,"PseudoClass") == 0)
  1785. X                image->class=PseudoClass;
  1786. X              else
  1787. X                if (strcmp(value,"DirectClass") == 0)
  1788. X                  image->class=DirectClass;
  1789. X                else
  1790. X                  image->class=UndefinedClass;
  1791. X            if (strcmp(keyword,"colors") == 0)
  1792. X              image->colors=(unsigned int) atoi(value);
  1793. X            if (strcmp(keyword,"compression") == 0)
  1794. SHAR_EOF
  1795. true || echo 'restore of ImageMagick/decode.c failed'
  1796. fi
  1797. echo 'End of ImageMagick part 14'
  1798. echo 'File ImageMagick/decode.c is continued in part 15'
  1799. echo 15 > _shar_seq_.tmp
  1800. exit 0
  1801.  
  1802. exit 0 # Just in case...
  1803. -- 
  1804.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1805. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1806.  "It's intuitively obvious to the |
  1807.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1808.