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

  1. Newsgroups: comp.sources.x
  2. From: cristy@eplrx7.es.duPont.com (Cristy)
  3. Subject: v20i066:  imagemagic - X11 image processing and display, Part10/38
  4. Message-ID: <1993Jul14.175519.1222@sparky.sterling.com>
  5. X-Md4-Signature: 8d8b0309544b48273cc98f2ccd1453af
  6. Sender: chris@sparky.sterling.com (Chris Olson)
  7. Organization: Sterling Software
  8. Date: Wed, 14 Jul 1993 17:55:19 GMT
  9. Approved: chris@sterling.com
  10.  
  11. Submitted-by: cristy@eplrx7.es.duPont.com (Cristy)
  12. Posting-number: Volume 20, Issue 66
  13. Archive-name: imagemagic/part10
  14. Environment: X11
  15. Supersedes: imagemagic: Volume 13, Issue 17-37
  16.  
  17. #!/bin/sh
  18. # this is magick.10 (part 10 of ImageMagick)
  19. # do not concatenate these parts, unpack them in order with /bin/sh
  20. # file ImageMagick/X.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" != 10; 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/X.c'
  36. else
  37. echo 'x - continuing file ImageMagick/X.c'
  38. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/X.c' &&
  39. X  if ((clip_info.x+(int) clip_info.width) > display_width)
  40. X    {
  41. X      if (clip_info.x >= display_width)
  42. X        return((Image *) NULL);
  43. X      clip_info.width=display_width-clip_info.x;
  44. X    }
  45. X  display_height=DisplayHeight(display,XDefaultScreen(display));
  46. X  if ((clip_info.y+(int) clip_info.height) > display_height)
  47. X    {
  48. X      if (clip_info.y >= display_height)
  49. X        return((Image *) NULL);
  50. X      clip_info.height=display_height-clip_info.y;
  51. X    }
  52. X  clip_info.x-=x_offset;
  53. X  clip_info.y-=y_offset;
  54. X  /*
  55. X    Alert user we are about to get an X region by flashing a border.
  56. X  */
  57. X  graphic_context_value.function=GXinvert;
  58. X  graphic_context_value.line_width=WindowBorderWidth;
  59. X  graphic_context_value.subwindow_mode=IncludeInferiors;
  60. X  graphic_context=XCreateGC(display,window,GCFunction | GCLineWidth |
  61. X    GCSubwindowMode,&graphic_context_value);
  62. X  if (graphic_context != (GC) NULL)
  63. X    XHighlightRegion(display,window,graphic_context,&clip_info);
  64. X  /*
  65. X    Get window X image.
  66. X  */
  67. X  ximage=XGetImage(display,window,clip_info.x,clip_info.y,clip_info.width,
  68. X    clip_info.height,AllPlanes,ZPixmap);
  69. X  if (ximage == (XImage *) NULL)
  70. X    return((Image *) NULL);
  71. X  if (graphic_context != (GC) NULL)
  72. X    {
  73. X      /*
  74. X        Alert user we got the X region by flashing a border.
  75. X      */
  76. X      XHighlightRegion(display,window,graphic_context,&clip_info);
  77. X      XFreeGC(display,graphic_context);
  78. X    }
  79. X  number_colors=0;
  80. X  colors=(XColor *) NULL;
  81. X  if (window_attributes.colormap != (Colormap) NULL)
  82. X    {
  83. X      ColormapList
  84. X        *p;
  85. X
  86. X      /*
  87. X        Search colormap list for window colormap.
  88. X      */
  89. X      number_colors=window_attributes.visual->map_entries;
  90. X      for (p=colormap_list; p != (ColormapList *) NULL; p=p->next)
  91. X        if (p->colormap == window_attributes.colormap)
  92. X          break;
  93. X      if (p == (ColormapList *) NULL)
  94. X        {
  95. X          /*
  96. X            Get the window colormap.
  97. X          */
  98. X          colors=(XColor *) malloc(number_colors*sizeof(XColor));
  99. X          if (colors == (XColor *) NULL)
  100. X            {
  101. X              XDestroyImage(ximage);
  102. X              return((Image *) NULL);
  103. X            }
  104. X          if ((window_attributes.visual->class != DirectColor) &&
  105. X              (window_attributes.visual->class != TrueColor))
  106. X            for (i=0; i < number_colors; i++)
  107. X            {
  108. X              colors[i].pixel=i;
  109. X              colors[i].pad=0;
  110. X            }
  111. X          else
  112. X            {
  113. X              unsigned long
  114. X                blue,
  115. X                blue_bit,
  116. X                green,
  117. X                green_bit,
  118. X                red,
  119. X                red_bit;
  120. X
  121. X              /*
  122. X                DirectColor or TrueColor visual.
  123. X              */
  124. X              red=0;
  125. X              green=0;
  126. X              blue=0;
  127. X              red_bit=window_attributes.visual->red_mask &
  128. X                (~(window_attributes.visual->red_mask)+1);
  129. X              green_bit=window_attributes.visual->green_mask &
  130. X                (~(window_attributes.visual->green_mask)+1);
  131. X              blue_bit=window_attributes.visual->blue_mask &
  132. X                (~(window_attributes.visual->blue_mask)+1);
  133. X              for (i=0; i < number_colors; i++)
  134. X              {
  135. X                colors[i].pixel=red | green | blue;
  136. X                colors[i].pad=0;
  137. X                red+=red_bit;
  138. X                if (red > window_attributes.visual->red_mask)
  139. X                  red=0;
  140. X                green+=green_bit;
  141. X                if (green > window_attributes.visual->green_mask)
  142. X                  green=0;
  143. X                blue+=blue_bit;
  144. X                if (blue > window_attributes.visual->blue_mask)
  145. X                  blue=0;
  146. X              }
  147. X            }
  148. X          XQueryColors(display,window_attributes.colormap,colors,
  149. X           (int) number_colors);
  150. X          /*
  151. X            Append colormap to colormap list.
  152. X          */
  153. X          p=(ColormapList *) malloc(sizeof(ColormapList));
  154. X          p->colormap=window_attributes.colormap;
  155. X          p->colors=colors;
  156. X          p->next=colormap_list;
  157. X          colormap_list=p;
  158. X        }
  159. X      colors=p->colors;
  160. X    }
  161. X  /*
  162. X    Allocate image structure.
  163. X  */
  164. X  image=AllocateImage("X");
  165. X  if (image == (Image *) NULL)
  166. X    {
  167. X      XDestroyImage(ximage);
  168. X      return((Image *) NULL);
  169. X    }
  170. X  /*
  171. X    Convert X image to MIFF format.
  172. X  */
  173. X  if ((window_attributes.visual->class != TrueColor) &&
  174. X      (window_attributes.visual->class != DirectColor))
  175. X    image->class=PseudoClass;
  176. X  image->columns=ximage->width;
  177. X  image->rows=ximage->height;
  178. X  image->packets=image->columns*image->rows;
  179. X  image->pixels=(RunlengthPacket *)
  180. X    malloc((unsigned int) image->packets*sizeof(RunlengthPacket));
  181. X  if (image->pixels == (RunlengthPacket *) NULL)
  182. X    {
  183. X      XDestroyImage(ximage);
  184. X      DestroyImage(image);
  185. X      return((Image *) NULL);
  186. X    }
  187. X  p=image->pixels;
  188. X  switch (image->class)
  189. X  {
  190. X    case DirectClass:
  191. X    {
  192. X      register unsigned long
  193. X        color,
  194. X        index;
  195. X
  196. X      unsigned long
  197. X        blue_mask,
  198. X        blue_shift,
  199. X        green_mask,
  200. X        green_shift,
  201. X        red_mask,
  202. X        red_shift;
  203. X
  204. X      /*
  205. X        Determine shift and mask for red, green, and blue.
  206. X      */
  207. X      red_mask=window_attributes.visual->red_mask;
  208. X      red_shift=0;
  209. X      while ((red_mask & 0x01) == 0)
  210. X      {
  211. X        red_mask>>=1;
  212. X        red_shift++;
  213. X      }
  214. X      green_mask=window_attributes.visual->green_mask;
  215. X      green_shift=0;
  216. X      while ((green_mask & 0x01) == 0)
  217. X      {
  218. X        green_mask>>=1;
  219. X        green_shift++;
  220. X      }
  221. X      blue_mask=window_attributes.visual->blue_mask;
  222. X      blue_shift=0;
  223. X      while ((blue_mask & 0x01) == 0)
  224. X      {
  225. X        blue_mask>>=1;
  226. X        blue_shift++;
  227. X      }
  228. X      /*
  229. X        Convert X image to DirectClass packets.
  230. X      */
  231. X      if ((number_colors > 0) &&
  232. X          (window_attributes.visual->class == DirectColor))
  233. X        for (y=0; y < image->rows; y++)
  234. X        {
  235. X          for (x=0; x < image->columns; x++)
  236. X          {
  237. X            pixel=XGetPixel(ximage,x,y);
  238. X            index=(pixel >> red_shift) & red_mask;
  239. X            p->red=(unsigned char) (colors[index].red >> 8);
  240. X            index=(pixel >> green_shift) & green_mask;
  241. X            p->green=(unsigned char) (colors[index].green >> 8);
  242. X            index=(pixel >> blue_shift) & blue_mask;
  243. X            p->blue=(unsigned char) (colors[index].blue >> 8);
  244. X            p->index=0;
  245. X            p->length=0;
  246. X            p++;
  247. X          }
  248. X        }
  249. X      else
  250. X        for (y=0; y < image->rows; y++)
  251. X          for (x=0; x < image->columns; x++)
  252. X          {
  253. X            pixel=XGetPixel(ximage,x,y);
  254. X            color=(pixel >> red_shift) & red_mask;
  255. X            p->red=(unsigned char)
  256. X              ((((unsigned long) color*65535)/red_mask) >> 8);
  257. X            color=(pixel >> green_shift) & green_mask;
  258. X            p->green=(unsigned char)
  259. X              ((((unsigned long) color*65535)/green_mask) >> 8);
  260. X            color=(pixel >> blue_shift) & blue_mask;
  261. X            p->blue=(unsigned char)
  262. X              ((((unsigned long) color*65535)/blue_mask) >> 8);
  263. X            p->index=0;
  264. X            p->length=0;
  265. X            p++;
  266. X          }
  267. X      break;
  268. X    }
  269. X    case PseudoClass:
  270. X    {
  271. X      register unsigned short
  272. X        index;
  273. X
  274. X      /*
  275. X        Create colormap.
  276. X      */
  277. X      image->colors=number_colors;
  278. X      image->colormap=(ColorPacket *) malloc(image->colors*sizeof(ColorPacket));
  279. X      if (image->colormap == (ColorPacket *) NULL)
  280. X        {
  281. X          XDestroyImage(ximage);
  282. X          DestroyImage(image);
  283. X          return((Image *) NULL);
  284. X        }
  285. X      for (i=0; i < image->colors; i++)
  286. X      {
  287. X        image->colormap[colors[i].pixel].red=colors[i].red >> 8;
  288. X        image->colormap[colors[i].pixel].green=colors[i].green >> 8;
  289. X        image->colormap[colors[i].pixel].blue=colors[i].blue >> 8;
  290. X      }
  291. X      /*
  292. X        Convert X image to PseudoClass packets.
  293. X      */
  294. X      for (y=0; y < image->rows; y++)
  295. X        for (x=0; x < image->columns; x++)
  296. X        {
  297. X          pixel=XGetPixel(ximage,x,y);
  298. X          index=(unsigned short) pixel;
  299. X          p->red=image->colormap[index].red;
  300. X          p->green=image->colormap[index].green;
  301. X          p->blue=image->colormap[index].blue;
  302. X          p->index=index;
  303. X          p->length=0;
  304. X          p++;
  305. X        }
  306. X      break;
  307. X    }
  308. X  }
  309. X  XDestroyImage(ximage);
  310. X  if (level != 0)
  311. X    {
  312. X      unsigned int
  313. X        number_children;
  314. X
  315. X      Window
  316. X        *children,
  317. X        parent;
  318. X
  319. X      /*
  320. X        Descend the window hierarchy and overlay with each subwindow image.
  321. X      */
  322. X      status=XQueryTree(display,window,&root_window,&parent,&children,
  323. X        &number_children);
  324. X      if ((status == True) && (number_children != 0))
  325. X        {
  326. X          Image
  327. X            *child_image;
  328. X
  329. X          /*
  330. X            Composite any children in back-to-front order.
  331. X          */
  332. X          for (i=0; i < number_children; i++)
  333. X          {
  334. X            child_image=XGetWindowImage(display,children[i],False,level+1);
  335. X            if (child_image != (Image *) NULL)
  336. X              {
  337. X                /*
  338. X                  Composite child window image.
  339. X                */
  340. X                XTranslateCoordinates(display,children[i],window,0,0,&x_offset,
  341. X                  &y_offset,&child);
  342. X                x_offset-=clip_info.x;
  343. X                if (x_offset < 0)
  344. X                  x_offset=0;
  345. X                y_offset-=clip_info.y;
  346. X                if (y_offset < 0)
  347. X                  y_offset=0;
  348. X                CompositeImage(image,ReplaceCompositeOp,child_image,x_offset,
  349. X                  y_offset);
  350. X                DestroyImage(child_image);
  351. X              }
  352. X          }
  353. X          XFree((void *) children);
  354. X        }
  355. X    }
  356. X  if (level <= 1)
  357. X    {
  358. X      ColormapList
  359. X        *next;
  360. X
  361. X      /*
  362. X        Free resources.
  363. X      */
  364. X      while (colormap_list != (ColormapList *) NULL)
  365. X      {
  366. X        next=colormap_list->next;
  367. X        (void) free((char *) colormap_list->colors);
  368. X        (void) free((char *) colormap_list);
  369. X        colormap_list=next;
  370. X      }
  371. X      if (image->class == PseudoClass)
  372. X        CompressColormap(image);
  373. X    }
  374. X  return(image);
  375. }
  376. X
  377. /*
  378. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  379. %                                                                             %
  380. %                                                                             %
  381. %                                                                             %
  382. %   X G e t W i n d o w I n f o                                               %
  383. %                                                                             %
  384. %                                                                             %
  385. %                                                                             %
  386. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  387. %
  388. %  Function XGetWindowInfo initializes the XWindowInfo structure.
  389. %
  390. %  The format of the XGetWindowInfo routine is:
  391. %
  392. %      XGetWindowInfo(display,visual_info,map_info,pixel_info,font_info,
  393. %        resource_info,window)
  394. %
  395. %  A description of each parameter follows:
  396. %
  397. %    o display: Specifies a connection to an X server; returned from
  398. %      XOpenDisplay.
  399. %
  400. %    o visual_info: Specifies a pointer to a X11 XVisualInfo structure;
  401. %      returned from XGetVisualInfo.
  402. %
  403. %    o map_info: If map_type is specified, this structure is initialized
  404. %      with info from the Standard Colormap.
  405. %
  406. %    o pixel_info: Specifies a pointer to a XPixelInfo structure.
  407. %
  408. %    o font_info: Specifies a pointer to a XFontStruct structure.
  409. %
  410. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  411. %
  412. %
  413. */
  414. void XGetWindowInfo(display,visual_info,map_info,pixel_info,font_info,
  415. X  resource_info,window)
  416. Display
  417. X  *display;
  418. X
  419. XXVisualInfo
  420. X  *visual_info;
  421. X
  422. XXStandardColormap
  423. X *map_info;
  424. X
  425. XXPixelInfo
  426. X  *pixel_info;
  427. X
  428. XXFontStruct
  429. X  *font_info;
  430. X
  431. XXResourceInfo
  432. X  *resource_info;
  433. X
  434. XXWindowInfo
  435. X  *window;
  436. {
  437. X  /*
  438. X    Initialize window info.
  439. X  */
  440. X  window->screen=visual_info->screen;
  441. X  window->depth=visual_info->depth;
  442. X  window->visual_info=visual_info;
  443. X  window->map_info=map_info;
  444. X  window->pixel_info=pixel_info;
  445. X  window->font_info=font_info;
  446. X  window->cursor=XCreateFontCursor(display,XC_arrow);
  447. X  XRecolorCursor(display,window->cursor,&pixel_info->background_color,
  448. X    &pixel_info->foreground_color);
  449. X  window->busy_cursor=XCreateFontCursor(display,XC_watch);
  450. X  XRecolorCursor(display,window->busy_cursor,&pixel_info->background_color,
  451. X    &pixel_info->foreground_color);
  452. X  window->graphic_context=(GC) NULL;
  453. X  window->name=(char *) NULL;
  454. X  window->geometry=(char *) NULL;
  455. X  window->icon_name=(char *) NULL;
  456. X  window->icon_geometry=resource_info->icon_geometry;
  457. X  window->clip_geometry=(char *) NULL;
  458. X  window->flags=PSize;
  459. X  window->x=0;
  460. X  window->y=0;
  461. X  window->width=1;
  462. X  window->height=1;
  463. X  window->min_width=1;
  464. X  window->min_height=1;
  465. X  window->width_inc=1;
  466. X  window->height_inc=1;
  467. X  window->border_width=resource_info->border_width;
  468. X  window->graphic_context=pixel_info->graphic_context;
  469. X  window->highlight_context=pixel_info->highlight_context;
  470. X  window->immutable=True;
  471. X  window->mask=CWBackingStore | CWBackPixel | CWBackPixmap | CWBitGravity | 
  472. X    CWBorderPixel | CWColormap | CWCursor | CWDontPropagate | CWEventMask |
  473. X    CWOverrideRedirect | CWSaveUnder | CWWinGravity;
  474. X  window->attributes.background_pixel=pixel_info->background_color.pixel;
  475. X  window->attributes.background_pixmap=(Pixmap) NULL;
  476. X  window->attributes.backing_store=NotUseful;
  477. X  window->attributes.bit_gravity=ForgetGravity;
  478. X  window->attributes.border_pixel=pixel_info->border_color.pixel;
  479. X  window->attributes.colormap=map_info->colormap;
  480. X  window->attributes.cursor=window->cursor;
  481. X  window->attributes.do_not_propagate_mask=NoEventMask;
  482. X  window->attributes.event_mask=NoEventMask;
  483. X  window->attributes.override_redirect=False;
  484. X  window->attributes.save_under=False;
  485. X  window->attributes.win_gravity=NorthWestGravity;
  486. X  if (window->id == (Window) NULL)
  487. X    {
  488. X      window->id=(Window) NULL;
  489. X      window->ximage=(XImage *) NULL;
  490. X      window->pixmap=(Pixmap) NULL;
  491. X    }
  492. }
  493. X
  494. /*
  495. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  496. %                                                                             %
  497. %                                                                             %
  498. %                                                                             %
  499. %   X H i g h l i g h t R e g i o n                                           %
  500. %                                                                             %
  501. %                                                                             %
  502. %                                                                             %
  503. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  504. %
  505. %  Function XHighlightRegion flashes a border on the X server around a region
  506. %  defined by highlight_info.
  507. %
  508. %  The format of the XHighlightRegion routine is:
  509. %
  510. %    XHighlightRegion(display,window,graphic_context,highlight_info)
  511. %
  512. %  A description of each parameter follows:
  513. %
  514. %    o display: Specifies a connection to an X server; returned from
  515. %      XOpenDisplay.
  516. %
  517. %    o window: Specifies a pointer to a Window structure.
  518. %
  519. %    o graphic_context: Specifies a pointer to a GC structure.
  520. %
  521. %    o clip_info: Specifies a pointer to a RectangleInfo structure.  It
  522. %      contains the extents of any highlighting rectangle.
  523. %
  524. %
  525. */
  526. static void XHighlightRegion(display,window,graphic_context,highlight_info)
  527. Display
  528. X  *display;
  529. X
  530. Window
  531. X  window;
  532. X
  533. GC
  534. X  graphic_context;
  535. X
  536. RectangleInfo
  537. X  *highlight_info;
  538. {
  539. X  static unsigned long
  540. X    plane_masks[8]=
  541. X    {
  542. X      0x01010101,
  543. X      0x02020203,
  544. X      0x04040405,
  545. X      0x08080809,
  546. X      0x10101011,
  547. X      0x20202021,
  548. X      0x40404041,
  549. X      0x80808081
  550. X    };
  551. X
  552. X  register int
  553. X    i;
  554. X
  555. X  XGCValues
  556. X    graphic_context_value;
  557. X
  558. X  /*
  559. X    Flash a border around the region defined by highlight_info.
  560. X  */
  561. X  (void) XGetGCValues(display,graphic_context,GCPlaneMask,
  562. X    &graphic_context_value);
  563. X  for (i=0; i < 16; i++)
  564. X  {
  565. X    XSetPlaneMask(display,graphic_context,plane_masks[i & 0x07]);
  566. X    XDrawRectangle(display,window,graphic_context,highlight_info->x,
  567. X      highlight_info->y,highlight_info->width-1,highlight_info->height-1);
  568. X    XFlush(display);
  569. X    XDrawRectangle(display,window,graphic_context,highlight_info->x,
  570. X      highlight_info->y,highlight_info->width-1,highlight_info->height-1);
  571. X    XFlush(display);
  572. X  }
  573. X  /*
  574. X    Restore oginginal plane mask setting.
  575. X  */
  576. X  XSetPlaneMask(display,graphic_context,graphic_context_value.plane_mask);
  577. }
  578. X
  579. /*
  580. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  581. %                                                                             %
  582. %                                                                             %
  583. %                                                                             %
  584. %   X M a k e I m a g e                                                       %
  585. %                                                                             %
  586. %                                                                             %
  587. %                                                                             %
  588. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  589. %
  590. %  Function XMakeImage creates an X11 image.  If the image size differs from
  591. %  the X11 image size, the image is first resized.
  592. %
  593. %  The format of the XMakeImage routine is:
  594. %
  595. %      status=XMakeImage(display,resource_info,window,image,width,height)
  596. %
  597. %  A description of each parameter follows:
  598. %
  599. %    o status: Function XMakeImage returns True if the X image is
  600. %      successfully created.  False is returned is there is a memory shortage.
  601. %
  602. %    o display: Specifies a connection to an X server; returned from
  603. %      XOpenDisplay.
  604. %
  605. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  606. %
  607. %    o window: Specifies a pointer to a XWindowInfo structure.
  608. %
  609. %    o image: Specifies a pointer to a Image structure;  returned from
  610. %      ReadImage.
  611. %
  612. %    o width: Specifies the width in pixels of the rectangular area to
  613. %      display.
  614. %
  615. %    o height: Specifies the height in pixels of the rectangular area to
  616. %      display.
  617. %
  618. %
  619. */
  620. unsigned int XMakeImage(display,resource_info,window,image,width,height)
  621. Display
  622. X  *display;
  623. X
  624. XXResourceInfo
  625. X  *resource_info;
  626. X
  627. XXWindowInfo
  628. X  *window;
  629. X
  630. Image
  631. X  *image;
  632. X
  633. unsigned int
  634. X  width,
  635. X  height;
  636. {
  637. X  Image
  638. X    *transformed_image;
  639. X
  640. X  int
  641. X    format;
  642. X
  643. X  XImage
  644. X    *ximage;
  645. X
  646. X  if ((window->width == 0) || (window->height == 0))
  647. X    return(False);
  648. X  /*
  649. X    Display busy cursor.
  650. X  */
  651. X  XDefineCursor(display,window->id,window->busy_cursor);
  652. X  XFlush(display);
  653. X  transformed_image=image;
  654. X  if (image != (Image *) NULL)
  655. X    {
  656. X      /*
  657. X        Apply user transforms to the image.
  658. X      */
  659. X      if (window->clip_geometry)
  660. X        {
  661. X          Image
  662. X            *clipped_image;
  663. X
  664. X          RectangleInfo
  665. X            clip_info;
  666. X
  667. X          /*
  668. X            Clip image.
  669. X          */
  670. X          (void) XParseGeometry(window->clip_geometry,&clip_info.x,&clip_info.y,
  671. X            &clip_info.width,&clip_info.height);
  672. X          transformed_image->orphan=True;
  673. X          clipped_image=ClipImage(transformed_image,&clip_info);
  674. X          transformed_image->orphan=False;
  675. X          if (transformed_image != image)
  676. X            DestroyImage(transformed_image);
  677. X          if (clipped_image == (Image *) NULL)
  678. X            return(False);
  679. X          transformed_image=clipped_image;
  680. X        }
  681. X      if ((width != transformed_image->columns) ||
  682. X          (height != transformed_image->rows))
  683. X        {
  684. X          Image
  685. X            *scaled_image;
  686. X
  687. X          /*
  688. X            Scale image.
  689. X          */
  690. X          transformed_image->orphan=True;
  691. X          scaled_image=ScaleImage(transformed_image,width,height);
  692. X          transformed_image->orphan=False;
  693. X          if (transformed_image != image)
  694. X            DestroyImage(transformed_image);
  695. X          if (scaled_image == (Image *) NULL)
  696. X            return(False);
  697. X          transformed_image=scaled_image;
  698. X        }
  699. X      width=transformed_image->columns;
  700. X      height=transformed_image->rows;
  701. X    }
  702. X  /*
  703. X    Create X image.
  704. X  */
  705. X  format=(window->depth == 1) ? XYBitmap : ZPixmap;
  706. X  ximage=XCreateImage(display,window->visual_info->visual,window->depth,format,
  707. X    0,(char *) NULL,width,height,XBitmapPad(display),0);
  708. X  if (ximage == (XImage *) NULL)
  709. X    {
  710. X      /*
  711. X        Unable to create X image.
  712. X      */
  713. X      XDefineCursor(display,window->id,window->cursor);
  714. X      return(False);
  715. X    }
  716. X  if (resource_info->debug)
  717. X    {
  718. X      (void) fprintf(stderr,"XImage:\n");
  719. X      (void) fprintf(stderr,"  width, height: %dx%d\n",ximage->width,
  720. X        ximage->height);
  721. X      (void) fprintf(stderr,"  format: %d\n",ximage->format);
  722. X      (void) fprintf(stderr,"  byte order: %d\n",ximage->byte_order);
  723. X      (void) fprintf(stderr,"  bitmap unit, bit order, pad: %d %d %d\n",
  724. X        ximage->bitmap_unit,ximage->bitmap_bit_order,ximage->bitmap_pad);
  725. X      (void) fprintf(stderr,"  depth: %d\n",ximage->depth);
  726. X      (void) fprintf(stderr,"  bytes per line: %d\n",ximage->bytes_per_line);
  727. X      (void) fprintf(stderr,"  bits per pixel: %d\n",ximage->bits_per_pixel);
  728. X      (void) fprintf(stderr,"  red, green, blue masks: 0x%lx 0x%lx 0x%lx\n",
  729. X        ximage->red_mask,ximage->green_mask,ximage->blue_mask);
  730. X    }
  731. X  /*
  732. X    Allocate X image pixel data.
  733. X  */
  734. X  if (ximage->format == XYBitmap)
  735. X    ximage->data=(char *)
  736. X      malloc(ximage->bytes_per_line*ximage->height*ximage->depth);
  737. X  else
  738. X    ximage->data=(char *) malloc(ximage->bytes_per_line*ximage->height);
  739. X  if (ximage->data == (char *) NULL)
  740. X    {
  741. X      /*
  742. X        Unable to allocate pixel data.
  743. X      */
  744. X      XDestroyImage(ximage);
  745. X      XDefineCursor(display,window->id,window->cursor);
  746. X      return(False);
  747. X    }
  748. X  if (window->ximage != (XImage *) NULL)
  749. X    XDestroyImage(window->ximage);
  750. X  window->ximage=ximage;
  751. X  if (image == (Image *) NULL)
  752. X    {
  753. X      XDefineCursor(display,window->id,window->cursor);
  754. X      return(True);
  755. X    }
  756. X  /*
  757. X    Convert runlength-encoded pixels to X image data.
  758. X  */
  759. X  if ((ximage->byte_order == LSBFirst) ||
  760. X      ((ximage->format == XYBitmap) && (ximage->bitmap_bit_order == LSBFirst)))
  761. X    XMakeImageLSBFirst(window,transformed_image,ximage);
  762. X  else
  763. X    XMakeImageMSBFirst(window,transformed_image,ximage);
  764. X  if (transformed_image != image)
  765. X    DestroyImage(transformed_image);
  766. X  /*
  767. X    Restore cursor.
  768. X  */
  769. X  XDefineCursor(display,window->id,window->cursor);
  770. X  return(True);
  771. }
  772. X
  773. /*
  774. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  775. %                                                                             %
  776. %                                                                             %
  777. %                                                                             %
  778. %   X M a k e I m a g e L S B F i r s t                                       %
  779. %                                                                             %
  780. %                                                                             %
  781. %                                                                             %
  782. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  783. %
  784. %  Function XMakeImageLSBFirst initializes the pixel data of an X11 Image.
  785. %  The X image pixels are copied in least-significant bit and byte first
  786. %  order.  The server's scanline pad is respected.  Rather than using one or
  787. %  two general cases, many special cases are found here to help speed up the
  788. %  image conversion.
  789. %
  790. %  The format of the XMakeImageLSBFirst routine is:
  791. %
  792. %      XMakeImageLSBFirst(window,image,ximage)
  793. %
  794. %  A description of each parameter follows:
  795. %
  796. %    o window: Specifies a pointer to a XWindowInfo structure.
  797. %
  798. %    o image: Specifies a pointer to a Image structure;  returned from
  799. %      ReadImage.
  800. %
  801. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  802. %      XCreateImage.
  803. %
  804. %
  805. */
  806. static void XMakeImageLSBFirst(window,image,ximage)
  807. XXWindowInfo
  808. X  *window;
  809. X
  810. Image
  811. X  *image;
  812. X
  813. XXImage
  814. X  *ximage;
  815. {
  816. X  register int
  817. X    i,
  818. X    j,
  819. X    x;
  820. X
  821. X  register RunlengthPacket
  822. X    *p;
  823. X
  824. X  register unsigned char
  825. X    *q;
  826. X
  827. X  register unsigned long
  828. X    pixel;
  829. X
  830. X  unsigned int
  831. X    scanline_pad;
  832. X
  833. X  unsigned long
  834. X    *pixels;
  835. X
  836. X  pixels=window->pixel_info->pixels;
  837. X  p=image->pixels;
  838. X  q=(unsigned char *) ximage->data;
  839. X  x=0;
  840. X  if (ximage->format == XYBitmap)
  841. X    {
  842. X      register unsigned char
  843. X        background,
  844. X        bit,
  845. X        byte,
  846. X        foreground;
  847. X
  848. X      register unsigned short
  849. X        polarity;
  850. X
  851. X      /*
  852. X        Convert image to big-endian bitmap.
  853. X      */
  854. X      background=(Intensity(window->pixel_info->foreground_color) >
  855. X        Intensity(window->pixel_info->background_color) ? 1 : 0) << 7;
  856. X      foreground=(Intensity(window->pixel_info->background_color) >
  857. X        Intensity(window->pixel_info->foreground_color) ? 1 : 0) << 7;
  858. X      polarity=Intensity(image->colormap[0]) > Intensity(image->colormap[1]);
  859. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  860. X      bit=0;
  861. X      byte=0;
  862. X      for (i=0; i < image->packets; i++)
  863. X      {
  864. X        for (j=0; j <= ((int) p->length); j++)
  865. X        {
  866. X          byte>>=1;
  867. X          if (p->index == polarity)
  868. X            byte|=foreground;
  869. X          else
  870. X            byte|=background;
  871. X          bit++;
  872. X          if (bit == 8)
  873. X            {
  874. X              *q++=byte;
  875. X              bit=0;
  876. X              byte=0;
  877. X            }
  878. X          x++;
  879. X          if (x == ximage->width)
  880. X            {
  881. X              /*
  882. X                Advance to the next scanline.
  883. X              */
  884. X              if (bit != 0)
  885. X                *q=byte >> (8-bit);
  886. X              q+=scanline_pad;
  887. X              bit=0;
  888. X              byte=0;
  889. X              x=0;
  890. X            }
  891. X        }
  892. X        p++;
  893. X      }
  894. X    }
  895. X  else
  896. X    {
  897. X      XStandardColormap
  898. X        *map_info;
  899. X
  900. X      /*
  901. X        Convert image to little-endian color-mapped X image.
  902. X      */
  903. X      map_info=window->map_info;
  904. X      scanline_pad=ximage->bytes_per_line-
  905. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  906. X      if (window->pixel_info->colors != 0)
  907. X        switch (ximage->bits_per_pixel)
  908. X        {
  909. X          case 2:
  910. X          {
  911. X            register unsigned int
  912. X              nibble;
  913. X
  914. X            /*
  915. X              Convert to 2 bit color-mapped X image.
  916. X            */
  917. X            nibble=0;
  918. X            for (i=0; i < image->packets; i++)
  919. X            {
  920. X              pixel=pixels[p->index] & 0xf;
  921. X              for (j=0; j <= ((int) p->length); j++)
  922. X              {
  923. X                switch (nibble)
  924. X                {
  925. X                  case 0:
  926. X                  {
  927. X                    *q=(unsigned char) pixel;
  928. X                    nibble++;
  929. X                    break;
  930. X                  }
  931. X                  case 1:
  932. X                  {
  933. X                    *q|=(unsigned char) (pixel << 2);
  934. X                    nibble++;
  935. X                    break;
  936. X                  }
  937. X                  case 2:
  938. X                  {
  939. X                    *q|=(unsigned char) (pixel << 4);
  940. X                    nibble++;
  941. X                    break;
  942. X                  }
  943. X                  case 3:
  944. X                  {
  945. X                    *q|=(unsigned char) (pixel << 6);
  946. X                    q++;
  947. X                    nibble=0;
  948. X                    break;
  949. X                  }
  950. X                }
  951. X                x++;
  952. X                if (x == ximage->width)
  953. X                  {
  954. X                    x=0;
  955. X                    nibble=0;
  956. X                    q+=scanline_pad;
  957. X                  }
  958. X              }
  959. X              p++;
  960. X            }
  961. X            break;
  962. X          }
  963. X          case 4:
  964. X          {
  965. X            register unsigned int
  966. X              nibble;
  967. X
  968. X            /*
  969. X              Convert to 4 bit color-mapped X image.
  970. X            */
  971. X            nibble=0;
  972. X            for (i=0; i < image->packets; i++)
  973. X            {
  974. X              pixel=pixels[p->index] & 0xf;
  975. X              for (j=0; j <= ((int) p->length); j++)
  976. X              {
  977. X                switch (nibble)
  978. X                {
  979. X                  case 0:
  980. X                  {
  981. X                    *q=(unsigned char) pixel;
  982. X                    nibble++;
  983. X                    break;
  984. X                  }
  985. X                  case 1:
  986. X                  {
  987. X                    *q|=(unsigned char) (pixel << 4);
  988. X                    q++;
  989. X                    nibble=0;
  990. X                    break;
  991. X                  }
  992. X                }
  993. X                x++;
  994. X                if (x == ximage->width)
  995. X                  {
  996. X                    x=0;
  997. X                    nibble=0;
  998. X                    q+=scanline_pad;
  999. X                  }
  1000. X              }
  1001. X              p++;
  1002. X            }
  1003. X            break;
  1004. X          }
  1005. X          case 6:
  1006. X          case 8:
  1007. X          {
  1008. X            /*
  1009. X              Convert to 8 bit color-mapped X image.
  1010. X            */
  1011. X            for (i=0; i < image->packets; i++)
  1012. X            {
  1013. X              pixel=pixels[p->index];
  1014. X              for (j=0; j <= ((int) p->length); j++)
  1015. X              {
  1016. X                *q++=(unsigned char) pixel;
  1017. X                x++;
  1018. X                if (x == ximage->width)
  1019. X                  {
  1020. X                    x=0;
  1021. X                    q+=scanline_pad;
  1022. X                  }
  1023. X              }
  1024. X              p++;
  1025. X            }
  1026. X            break;
  1027. X          }
  1028. X          default:
  1029. X          {
  1030. X            register int
  1031. X              k;
  1032. X
  1033. X            register unsigned int
  1034. X              bytes_per_pixel;
  1035. X
  1036. X            unsigned char
  1037. X              channel[sizeof(unsigned long)];
  1038. X
  1039. X            /*
  1040. X              Convert to multi-byte color-mapped X image.
  1041. X            */
  1042. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1043. X            for (i=0; i < image->packets; i++)
  1044. X            {
  1045. X              pixel=pixels[p->index];
  1046. X              for (k=0; k < bytes_per_pixel; k++)
  1047. X              {
  1048. X                channel[k]=(unsigned char) pixel;
  1049. X                pixel>>=8;
  1050. X              }
  1051. X              for (j=0; j <= ((int) p->length); j++)
  1052. X              {
  1053. X                for (k=0; k < bytes_per_pixel; k++)
  1054. X                  *q++=channel[k];
  1055. X                x++;
  1056. X                if (x == ximage->width)
  1057. X                  {
  1058. X                    x=0;
  1059. X                    q+=scanline_pad;
  1060. X                  }
  1061. X              }
  1062. X              p++;
  1063. X            }
  1064. X            break;
  1065. X          }
  1066. X        }
  1067. X      else
  1068. X        {
  1069. X          /*
  1070. X            Convert image to little-endian continuous-tone X image.
  1071. X          */
  1072. X          switch (ximage->bits_per_pixel)
  1073. X          {
  1074. X            case 2:
  1075. X            {
  1076. X              register unsigned int
  1077. X                nibble;
  1078. X
  1079. X              /*
  1080. X                Convert to contiguous 2 bit continuous-tone X image.
  1081. X              */
  1082. X              nibble=0;
  1083. X              for (i=0; i < image->packets; i++)
  1084. X              {
  1085. X                pixel=XStandardPixel(map_info,(*p),8);
  1086. X                pixel&=0xf;
  1087. X                for (j=0; j <= ((int) p->length); j++)
  1088. X                {
  1089. X                  switch (nibble)
  1090. X                  {
  1091. X                    case 0:
  1092. X                    {
  1093. X                      *q=(unsigned char) pixel;
  1094. X                      nibble++;
  1095. X                      break;
  1096. X                    }
  1097. X                    case 1:
  1098. X                    {
  1099. X                      *q|=(unsigned char) (pixel << 2);
  1100. X                      nibble++;
  1101. X                      break;
  1102. X                    }
  1103. X                    case 2:
  1104. X                    {
  1105. X                      *q|=(unsigned char) (pixel << 4);
  1106. X                      nibble++;
  1107. X                      break;
  1108. X                    }
  1109. X                    case 3:
  1110. X                    {
  1111. X                      *q|=(unsigned char) (pixel << 6);
  1112. X                      q++;
  1113. X                      nibble=0;
  1114. X                      break;
  1115. X                    }
  1116. X                  }
  1117. X                  x++;
  1118. X                  if (x == ximage->width)
  1119. X                    {
  1120. X                      x=0;
  1121. X                      nibble=0;
  1122. X                      q+=scanline_pad;
  1123. X                    }
  1124. X                }
  1125. X                p++;
  1126. X              }
  1127. X              break;
  1128. X            }
  1129. X            case 4:
  1130. X            {
  1131. X              register unsigned int
  1132. X                nibble;
  1133. X
  1134. X              /*
  1135. X                Convert to contiguous 4 bit continuous-tone X image.
  1136. X              */
  1137. X              nibble=0;
  1138. X              for (i=0; i < image->packets; i++)
  1139. X              {
  1140. X                pixel=XStandardPixel(map_info,(*p),8);
  1141. X                pixel&=0xf;
  1142. X                for (j=0; j <= ((int) p->length); j++)
  1143. X                {
  1144. X                  switch (nibble)
  1145. X                  {
  1146. X                    case 0:
  1147. X                    {
  1148. X                      *q=(unsigned char) pixel;
  1149. X                      nibble++;
  1150. X                      break;
  1151. X                    }
  1152. X                    case 1:
  1153. X                    {
  1154. X                      *q|=(unsigned char) (pixel << 4);
  1155. X                      q++;
  1156. X                      nibble=0;
  1157. X                      break;
  1158. X                    }
  1159. X                  }
  1160. X                  x++;
  1161. X                  if (x == ximage->width)
  1162. X                    {
  1163. X                      x=0;
  1164. X                      nibble=0;
  1165. X                      q+=scanline_pad;
  1166. X                    }
  1167. X                }
  1168. X                p++;
  1169. X              }
  1170. X              break;
  1171. X            }
  1172. X            case 6:
  1173. X            case 8:
  1174. X            {
  1175. X              /*
  1176. X                Convert to contiguous 8 bit continuous-tone X image.
  1177. X              */
  1178. X              for (i=0; i < image->packets; i++)
  1179. X              {
  1180. X                pixel=XStandardPixel(map_info,(*p),8);
  1181. X                for (j=0; j <= ((int) p->length); j++)
  1182. X                {
  1183. X                  *q++=(unsigned char) pixel;
  1184. X                  x++;
  1185. X                  if (x == ximage->width)
  1186. X                    {
  1187. X                      x=0;
  1188. X                      q+=scanline_pad;
  1189. X                    }
  1190. X                }
  1191. X                p++;
  1192. X              }
  1193. X              break;
  1194. X            }
  1195. X            default:
  1196. X            {
  1197. X              if ((ximage->bits_per_pixel == 32) &&
  1198. X                  (map_info->red_max == 255) &&
  1199. X                  (map_info->green_max == 255) &&
  1200. X                  (map_info->blue_max == 255) &&
  1201. X                  (map_info->red_mult == 65536) &&
  1202. X                  (map_info->green_mult == 256) &&
  1203. X                  (map_info->blue_mult == 1))
  1204. X                {
  1205. X                  /*
  1206. X                    Convert to 32 bit continuous-tone X image.
  1207. X                  */
  1208. X                  for (i=0; i < image->packets; i++)
  1209. X                  {
  1210. X                    for (j=0; j <= ((int) p->length); j++)
  1211. X                    {
  1212. X                      *q++=p->blue;
  1213. X                      *q++=p->green;
  1214. X                      *q++=p->red;
  1215. X                      *q++=0;
  1216. X                    }
  1217. X                    p++;
  1218. X                  }
  1219. X                }
  1220. X              else
  1221. X                {
  1222. X                  register int
  1223. X                    k;
  1224. X
  1225. X                  register unsigned int
  1226. X                    bytes_per_pixel;
  1227. X
  1228. X                  unsigned char
  1229. X                    channel[sizeof(unsigned long)];
  1230. X
  1231. X                  /*
  1232. X                    Convert to multi-byte continuous-tone X image.
  1233. X                  */
  1234. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1235. X                  for (i=0; i < image->packets; i++)
  1236. X                  {
  1237. X                    pixel=XStandardPixel(map_info,(*p),8);
  1238. X                    for (k=0; k < bytes_per_pixel; k++)
  1239. X                    {
  1240. X                      channel[k]=(unsigned char) pixel;
  1241. X                      pixel>>=8;
  1242. X                    }
  1243. X                    for (j=0; j <= ((int) p->length); j++)
  1244. X                    {
  1245. X                      for (k=0; k < bytes_per_pixel; k++)
  1246. X                        *q++=channel[k];
  1247. X                      x++;
  1248. X                      if (x == ximage->width)
  1249. X                        {
  1250. X                          x=0;
  1251. X                          q+=scanline_pad;
  1252. X                        }
  1253. X                    }
  1254. X                    p++;
  1255. X                  }
  1256. X                }
  1257. X              break;
  1258. X            }
  1259. X          }
  1260. X        }
  1261. X    }
  1262. }
  1263. X
  1264. /*
  1265. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1266. %                                                                             %
  1267. %                                                                             %
  1268. %                                                                             %
  1269. %   X M a k e I m a g e M S B F i r s t                                       %
  1270. %                                                                             %
  1271. %                                                                             %
  1272. %                                                                             %
  1273. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1274. %
  1275. %  Function XMakeImageMSBFirst initializes the pixel data of an X11 Image.
  1276. %  The X image pixels are copied in most-significant bit and byte first order.
  1277. %  The server's scanline pad is also resprected. Rather than using one or two
  1278. %  general cases, many special cases are found here to help speed up the image
  1279. %  conversion.
  1280. %
  1281. %  The format of the XMakeImageMSBFirst routine is:
  1282. %
  1283. %      XMakeImageMSBFirst(window,image,ximage)
  1284. %
  1285. %  A description of each parameter follows:
  1286. %
  1287. %    o window: Specifies a pointer to a XWindowInfo structure.
  1288. %
  1289. %    o image: Specifies a pointer to a Image structure;  returned from
  1290. %      ReadImage.
  1291. %
  1292. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  1293. %      XCreateImage.
  1294. %
  1295. %
  1296. */
  1297. static void XMakeImageMSBFirst(window,image,ximage)
  1298. XXWindowInfo
  1299. X  *window;
  1300. X
  1301. Image
  1302. X  *image;
  1303. X
  1304. XXImage
  1305. X  *ximage;
  1306. {
  1307. X  register int
  1308. X    i,
  1309. X    j,
  1310. X    x;
  1311. X
  1312. X  register RunlengthPacket
  1313. X    *p;
  1314. X
  1315. X  register unsigned char
  1316. X    *q;
  1317. X
  1318. X  register unsigned long
  1319. X    pixel;
  1320. X
  1321. X  unsigned int
  1322. X    scanline_pad;
  1323. X
  1324. X  unsigned long
  1325. X    *pixels;
  1326. X
  1327. X  pixels=window->pixel_info->pixels;
  1328. X  p=image->pixels;
  1329. X  q=(unsigned char *) ximage->data;
  1330. X  x=0;
  1331. X  if (ximage->format == XYBitmap)
  1332. X    {
  1333. X      register unsigned char
  1334. X        background,
  1335. X        bit,
  1336. X        byte,
  1337. X        foreground;
  1338. X
  1339. X      register unsigned short
  1340. X        polarity;
  1341. X
  1342. X      /*
  1343. X        Convert image to big-endian bitmap.
  1344. X      */
  1345. X      background=(Intensity(window->pixel_info->foreground_color) >
  1346. X        Intensity(window->pixel_info->background_color));
  1347. X      foreground=(Intensity(window->pixel_info->background_color) >
  1348. X        Intensity(window->pixel_info->foreground_color));
  1349. X      polarity=Intensity(image->colormap[0]) > Intensity(image->colormap[1]);
  1350. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  1351. X      bit=0;
  1352. X      byte=0;
  1353. X      for (i=0; i < image->packets; i++)
  1354. X      {
  1355. X        for (j=0; j <= ((int) p->length); j++)
  1356. X        {
  1357. X          byte<<=1;
  1358. X          if (p->index == polarity)
  1359. X            byte|=foreground;
  1360. X          else
  1361. X            byte|=background;
  1362. X          bit++;
  1363. X          if (bit == 8)
  1364. X            {
  1365. X              *q++=byte;
  1366. X              bit=0;
  1367. X              byte=0;
  1368. X            }
  1369. X          x++;
  1370. X          if (x == ximage->width)
  1371. X            {
  1372. X              /*
  1373. X                Advance to the next scanline.
  1374. X              */
  1375. X              if (bit != 0)
  1376. X                *q=byte << (8-bit);
  1377. X              q+=scanline_pad;
  1378. X              bit=0;
  1379. X              byte=0;
  1380. X              x=0;
  1381. X            }
  1382. X        }
  1383. X        p++;
  1384. X      }
  1385. X    }
  1386. X  else
  1387. X    {
  1388. X      XStandardColormap
  1389. X        *map_info;
  1390. X
  1391. X      /*
  1392. X        Convert image to big-endian X image.
  1393. X      */
  1394. X      map_info=window->map_info;
  1395. X      scanline_pad=ximage->bytes_per_line-
  1396. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  1397. X      if (window->pixel_info->colors != 0)
  1398. X        switch (ximage->bits_per_pixel)
  1399. X        {
  1400. X          case 2:
  1401. X          {
  1402. X            register unsigned int
  1403. X              nibble;
  1404. X
  1405. X            /*
  1406. X              Convert to 2 bit color-mapped X image.
  1407. X            */
  1408. X            nibble=0;
  1409. X            for (i=0; i < image->packets; i++)
  1410. X            {
  1411. X              pixel=pixels[p->index] & 0xf;
  1412. X              for (j=0; j <= ((int) p->length); j++)
  1413. X              {
  1414. X                switch (nibble)
  1415. X                {
  1416. X                  case 0:
  1417. X                  {
  1418. X                    *q=(unsigned char) (pixel << 6);
  1419. X                    nibble++;
  1420. X                    break;
  1421. X                  }
  1422. X                  case 1:
  1423. X                  {
  1424. X                    *q|=(unsigned char) (pixel << 4);
  1425. X                    nibble++;
  1426. X                    break;
  1427. X                  }
  1428. X                  case 2:
  1429. X                  {
  1430. X                    *q|=(unsigned char) (pixel << 2);
  1431. X                    nibble++;
  1432. X                    break;
  1433. X                  }
  1434. X                  case 3:
  1435. X                  {
  1436. X                    *q|=(unsigned char) pixel;
  1437. X                    q++;
  1438. X                    nibble=0;
  1439. X                    break;
  1440. X                  }
  1441. X                }
  1442. X                x++;
  1443. X                if (x == ximage->width)
  1444. X                  {
  1445. X                    x=0;
  1446. X                    nibble=0;
  1447. X                    q+=scanline_pad;
  1448. X                  }
  1449. X              }
  1450. X              p++;
  1451. X            }
  1452. X            break;
  1453. X          }
  1454. X          case 4:
  1455. X          {
  1456. X            register unsigned int
  1457. X              nibble;
  1458. X
  1459. X            /*
  1460. X              Convert to 4 bit color-mapped X image.
  1461. X            */
  1462. X            nibble=0;
  1463. X            for (i=0; i < image->packets; i++)
  1464. X            {
  1465. X              pixel=pixels[p->index] & 0xf;
  1466. X              for (j=0; j <= ((int) p->length); j++)
  1467. X              {
  1468. X                switch (nibble)
  1469. X                {
  1470. X                  case 0:
  1471. X                  {
  1472. X                    *q=(unsigned char) (pixel << 4);
  1473. X                    nibble++;
  1474. X                    break;
  1475. X                  }
  1476. X                  case 1:
  1477. X                  {
  1478. X                    *q|=(unsigned char) pixel;
  1479. X                    q++;
  1480. X                    nibble=0;
  1481. X                    break;
  1482. X                  }
  1483. X                }
  1484. X                x++;
  1485. X                if (x == ximage->width)
  1486. X                  {
  1487. X                    x=0;
  1488. X                    nibble=0;
  1489. X                    q+=scanline_pad;
  1490. X                  }
  1491. X              }
  1492. X              p++;
  1493. X            }
  1494. X            break;
  1495. X          }
  1496. X          case 8:
  1497. X          {
  1498. X            /*
  1499. X              Convert to 8 bit color-mapped X image.
  1500. X            */
  1501. X            for (i=0; i < image->packets; i++)
  1502. X            {
  1503. X              pixel=pixels[p->index];
  1504. X              for (j=0; j <= ((int) p->length); j++)
  1505. X              {
  1506. X                *q++=(unsigned char) pixel;
  1507. X                x++;
  1508. X                if (x == ximage->width)
  1509. X                  {
  1510. X                    x=0;
  1511. X                    q+=scanline_pad;
  1512. X                  }
  1513. X              }
  1514. X              p++;
  1515. X            }
  1516. X            break;
  1517. X          }
  1518. X          default:
  1519. X          {
  1520. X            register int
  1521. X              k;
  1522. X
  1523. X            register unsigned int
  1524. X              bytes_per_pixel;
  1525. X
  1526. X            unsigned char
  1527. X              channel[sizeof(unsigned long)];
  1528. X
  1529. X            /*
  1530. X              Convert to 8 bit color-mapped X image.
  1531. X            */
  1532. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1533. X            for (i=0; i < image->packets; i++)
  1534. X            {
  1535. X              pixel=pixels[p->index];
  1536. X              for (k=bytes_per_pixel-1; k >= 0; k--)
  1537. X              {
  1538. X                channel[k]=(unsigned char) pixel;
  1539. X                pixel>>=8;
  1540. X              }
  1541. X              for (j=0; j <= ((int) p->length); j++)
  1542. X              {
  1543. X                for (k=0; k < bytes_per_pixel; k++)
  1544. X                  *q++=channel[k];
  1545. X                x++;
  1546. X                if (x == ximage->width)
  1547. X                  {
  1548. X                    x=0;
  1549. X                    q+=scanline_pad;
  1550. X                  }
  1551. X              }
  1552. X              p++;
  1553. X            }
  1554. X            break;
  1555. X          }
  1556. X        }
  1557. X      else
  1558. X        {
  1559. X          /*
  1560. X            Convert to big-endian continuous-tone X image.
  1561. X          */
  1562. X          switch (ximage->bits_per_pixel)
  1563. X          {
  1564. X            case 2:
  1565. X            {
  1566. X              register unsigned int
  1567. X                nibble;
  1568. X
  1569. X              /*
  1570. X                Convert to 4 bit continuous-tone X image.
  1571. X              */
  1572. X              nibble=0;
  1573. X              for (i=0; i < image->packets; i++)
  1574. X              {
  1575. X                pixel=XStandardPixel(map_info,(*p),8);
  1576. X                pixel&=0xf;
  1577. X                for (j=0; j <= ((int) p->length); j++)
  1578. X                {
  1579. X                  switch (nibble)
  1580. X                  {
  1581. X                    case 0:
  1582. X                    {
  1583. X                      *q=(unsigned char) (pixel << 6);
  1584. X                      nibble++;
  1585. X                      break;
  1586. X                    }
  1587. X                    case 1:
  1588. X                    {
  1589. X                      *q|=(unsigned char) (pixel << 4);
  1590. X                      nibble++;
  1591. X                      break;
  1592. X                    }
  1593. X                    case 2:
  1594. X                    {
  1595. X                      *q|=(unsigned char) (pixel << 2);
  1596. X                      nibble++;
  1597. X                      break;
  1598. X                    }
  1599. X                    case 3:
  1600. X                    {
  1601. X                      *q|=(unsigned char) pixel;
  1602. X                      q++;
  1603. X                      nibble=0;
  1604. X                      break;
  1605. X                    }
  1606. X                  }
  1607. X                  x++;
  1608. X                  if (x == ximage->width)
  1609. X                    {
  1610. X                      x=0;
  1611. X                      nibble=0;
  1612. X                      q+=scanline_pad;
  1613. X                    }
  1614. X                }
  1615. X                p++;
  1616. X              }
  1617. X              break;
  1618. X            }
  1619. X            case 4:
  1620. X            {
  1621. X              register unsigned int
  1622. X                nibble;
  1623. X
  1624. X              /*
  1625. X                Convert to 4 bit continuous-tone X image.
  1626. X              */
  1627. X              nibble=0;
  1628. X              for (i=0; i < image->packets; i++)
  1629. X              {
  1630. X                pixel=XStandardPixel(map_info,(*p),8);
  1631. X                pixel&=0xf;
  1632. X                for (j=0; j <= ((int) p->length); j++)
  1633. X                {
  1634. X                  switch (nibble)
  1635. X                  {
  1636. X                    case 0:
  1637. X                    {
  1638. X                      *q=(unsigned char) (pixel << 4);
  1639. X                      nibble++;
  1640. X                      break;
  1641. X                    }
  1642. X                    case 1:
  1643. X                    {
  1644. X                      *q|=(unsigned char) pixel;
  1645. X                      q++;
  1646. X                      nibble=0;
  1647. X                      break;
  1648. X                    }
  1649. X                  }
  1650. X                  x++;
  1651. X                  if (x == ximage->width)
  1652. X                    {
  1653. X                      x=0;
  1654. X                      nibble=0;
  1655. X                      q+=scanline_pad;
  1656. X                    }
  1657. X                }
  1658. X                p++;
  1659. X              }
  1660. X              break;
  1661. X            }
  1662. X            case 8:
  1663. X            {
  1664. X              /*
  1665. X                Convert to 8 bit continuous-tone X image.
  1666. X              */
  1667. X              for (i=0; i < image->packets; i++)
  1668. X              {
  1669. X                pixel=XStandardPixel(map_info,(*p),8);
  1670. X                for (j=0; j <= ((int) p->length); j++)
  1671. X                {
  1672. X                  *q++=(unsigned char) pixel;
  1673. X                  x++;
  1674. X                  if (x == ximage->width)
  1675. X                    {
  1676. X                      x=0;
  1677. X                      q+=scanline_pad;
  1678. X                    }
  1679. X                }
  1680. X                p++;
  1681. X              }
  1682. X              break;
  1683. X            }
  1684. X            default:
  1685. X            {
  1686. X              if ((ximage->bits_per_pixel == 32) &&
  1687. X                  (map_info->red_max == 255) &&
  1688. X                  (map_info->green_max == 255) &&
  1689. X                  (map_info->blue_max == 255) &&
  1690. X                  (map_info->red_mult == 65536) &&
  1691. X                  (map_info->green_mult == 256) &&
  1692. X                  (map_info->blue_mult == 1))
  1693. X                {
  1694. X                  /*
  1695. X                    Convert to 32 bit continuous-tone X image.
  1696. X                  */
  1697. X                  for (i=0; i < image->packets; i++)
  1698. X                  {
  1699. X                    for (j=0; j <= ((int) p->length); j++)
  1700. X                    {
  1701. X                      *q++=0;
  1702. X                      *q++=p->red;
  1703. X                      *q++=p->green;
  1704. X                      *q++=p->blue;
  1705. X                    }
  1706. X                    p++;
  1707. X                  }
  1708. X                }
  1709. X              else
  1710. X                {
  1711. X                  register int
  1712. X                    k;
  1713. X
  1714. X                  register unsigned int
  1715. X                    bytes_per_pixel;
  1716. X
  1717. X                  unsigned char
  1718. X                    channel[sizeof(unsigned long)];
  1719. X
  1720. X                  /*
  1721. X                    Convert to multi-byte continuous-tone X image.
  1722. X                  */
  1723. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1724. X                  for (i=0; i < image->packets; i++)
  1725. SHAR_EOF
  1726. true || echo 'restore of ImageMagick/X.c failed'
  1727. fi
  1728. echo 'End of ImageMagick part 10'
  1729. echo 'File ImageMagick/X.c is continued in part 11'
  1730. echo 11 > _shar_seq_.tmp
  1731. exit 0
  1732.  
  1733. exit 0 # Just in case...
  1734. -- 
  1735.   // chris@Sterling.COM           | Send comp.sources.x submissions to:
  1736. \X/  Amiga - The only way to fly! |    sources-x@sterling.com
  1737.  "It's intuitively obvious to the |
  1738.   most casual observer..."        | GCS d+/-- p+ c++ l+ m+ s++/+ g+ w+ t+ r+ x+
  1739.