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

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i077:  imagemagic - X11 image processing and display, Part21/38
  4. Message-ID: <1993Jul14.231854.21892@sparky.sterling.com>
  5. X-Md4-Signature: 0b8747843f0f2df19ca118352736fc20
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 23:18:54 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 77
  13. Archive-name: imagemagic/part21
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.21 (part 21 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/compress.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" != 21; 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/compress.c'
  36. else
  37. echo 'x - continuing file ImageMagick/compress.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/compress.c' &&
  39. X      {
  40. X        /*
  41. X          Add string.
  42. X        */
  43. X        OutputCode(last_code);
  44. X        table[next_index].prefix=last_code;
  45. X        table[next_index].suffix=pixels[i];
  46. X        table[next_index].next=table[last_code].next;
  47. X        table[last_code].next=next_index;
  48. X        next_index++;
  49. X        /*
  50. X          Did we just move up to next bit width?
  51. X          */
  52. X        if ((next_index >> code_width) != 0)
  53. X          {
  54. X            code_width++;
  55. X            if (code_width > 12)
  56. X              {
  57. X                /*
  58. X                  Did we overflow the max bit width?
  59. X                */
  60. X                code_width--;
  61. X                OutputCode(LZWClr);
  62. X                for (index=0; index < 256; index++)
  63. X                {
  64. X                  table[index].prefix=(-1);
  65. X                  table[index].suffix=index;
  66. X                  table[index].next=(-1);
  67. X                }
  68. X                next_index=LZWEod+1;
  69. X                code_width=9;
  70. X              }
  71. X            }
  72. X          last_code=pixels[i];
  73. X      }
  74. X  }
  75. X  /*
  76. X    Flush tables.
  77. X  */
  78. X  OutputCode(last_code);
  79. X  OutputCode(LZWEod);
  80. X  if (number_bits != 0)
  81. X    PrintByte(accumulator >> 24);
  82. X  (void) free(table);
  83. X  return(True);
  84. }
  85. X
  86. /*
  87. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  88. %                                                                             %
  89. %                                                                             %
  90. %                                                                             %
  91. %   L Z W E n c o d e I m a g e                                               %
  92. %                                                                             %
  93. %                                                                             %
  94. %                                                                             %
  95. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  96. %
  97. %  Function LZWEncodeImage compresses an image via LZW-coding.
  98. %
  99. %  The format of the LZWEncodeImage routine is:
  100. %
  101. %      status=LZWEncodeImage(image,data_size)
  102. %
  103. %  A description of each parameter follows:
  104. %
  105. %    o status:  Function LZWEncodeImage returns True if all the pixels are
  106. %      compressed without error, otherwise False.
  107. %
  108. %    o image: The address of a structure of type Image.
  109. %
  110. %
  111. */
  112. unsigned int LZWEncodeImage(image,data_size)
  113. Image
  114. X  *image;
  115. X
  116. unsigned int
  117. X  data_size;
  118. {
  119. #define MaxCode(number_bits)  ((1 << (number_bits))-1)
  120. #define MaxHashTable  5003
  121. #define MaxLZWBits  12
  122. #define MaxLZWTable  (1 << MaxLZWBits)
  123. #define LZWOutputCode(code) \
  124. { \
  125. X  /*  \
  126. X    Emit a code. \
  127. X  */ \
  128. X  if (bits > 0) \
  129. X    datum|=((long) code << bits); \
  130. X  else \
  131. X    datum=(long) code; \
  132. X  bits+=number_bits; \
  133. X  while (bits >= 8)  \
  134. X  { \
  135. X    /*  \
  136. X      Add a character to current packet. \
  137. X    */ \
  138. X    byte_count++; \
  139. X    packet[byte_count]=(unsigned char) (datum & 0xff); \
  140. X    if (byte_count >= 255) \
  141. X      { \
  142. X        packet[0]=(unsigned char) byte_count++; \
  143. X        (void) fwrite((char *) packet,1,byte_count,image->file); \
  144. X        byte_count=0; \
  145. X      } \
  146. X    datum>>=8; \
  147. X    bits-=8; \
  148. X  } \
  149. X  if (free_code > max_code)  \
  150. X    { \
  151. X      number_bits++; \
  152. X      if (number_bits == MaxLZWBits) \
  153. X        max_code=MaxLZWTable; \
  154. X      else \
  155. X        max_code=MaxCode(number_bits); \
  156. X    } \
  157. }
  158. X
  159. X  int
  160. X    bits,
  161. X    byte_count,
  162. X    next_pixel,
  163. X    number_bits;
  164. X
  165. X  long
  166. X    datum;
  167. X
  168. X  register int
  169. X    displacement,
  170. X    i,
  171. X    j,
  172. X    k;
  173. X
  174. X  register RunlengthPacket
  175. X    *p;
  176. X
  177. X  short
  178. X    clear_code,
  179. X    end_of_information_code,
  180. X    free_code,
  181. X    *hash_code,
  182. X    *hash_prefix,
  183. X    index,
  184. X    max_code,
  185. X    waiting_code;
  186. X
  187. X  unsigned char
  188. X    *packet,
  189. X    *hash_suffix;
  190. X
  191. X  /*
  192. X    Allocate encoder tables.
  193. X  */
  194. X  packet=(unsigned char *) malloc(256*sizeof(unsigned char));
  195. X  hash_code=(short *) malloc(MaxHashTable*sizeof(short));
  196. X  hash_prefix=(short *) malloc(MaxHashTable*sizeof(short));
  197. X  hash_suffix=(unsigned char *) malloc(MaxHashTable*sizeof(unsigned char));
  198. X  if ((packet == (unsigned char *) NULL) || (hash_code == (short *) NULL) ||
  199. X      (hash_prefix == (short *) NULL) ||
  200. X      (hash_suffix == (unsigned char *) NULL))
  201. X    return(False);
  202. X  /*
  203. X    Initialize LZW encoder.
  204. X  */
  205. X  number_bits=data_size;
  206. X  max_code=MaxCode(number_bits);
  207. X  clear_code=((short) 1 << (data_size-1));
  208. X  end_of_information_code=clear_code+1;
  209. X  free_code=clear_code+2;
  210. X  byte_count=0;
  211. X  datum=0;
  212. X  bits=0;
  213. X  for (i=0; i < MaxHashTable; i++)
  214. X    hash_code[i]=0;
  215. X  LZWOutputCode(clear_code);
  216. X  /*
  217. X    Encode pixels.
  218. X  */
  219. X  p=image->pixels;
  220. X  waiting_code=p->index;
  221. X  for (i=0; i < image->packets; i++)
  222. X  {
  223. X    for (j=0; j <= ((int) p->length); j++)
  224. X    {
  225. X      /*
  226. X        Probe hash table.
  227. X      */
  228. X      index=p->index & 0xff;
  229. X      k=(int) ((int) index << (MaxLZWBits-8))+waiting_code;
  230. X      if (k >= MaxHashTable)
  231. X        k-=MaxHashTable;
  232. X      if (hash_code[k] > 0)
  233. X        {
  234. X          if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index))
  235. X            {
  236. X              waiting_code=hash_code[k];
  237. X              continue;
  238. X            }
  239. X          if (k == 0)
  240. X            displacement=1;
  241. X          else
  242. X            displacement=MaxHashTable-k;
  243. X          next_pixel=False;
  244. X          for ( ; ; )
  245. X          {
  246. X            k-=displacement;
  247. X            if (k < 0)
  248. X              k+=MaxHashTable;
  249. X            if (hash_code[k] == 0)
  250. X              break;
  251. X            if ((hash_prefix[k] == waiting_code) && (hash_suffix[k] == index))
  252. X              {
  253. X                waiting_code=hash_code[k];
  254. X                next_pixel=True;
  255. X                break;
  256. X              }
  257. X          }
  258. X          if (next_pixel == True)
  259. X            continue;
  260. X        }
  261. X      LZWOutputCode(waiting_code);
  262. X      if (free_code < MaxLZWTable)
  263. X        {
  264. X          hash_code[k]=free_code++;
  265. X          hash_prefix[k]=waiting_code;
  266. X          hash_suffix[k]=index;
  267. X        }
  268. X      else
  269. X        {
  270. X          /*
  271. X            Fill the hash table with empty entries.
  272. X          */
  273. X          for (k=0; k < MaxHashTable; k++)
  274. X            hash_code[k]=0;
  275. X          /*
  276. X            Reset compressor and issue a clear code.
  277. X          */
  278. X          free_code=clear_code+2;
  279. X          LZWOutputCode(clear_code);
  280. X          number_bits=data_size;
  281. X          max_code=MaxCode(number_bits);
  282. X        }
  283. X      waiting_code=index;
  284. X    }
  285. X    p++;
  286. X  }
  287. X  /*
  288. X    Flush out the buffered code.
  289. X  */
  290. X  LZWOutputCode(waiting_code);
  291. X  LZWOutputCode(end_of_information_code);
  292. X  if (bits > 0)
  293. X    {
  294. X      /*
  295. X        Add a character to current packet.
  296. X      */
  297. X      byte_count++;
  298. X      packet[byte_count]=(unsigned char) (datum & 0xff);
  299. X      if (byte_count >= 255)
  300. X        {
  301. X          packet[0]=(unsigned char) byte_count++;
  302. X          (void) fwrite((char *) packet,1,byte_count,image->file);
  303. X          byte_count=0;
  304. X        }
  305. X    }
  306. X  /*
  307. X    Flush accumulated data.
  308. X  */
  309. X  if (byte_count > 0)
  310. X    {
  311. X      packet[0]=(unsigned char) byte_count++;
  312. X      (void) fwrite((char *) packet,1,byte_count,image->file);
  313. X      byte_count=0;
  314. X    }
  315. X  /*
  316. X    Free encoder memory.
  317. X  */
  318. X  (void) free((char *) hash_suffix);
  319. X  (void) free((char *) hash_prefix);
  320. X  (void) free((char *) hash_code);
  321. X  (void) free((char *) packet);
  322. X  if (i < image->packets)
  323. X    return(False);
  324. X  return(True);
  325. }
  326. X
  327. /*
  328. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  329. %                                                                             %
  330. %                                                                             %
  331. %                                                                             %
  332. %   P a c k b i t s E n c o d e I m a g e                                     %
  333. %                                                                             %
  334. %                                                                             %
  335. %                                                                             %
  336. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  337. %
  338. %  Function PackbitsEncodeImage compresses an image via Macintosh pack bits
  339. %  encoding.
  340. %
  341. %  The format of the PackbitsEncodeImage routine is:
  342. %
  343. %      status=PackbitsEncodeImage(image,scanline,packbits)
  344. %
  345. %  A description of each parameter follows:
  346. %
  347. %    o status:  Function PackbitsEncodeImage returns True if all the pixels are
  348. %      compressed without error, otherwise False.
  349. %
  350. %    o image: The address of a structure of type Image.
  351. %
  352. %    o scanline: A pointer to an array of characters to pack.
  353. %
  354. %    o packbits: A pointer to an array of characters where the packed
  355. %      characters are stored.
  356. %
  357. %
  358. */
  359. unsigned int PackbitsEncodeImage(image,scanline,packbits)
  360. Image
  361. X  *image;
  362. X
  363. unsigned char
  364. X  *scanline,
  365. X  *packbits;
  366. {
  367. #define MaxCount  128
  368. #define MaxPackBitsRunlength  128
  369. X
  370. X  int
  371. X    count,
  372. X    number_packets,
  373. X    repeat_count,
  374. X    runlength;
  375. X
  376. X  register int
  377. X    i;
  378. X
  379. X  register unsigned char
  380. X    *p,
  381. X    *q;
  382. X
  383. X  unsigned char
  384. X    index;
  385. X
  386. X  /*
  387. X    Pack scanline.
  388. X  */
  389. X  count=0;
  390. X  runlength=0;
  391. X  p=scanline+(image->columns-1);
  392. X  q=packbits;
  393. X  index=(*p);
  394. X  for (i=image->columns-1; i >= 0; i--)
  395. X  {
  396. X    if (index == *p)
  397. X      runlength++;
  398. X    else
  399. X      {
  400. X        if (runlength < 3)
  401. X          while (runlength > 0)
  402. X          {
  403. X            *q++=index;
  404. X            runlength--;
  405. X            count++;
  406. X            if (count == MaxCount)
  407. X              {
  408. X                *q++=MaxCount-1;
  409. X                count-=MaxCount;
  410. X              }
  411. X          }
  412. X        else
  413. X          {
  414. X            if (count > 0)
  415. X              *q++=count-1;
  416. X            count=0;
  417. X            while (runlength > 0)
  418. X            {
  419. X              repeat_count=runlength;
  420. X              if (repeat_count > MaxPackBitsRunlength)
  421. X                repeat_count=MaxPackBitsRunlength;
  422. X              *q++=index;
  423. X              *q++=257-repeat_count;
  424. X              runlength-=repeat_count;
  425. X            }
  426. X          }
  427. X        runlength=1;
  428. X      }
  429. X    index=(*p);
  430. X    p--;
  431. X  }
  432. X  if (runlength < 3)
  433. X    while (runlength > 0)
  434. X    {
  435. X      *q++=index;
  436. X      runlength--;
  437. X      count++;
  438. X      if (count == MaxCount)
  439. X        {
  440. X          *q++=MaxCount-1;
  441. X          count-=MaxCount;
  442. X        }
  443. X    }
  444. X  else
  445. X    {
  446. X      if (count > 0)
  447. X        *q++=count-1;
  448. X      count=0;
  449. X      while (runlength > 0)
  450. X      {
  451. X        repeat_count=runlength;
  452. X        if (repeat_count > MaxPackBitsRunlength)
  453. X          repeat_count=MaxPackBitsRunlength;
  454. X        *q++=index;
  455. X        *q++=257-repeat_count;
  456. X        runlength-=repeat_count;
  457. X      }
  458. X    }
  459. X  if (count > 0)
  460. X    *q++=count-1;
  461. X  /*
  462. X    Write the number of and the packed packets.
  463. X  */
  464. X  number_packets=q-packbits;
  465. X  if ((image->columns-1) > 250)
  466. X    {
  467. X      MSBFirstWriteShort((unsigned short) number_packets,image->file);
  468. X      number_packets+=2;
  469. X    }
  470. X  else
  471. X    {
  472. X      index=number_packets;
  473. X      (void) fputc((char) index,image->file);
  474. X      number_packets++;
  475. X    }
  476. X  while (q != packbits)
  477. X  {
  478. X    q--;
  479. X    (void) fputc((char) *q,image->file);
  480. X  }
  481. X  return(number_packets);
  482. }
  483. X
  484. /*
  485. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  486. %                                                                             %
  487. %                                                                             %
  488. %                                                                             %
  489. %   Q D e c o d e I m a g e                                                   %
  490. %                                                                             %
  491. %                                                                             %
  492. %                                                                             %
  493. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  494. %
  495. %  Function QDecodeImage uncompresses an image via Q-coding.
  496. %
  497. %  The format of the QDecodeImage routine is:
  498. %
  499. %      count=QDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  500. %
  501. %  A description of each parameter follows:
  502. %
  503. %    o count:  The QDecodeImage routine returns this integer value.  It is
  504. %      the actual number of pixels created by the decompression process.
  505. %
  506. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  507. %      pixel data.
  508. %
  509. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  510. %      the uncompression process.  The number of bytes in this array
  511. %      must be at least equal to the number columns times the number of rows
  512. %      of the source pixels.
  513. %
  514. %    o number_columns:  An integer value that is the number of columns or
  515. %      width in pixels of your source image.
  516. %
  517. %    o number_rows:  An integer value that is the number of rows or
  518. %      heigth in pixels of your source image.
  519. %
  520. %
  521. */
  522. unsigned int QDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  523. unsigned char
  524. X  *compressed_pixels,
  525. X  *pixels;
  526. X
  527. unsigned int
  528. X  number_columns,
  529. X  number_rows;
  530. {
  531. X  int
  532. X    decision,
  533. X    i,
  534. X    prediction,
  535. X    row;
  536. X
  537. X  register int
  538. X    column,
  539. X    magnitude,
  540. X    sign,
  541. X    state,
  542. X    value;
  543. X
  544. X  register ScanlinePacket
  545. X    *cs,
  546. X    *ls;
  547. X
  548. X  register unsigned char
  549. X    *p;
  550. X
  551. X  ScanlinePacket
  552. X    *scanline;
  553. X
  554. X  for (i=0; i < MaxContextStates; i++)
  555. X  {
  556. X    probability_estimate[i]=0;
  557. X    more_probable[i]=0;
  558. X    less_probable[i]=1;
  559. X  }
  560. X  /*
  561. X    Allocate scanline for row values and states
  562. X  */
  563. X  scanline=(ScanlinePacket *)
  564. X    malloc((2*(number_columns+1)*sizeof(ScanlinePacket)));
  565. X  if (scanline == (ScanlinePacket *) NULL)
  566. X    {
  567. X      Warning("unable to compress image, unable to allocate memory",
  568. X        (char *) NULL);
  569. X      exit(1);
  570. X    }
  571. X  cs=scanline;
  572. X  for (i=0; i < 2*(number_columns+1); i++)
  573. X  {
  574. X    cs->pixel=0;
  575. X    cs->state=ZeroState;
  576. X    cs++;
  577. X  }
  578. X  interval=MinimumIntervalD;
  579. X  p=pixels;
  580. X  q=compressed_pixels+1;
  581. X  /*
  582. X    Add a new unsigned char of compressed data to the Code register.
  583. X  */
  584. X  code=(int) (*q) << 16;
  585. X  if ((*q++) == 0xff)
  586. X    code+=((int) (*q) << 9)+0x02;
  587. X  else
  588. X    code+=((*q) << 8)+0x01;
  589. X  code<<=4;
  590. X  code+=(interval << 16);
  591. X  /*
  592. X    Decode each image scanline.
  593. X  */
  594. X  for (row=0; row < number_rows; row++)
  595. X  {
  596. X    ls=scanline+(number_columns+1)*((row+0) % 2);
  597. X    cs=scanline+(number_columns+1)*((row+1) % 2);
  598. X    for (column=0; column < number_columns; column++)
  599. X    {
  600. X      prediction=(int) cs->pixel-(int) ls->pixel;
  601. X      ls++;
  602. X      prediction+=(int) ls->pixel;
  603. X      state=statistics[cs->state][ls->state];
  604. X      cs++;
  605. X      cs->state=ZeroState;
  606. X      /*
  607. X        Branch for zero code value
  608. X      */
  609. X      Decode(state,&decision);
  610. X      if (decision == No)
  611. X        value=0;
  612. X      else
  613. X        {
  614. X          /*
  615. X            Decode sign information
  616. X          */
  617. X          state++;
  618. X          Decode(state,&decision);
  619. X          if (decision == Yes)
  620. X            sign=(-1);
  621. X          else
  622. X            {
  623. X              sign=1;
  624. X              state++;
  625. X            }
  626. X          state++;
  627. X          /*
  628. X            Branch for value=+-1
  629. X          */
  630. X          Decode(state,&decision);
  631. X          if (decision == No)
  632. X            value=1;
  633. X          else
  634. X            {
  635. X              /*
  636. X                Establish magnitude of value.
  637. X              */
  638. X              magnitude=2;
  639. X              state=100;
  640. X              Decode(state,&decision);
  641. X              while (decision != No)
  642. X              {
  643. X                if (state < 107)
  644. X                  state++;
  645. X                magnitude<<=1;
  646. X                Decode(state,&decision);
  647. X              }
  648. X              /*
  649. X                Code remaining bits.
  650. X              */
  651. X              state+=7;
  652. X              value=1;
  653. X              magnitude>>=2;
  654. X              if (magnitude != 0)
  655. X                {
  656. X                  Decode(state,&decision);
  657. X                  state+=6;
  658. X                  value=(value << 1) | decision;
  659. X                  magnitude>>=1;
  660. X                  while (magnitude != 0)
  661. X                  {
  662. X                    Decode(state,&decision);
  663. X                    value=(value << 1) | decision;
  664. X                    magnitude>>=1;
  665. X                  }
  666. X                }
  667. X              value++;
  668. X            }
  669. X          if (value > LowerBound)
  670. X            if (value <= UpperBound)
  671. X              cs->state=
  672. X                (sign < ZeroState ? SmallPostitiveState : SmallNegativeState);
  673. X            else
  674. X              cs->state=
  675. X                (sign < ZeroState ? LargePostitiveState : LargeNegativeState);
  676. X          if (sign < 0)
  677. X            value=(-value);
  678. X        }
  679. X      cs->pixel=(unsigned char) (value+prediction);
  680. X      *p++=cs->pixel;
  681. X    }
  682. X  }
  683. X  (void) free((char *) scanline);
  684. X  return((unsigned int) (p-pixels));
  685. }
  686. X
  687. /*
  688. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  689. %                                                                             %
  690. %                                                                             %
  691. %                                                                             %
  692. %   Q E n c o d e I m a g e                                                   %
  693. %                                                                             %
  694. %                                                                             %
  695. %                                                                             %
  696. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  697. %
  698. %  Function QEncodeImage compresses an image via q-coding.  QEncodeImage uses
  699. %  a simple predictive method.  The predictor combines three neighboring
  700. %  samples (A, B, and C) to form a prediction of the sample X:
  701. %
  702. %    C B
  703. %    A X
  704. %
  705. %  The prediction formula is A + B - C.  The prediction is subtracted from
  706. %  from the actual sample X and the difference is encoded by an arithmetic
  707. %  entropy coding method.
  708. %
  709. %  The format of the QEncodeImage routine is:
  710. %
  711. %      count=QEncodeImage(pixels,compressed_pixels,number_columns,number_rows)
  712. %
  713. %  A description of each parameter follows:
  714. %
  715. %    o count:  The QEncodeImage routine returns this integer value.  It is
  716. %      the actual number of compressed pixels created by the compression
  717. %      process.
  718. %
  719. %    o pixels:  The address of a byte (8 bits) array of pixel data.
  720. %
  721. %    o compressed_pixels:  The address of a byte (8 bits) array of pixel data
  722. %      created by the compression process.  The number of bytes in this array
  723. %      must be at least equal to the number columns times the number of rows
  724. %      of the source pixels to allow for the possibility that no compression
  725. %      is possible.  The actual number of bytes used is reflected by the
  726. %      count parameter.
  727. %
  728. %    o number_columns:  An integer value that is the number of columns or
  729. %      width in pixels of your source image.
  730. %
  731. %    o number_rows:  An integer value that is the number of rows or
  732. %      heigth in pixels of your source image.
  733. %
  734. %
  735. %
  736. */
  737. unsigned int QEncodeImage(pixels,compressed_pixels,number_columns,number_rows)
  738. unsigned char
  739. X  *pixels,
  740. X  *compressed_pixels;
  741. X
  742. unsigned int
  743. X  number_columns,
  744. X  number_rows;
  745. {
  746. X  int
  747. X    i,
  748. X    prediction,
  749. X    row;
  750. X
  751. X  register int
  752. X    column,
  753. X    magnitude,
  754. X    sign,
  755. X    state,
  756. X    value;
  757. X
  758. X  register ScanlinePacket
  759. X    *cs,
  760. X    *ls;
  761. X
  762. X  register unsigned char
  763. X    *p;
  764. X
  765. X  ScanlinePacket
  766. X    *scanline;
  767. X
  768. X  void
  769. X    Flush();
  770. X
  771. X  for (i=0; i < MaxContextStates; i++)
  772. X  {
  773. X    probability_estimate[i]=0;
  774. X    more_probable[i]=0;
  775. X  }
  776. X  /*
  777. X    Allocate scanline for row values and states.
  778. X  */
  779. X  scanline=(ScanlinePacket *)
  780. X    malloc((2*(number_columns+1)*sizeof(ScanlinePacket)));
  781. X  if (scanline == (ScanlinePacket *) NULL)
  782. X    {
  783. X      Warning("unable to compress image, unable to allocate memory",
  784. X        (char *) NULL);
  785. X      exit(1);
  786. X    }
  787. X  cs=scanline;
  788. X  for (i=0; i < 2*(number_columns+1); i++)
  789. X  {
  790. X    cs->pixel=0;
  791. X    cs->state=ZeroState;
  792. X    cs++;
  793. X  }
  794. X  interval=MinimumIntervalE;
  795. X  p=pixels;
  796. X  q=compressed_pixels;
  797. X  (*q)++;
  798. X  code=0x00180000;
  799. X  /*
  800. X    Encode each scanline.
  801. X  */
  802. X  for (row=0; row < number_rows; row++)
  803. X  {
  804. X    ls=scanline+(number_columns+1)*((row+0) % 2);
  805. X    cs=scanline+(number_columns+1)*((row+1) % 2);
  806. X    for (column=0; column < number_columns; column++)
  807. X    {
  808. X      prediction=(int) cs->pixel-(int) ls->pixel;
  809. X      ls++;
  810. X      prediction+=(int) ls->pixel;
  811. X      state=statistics[cs->state][ls->state];
  812. X      cs++;
  813. X      cs->pixel=(*p++);
  814. X      cs->state=ZeroState;
  815. X      value=(int) cs->pixel-prediction;
  816. X      Encode(state,(value == 0 ? No : Yes));
  817. X      if (value != 0)
  818. X        {
  819. X          /*
  820. X            Code sign information
  821. X          */
  822. X          state++;
  823. X          sign=(value < 0 ? -1 : 1);
  824. X          Encode(state,(sign >= 0 ? No : Yes));
  825. X          if (sign < 0)
  826. X            value=(-value);
  827. X          else
  828. X            state++;
  829. X          state++;
  830. X          value--;
  831. X          /*
  832. X            Branch for code=+-1
  833. X          */
  834. X          Encode(state,(value == 0 ? No : Yes));
  835. X          if (value != 0)
  836. X            {
  837. X              /*
  838. X                Establish magnitude of value.
  839. X              */
  840. X              state=100;
  841. X              magnitude=2;
  842. X              while (value >= magnitude)
  843. X              {
  844. X                Encode(state,Yes);
  845. X                if (state < 107)
  846. X                  state++;
  847. X                magnitude<<=1;
  848. X              }
  849. X              Encode(state,No);
  850. X              /*
  851. X                Code remaining bits
  852. X              */
  853. X              state+=7;
  854. X              magnitude>>=2;
  855. X              if (magnitude != 0)
  856. X                {
  857. X                  Encode(state,((magnitude & value) == 0 ? No : Yes));
  858. X                  state+=6;
  859. X                  magnitude>>=1;
  860. X                  while (magnitude != 0)
  861. X                  {
  862. X                    Encode(state,((magnitude & value) == 0 ? No : Yes));
  863. X                    magnitude>>=1;
  864. X                  }
  865. X                }
  866. X            }
  867. X          if (value >= LowerBound)
  868. X            if (value < UpperBound)
  869. X              cs->state=
  870. X                (sign < ZeroState ? SmallPostitiveState : SmallNegativeState);
  871. X            else
  872. X              cs->state=
  873. X                (sign < ZeroState ? LargePostitiveState : LargeNegativeState);
  874. X        }
  875. X    }
  876. X  }
  877. X  Flush();
  878. X  (void) free((char *) scanline);
  879. X  return((unsigned int) (q-compressed_pixels));
  880. }
  881. X
  882. /*
  883. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  884. %                                                                             %
  885. %                                                                             %
  886. %                                                                             %
  887. %   R u n l e n g t h D e c o d e I m a g e                                   %
  888. %                                                                             %
  889. %                                                                             %
  890. %                                                                             %
  891. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  892. %
  893. %  Function RunlengthDecodeImage unpacks the packed image pixels into
  894. %  runlength-encoded pixel packets.  The packed image pixel memory is then
  895. %  freed.
  896. %
  897. %  The format of the RunlengthDecodeImage routine is:
  898. %
  899. %      status=RunlengthDecodeImage(image)
  900. %
  901. %  A description of each parameter follows:
  902. %
  903. %    o status: Function RunlengthDecodeImage return True if the image is
  904. %      decoded.  False is returned if there is an error occurs.
  905. %
  906. %    o image: The address of a structure of type Image.
  907. %
  908. %
  909. */
  910. unsigned int RunlengthDecodeImage(image)
  911. Image
  912. X  *image;
  913. {
  914. X  register int
  915. X    i;
  916. X
  917. X  register RunlengthPacket
  918. X    *q;
  919. X
  920. X  register unsigned char
  921. X    *p;
  922. X
  923. X  unsigned long
  924. X    count;
  925. X
  926. X  if (image->packed_pixels == (unsigned char *) NULL)
  927. X    {
  928. X      Warning("unable to unpack pixels","no packed image pixels");
  929. X      return(False);
  930. X    }
  931. X  /*
  932. X    Allocate pixels.
  933. X  */
  934. X  if (image->pixels != (RunlengthPacket *) NULL)
  935. X    (void) free((char *) image->pixels);
  936. X  image->pixels=(RunlengthPacket *)
  937. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  938. X  if (image->pixels == (RunlengthPacket *) NULL)
  939. X    {
  940. X      Warning("unable to unpack pixels","memory allocation failed");
  941. X      return(False);
  942. X    }
  943. X  /*
  944. X    Unpack the packed image pixels into runlength-encoded pixel packets.
  945. X  */
  946. X  p=image->packed_pixels;
  947. X  q=image->pixels;
  948. X  count=0;
  949. X  if (image->class == DirectClass)
  950. X    {
  951. X      register int
  952. X        alpha;
  953. X
  954. X      alpha=image->alpha;
  955. X      if (image->compression == RunlengthEncodedCompression)
  956. X        for (i=0; i < image->packets; i++)
  957. X        {
  958. X          q->red=(*p++);
  959. X          q->green=(*p++);
  960. X          q->blue=(*p++);
  961. X          q->index=(unsigned short) (alpha ? (*p++) : 0);
  962. X          q->length=(*p++);
  963. X          count+=(q->length+1);
  964. X          q++;
  965. X        }
  966. X      else
  967. X        for (i=0; i < image->packets; i++)
  968. X        {
  969. X          q->red=(*p++);
  970. X          q->green=(*p++);
  971. X          q->blue=(*p++);
  972. X          q->index=(unsigned short) (alpha ? (*p++) : 0);
  973. X          q->length=0;
  974. X          count++;
  975. X          q++;
  976. X        }
  977. X    }
  978. X  else
  979. X    {
  980. X      register unsigned short
  981. X        index;
  982. X
  983. X      if (image->compression == RunlengthEncodedCompression)
  984. X        {
  985. X          if (image->colors <= 256)
  986. X            for (i=0; i < image->packets; i++)
  987. X            {
  988. X              index=(*p++);
  989. X              q->red=image->colormap[index].red;
  990. X              q->green=image->colormap[index].green;
  991. X              q->blue=image->colormap[index].blue;
  992. X              q->index=index;
  993. X              q->length=(*p++);
  994. X              count+=(q->length+1);
  995. X              q++;
  996. X            }
  997. X          else
  998. X            for (i=0; i < image->packets; i++)
  999. X            {
  1000. X              index=(*p++) << 8;
  1001. X              index|=(*p++);
  1002. X              q->red=image->colormap[index].red;
  1003. X              q->green=image->colormap[index].green;
  1004. X              q->blue=image->colormap[index].blue;
  1005. X              q->index=index;
  1006. X              q->length=(*p++);
  1007. X              count+=(q->length+1);
  1008. X              q++;
  1009. X            }
  1010. X        }
  1011. X      else
  1012. X        if (image->colors <= 256)
  1013. X          for (i=0; i < image->packets; i++)
  1014. X          {
  1015. X            index=(*p++);
  1016. X            q->red=image->colormap[index].red;
  1017. X            q->green=image->colormap[index].green;
  1018. X            q->blue=image->colormap[index].blue;
  1019. X            q->index=index;
  1020. X            q->length=0;
  1021. X            count++;
  1022. X            q++;
  1023. X          }
  1024. X        else
  1025. X          for (i=0; i < image->packets; i++)
  1026. X          {
  1027. X            index=(*p++) << 8;
  1028. X            index|=(*p++);
  1029. X            q->red=image->colormap[index].red;
  1030. X            q->green=image->colormap[index].green;
  1031. X            q->blue=image->colormap[index].blue;
  1032. X            q->index=index;
  1033. X            q->length=0;
  1034. X            count++;
  1035. X            q++;
  1036. X          }
  1037. X    }
  1038. X  /*
  1039. X    Free packed pixels memory.
  1040. X  */
  1041. X  (void) free((char *) image->packed_pixels);
  1042. X  image->packed_pixels=(unsigned char *) NULL;
  1043. X  /*
  1044. X    Guarentee the correct number of pixel packets.
  1045. X  */
  1046. X  if (count > (image->columns*image->rows))
  1047. X    {
  1048. X      Warning("insufficient image data in file",image->filename);
  1049. X      return(False);
  1050. X    }
  1051. X  else
  1052. X    if (count < (image->columns*image->rows))
  1053. X      {
  1054. X        Warning("too much image data in file",image->filename);
  1055. X        return(False);
  1056. X      }
  1057. X  return(True);
  1058. }
  1059. X
  1060. /*
  1061. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1062. %                                                                             %
  1063. %                                                                             %
  1064. %                                                                             %
  1065. %   R u n l e n g t h E n c o d e I m a g e                                   %
  1066. %                                                                             %
  1067. %                                                                             %
  1068. %                                                                             %
  1069. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1070. %
  1071. %  Function RunlengthEncodeImage packs the runlength-encoded pixel packets
  1072. %  into the minimum number of bytes.
  1073. %
  1074. %  The format of the RunlengthEncodeImage routine is:
  1075. %
  1076. %      status=RunlengthEncodeImage(image)
  1077. %
  1078. %  A description of each parameter follows:
  1079. %
  1080. %    o status: Function RunlengthEncodeImage return True if the image is
  1081. %      packed.  False is returned if an error occurs.
  1082. %
  1083. %    o image: The address of a structure of type Image.
  1084. %
  1085. %
  1086. */
  1087. unsigned int RunlengthEncodeImage(image)
  1088. Image
  1089. X  *image;
  1090. {
  1091. X  register int
  1092. X    i,
  1093. X    j;
  1094. X
  1095. X  register RunlengthPacket
  1096. X    *p;
  1097. X
  1098. X  register unsigned char
  1099. X    *q;
  1100. X
  1101. X  unsigned long
  1102. X    count,
  1103. X    packets;
  1104. X
  1105. X  if (image->pixels == (RunlengthPacket *) NULL)
  1106. X    {
  1107. X      Warning("unable to pack pixels","no image pixels");
  1108. X      return(False);
  1109. X    }
  1110. X  /*
  1111. X    Runlength-encode only if it consumes less memory than no compression.
  1112. X  */
  1113. X  if (image->compression == RunlengthEncodedCompression)
  1114. X    if (image->class == DirectClass)
  1115. X      {
  1116. X        if (image->packets >= ((image->columns*image->rows*3) >> 2))
  1117. X          image->compression=NoCompression;
  1118. X      }
  1119. X    else
  1120. X      if (image->packets >= ((image->columns*image->rows) >> 1))
  1121. X        image->compression=NoCompression;
  1122. X  /*
  1123. X    Determine packed packet size.
  1124. X  */
  1125. X  if (image->class == PseudoClass)
  1126. X    {
  1127. X      image->packet_size=1;
  1128. X      if (image->colors > 256)
  1129. X        image->packet_size++;
  1130. X    }
  1131. X  else
  1132. X    {
  1133. X      image->packet_size=3;
  1134. X      if (image->alpha)
  1135. X        image->packet_size++;
  1136. X    }
  1137. X  if (image->compression == RunlengthEncodedCompression)
  1138. X    image->packet_size++;
  1139. X  /*
  1140. X    Allocate packed pixel memory.
  1141. X  */
  1142. X  if (image->packed_pixels != (unsigned char *) NULL)
  1143. X    (void) free((char *) image->packed_pixels);
  1144. X  packets=image->packets;
  1145. X  if (image->compression != RunlengthEncodedCompression)
  1146. X    packets=image->columns*image->rows;
  1147. X  image->packed_pixels=(unsigned char *)
  1148. X    malloc((unsigned int) packets*image->packet_size*sizeof(unsigned char));
  1149. X  if (image->packed_pixels == (unsigned char *) NULL)
  1150. X    {
  1151. X      Warning("unable to pack pixels","memory allocation failed");
  1152. X      return(False);
  1153. X    }
  1154. X  /*
  1155. X    Packs the runlength-encoded pixel packets into the minimum number of bytes.
  1156. X  */
  1157. X  p=image->pixels;
  1158. X  q=image->packed_pixels;
  1159. X  count=0;
  1160. X  if (image->class == DirectClass)
  1161. X    {
  1162. X      register int
  1163. X        alpha;
  1164. X
  1165. X      alpha=image->alpha;
  1166. X      if (image->compression == RunlengthEncodedCompression)
  1167. X        for (i=0; i < image->packets; i++)
  1168. X        {
  1169. X          *q++=p->red;
  1170. X          *q++=p->green;
  1171. X          *q++=p->blue;
  1172. X          if (alpha)
  1173. X            *q++=(unsigned char) p->index;
  1174. X          *q++=p->length;
  1175. X          count+=(p->length+1);
  1176. X          p++;
  1177. X        }
  1178. X      else
  1179. X        for (i=0; i < image->packets; i++)
  1180. X        {
  1181. X          for (j=0; j <= ((int) p->length); j++)
  1182. X          {
  1183. X            *q++=p->red;
  1184. X            *q++=p->green;
  1185. X            *q++=p->blue;
  1186. X            if (alpha)
  1187. X              *q++=(unsigned char) p->index;
  1188. X          }
  1189. X          count+=(p->length+1);
  1190. X          p++;
  1191. X        }
  1192. X    }
  1193. X  else
  1194. X    if (image->compression == RunlengthEncodedCompression)
  1195. X      {
  1196. X        if (image->colors <= 256)
  1197. X          for (i=0; i < image->packets; i++)
  1198. X          {
  1199. X            *q++=(unsigned char) p->index;
  1200. X            *q++=p->length;
  1201. X            count+=(p->length+1);
  1202. X            p++;
  1203. X          }
  1204. X        else
  1205. X          for (i=0; i < image->packets; i++)
  1206. X          {
  1207. X            *q++=(unsigned char) (p->index >> 8);
  1208. X            *q++=(unsigned char) p->index;
  1209. X            *q++=p->length;
  1210. X            count+=(p->length+1);
  1211. X            p++;
  1212. X          }
  1213. X      }
  1214. X    else
  1215. X      if (image->colors <= 256)
  1216. X        for (i=0; i < image->packets; i++)
  1217. X        {
  1218. X          for (j=0; j <= ((int) p->length); j++)
  1219. X            *q++=(unsigned char) p->index;
  1220. X          count+=(p->length+1);
  1221. X          p++;
  1222. X        }
  1223. X      else
  1224. X        {
  1225. X          register unsigned char
  1226. X            xff00,
  1227. X            xff;
  1228. X
  1229. X          for (i=0; i < image->packets; i++)
  1230. X          {
  1231. X            xff00=(unsigned char) (p->index >> 8);
  1232. X            xff=(unsigned char) p->index;
  1233. X            for (j=0; j <= ((int) p->length); j++)
  1234. X            {
  1235. X              *q++=xff00;
  1236. X              *q++=xff;
  1237. X            }
  1238. X            count+=(p->length+1);
  1239. X            p++;
  1240. X          }
  1241. X        }
  1242. X  /*
  1243. X    Guarentee the correct number of pixel packets.
  1244. X  */
  1245. X  if (count < (image->columns*image->rows))
  1246. X    {
  1247. X      Warning("insufficient image data in",image->filename);
  1248. X      return(False);
  1249. X    }
  1250. X  else
  1251. X    if (count > (image->columns*image->rows))
  1252. X      {
  1253. X        Warning("too much image data in",image->filename);
  1254. X        return(False);
  1255. X      }
  1256. X  return(True);
  1257. }
  1258. X
  1259. /*
  1260. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1261. %                                                                             %
  1262. %                                                                             %
  1263. %                                                                             %
  1264. %   S U N D e c o d e I m a g e                                               %
  1265. %                                                                             %
  1266. %                                                                             %
  1267. %                                                                             %
  1268. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1269. %
  1270. %  Function SUNDecodeImage unpacks the packed image pixels into
  1271. %  runlength-encoded pixel packets.
  1272. %
  1273. %  The format of the SUNDecodeImage routine is:
  1274. %
  1275. %      status=SUNDecodeImage(compressed_pixels,pixels,number_columns,
  1276. %        number_rows)
  1277. %
  1278. %  A description of each parameter follows:
  1279. %
  1280. %    o status:  Function SUNDecodeImage returns True if all the pixels are
  1281. %      uncompressed without error, otherwise False.
  1282. %
  1283. %    o compressed_pixels:  The address of a byte (8 bits) array of compressed
  1284. %      pixel data.
  1285. %
  1286. %    o pixels:  The address of a byte (8 bits) array of pixel data created by
  1287. %      the uncompression process.  The number of bytes in this array
  1288. %      must be at least equal to the number columns times the number of rows
  1289. %      of the source pixels.
  1290. %
  1291. %    o number_columns:  An integer value that is the number of columns or
  1292. %      width in pixels of your source image.
  1293. %
  1294. %    o number_rows:  An integer value that is the number of rows or
  1295. %      heigth in pixels of your source image.
  1296. %
  1297. %
  1298. */
  1299. unsigned int SUNDecodeImage(compressed_pixels,pixels,number_columns,number_rows)
  1300. unsigned char
  1301. X  *compressed_pixels,
  1302. X  *pixels;
  1303. X
  1304. unsigned int
  1305. X  number_columns,
  1306. X  number_rows;
  1307. {
  1308. X  register int
  1309. X    count;
  1310. X
  1311. X  register unsigned char
  1312. X    *p,
  1313. X    *q;
  1314. X
  1315. X  unsigned char
  1316. X    byte;
  1317. X
  1318. X  p=compressed_pixels;
  1319. X  q=pixels;
  1320. X  while ((q-pixels) <= (number_columns*number_rows))
  1321. X  {
  1322. X    byte=(*p++);
  1323. X    if (byte != 128)
  1324. X      *q++=byte;
  1325. X    else
  1326. X      {
  1327. X        /*
  1328. X          Runlength-encoded packet: <count><byte>
  1329. X        */
  1330. X        count=(*p++);
  1331. X        if (count > 0)
  1332. X          byte=(*p++);
  1333. X        while (count >= 0)
  1334. X        {
  1335. X          *q++=byte;
  1336. X          count--;
  1337. X        }
  1338. X     }
  1339. X  }
  1340. X  return(True);
  1341. }
  1342. SHAR_EOF
  1343. echo 'File ImageMagick/compress.c is complete' &&
  1344. chmod 0644 ImageMagick/compress.c ||
  1345. echo 'restore of ImageMagick/compress.c failed'
  1346. Wc_c="`wc -c < 'ImageMagick/compress.c'`"
  1347. test 68487 -eq "$Wc_c" ||
  1348.     echo 'ImageMagick/compress.c: original size 68487, current size' "$Wc_c"
  1349. rm -f _shar_wnt_.tmp
  1350. fi
  1351. # ============= ImageMagick/compress.h ==============
  1352. if test -f 'ImageMagick/compress.h' -a X"$1" != X"-c"; then
  1353.     echo 'x - skipping ImageMagick/compress.h (File already exists)'
  1354.     rm -f _shar_wnt_.tmp
  1355. else
  1356. > _shar_wnt_.tmp
  1357. echo 'x - extracting ImageMagick/compress.h (Text)'
  1358. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/compress.h' &&
  1359. /*
  1360. X  Compress utility routines.
  1361. */
  1362. extern unsigned int
  1363. X  BMPDecodeImage _Declare((unsigned char *,unsigned char *,unsigned int,
  1364. X    unsigned int)),
  1365. X  HuffmanEncodeImage _Declare((Image *)),
  1366. X  LZWDecodeImage _Declare((Image *)),
  1367. X  LZWEncodeFilter _Declare((FILE *,unsigned char *,unsigned int)),
  1368. X  LZWEncodeImage _Declare((Image *,unsigned int)),
  1369. X  PackbitsEncodeImage _Declare((Image *,unsigned char *,unsigned char *)),
  1370. X  QDecodeImage _Declare((unsigned char *,unsigned char *,unsigned int,
  1371. X    unsigned int)),
  1372. X  QEncodeImage _Declare((unsigned char *,unsigned char *,unsigned int,
  1373. X    unsigned int)),
  1374. X  RunlengthDecodeImage _Declare((Image *)),
  1375. X  RunlengthEncodeImage _Declare((Image *)),
  1376. X  SUNDecodeImage _Declare((unsigned char *,unsigned char *,unsigned int,
  1377. X    unsigned int));
  1378. SHAR_EOF
  1379. chmod 0644 ImageMagick/compress.h ||
  1380. echo 'restore of ImageMagick/compress.h failed'
  1381. Wc_c="`wc -c < 'ImageMagick/compress.h'`"
  1382. test 784 -eq "$Wc_c" ||
  1383.     echo 'ImageMagick/compress.h: original size 784, current size' "$Wc_c"
  1384. rm -f _shar_wnt_.tmp
  1385. fi
  1386. # ============= ImageMagick/display.c ==============
  1387. if test -f 'ImageMagick/display.c' -a X"$1" != X"-c"; then
  1388.     echo 'x - skipping ImageMagick/display.c (File already exists)'
  1389.     rm -f _shar_wnt_.tmp
  1390. else
  1391. > _shar_wnt_.tmp
  1392. echo 'x - extracting ImageMagick/display.c (Text)'
  1393. sed 's/^X//' << 'SHAR_EOF' > 'ImageMagick/display.c' &&
  1394. /*
  1395. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1396. %                                                                             %
  1397. %                                                                             %
  1398. %                                                                             %
  1399. %             DDDD   IIIII  SSSSS  PPPP   L       AAA   Y   Y                 %
  1400. %             D   D    I    SS     P   P  L      A   A   Y Y                  %
  1401. %             D   D    I     SSS   PPPP   L      AAAAA    Y                   %
  1402. %             D   D    I       SS  P      L      A   A    Y                   %
  1403. %             DDDD   IIIII  SSSSS  P      LLLLL  A   A    Y                   %
  1404. %                                                                             %
  1405. %                                                                             %
  1406. %          Display Machine Independent File Format Image via X11.             %
  1407. %                                                                             %
  1408. %                                                                             %
  1409. %                                                                             %
  1410. %                           Software Design                                   %
  1411. %                             John Cristy                                     %
  1412. %                              July 1992                                      %
  1413. %                                                                             %
  1414. %                                                                             %
  1415. %  Copyright 1993 E. I. du Pont de Nemours & Company                          %
  1416. %                                                                             %
  1417. %  Permission to use, copy, modify, distribute, and sell this software and    %
  1418. %  its documentation for any purpose is hereby granted without fee,           %
  1419. %  provided that the above Copyright notice appear in all copies and that     %
  1420. %  both that Copyright notice and this permission notice appear in            %
  1421. %  supporting documentation, and that the name of E. I. du Pont de Nemours    %
  1422. %  & Company not be used in advertising or publicity pertaining to            %
  1423. %  distribution of the software without specific, written prior               %
  1424. %  permission.  E. I. du Pont de Nemours & Company makes no representations   %
  1425. %  about the suitability of this software for any purpose.  It is provided    %
  1426. %  "as is" without express or implied warranty.                               %
  1427. %                                                                             %
  1428. %  E. I. du Pont de Nemours & Company disclaims all warranties with regard    %
  1429. %  to this software, including all implied warranties of merchantability      %
  1430. %  and fitness, in no event shall E. I. du Pont de Nemours & Company be       %
  1431. %  liable for any special, indirect or consequential damages or any           %
  1432. %  damages whatsoever resulting from loss of use, data or profits, whether    %
  1433. %  in an action of contract, negligence or other tortious action, arising     %
  1434. %  out of or in connection with the use or performance of this software.      %
  1435. %                                                                             %
  1436. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1437. %
  1438. %  Display is a machine architecture independent image processing
  1439. %  and display program.  It can display any image in the MIFF format on
  1440. %  any workstation display running X.  Display first determines the
  1441. %  hardware capabilities of the workstation.  If the number of unique
  1442. %  colors in the image is less than or equal to the number the workstation
  1443. %  can support, the image is displayed in an X window.  Otherwise the
  1444. %  number of colors in the image is first reduced to match the color
  1445. %  resolution of the workstation before it is displayed.
  1446. %
  1447. %  This means that a continuous-tone 24 bits-per-pixel image can display on a
  1448. %  8 bit pseudo-color device or monochrome device.  In most instances the
  1449. %  reduced color image closely resembles the original.  Alternatively, a
  1450. %  monochrome or pseudo-color image can display on a continuous-tone 24
  1451. %  bits-per-pixel device.
  1452. %
  1453. %  The Display program command syntax is:
  1454. %
  1455. %  Usage: display [options ...] file [ [options ...] file ...]
  1456. %
  1457. %  Where options include:
  1458. %    -backdrop           display image centered on a backdrop
  1459. %    -clip geometry      preferred size and location of the clipped image
  1460. %    -colormap type      Shared or Private
  1461. %    -colors value       preferred number of colors in the image
  1462. %    -colorspace type    GRAY, RGB, XYZ, YCbCr, YIQ, or YUV
  1463. %    -compress type      RunlengthEncoded or QEncoded
  1464. %    -delay seconds      display the next image after pausing
  1465. %    -density geometry   vertical and horizonal density of the image
  1466. %    -display server     display image to this X server
  1467. %    -dither             apply Floyd/Steinberg error diffusion to image
  1468. %    -enhance            apply a digital filter to enhance a noisy image
  1469. %    -gamma value        level of gamma correction
  1470. %    -geometry geometry  preferred size and location of the image window
  1471. %    -interlace type     NONE, LINE, or PLANE
  1472. %    -inverse            apply color inversion to image
  1473. %    -map type           display image using this Standard Colormap
  1474. %    -monochrome         transform image to black and white
  1475. %    -noise              reduce noise with a noise peak elimination filter
  1476. %    -normalize          tranform image to span the full the range of colors
  1477. %    -page geometry      size and location of the Postscript page
  1478. %    -quality value      JPEG quality setting
  1479. %    -reflect            reverse image scanlines
  1480. %    -rotate degrees     apply Paeth rotation to the image
  1481. %    -scale geometry     preferred size factors of the image
  1482. %    -scene value        image scene number
  1483. %    -treedepth value    depth of the color classification tree
  1484. %    -update seconds     detect when image file is modified and redisplay
  1485. %    -verbose            print detailed information about the image
  1486. %    -visual type        display image using this visual type
  1487. %    -window id          display image to background of this window
  1488. %    -write filename     write image to a file
  1489. %
  1490. %  In addition to those listed above, you can specify these standard X
  1491. %  resources as command line options:  -background, -bordercolor,
  1492. %  -borderwidth, -font, -foreground, -iconGeometry, -iconic, -name, or
  1493. %  -title.
  1494. %
  1495. %  Change '-' to '+' in any option above to reverse its effect.  For
  1496. %  example, specify +compress to store the image as uncompressed.
  1497. %
  1498. %  By default, the image format of `file' is determined by its magic
  1499. %  number.  To specify a particular image format, precede the filename
  1500. %  with an image format name and a colon (i.e. ps:image) or specify the
  1501. %  image type as the filename suffix (i.e. image.ps).  Specify 'file' as
  1502. %  '-' for standard input or output.
  1503. %
  1504. %  Buttons:
  1505. %    1    press and drag to select a command from a pop-up menu
  1506. %    2    press and drag to define a region of the image to clip
  1507. %    3    press and drag to define a region of the image to magnify
  1508. %
  1509. %  Keyboard accelerators:
  1510. %    i    display information about the image
  1511. %    r    reflect the image scanlines
  1512. %    /    rotate the image 90 degrees clockwise
  1513. %    \    rotate the image 90 degrees counter-clockwise
  1514. %    <    half the image size
  1515. %    >    double the image size
  1516. %    o    restore the image to its original size
  1517. %    a    annotate the image with text
  1518. %    c    composite image with another
  1519. %    l    load an image from a file
  1520. %    w    write the image to a file
  1521. %    n    display the next image
  1522. %    p    display the previous image
  1523. %    q    discard all images and exit program
  1524. %    1-9  change the level of magnification
  1525. %
  1526. %
  1527. */
  1528. X
  1529. /*
  1530. X  Include declarations.
  1531. */
  1532. #include "display.h"
  1533. #include "image.h"
  1534. #include "X.h"
  1535. X
  1536. /*
  1537. X  State declarations.
  1538. */
  1539. #define ControlState  0x0001
  1540. #define DefaultState  0x0000
  1541. #define EscapeState  0x0002
  1542. #define ExitState  0x0004
  1543. #define ImageMappedState  0x0008
  1544. #define InfoMappedState  0x0010
  1545. #define LastImageState  0x0020
  1546. #define MagnifyMappedState  0x0040
  1547. #define NextImageState  0x0080
  1548. #define PanMappedState  0x0100
  1549. #define ReconfigureImageState  0x0200
  1550. #define UpdateColormapState  0x0400
  1551. #define UpdateConfigurationState  0x0800
  1552. X
  1553. /*
  1554. X  Global declarations.
  1555. */
  1556. char
  1557. X  *client_name;
  1558. X
  1559. /*
  1560. X  Forward declarations.
  1561. */
  1562. static Cursor
  1563. X  XMakeMagnifyCursor _Declare((Display *,Colormap,Window,char *,char *));
  1564. X
  1565. static Image
  1566. X  *XImageWindowCommand _Declare((Display *,XResourceInfo *,XWindows *,
  1567. X    KeySym,Image **,unsigned long *)),
  1568. X  *XLoadImageWindow _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  1569. X  *XTileImageWindow _Declare((Display *,XResourceInfo *,XWindows *,Image *,
  1570. X    XEvent *));
  1571. X
  1572. static unsigned int
  1573. X  XConfigureImageWindow _Declare((Display *,XResourceInfo *,XWindows *,
  1574. X    unsigned int,unsigned int,Image *)),
  1575. X  XReflectImageWindow _Declare((Display *,XWindows *,Image **)),
  1576. X  XRotateImageWindow _Declare((Display *,XWindows *,unsigned int,Image **)),
  1577. X  XWriteImageWindow _Declare((Display *,XResourceInfo *,XWindows *,Image **));
  1578. X
  1579. static void
  1580. X  XDrawPanRectangle _Declare((Display *,XWindows *)),
  1581. X  XMagnifyImageWindow _Declare((Display *,XResourceInfo *,XWindows *,XEvent *)),
  1582. X  XMakeMagnifyImage _Declare((Display *,XResourceInfo *,XWindows *)),
  1583. X  XMakePanImage _Declare((Display *,XResourceInfo *,XWindows *,Image *)),
  1584. X  XPanImageWindow _Declare((Display *,XWindows *,XEvent *)),
  1585. X  XMagnifyWindowCommand _Declare((Display *,XResourceInfo *,XWindows *,KeySym)),
  1586. X  XSetClipGeometry _Declare((Display *,XWindows *,RectangleInfo *,Image *));
  1587. X
  1588. /*
  1589. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1590. %                                                                             %
  1591. %                                                                             %
  1592. %                                                                             %
  1593. %   E r r o r                                                                 %
  1594. %                                                                             %
  1595. %                                                                             %
  1596. %                                                                             %
  1597. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1598. %
  1599. %  Function Error displays an error message and then terminates the program.
  1600. %
  1601. %  The format of the Error routine is:
  1602. %
  1603. %      Error(message,qualifier)
  1604. %
  1605. %  A description of each parameter follows:
  1606. %
  1607. %    o message: Specifies the message to display before terminating the
  1608. %      program.
  1609. %
  1610. %    o qualifier: Specifies any qualifier to the message.
  1611. %
  1612. %
  1613. */
  1614. void Error(message,qualifier)
  1615. char
  1616. X  *message,
  1617. X  *qualifier;
  1618. {
  1619. X  (void) fprintf(stderr,"%s: %s",client_name,message);
  1620. X  if (qualifier != (char *) NULL)
  1621. X    (void) fprintf(stderr," (%s)",qualifier);
  1622. X  (void) fprintf(stderr,".\n");
  1623. X  exit(1);
  1624. }
  1625. X
  1626. /*
  1627. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1628. %                                                                             %
  1629. %                                                                             %
  1630. %                                                                             %
  1631. %   U s a g e                                                                 %
  1632. %                                                                             %
  1633. %                                                                             %
  1634. %                                                                             %
  1635. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1636. %
  1637. %  Function Usage displays the program command syntax.
  1638. %
  1639. %  The format of the Usage routine is:
  1640. %
  1641. %      Usage(terminate)
  1642. %
  1643. %  A description of each parameter follows:
  1644. %
  1645. %    o terminate: A value other than zero is returned if the program is to
  1646. SHAR_EOF
  1647. true || echo 'restore of ImageMagick/display.c failed'
  1648. fi
  1649. echo 'End of ImageMagick part 21'
  1650. echo 'File ImageMagick/display.c is continued in part 22'
  1651. echo 22 > _shar_seq_.tmp
  1652. exit 0
  1653.  
  1654. exit 0 # Just in case...
  1655. -- 
  1656.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1657. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1658.  "It's intuitively obvious to the |
  1659.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1660.