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

  1. Newsgroups: comp.sources.misc
  2. From: cristy@eplrx7.es.duPont.com (John Cristy)
  3. Subject:  v34i044:  imagemagick - X11 image processing and display v2.2, Part16/26
  4. Message-ID: <1992Dec15.035342.21985@sparky.imd.sterling.com>
  5. X-Md4-Signature: 67108793785958dbf109fa9f6222b620
  6. Date: Tue, 15 Dec 1992 03:53:42 GMT
  7. Approved: kent@sparky.imd.sterling.com
  8.  
  9. Submitted-by: cristy@eplrx7.es.duPont.com (John Cristy)
  10. Posting-number: Volume 34, Issue 44
  11. Archive-name: imagemagick/part16
  12. Environment: UNIX, VMS, X11, SGI, DEC, Cray, Sun, Vax
  13.  
  14. #!/bin/sh
  15. # this is Part.16 (part 16 of a multipart archive)
  16. # do not concatenate these parts, unpack them in order with /bin/sh
  17. # file ImageMagick/X.c continued
  18. #
  19. if test ! -r _shar_seq_.tmp; then
  20.     echo 'Please unpack part 1 first!'
  21.     exit 1
  22. fi
  23. (read Scheck
  24.  if test "$Scheck" != 16; then
  25.     echo Please unpack part "$Scheck" next!
  26.     exit 1
  27.  else
  28.     exit 0
  29.  fi
  30. ) < _shar_seq_.tmp || exit 1
  31. if test ! -f _shar_wnt_.tmp; then
  32.     echo 'x - still skipping ImageMagick/X.c'
  33. else
  34. echo 'x - continuing file ImageMagick/X.c'
  35. sed 's/^X//' << 'SHAR_EOF' >> 'ImageMagick/X.c' &&
  36. X  }
  37. X  pixel_info->annotate_color=pixel_info->foreground_color;
  38. X  if (image != (Image *) NULL)
  39. X    if (image->class == PseudoClass)
  40. X      {
  41. X        /*
  42. X          Initialize pixel array for images of type PseudoClass.
  43. X        */
  44. X        for (i=0; i < image->colors; i++)
  45. X          pixel_info->pixels[i]=XStandardPixel(map_info,image->colormap[i],8);
  46. X        for (i=0; i < MaxNumberPens; i++)
  47. X          pixel_info->pixels[image->colors+i]=pixel_info->pen_color[i].pixel;
  48. X        pixel_info->colors+=MaxNumberPens;
  49. X      }
  50. }
  51. X
  52. /*
  53. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  54. %                                                                             %
  55. %                                                                             %
  56. %                                                                             %
  57. %   X G e t R e s o u r c e                                                   %
  58. %                                                                             %
  59. %                                                                             %
  60. %                                                                             %
  61. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  62. %
  63. %  Function XGetResource queries the X server for the specified resource name
  64. %  or class.  If the resource name or class is not defined in the database, the
  65. %  supplied default value is returned.
  66. %
  67. %  The format of the XGetResource routine is:
  68. %
  69. %      value=XGetResource(database,application_name,name,class,resource_default)
  70. %
  71. %  A description of each parameter follows:
  72. %
  73. %    o value: Function XGetResource returns the resource value associated with
  74. %      the name or class.  If none is found, the supplied default value is
  75. %      returned.
  76. %
  77. %    o database: Specifies a resource database; returned from
  78. %      XrmGetStringDatabase.
  79. %
  80. %    o application_name:  Specifies the application name used to retrieve
  81. %      resource info from the X server database.
  82. %
  83. %    o name: Specifies the name of the value being retrieved.
  84. %
  85. %    o class: Specifies the class of the value being retrieved.
  86. %
  87. %    o resource_default: Specifies the default value to return if the query
  88. %      fails to find the specified name/class.
  89. %
  90. %
  91. */
  92. char *XGetResource(database,application_name,name,class,resource_default)
  93. XXrmDatabase
  94. X  database;
  95. X
  96. char
  97. X  *application_name,
  98. X  *name,
  99. X  *class,
  100. X  *resource_default;
  101. {
  102. X  char
  103. X    *resource_type,
  104. X    resource_class[2048],
  105. X    resource_name[2048];
  106. X
  107. X  int
  108. X    status;
  109. X
  110. X  XrmValue
  111. X    resource_value;
  112. X
  113. X  if (database == (XrmDatabase) NULL)
  114. X    return(resource_default);
  115. X  *resource_name='\0';
  116. X  if (name != (char *) NULL)
  117. X    (void) sprintf(resource_name,"%s.%s\0",application_name,name);
  118. X  *resource_class='\0';
  119. X  if (class != (char *) NULL)
  120. X    (void) sprintf(resource_class,"%s.%s\0",application_name,class);
  121. X  status=XrmGetResource(database,resource_name,resource_class,&resource_type,
  122. X    &resource_value);
  123. X  if (status == False)
  124. X    return(resource_default);
  125. X  return(resource_value.addr);
  126. }
  127. X
  128. /*
  129. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  130. %                                                                             %
  131. %                                                                             %
  132. %                                                                             %
  133. %   X G e t R e s o u r c e I n f o                                           %
  134. %                                                                             %
  135. %                                                                             %
  136. %                                                                             %
  137. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  138. %
  139. %  Function XGetResourceInfo initializes the ResourceInfo structure.
  140. %
  141. %  The format of the XGetResourceInfo routine is:
  142. %
  143. %      XGetResourceInfo(resource_database,application_name,resource_info)
  144. %
  145. %  A description of each parameter follows:
  146. %
  147. %    o resource_database: Specifies a resource database; returned from
  148. %      XrmGetStringDatabase.
  149. %
  150. %    o application_name:  Specifies the application name used to retrieve
  151. %      resource info from the X server database.
  152. %
  153. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  154. %
  155. %
  156. */
  157. void XGetResourceInfo(resource_database,application_name,resource_info)
  158. XXrmDatabase
  159. X  resource_database;
  160. X
  161. char
  162. X  *application_name;
  163. X
  164. XXResourceInfo
  165. X  *resource_info;
  166. {
  167. X  char
  168. X    *resource_value;
  169. X
  170. X  /*
  171. X    Initialize resource info fields.
  172. X  */
  173. X  resource_value=XGetResource(resource_database,application_name,"backdrop",
  174. X    (char *) NULL,"False");
  175. X  resource_info->backdrop=IsTrue(resource_value);
  176. X  resource_info->background_color=XGetResource(resource_database,
  177. X    application_name,"background","Background","black");
  178. X  resource_info->border_color=XGetResource(resource_database,application_name,
  179. X    "borderColor","BorderColor","white");
  180. X  resource_value=XGetResource(resource_database,application_name,"borderWidth",
  181. X    "BorderWidth","2");
  182. X  resource_info->border_width=atoi(resource_value);
  183. X  resource_value=XGetResource(resource_database,application_name,"colormap",
  184. X    (char *) NULL,"shared");
  185. X  resource_info->colormap=UndefinedColormap;
  186. X  if (Latin1Compare("private",resource_value) == 0)
  187. X    resource_info->colormap=PrivateColormap;
  188. X  if (Latin1Compare("shared",resource_value) == 0)
  189. X    resource_info->colormap=SharedColormap;
  190. X  if (resource_info->colormap == UndefinedColormap)
  191. X    Warning("unrecognized colormap type",resource_value);
  192. X  resource_value=XGetResource(resource_database,application_name,"colors",
  193. X    (char *) NULL,"0");
  194. X  resource_info->number_colors=atoi(resource_value);
  195. X  resource_value=XGetResource(resource_database,application_name,"colorspace",
  196. X    (char *) NULL,"rgb");
  197. X  resource_info->colorspace=UndefinedColorspace;
  198. X  if (Latin1Compare("gray",resource_value) == 0)
  199. X    resource_info->colorspace=GRAYColorspace;
  200. X  if (Latin1Compare("rgb",resource_value) == 0)
  201. X    resource_info->colorspace=RGBColorspace;
  202. X  if (Latin1Compare("yiq",resource_value) == 0)
  203. X    resource_info->colorspace=YIQColorspace;
  204. X  if (Latin1Compare("yuv",resource_value) == 0)
  205. X    resource_info->colorspace=YUVColorspace;
  206. X  if (Latin1Compare("xyz",resource_value) == 0)
  207. X    resource_info->colorspace=XYZColorspace;
  208. X  if (resource_info->colorspace == UndefinedColorspace)
  209. X    Warning("unrecognized colorspace type",resource_value);
  210. X  resource_value=XGetResource(resource_database,application_name,"debug",
  211. X    (char *) NULL,"False");
  212. X  resource_info->debug=IsTrue(resource_value);
  213. X  resource_value=XGetResource(resource_database,application_name,"delay",
  214. X    (char *) NULL,"0");
  215. X  resource_info->delay=atoi(resource_value);
  216. X  resource_value=XGetResource(resource_database,application_name,"dither",
  217. X    (char *) NULL,"False");
  218. X  resource_info->dither=IsTrue(resource_value);
  219. X  resource_info->font=XGetResource(resource_database,application_name,
  220. X    "font","Font",(char *) NULL);
  221. X  resource_info->font_name[0]=XGetResource(resource_database,application_name,
  222. X    "font1","Font1","fixed");
  223. X  resource_info->font_name[1]=XGetResource(resource_database,application_name,
  224. X    "font2","Font2","variable");
  225. X  resource_info->font_name[2]=XGetResource(resource_database,application_name,
  226. X    "font3","Font3","5x8");
  227. X  resource_info->font_name[3]=XGetResource(resource_database,application_name,
  228. X    "font4","Font4","6x10");
  229. X  resource_info->font_name[4]=XGetResource(resource_database,application_name,
  230. X    "font5","Font5","7x13bold");
  231. X  resource_info->font_name[5]=XGetResource(resource_database,application_name,
  232. X    "font6","Font6","8x13bold");
  233. X  resource_info->font_name[6]=XGetResource(resource_database,application_name,
  234. X    "font7","Font7","9x15bold");
  235. X  resource_info->font_name[7]=XGetResource(resource_database,application_name,
  236. X    "font8","Font8","10x20");
  237. X  resource_info->font_name[8]=XGetResource(resource_database,application_name,
  238. X    "font9","Font9","12x24");
  239. X  resource_info->foreground_color=XGetResource(resource_database,
  240. X    application_name,"foreground","Foreground","white");
  241. X  resource_info->icon_geometry=XGetResource(resource_database,
  242. X    application_name,"iconGeometry","IconGeometry",(char *) NULL);
  243. X  resource_value=XGetResource(resource_database,application_name,"gravity",
  244. X    (char *) NULL,"Center");
  245. X  resource_info->gravity=(-1);
  246. X  if (Latin1Compare("Forget",resource_value) == 0)
  247. X    resource_info->gravity=ForgetGravity;
  248. X  if (Latin1Compare("NorthWest",resource_value) == 0)
  249. X    resource_info->gravity=NorthWestGravity;
  250. X  if (Latin1Compare("North",resource_value) == 0)
  251. X    resource_info->gravity=NorthGravity;
  252. X  if (Latin1Compare("NorthEast",resource_value) == 0)
  253. X    resource_info->gravity=NorthEastGravity;
  254. X  if (Latin1Compare("West",resource_value) == 0)
  255. X    resource_info->gravity=WestGravity;
  256. X  if (Latin1Compare("Center",resource_value) == 0)
  257. X    resource_info->gravity=CenterGravity;
  258. X  if (Latin1Compare("East",resource_value) == 0)
  259. X    resource_info->gravity=EastGravity;
  260. X  if (Latin1Compare("SouthWest",resource_value) == 0)
  261. X    resource_info->gravity=SouthWestGravity;
  262. X  if (Latin1Compare("South",resource_value) == 0)
  263. X    resource_info->gravity=SouthGravity;
  264. X  if (Latin1Compare("SouthEast",resource_value) == 0)
  265. X    resource_info->gravity=SouthEastGravity;
  266. X  if (Latin1Compare("Static",resource_value) == 0)
  267. X    resource_info->gravity=StaticGravity;
  268. X  if (resource_info->gravity == (-1))
  269. X    Warning("unrecognized gravity type",resource_value);
  270. X  resource_value=XGetResource(resource_database,application_name,"iconic",
  271. X    "Iconic","False");
  272. X  resource_info->iconic=IsTrue(resource_value);
  273. X  resource_info->image_geometry=XGetResource(resource_database,
  274. X    application_name,"imageGeometry","ImageGeometry",(char *) NULL);
  275. X  resource_value=XGetResource(resource_database,application_name,"magnify",
  276. X    (char *) NULL,"2");
  277. X  resource_info->magnify=atoi(resource_value);
  278. X  resource_info->map_type=XGetResource(resource_database,application_name,"map",
  279. X    "Map",(char *) NULL);
  280. X  resource_value=XGetResource(resource_database,application_name,"monochrome",
  281. X    (char *) NULL,"False");
  282. X  resource_info->monochrome=IsTrue(resource_value);
  283. X  resource_info->name=XGetResource(resource_database,application_name,"name",
  284. X    "Name",(char *) NULL);
  285. X  resource_info->pen_color[0]=XGetResource(resource_database,application_name,
  286. X    "pen1","Pen1","black");
  287. X  resource_info->pen_color[1]=XGetResource(resource_database,application_name,
  288. X    "pen2","Pen2","blue");
  289. X  resource_info->pen_color[2]=XGetResource(resource_database,application_name,
  290. X    "pen3","Pen3","cyan");
  291. X  resource_info->pen_color[3]=XGetResource(resource_database,application_name,
  292. X    "pen4","Pen4","green");
  293. X  resource_info->pen_color[4]=XGetResource(resource_database,application_name,
  294. X    "pen5","Pen5","gray");
  295. X  resource_info->pen_color[5]=XGetResource(resource_database,application_name,
  296. X    "pen6","Pen6","red");
  297. X  resource_info->pen_color[6]=XGetResource(resource_database,application_name,
  298. X    "pen7","Pen7","magenta");
  299. X  resource_info->pen_color[7]=XGetResource(resource_database,application_name,
  300. X    "pen8","Pen8","yellow");
  301. X  resource_info->pen_color[8]=XGetResource(resource_database,application_name,
  302. X    "pen9","Pen9","white");
  303. X  resource_info->print_filename=XGetResource(resource_database,application_name,
  304. X    "printFilename",(char *) NULL,(char *) NULL);
  305. X  resource_info->title=XGetResource(resource_database,application_name,"title",
  306. X    "Title",(char *) NULL);
  307. X  resource_value=XGetResource(resource_database,application_name,"treeDepth",
  308. X    (char *) NULL,"0");
  309. X  resource_info->tree_depth=atoi(resource_value);
  310. X  resource_info->write_filename=XGetResource(resource_database,application_name,
  311. X    "writeFilename",(char *) NULL,(char *) NULL);
  312. X  resource_info->visual_type=XGetResource(resource_database,application_name,
  313. X    "visual","Visual",(char *) NULL);
  314. }
  315. X
  316. /*
  317. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  318. %                                                                             %
  319. %                                                                             %
  320. %                                                                             %
  321. %   X G e t W i n d o w I n f o                                               %
  322. %                                                                             %
  323. %                                                                             %
  324. %                                                                             %
  325. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  326. %
  327. %  Function XGetWindowInfo initializes the XWindowInfo structure.
  328. %
  329. %  The format of the XGetWindowInfo routine is:
  330. %
  331. %      XGetWindowInfo(superclass,window)
  332. %
  333. %  A description of each parameter follows:
  334. %
  335. %    o superclass_window: Specifies a pointer to a XWindowInfo structure.
  336. %
  337. %    o window: Specifies a pointer to a XWindowInfo structure.
  338. %
  339. %
  340. */
  341. void XGetWindowInfo(superclass_window,window)
  342. XXWindowInfo
  343. X  *superclass_window,
  344. X  *window;
  345. {
  346. X  Window
  347. X    id;
  348. X
  349. X  /*
  350. X    Window is initialized from superclass-- protect window ID.
  351. X  */
  352. X  id=window->id;
  353. X  if (id == (Window) NULL)
  354. X    *window=(*superclass_window);
  355. X  else
  356. X    {
  357. X      Pixmap
  358. X        pixmap;
  359. X
  360. X      XImage
  361. X        *ximage;
  362. X
  363. X      /*
  364. X        Protect X image, and pixmap.
  365. X      */
  366. X      ximage=window->ximage;
  367. X      pixmap=window->pixmap;
  368. X      *window=(*superclass_window);
  369. X      window->ximage=ximage;
  370. X      window->pixmap=pixmap;
  371. X    }
  372. X  window->id=id;
  373. }
  374. X
  375. /*
  376. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  377. %                                                                             %
  378. %                                                                             %
  379. %                                                                             %
  380. %   X M a k e I m a g e                                                       %
  381. %                                                                             %
  382. %                                                                             %
  383. %                                                                             %
  384. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  385. %
  386. %  Function XMakeImage creates an X11 image.  If the image size differs from
  387. %  the X11 image size, the image is first resized.
  388. %
  389. %  The format of the XMakeImage routine is:
  390. %
  391. %      status=XMakeImage(display,resource_info,window,image,width,height)
  392. %
  393. %  A description of each parameter follows:
  394. %
  395. %    o status: Function XMakeImage returns True if the X image is
  396. %      successfully created.  False is returned is there is a memory shortage.
  397. %
  398. %    o display: Specifies a connection to an X server; returned from
  399. %      XOpenDisplay.
  400. %
  401. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  402. %
  403. %    o window: Specifies a pointer to a XWindowInfo structure.
  404. %
  405. %    o image: Specifies a pointer to a Image structure;  returned from
  406. %      ReadImage.
  407. %
  408. %    o width: Specifies the width in pixels of the rectangular area to
  409. %      display.
  410. %
  411. %    o height: Specifies the height in pixels of the rectangular area to
  412. %      display.
  413. %
  414. %
  415. */
  416. unsigned int XMakeImage(display,resource_info,window,image,width,height)
  417. Display
  418. X  *display;
  419. X
  420. XXResourceInfo
  421. X  *resource_info;
  422. X
  423. XXWindowInfo
  424. X  *window;
  425. X
  426. Image
  427. X  *image;
  428. X
  429. unsigned int
  430. X  width,
  431. X  height;
  432. {
  433. X  Image
  434. X    *transformed_image;
  435. X
  436. X  int
  437. X    format;
  438. X
  439. X  XImage
  440. X    *ximage;
  441. X
  442. X  if ((window->width == 0) || (window->height == 0))
  443. X    return(False);
  444. X  /*
  445. X    Display busy cursor.
  446. X  */
  447. X  XDefineCursor(display,window->id,window->busy_cursor);
  448. X  XFlush(display);
  449. X  if (image != (Image *) NULL)
  450. X    {
  451. X      /*
  452. X        Apply user transforms to the image.
  453. X      */
  454. X      image->orphan=True;
  455. X      transformed_image=image;
  456. X      if (window->clip_geometry)
  457. X        {
  458. X          Image
  459. X            *clipped_image;
  460. X
  461. X          int
  462. X            clip_x,
  463. X            clip_y;
  464. X
  465. X          unsigned
  466. X            clip_height,
  467. X            clip_width;
  468. X
  469. X          /*
  470. X            Clip image.
  471. X          */
  472. X          (void) XParseGeometry(window->clip_geometry,&clip_x,&clip_y,
  473. X            &clip_width,&clip_height);
  474. X          clipped_image=
  475. X            ClipImage(transformed_image,clip_x,clip_y,clip_width,clip_height);
  476. X          if (clipped_image != (Image *) NULL)
  477. X            {
  478. X              if (transformed_image != image)
  479. X                DestroyImage(transformed_image);
  480. X              transformed_image=clipped_image;
  481. X            }
  482. X        }
  483. X      if ((width != transformed_image->columns) ||
  484. X          (height != transformed_image->rows))
  485. X        {
  486. X          Image
  487. X            *scaled_image;
  488. X
  489. X          /*
  490. X            Scale image.
  491. X          */
  492. X          scaled_image=ScaleImage(transformed_image,width,height);
  493. X          if (scaled_image != (Image *) NULL)
  494. X            {
  495. X              if (transformed_image != image)
  496. X                DestroyImage(transformed_image);
  497. X              transformed_image=scaled_image;
  498. X            }
  499. X        }
  500. X      width=transformed_image->columns;
  501. X      height=transformed_image->rows;
  502. X      image->orphan=False;
  503. X    }
  504. X  /*
  505. X    Create X image.
  506. X  */
  507. X  format=(window->depth == 1) ? XYBitmap : ZPixmap;
  508. X  ximage=XCreateImage(display,window->visual_info->visual,window->depth,format,
  509. X    0,(char *) NULL,width,height,XBitmapPad(display),0);
  510. X  if (ximage == (XImage *) NULL)
  511. X    {
  512. X      /*
  513. X        Unable to create X image.
  514. X      */
  515. X      XDefineCursor(display,window->id,window->cursor);
  516. X      return(False);
  517. X    }
  518. X  if (resource_info->debug)
  519. X    {
  520. X      (void) fprintf(stderr,"XImage:\n");
  521. X      (void) fprintf(stderr,"  width, height: %dx%d\n",ximage->width,
  522. X        ximage->height);
  523. X      (void) fprintf(stderr,"  format: %d\n",ximage->format);
  524. X      (void) fprintf(stderr,"  byte order: %d\n",ximage->byte_order);
  525. X      (void) fprintf(stderr,"  bitmap unit, bit order, pad: %d %d %d\n",
  526. X        ximage->bitmap_unit,ximage->bitmap_bit_order,ximage->bitmap_pad);
  527. X      (void) fprintf(stderr,"  depth: %d\n",ximage->depth);
  528. X      (void) fprintf(stderr,"  bytes per line: %d\n",ximage->bytes_per_line);
  529. X      (void) fprintf(stderr,"  bits per pixel: %d\n",ximage->bits_per_pixel);
  530. X      (void) fprintf(stderr,"  red, green, blue masks: 0x%lx 0x%lx 0x%lx\n",
  531. X        ximage->red_mask,ximage->green_mask,ximage->blue_mask);
  532. X    }
  533. X  /*
  534. X    Allocate X image pixel data.
  535. X  */
  536. X  if (ximage->format == XYBitmap)
  537. X    ximage->data=(char *)
  538. X      malloc(ximage->bytes_per_line*ximage->height*ximage->depth);
  539. X  else
  540. X    ximage->data=(char *) malloc(ximage->bytes_per_line*ximage->height);
  541. X  if (ximage->data == (char *) NULL)
  542. X    {
  543. X      /*
  544. X        Unable to allocate pixel data.
  545. X      */
  546. X      XDestroyImage(ximage);
  547. X      XDefineCursor(display,window->id,window->cursor);
  548. X      return(False);
  549. X    }
  550. X  if (window->ximage != (XImage *) NULL)
  551. X    XDestroyImage(window->ximage);
  552. X  window->ximage=ximage;
  553. X  if (image == (Image *) NULL)
  554. X    {
  555. X      XDefineCursor(display,window->id,window->cursor);
  556. X      return(True);
  557. X    }
  558. X  /*
  559. X    Convert runlength-encoded pixels to X image data.
  560. X  */
  561. X  if ((ximage->byte_order == LSBFirst) ||
  562. X      ((ximage->format == XYBitmap) && (ximage->bitmap_bit_order == LSBFirst)))
  563. X    XMakeImageLSBFirst(window,transformed_image,ximage);
  564. X  else
  565. X    XMakeImageMSBFirst(window,transformed_image,ximage);
  566. X  if (transformed_image != image)
  567. X    DestroyImage(transformed_image);
  568. X  /*
  569. X    Restore cursor.
  570. X  */
  571. X  XDefineCursor(display,window->id,window->cursor);
  572. X  return(True);
  573. }
  574. X
  575. /*
  576. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  577. %                                                                             %
  578. %                                                                             %
  579. %                                                                             %
  580. %   X M a k e I m a g e L S B F i r s t                                       %
  581. %                                                                             %
  582. %                                                                             %
  583. %                                                                             %
  584. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  585. %
  586. %  Function XMakeImageLSBFirst initializes the pixel data of an X11 Image.
  587. %  The X image pixels are copied in least-significant bit and byte first
  588. %  order.  The server's scanline pad is respected.  Rather than using one or
  589. %  two general cases, many special cases are found here to help speed up the
  590. %  image conversion.
  591. %
  592. %  The format of the XMakeImageLSBFirst routine is:
  593. %
  594. %      XMakeImageLSBFirst(window,image,ximage)
  595. %
  596. %  A description of each parameter follows:
  597. %
  598. %    o window: Specifies a pointer to a XWindowInfo structure.
  599. %
  600. %    o image: Specifies a pointer to a Image structure;  returned from
  601. %      ReadImage.
  602. %
  603. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  604. %      XCreateImage.
  605. %
  606. %
  607. */
  608. static void XMakeImageLSBFirst(window,image,ximage)
  609. XXWindowInfo
  610. X  *window;
  611. X
  612. Image
  613. X  *image;
  614. X
  615. XXImage
  616. X  *ximage;
  617. {
  618. X  register int
  619. X    i,
  620. X    j,
  621. X    x;
  622. X
  623. X  register RunlengthPacket
  624. X    *p;
  625. X
  626. X  register unsigned char
  627. X    *q;
  628. X
  629. X  register unsigned long
  630. X    pixel;
  631. X
  632. X  unsigned int
  633. X    scanline_pad;
  634. X
  635. X  unsigned long
  636. X    *pixels;
  637. X
  638. X  pixels=window->pixel_info->pixels;
  639. X  p=image->pixels;
  640. X  q=(unsigned char *) ximage->data;
  641. X  x=0;
  642. X  if (ximage->format == XYBitmap)
  643. X    {
  644. X      register unsigned char
  645. X        background,
  646. X        bit,
  647. X        byte,
  648. X        foreground;
  649. X
  650. X      register unsigned short
  651. X        polarity;
  652. X
  653. X      /*
  654. X        Convert image to big-endian bitmap.
  655. X      */
  656. X      background=(Intensity(window->pixel_info->foreground_color) >
  657. X        Intensity(window->pixel_info->background_color) ? 1 : 0) << 7;
  658. X      foreground=(Intensity(window->pixel_info->background_color) >
  659. X        Intensity(window->pixel_info->foreground_color) ? 1 : 0) << 7;
  660. X      polarity=Intensity(image->colormap[0]) > Intensity(image->colormap[1]);
  661. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  662. X      bit=0;
  663. X      byte=0;
  664. X      for (i=0; i < image->packets; i++)
  665. X      {
  666. X        for (j=0; j <= ((int) p->length); j++)
  667. X        {
  668. X          byte>>=1;
  669. X          if (p->index == polarity)
  670. X            byte|=foreground;
  671. X          else
  672. X            byte|=background;
  673. X          bit++;
  674. X          if (bit == 8)
  675. X            {
  676. X              *q++=byte;
  677. X              bit=0;
  678. X              byte=0;
  679. X            }
  680. X          x++;
  681. X          if (x == ximage->width)
  682. X            {
  683. X              /*
  684. X                Advance to the next scanline.
  685. X              */
  686. X              if (bit > 0)
  687. X                *q=byte >> (8-bit);
  688. X              q+=scanline_pad;
  689. X              bit=0;
  690. X              byte=0;
  691. X              x=0;
  692. X            }
  693. X        }
  694. X        p++;
  695. X      }
  696. X    }
  697. X  else
  698. X    {
  699. X      XStandardColormap
  700. X        *map_info;
  701. X
  702. X      /*
  703. X        Convert image to little-endian color-mapped X image.
  704. X      */
  705. X      map_info=window->map_info;
  706. X      scanline_pad=ximage->bytes_per_line-
  707. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  708. X      if (window->pixel_info->colors > 0)
  709. X        switch (ximage->bits_per_pixel)
  710. X        {
  711. X          case 2:
  712. X          {
  713. X            register unsigned int
  714. X              nibble;
  715. X
  716. X            /*
  717. X              Convert to 2 bit color-mapped X image.
  718. X            */
  719. X            nibble=0;
  720. X            for (i=0; i < image->packets; i++)
  721. X            {
  722. X              pixel=pixels[p->index] & 0xf;
  723. X              for (j=0; j <= ((int) p->length); j++)
  724. X              {
  725. X                switch (nibble)
  726. X                {
  727. X                  case 0:
  728. X                  {
  729. X                    *q=(unsigned char) pixel;
  730. X                    nibble++;
  731. X                    break;
  732. X                  }
  733. X                  case 1:
  734. X                  {
  735. X                    *q|=(unsigned char) (pixel << 2);
  736. X                    nibble++;
  737. X                    break;
  738. X                  }
  739. X                  case 2:
  740. X                  {
  741. X                    *q|=(unsigned char) (pixel << 4);
  742. X                    nibble++;
  743. X                    break;
  744. X                  }
  745. X                  case 3:
  746. X                  {
  747. X                    *q|=(unsigned char) (pixel << 6);
  748. X                    q++;
  749. X                    nibble=0;
  750. X                    break;
  751. X                  }
  752. X                }
  753. X                x++;
  754. X                if (x == ximage->width)
  755. X                  {
  756. X                    x=0;
  757. X                    nibble=0;
  758. X                    q+=scanline_pad;
  759. X                  }
  760. X              }
  761. X              p++;
  762. X            }
  763. X            break;
  764. X          }
  765. X          case 4:
  766. X          {
  767. X            register unsigned int
  768. X              nibble;
  769. X
  770. X            /*
  771. X              Convert to 4 bit color-mapped X image.
  772. X            */
  773. X            nibble=0;
  774. X            for (i=0; i < image->packets; i++)
  775. X            {
  776. X              pixel=pixels[p->index] & 0xf;
  777. X              for (j=0; j <= ((int) p->length); j++)
  778. X              {
  779. X                switch (nibble)
  780. X                {
  781. X                  case 0:
  782. X                  {
  783. X                    *q=(unsigned char) pixel;
  784. X                    nibble++;
  785. X                    break;
  786. X                  }
  787. X                  case 1:
  788. X                  {
  789. X                    *q|=(unsigned char) (pixel << 4);
  790. X                    q++;
  791. X                    nibble=0;
  792. X                    break;
  793. X                  }
  794. X                }
  795. X                x++;
  796. X                if (x == ximage->width)
  797. X                  {
  798. X                    x=0;
  799. X                    nibble=0;
  800. X                    q+=scanline_pad;
  801. X                  }
  802. X              }
  803. X              p++;
  804. X            }
  805. X            break;
  806. X          }
  807. X          case 6:
  808. X          case 8:
  809. X          {
  810. X            /*
  811. X              Convert to 8 bit color-mapped X image.
  812. X            */
  813. X            for (i=0; i < image->packets; i++)
  814. X            {
  815. X              pixel=pixels[p->index];
  816. X              for (j=0; j <= ((int) p->length); j++)
  817. X              {
  818. X                *q++=(unsigned char) pixel;
  819. X                x++;
  820. X                if (x == ximage->width)
  821. X                  {
  822. X                    x=0;
  823. X                    q+=scanline_pad;
  824. X                  }
  825. X              }
  826. X              p++;
  827. X            }
  828. X            break;
  829. X          }
  830. X          default:
  831. X          {
  832. X            register int
  833. X              k;
  834. X
  835. X            register unsigned int
  836. X              bytes_per_pixel;
  837. X
  838. X            unsigned char
  839. X              channel[sizeof(unsigned long)];
  840. X
  841. X            /*
  842. X              Convert to multi-byte color-mapped X image.
  843. X            */
  844. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  845. X            for (i=0; i < image->packets; i++)
  846. X            {
  847. X              pixel=pixels[p->index];
  848. X              for (k=0; k < bytes_per_pixel; k++)
  849. X              {
  850. X                channel[k]=(unsigned char) pixel;
  851. X                pixel>>=8;
  852. X              }
  853. X              for (j=0; j <= ((int) p->length); j++)
  854. X              {
  855. X                for (k=0; k < bytes_per_pixel; k++)
  856. X                  *q++=channel[k];
  857. X                x++;
  858. X                if (x == ximage->width)
  859. X                  {
  860. X                    x=0;
  861. X                    q+=scanline_pad;
  862. X                  }
  863. X              }
  864. X              p++;
  865. X            }
  866. X            break;
  867. X          }
  868. X        }
  869. X      else
  870. X        {
  871. X          /*
  872. X            Convert image to little-endian continuous-tone X image.
  873. X          */
  874. X          switch (ximage->bits_per_pixel)
  875. X          {
  876. X            case 2:
  877. X            {
  878. X              register unsigned int
  879. X                nibble;
  880. X
  881. X              /*
  882. X                Convert to contiguous 2 bit continuous-tone X image.
  883. X              */
  884. X              nibble=0;
  885. X              for (i=0; i < image->packets; i++)
  886. X              {
  887. X                pixel=XStandardPixel(map_info,(*p),8);
  888. X                pixel&=0xf;
  889. X                for (j=0; j <= ((int) p->length); j++)
  890. X                {
  891. X                  switch (nibble)
  892. X                  {
  893. X                    case 0:
  894. X                    {
  895. X                      *q=(unsigned char) pixel;
  896. X                      nibble++;
  897. X                      break;
  898. X                    }
  899. X                    case 1:
  900. X                    {
  901. X                      *q|=(unsigned char) (pixel << 2);
  902. X                      nibble++;
  903. X                      break;
  904. X                    }
  905. X                    case 2:
  906. X                    {
  907. X                      *q|=(unsigned char) (pixel << 4);
  908. X                      nibble++;
  909. X                      break;
  910. X                    }
  911. X                    case 3:
  912. X                    {
  913. X                      *q|=(unsigned char) (pixel << 6);
  914. X                      q++;
  915. X                      nibble=0;
  916. X                      break;
  917. X                    }
  918. X                  }
  919. X                  x++;
  920. X                  if (x == ximage->width)
  921. X                    {
  922. X                      x=0;
  923. X                      nibble=0;
  924. X                      q+=scanline_pad;
  925. X                    }
  926. X                }
  927. X                p++;
  928. X              }
  929. X              break;
  930. X            }
  931. X            case 4:
  932. X            {
  933. X              register unsigned int
  934. X                nibble;
  935. X
  936. X              /*
  937. X                Convert to contiguous 4 bit continuous-tone X image.
  938. X              */
  939. X              nibble=0;
  940. X              for (i=0; i < image->packets; i++)
  941. X              {
  942. X                pixel=XStandardPixel(map_info,(*p),8);
  943. X                pixel&=0xf;
  944. X                for (j=0; j <= ((int) p->length); j++)
  945. X                {
  946. X                  switch (nibble)
  947. X                  {
  948. X                    case 0:
  949. X                    {
  950. X                      *q=(unsigned char) pixel;
  951. X                      nibble++;
  952. X                      break;
  953. X                    }
  954. X                    case 1:
  955. X                    {
  956. X                      *q|=(unsigned char) (pixel << 4);
  957. X                      q++;
  958. X                      nibble=0;
  959. X                      break;
  960. X                    }
  961. X                  }
  962. X                  x++;
  963. X                  if (x == ximage->width)
  964. X                    {
  965. X                      x=0;
  966. X                      nibble=0;
  967. X                      q+=scanline_pad;
  968. X                    }
  969. X                }
  970. X                p++;
  971. X              }
  972. X              break;
  973. X            }
  974. X            case 6:
  975. X            case 8:
  976. X            {
  977. X              /*
  978. X                Convert to contiguous 8 bit continuous-tone X image.
  979. X              */
  980. X              for (i=0; i < image->packets; i++)
  981. X              {
  982. X                pixel=XStandardPixel(map_info,(*p),8);
  983. X                for (j=0; j <= ((int) p->length); j++)
  984. X                {
  985. X                  *q++=(unsigned char) pixel;
  986. X                  x++;
  987. X                  if (x == ximage->width)
  988. X                    {
  989. X                      x=0;
  990. X                      q+=scanline_pad;
  991. X                    }
  992. X                }
  993. X                p++;
  994. X              }
  995. X              break;
  996. X            }
  997. X            default:
  998. X            {
  999. X              if ((ximage->bits_per_pixel == 32) &&
  1000. X                  (map_info->red_max == 255) &&
  1001. X                  (map_info->green_max == 255) &&
  1002. X                  (map_info->blue_max == 255) &&
  1003. X                  (map_info->red_mult == 65536) &&
  1004. X                  (map_info->green_mult == 256) &&
  1005. X                  (map_info->blue_mult == 1))
  1006. X                {
  1007. X                  /*
  1008. X                    Convert to 32 bit continuous-tone X image.
  1009. X                  */
  1010. X                  for (i=0; i < image->packets; i++)
  1011. X                  {
  1012. X                    for (j=0; j <= ((int) p->length); j++)
  1013. X                    {
  1014. X                      *q++=p->blue;
  1015. X                      *q++=p->green;
  1016. X                      *q++=p->red;
  1017. X                      *q++=0;
  1018. X                    }
  1019. X                    p++;
  1020. X                  }
  1021. X                }
  1022. X              else
  1023. X                {
  1024. X                  register int
  1025. X                    k;
  1026. X
  1027. X                  register unsigned int
  1028. X                    bytes_per_pixel;
  1029. X
  1030. X                  unsigned char
  1031. X                    channel[sizeof(unsigned long)];
  1032. X
  1033. X                  /*
  1034. X                    Convert to multi-byte continuous-tone X image.
  1035. X                  */
  1036. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1037. X                  for (i=0; i < image->packets; i++)
  1038. X                  {
  1039. X                    pixel=XStandardPixel(map_info,(*p),8);
  1040. X                    for (k=0; k < bytes_per_pixel; k++)
  1041. X                    {
  1042. X                      channel[k]=(unsigned char) pixel;
  1043. X                      pixel>>=8;
  1044. X                    }
  1045. X                    for (j=0; j <= ((int) p->length); j++)
  1046. X                    {
  1047. X                      for (k=0; k < bytes_per_pixel; k++)
  1048. X                        *q++=channel[k];
  1049. X                      x++;
  1050. X                      if (x == ximage->width)
  1051. X                        {
  1052. X                          x=0;
  1053. X                          q+=scanline_pad;
  1054. X                        }
  1055. X                    }
  1056. X                    p++;
  1057. X                  }
  1058. X                }
  1059. X              break;
  1060. X            }
  1061. X          }
  1062. X        }
  1063. X    }
  1064. }
  1065. X
  1066. /*
  1067. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1068. %                                                                             %
  1069. %                                                                             %
  1070. %                                                                             %
  1071. %   X M a k e I m a g e M S B F i r s t                                       %
  1072. %                                                                             %
  1073. %                                                                             %
  1074. %                                                                             %
  1075. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1076. %
  1077. %  Function XMakeImageMSBFirst initializes the pixel data of an X11 Image.
  1078. %  The X image pixels are copied in most-significant bit and byte first order.
  1079. %  The server's scanline pad is also resprected. Rather than using one or two
  1080. %  general cases, many special cases are found here to help speed up the image
  1081. %  conversion.
  1082. %
  1083. %  The format of the XMakeImageMSBFirst routine is:
  1084. %
  1085. %      XMakeImageMSBFirst(window,image,ximage)
  1086. %
  1087. %  A description of each parameter follows:
  1088. %
  1089. %    o window: Specifies a pointer to a XWindowInfo structure.
  1090. %
  1091. %    o image: Specifies a pointer to a Image structure;  returned from
  1092. %      ReadImage.
  1093. %
  1094. %    o ximage: Specifies a pointer to a XImage structure;  returned from
  1095. %      XCreateImage.
  1096. %
  1097. %
  1098. */
  1099. static void XMakeImageMSBFirst(window,image,ximage)
  1100. XXWindowInfo
  1101. X  *window;
  1102. X
  1103. Image
  1104. X  *image;
  1105. X
  1106. XXImage
  1107. X  *ximage;
  1108. {
  1109. X  register int
  1110. X    i,
  1111. X    j,
  1112. X    x;
  1113. X
  1114. X  register RunlengthPacket
  1115. X    *p;
  1116. X
  1117. X  register unsigned char
  1118. X    *q;
  1119. X
  1120. X  register unsigned long
  1121. X    pixel;
  1122. X
  1123. X  unsigned int
  1124. X    scanline_pad;
  1125. X
  1126. X  unsigned long
  1127. X    *pixels;
  1128. X
  1129. X  pixels=window->pixel_info->pixels;
  1130. X  p=image->pixels;
  1131. X  q=(unsigned char *) ximage->data;
  1132. X  x=0;
  1133. X  if (ximage->format == XYBitmap)
  1134. X    {
  1135. X      register unsigned char
  1136. X        background,
  1137. X        bit,
  1138. X        byte,
  1139. X        foreground;
  1140. X
  1141. X      register unsigned short
  1142. X        polarity;
  1143. X
  1144. X      /*
  1145. X        Convert image to big-endian bitmap.
  1146. X      */
  1147. X      background=(Intensity(window->pixel_info->foreground_color) >
  1148. X        Intensity(window->pixel_info->background_color));
  1149. X      foreground=(Intensity(window->pixel_info->background_color) >
  1150. X        Intensity(window->pixel_info->foreground_color));
  1151. X      polarity=Intensity(image->colormap[0]) > Intensity(image->colormap[1]);
  1152. X      scanline_pad=ximage->bytes_per_line-(ximage->width >> 3);
  1153. X      bit=0;
  1154. X      byte=0;
  1155. X      for (i=0; i < image->packets; i++)
  1156. X      {
  1157. X        for (j=0; j <= ((int) p->length); j++)
  1158. X        {
  1159. X          byte<<=1;
  1160. X          if (p->index == polarity)
  1161. X            byte|=foreground;
  1162. X          else
  1163. X            byte|=background;
  1164. X          bit++;
  1165. X          if (bit == 8)
  1166. X            {
  1167. X              *q++=byte;
  1168. X              bit=0;
  1169. X              byte=0;
  1170. X            }
  1171. X          x++;
  1172. X          if (x == ximage->width)
  1173. X            {
  1174. X              /*
  1175. X                Advance to the next scanline.
  1176. X              */
  1177. X              if (bit > 0)
  1178. X                *q=byte << (8-bit);
  1179. X              q+=scanline_pad;
  1180. X              bit=0;
  1181. X              byte=0;
  1182. X              x=0;
  1183. X            }
  1184. X        }
  1185. X        p++;
  1186. X      }
  1187. X    }
  1188. X  else
  1189. X    {
  1190. X      XStandardColormap
  1191. X        *map_info;
  1192. X
  1193. X      /*
  1194. X        Convert image to big-endian X image.
  1195. X      */
  1196. X      map_info=window->map_info;
  1197. X      scanline_pad=ximage->bytes_per_line-
  1198. X        ((ximage->width*ximage->bits_per_pixel) >> 3);
  1199. X      if (window->pixel_info->colors > 0)
  1200. X        switch (ximage->bits_per_pixel)
  1201. X        {
  1202. X          case 2:
  1203. X          {
  1204. X            register unsigned int
  1205. X              nibble;
  1206. X
  1207. X            /*
  1208. X              Convert to 2 bit color-mapped X image.
  1209. X            */
  1210. X            nibble=0;
  1211. X            for (i=0; i < image->packets; i++)
  1212. X            {
  1213. X              pixel=pixels[p->index] & 0xf;
  1214. X              for (j=0; j <= ((int) p->length); j++)
  1215. X              {
  1216. X                switch (nibble)
  1217. X                {
  1218. X                  case 0:
  1219. X                  {
  1220. X                    *q=(unsigned char) (pixel << 6);
  1221. X                    nibble++;
  1222. X                    break;
  1223. X                  }
  1224. X                  case 1:
  1225. X                  {
  1226. X                    *q|=(unsigned char) (pixel << 4);
  1227. X                    nibble++;
  1228. X                    break;
  1229. X                  }
  1230. X                  case 2:
  1231. X                  {
  1232. X                    *q|=(unsigned char) (pixel << 2);
  1233. X                    nibble++;
  1234. X                    break;
  1235. X                  }
  1236. X                  case 3:
  1237. X                  {
  1238. X                    *q|=(unsigned char) pixel;
  1239. X                    q++;
  1240. X                    nibble=0;
  1241. X                    break;
  1242. X                  }
  1243. X                }
  1244. X                x++;
  1245. X                if (x == ximage->width)
  1246. X                  {
  1247. X                    x=0;
  1248. X                    nibble=0;
  1249. X                    q+=scanline_pad;
  1250. X                  }
  1251. X              }
  1252. X              p++;
  1253. X            }
  1254. X            break;
  1255. X          }
  1256. X          case 4:
  1257. X          {
  1258. X            register unsigned int
  1259. X              nibble;
  1260. X
  1261. X            /*
  1262. X              Convert to 4 bit color-mapped X image.
  1263. X            */
  1264. X            nibble=0;
  1265. X            for (i=0; i < image->packets; i++)
  1266. X            {
  1267. X              pixel=pixels[p->index] & 0xf;
  1268. X              for (j=0; j <= ((int) p->length); j++)
  1269. X              {
  1270. X                switch (nibble)
  1271. X                {
  1272. X                  case 0:
  1273. X                  {
  1274. X                    *q=(unsigned char) (pixel << 4);
  1275. X                    nibble++;
  1276. X                    break;
  1277. X                  }
  1278. X                  case 1:
  1279. X                  {
  1280. X                    *q|=(unsigned char) pixel;
  1281. X                    q++;
  1282. X                    nibble=0;
  1283. X                    break;
  1284. X                  }
  1285. X                }
  1286. X                x++;
  1287. X                if (x == ximage->width)
  1288. X                  {
  1289. X                    x=0;
  1290. X                    nibble=0;
  1291. X                    q+=scanline_pad;
  1292. X                  }
  1293. X              }
  1294. X              p++;
  1295. X            }
  1296. X            break;
  1297. X          }
  1298. X          case 8:
  1299. X          {
  1300. X            /*
  1301. X              Convert to 8 bit color-mapped X image.
  1302. X            */
  1303. X            for (i=0; i < image->packets; i++)
  1304. X            {
  1305. X              pixel=pixels[p->index];
  1306. X              for (j=0; j <= ((int) p->length); j++)
  1307. X              {
  1308. X                *q++=(unsigned char) pixel;
  1309. X                x++;
  1310. X                if (x == ximage->width)
  1311. X                  {
  1312. X                    x=0;
  1313. X                    q+=scanline_pad;
  1314. X                  }
  1315. X              }
  1316. X              p++;
  1317. X            }
  1318. X            break;
  1319. X          }
  1320. X          default:
  1321. X          {
  1322. X            register int
  1323. X              k;
  1324. X
  1325. X            register unsigned int
  1326. X              bytes_per_pixel;
  1327. X
  1328. X            unsigned char
  1329. X              channel[sizeof(unsigned long)];
  1330. X
  1331. X            /*
  1332. X              Convert to 8 bit color-mapped X image.
  1333. X            */
  1334. X            bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1335. X            for (i=0; i < image->packets; i++)
  1336. X            {
  1337. X              pixel=pixels[p->index];
  1338. X              for (k=bytes_per_pixel-1; k >= 0; k--)
  1339. X              {
  1340. X                channel[k]=(unsigned char) pixel;
  1341. X                pixel>>=8;
  1342. X              }
  1343. X              for (j=0; j <= ((int) p->length); j++)
  1344. X              {
  1345. X                for (k=0; k < bytes_per_pixel; k++)
  1346. X                  *q++=channel[k];
  1347. X                x++;
  1348. X                if (x == ximage->width)
  1349. X                  {
  1350. X                    x=0;
  1351. X                    q+=scanline_pad;
  1352. X                  }
  1353. X              }
  1354. X              p++;
  1355. X            }
  1356. X            break;
  1357. X          }
  1358. X        }
  1359. X      else
  1360. X        {
  1361. X          /*
  1362. X            Convert to big-endian continuous-tone X image.
  1363. X          */
  1364. X          switch (ximage->bits_per_pixel)
  1365. X          {
  1366. X            case 2:
  1367. X            {
  1368. X              register unsigned int
  1369. X                nibble;
  1370. X
  1371. X              /*
  1372. X                Convert to 4 bit continuous-tone X image.
  1373. X              */
  1374. X              nibble=0;
  1375. X              for (i=0; i < image->packets; i++)
  1376. X              {
  1377. X                pixel=XStandardPixel(map_info,(*p),8);
  1378. X                pixel&=0xf;
  1379. X                for (j=0; j <= ((int) p->length); j++)
  1380. X                {
  1381. X                  switch (nibble)
  1382. X                  {
  1383. X                    case 0:
  1384. X                    {
  1385. X                      *q=(unsigned char) (pixel << 6);
  1386. X                      nibble++;
  1387. X                      break;
  1388. X                    }
  1389. X                    case 1:
  1390. X                    {
  1391. X                      *q|=(unsigned char) (pixel << 4);
  1392. X                      nibble++;
  1393. X                      break;
  1394. X                    }
  1395. X                    case 2:
  1396. X                    {
  1397. X                      *q|=(unsigned char) (pixel << 2);
  1398. X                      nibble++;
  1399. X                      break;
  1400. X                    }
  1401. X                    case 3:
  1402. X                    {
  1403. X                      *q|=(unsigned char) pixel;
  1404. X                      q++;
  1405. X                      nibble=0;
  1406. X                      break;
  1407. X                    }
  1408. X                  }
  1409. X                  x++;
  1410. X                  if (x == ximage->width)
  1411. X                    {
  1412. X                      x=0;
  1413. X                      nibble=0;
  1414. X                      q+=scanline_pad;
  1415. X                    }
  1416. X                }
  1417. X                p++;
  1418. X              }
  1419. X              break;
  1420. X            }
  1421. X            case 4:
  1422. X            {
  1423. X              register unsigned int
  1424. X                nibble;
  1425. X
  1426. X              /*
  1427. X                Convert to 4 bit continuous-tone X image.
  1428. X              */
  1429. X              nibble=0;
  1430. X              for (i=0; i < image->packets; i++)
  1431. X              {
  1432. X                pixel=XStandardPixel(map_info,(*p),8);
  1433. X                pixel&=0xf;
  1434. X                for (j=0; j <= ((int) p->length); j++)
  1435. X                {
  1436. X                  switch (nibble)
  1437. X                  {
  1438. X                    case 0:
  1439. X                    {
  1440. X                      *q=(unsigned char) (pixel << 4);
  1441. X                      nibble++;
  1442. X                      break;
  1443. X                    }
  1444. X                    case 1:
  1445. X                    {
  1446. X                      *q|=(unsigned char) pixel;
  1447. X                      q++;
  1448. X                      nibble=0;
  1449. X                      break;
  1450. X                    }
  1451. X                  }
  1452. X                  x++;
  1453. X                  if (x == ximage->width)
  1454. X                    {
  1455. X                      x=0;
  1456. X                      nibble=0;
  1457. X                      q+=scanline_pad;
  1458. X                    }
  1459. X                }
  1460. X                p++;
  1461. X              }
  1462. X              break;
  1463. X            }
  1464. X            case 8:
  1465. X            {
  1466. X              /*
  1467. X                Convert to 8 bit continuous-tone X image.
  1468. X              */
  1469. X              for (i=0; i < image->packets; i++)
  1470. X              {
  1471. X                pixel=XStandardPixel(map_info,(*p),8);
  1472. X                for (j=0; j <= ((int) p->length); j++)
  1473. X                {
  1474. X                  *q++=(unsigned char) pixel;
  1475. X                  x++;
  1476. X                  if (x == ximage->width)
  1477. X                    {
  1478. X                      x=0;
  1479. X                      q+=scanline_pad;
  1480. X                    }
  1481. X                }
  1482. X                p++;
  1483. X              }
  1484. X              break;
  1485. X            }
  1486. X            default:
  1487. X            {
  1488. X              if ((ximage->bits_per_pixel == 32) &&
  1489. X                  (map_info->red_max == 255) &&
  1490. X                  (map_info->green_max == 255) &&
  1491. X                  (map_info->blue_max == 255) &&
  1492. X                  (map_info->red_mult == 65536) &&
  1493. X                  (map_info->green_mult == 256) &&
  1494. X                  (map_info->blue_mult == 1))
  1495. X                {
  1496. X                  /*
  1497. X                    Convert to 32 bit continuous-tone X image.
  1498. X                  */
  1499. X                  for (i=0; i < image->packets; i++)
  1500. X                  {
  1501. X                    for (j=0; j <= ((int) p->length); j++)
  1502. X                    {
  1503. X                      *q++=0;
  1504. X                      *q++=p->red;
  1505. X                      *q++=p->green;
  1506. X                      *q++=p->blue;
  1507. X                    }
  1508. X                    p++;
  1509. X                  }
  1510. X                }
  1511. X              else
  1512. X                {
  1513. X                  register int
  1514. X                    k;
  1515. X
  1516. X                  register unsigned int
  1517. X                    bytes_per_pixel;
  1518. X
  1519. X                  unsigned char
  1520. X                    channel[sizeof(unsigned long)];
  1521. X
  1522. X                  /*
  1523. X                    Convert to multi-byte continuous-tone X image.
  1524. X                  */
  1525. X                  bytes_per_pixel=ximage->bits_per_pixel >> 3;
  1526. X                  for (i=0; i < image->packets; i++)
  1527. X                  {
  1528. X                    pixel=XStandardPixel(map_info,(*p),8);
  1529. X                    for (k=bytes_per_pixel-1; k >= 0; k--)
  1530. X                    {
  1531. X                      channel[k]=(unsigned char) pixel;
  1532. X                      pixel>>=8;
  1533. X                    }
  1534. X                    for (j=0; j <= ((int) p->length); j++)
  1535. X                    {
  1536. X                      for (k=0; k < bytes_per_pixel; k++)
  1537. X                        *q++=channel[k];
  1538. X                      x++;
  1539. X                      if (x == ximage->width)
  1540. X                        {
  1541. X                          x=0;
  1542. X                          q+=scanline_pad;
  1543. X                        }
  1544. X                    }
  1545. X                    p++;
  1546. X                  }
  1547. X                }
  1548. X              break;
  1549. X            }
  1550. X          }
  1551. X        }
  1552. X    }
  1553. }
  1554. X
  1555. /*
  1556. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1557. %                                                                             %
  1558. %                                                                             %
  1559. %                                                                             %
  1560. %   X M a k e I n v i s i b l e C u r s o r                                   %
  1561. %                                                                             %
  1562. %                                                                             %
  1563. %                                                                             %
  1564. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1565. %
  1566. %  Function XMakeInvisibleCursor creates an invisible X11 cursor.
  1567. %
  1568. %  The format of the XMakeInvisibleCursor routine is:
  1569. %
  1570. %      XMakeInvisibleCursor(display,window)
  1571. %
  1572. %  A description of each parameter follows:
  1573. %
  1574. %    o display: Specifies a connection to an X server;  returned from
  1575. %      XOpenDisplay.
  1576. %
  1577. %    o window: Specifies the ID of the window for which the cursor is
  1578. %      assigned.
  1579. %
  1580. %
  1581. */
  1582. Cursor XMakeInvisibleCursor(display,window)
  1583. Display
  1584. X  *display;
  1585. X
  1586. Window
  1587. X  window;
  1588. {
  1589. X  Cursor
  1590. X    cursor;
  1591. X
  1592. X  Pixmap
  1593. X    pixmap;
  1594. X
  1595. X  XColor
  1596. X    color;
  1597. X
  1598. X  static char
  1599. X    bits[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
  1600. X
  1601. X  color.red=0;
  1602. X  color.green=0;
  1603. X  color.blue=0;
  1604. X  pixmap=XCreateBitmapFromData(display,window,bits,8,8);
  1605. X  cursor=XCreatePixmapCursor(display,pixmap,pixmap,&color,&color,0,0);
  1606. X  XFreePixmap(display,pixmap);
  1607. X  return(cursor);
  1608. }
  1609. X
  1610. /*
  1611. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1612. %                                                                             %
  1613. %                                                                             %
  1614. %                                                                             %
  1615. %   X M a k e P i x m a p                                                     %
  1616. %                                                                             %
  1617. %                                                                             %
  1618. %                                                                             %
  1619. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1620. %
  1621. %  Function XMakePixmap creates an X11 pixmap.
  1622. %
  1623. %  The format of the XMakePixmap routine is:
  1624. %
  1625. %      status=XMakePixmap(display,resource_info,window)
  1626. %
  1627. %  A description of each parameter follows:
  1628. %
  1629. %    o status: Function XMakePixmap returns True if the X pixmap is
  1630. %      successfully created.  False is returned is there is a memory shortage.
  1631. %
  1632. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  1633. %
  1634. %    o display: Specifies a connection to an X server; returned from
  1635. %      XOpenDisplay.
  1636. %
  1637. %    o window: Specifies a pointer to a XWindowInfo structure.
  1638. %
  1639. %
  1640. */
  1641. unsigned int XMakePixmap(display,resource_info,window)
  1642. Display
  1643. X  *display;
  1644. X
  1645. XXResourceInfo
  1646. X  *resource_info;
  1647. X
  1648. XXWindowInfo
  1649. X  *window;
  1650. {
  1651. X  if (window->ximage == (XImage *) NULL)
  1652. X    return(False);
  1653. X  /*
  1654. X    Display busy cursor.
  1655. X  */
  1656. X  XDefineCursor(display,window->id,window->busy_cursor);
  1657. X  XFlush(display);
  1658. X  /*
  1659. X    Create pixmap.
  1660. X  */
  1661. X  if (window->pixmap != (Pixmap) NULL)
  1662. X    XFreePixmap(display,window->pixmap);
  1663. X  window->pixmap=XCreatePixmap(display,window->id,(unsigned int)
  1664. X    window->ximage->width,(unsigned int) window->ximage->height,window->depth);
  1665. X  if (window->pixmap == (Pixmap) NULL)
  1666. X    {
  1667. X      /*
  1668. X        Unable to allocate pixmap.
  1669. X      */
  1670. X      XDefineCursor(display,window->id,window->cursor);
  1671. X      return(False);
  1672. X    }
  1673. X  /*
  1674. X    Copy X image to pixmap.
  1675. X  */
  1676. X  XPutImage(display,window->pixmap,window->graphic_context,window->ximage,
  1677. X    0,0,0,0,(unsigned int) window->ximage->width,
  1678. X    (unsigned int) window->ximage->height);
  1679. X  if (resource_info->debug)
  1680. X    {
  1681. X      (void) fprintf(stderr,"Pixmap:\n");
  1682. X      (void) fprintf(stderr,"  width, height: %dx%d\n",window->ximage->width,
  1683. X        window->ximage->height);
  1684. X    }
  1685. X  /*
  1686. X    Restore cursor.
  1687. X  */
  1688. X  XDefineCursor(display,window->id,window->cursor);
  1689. X  return(True);
  1690. }
  1691. X
  1692. /*
  1693. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1694. %                                                                             %
  1695. %                                                                             %
  1696. %                                                                             %
  1697. %   X M a k e S t a n d a r d C o l o r m a p                                 %
  1698. %                                                                             %
  1699. %                                                                             %
  1700. %                                                                             %
  1701. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  1702. %
  1703. %  Function XMakeStandardColormap creates an X11 Standard Colormap.
  1704. %
  1705. %  The format of the XMakeStandardColormap routine is:
  1706. %
  1707. %      XMakeStandardColormap(display,visual_info,resource_info,pixel_info,
  1708. %        image,map_info)
  1709. %
  1710. %  A description of each parameter follows:
  1711. %
  1712. %    o display: Specifies a connection to an X server; returned from
  1713. %      XOpenDisplay.
  1714. %
  1715. %    o visual_info: Specifies a pointer to a X11 XVisualInfo structure;
  1716. %      returned from XGetVisualInfo.
  1717. %
  1718. %    o resource_info: Specifies a pointer to a X11 XResourceInfo structure.
  1719. %
  1720. %    o pixel_info: Specifies a pointer to a XPixelInfo structure.
  1721. %
  1722. %    o image: Specifies a pointer to a Image structure;  returned from
  1723. %      ReadImage.
  1724. %
  1725. %    o map_info: If a Standard Colormap type is specified, this structure is
  1726. %      initialized with info from the Standard Colormap.
  1727. %
  1728. %
  1729. */
  1730. static int IntensityCompare(x,y)
  1731. const void
  1732. X  *x,
  1733. X  *y;
  1734. {
  1735. X  DiversityPacket
  1736. X    *color_1,
  1737. X    *color_2;
  1738. X  color_1=(DiversityPacket *) x;
  1739. X  color_2=(DiversityPacket *) y;
  1740. X  return((int) Intensity(*color_2)-(int) Intensity(*color_1));
  1741. }
  1742. X
  1743. static int PopularityCompare(x,y)
  1744. const void
  1745. X  *x,
  1746. X  *y;
  1747. {
  1748. X  DiversityPacket
  1749. X    *color_1,
  1750. X    *color_2;
  1751. X
  1752. X  color_1=(DiversityPacket *) x;
  1753. X  color_2=(DiversityPacket *) y;
  1754. X  return((int) color_2->count-(int) color_1->count);
  1755. }
  1756. X
  1757. void XMakeStandardColormap(display,visual_info,resource_info,pixel_info,image,
  1758. X  map_info)
  1759. Display
  1760. X  *display;
  1761. X
  1762. XXVisualInfo
  1763. X  *visual_info;
  1764. X
  1765. XXResourceInfo
  1766. X  *resource_info;
  1767. X
  1768. XXPixelInfo
  1769. X  *pixel_info;
  1770. X
  1771. Image
  1772. X  *image;
  1773. X
  1774. XXStandardColormap
  1775. X  *map_info;
  1776. {
  1777. X  Colormap
  1778. X    colormap;
  1779. X
  1780. X  int
  1781. X    status;
  1782. X
  1783. X  register int
  1784. X    i;
  1785. X
  1786. X  unsigned int
  1787. X    gray_value,
  1788. X    number_colors,
  1789. X    retain_colors;
  1790. X
  1791. X  XColor
  1792. X    color,
  1793. X    *colors,
  1794. X    *p;
  1795. X
  1796. X  if (resource_info->map_type != (char *) NULL)
  1797. X    {
  1798. X      /*
  1799. X        Standard Colormap is already defined (i.e. xstdcmap).
  1800. X      */
  1801. X      if (pixel_info->pixels != (unsigned long *) NULL)
  1802. X        (void) free((char *) pixel_info->pixels);
  1803. X      XGetPixelInfo(display,visual_info,map_info,resource_info,image,
  1804. X        pixel_info);
  1805. X      return;
  1806. X    }
  1807. X  if ((visual_info->class != DirectColor) && (visual_info->class != TrueColor))
  1808. X    if ((image->class == DirectClass) ||
  1809. X        (image->colors > visual_info->colormap_size))
  1810. X      {
  1811. X        /*
  1812. X          Image has more colors than the visual supports.
  1813. X        */
  1814. X        QuantizeImage(image,(unsigned int) visual_info->colormap_size,
  1815. X          resource_info->tree_depth,resource_info->dither,
  1816. X          resource_info->colorspace,False);
  1817. X        image->class=DirectClass;  /* promote to DirectClass */
  1818. X      }
  1819. X  /*
  1820. X    Free previous and create new colormap.
  1821. X  */
  1822. X  XFreeStandardColormap(display,visual_info,pixel_info,map_info);
  1823. X  colormap=XDefaultColormap(display,visual_info->screen);
  1824. X  if (visual_info->visual != XDefaultVisual(display,visual_info->screen))
  1825. X    colormap=XCreateColormap(display,
  1826. X      XRootWindow(display,visual_info->screen),visual_info->visual,
  1827. X      visual_info->class == DirectColor ? AllocAll : AllocNone);
  1828. X  if (colormap == (Colormap) NULL)
  1829. X    Error("unable to create colormap",(char *) NULL);
  1830. X  /*
  1831. X    Initialize the Standard Colormap attributes.
  1832. X  */
  1833. X  map_info->colormap=colormap;
  1834. X  map_info->red_max=visual_info->red_mask;
  1835. X  map_info->red_mult=map_info->red_max > 0 ? 1 : 0;
  1836. X  if (map_info->red_max > 0)
  1837. X    while ((map_info->red_max & 0x01) == 0)
  1838. X    {
  1839. X      map_info->red_max>>=1;
  1840. X      map_info->red_mult<<=1;
  1841. X    }
  1842. X  map_info->green_max=visual_info->green_mask;
  1843. X  map_info->green_mult=map_info->green_max > 0 ? 1 : 0;
  1844. X  if (map_info->green_max > 0)
  1845. X    while ((map_info->green_max & 0x01) == 0)
  1846. X    {
  1847. X      map_info->green_max>>=1;
  1848. X      map_info->green_mult<<=1;
  1849. X    }
  1850. X  map_info->blue_max=visual_info->blue_mask;
  1851. X  map_info->blue_mult=map_info->blue_max > 0 ? 1 : 0;
  1852. X  if (map_info->blue_max > 0)
  1853. X    while ((map_info->blue_max & 0x01) == 0)
  1854. X    {
  1855. X      map_info->blue_max>>=1;
  1856. X      map_info->blue_mult<<=1;
  1857. X    }
  1858. X  map_info->base_pixel=0;
  1859. X  XGetPixelInfo(display,visual_info,map_info,resource_info,image,pixel_info);
  1860. X  /*
  1861. X    Allocating colors in server colormap is based on visual class.
  1862. X  */
  1863. X  switch (visual_info->class)
  1864. X  {
  1865. X    case StaticGray:
  1866. X    case StaticColor:
  1867. X    {
  1868. X      /*
  1869. X        Define Standard Colormap for StaticGray or StaticColor visual.
  1870. X      */
  1871. X      number_colors=image->colors;
  1872. X      colors=(XColor *) malloc(visual_info->colormap_size*sizeof(XColor));
  1873. SHAR_EOF
  1874. true || echo 'restore of ImageMagick/X.c failed'
  1875. fi
  1876. echo 'End of  part 16'
  1877. echo 'File ImageMagick/X.c is continued in part 17'
  1878. echo 17 > _shar_seq_.tmp
  1879. exit 0
  1880. exit 0 # Just in case...
  1881.