Graphics.c 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these librararies and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /************************************<+>*************************************
  24. ****************************************************************************
  25. **
  26. ** File: Graphics.c
  27. **
  28. ** Project: CDE Help System
  29. **
  30. ** Description: This code processes a graphic (X11 bitmap, X11 pixmap,
  31. ** X11 XWD file, and TIFF). It will degrade graphics to
  32. ** match the type of monitor the image is displayed upon.
  33. **
  34. ** (c) Copyright 1987, 1988, 1989, 1990, 1991, 1992 Hewlett-Packard Company
  35. **
  36. ** (c) Copyright 1993, 1994 Hewlett-Packard Company
  37. ** (c) Copyright 1993, 1994 International Business Machines Corp.
  38. ** (c) Copyright 1993, 1994 Sun Microsystems, Inc.
  39. ** (c) Copyright 1993, 1994 Novell, Inc.
  40. **
  41. ****************************************************************************
  42. ************************************<+>*************************************/
  43. /******************************************************************************
  44. ** The GreyScale and Perform_Dither routine were take and modified from
  45. ** the g-job 'xgedit' created by Mark F. Cook. The following is the
  46. ** disclaimer from the dither.c source code
  47. ******************************************************************************
  48. ** Program: xgedit.c
  49. **
  50. ** Description: X11-based graphics editor
  51. **
  52. ** File: dither.c, containing the following
  53. ** subroutines/functions:
  54. ** Dither_Image()
  55. ** Perform_Dither()
  56. **
  57. ** Copyright 1988 by Mark F. Cook and Hewlett-Packard, Corvallis
  58. ** Workstation Operation. All rights reserved.
  59. **
  60. ** Permission to use, copy, and modify this software is granted, provided
  61. ** that this copyright appears in all copies and that both this copyright
  62. ** and permission notice appear in all supporting documentation, and that
  63. ** the name of Mark F. Cook and/or Hewlett-Packard not be used in advertising
  64. ** without specific, written prior permission. Neither Mark F. Cook or
  65. ** Hewlett-Packard make any representations about the suitibility of this
  66. ** software for any purpose. It is provided "as is" without express or
  67. ** implied warranty.
  68. **
  69. ******************************************************************************
  70. ** CHANGE LOG:
  71. **
  72. ** 1.00.00 - 1/11/89 - Start of Rev. number.
  73. **
  74. ** @@@ 4.05.00 - 12/07/89 - Created Dither_Image() and Perform_Dither() to
  75. ** @@@ allow conversion of color images into a dithered black &
  76. ** @@@ white image suitable for output on a laser printer.
  77. **
  78. ******************************************************************************/
  79. /*
  80. static char rcs_id[]="$XConsortium: Graphics.c /main/23 1996/12/06 11:12:54 cde-hp $";
  81. */
  82. #include <limits.h>
  83. #include <stdio.h>
  84. #include <stdlib.h>
  85. #include <string.h>
  86. #include <errno.h>
  87. #include <sys/stat.h>
  88. #include <X11/Xlib.h>
  89. #include <X11/XWDFile.h>
  90. #include <X11/ImUtil.h>
  91. #ifndef STUB
  92. #include "il.h"
  93. #include "ilfile.h"
  94. #include "ilX.h"
  95. #include "ilerrors.h"
  96. #endif
  97. #include <Xm/Xm.h>
  98. #include <Xm/XmPrivate.h>
  99. #include <Dt/Dts.h>
  100. #include <Dt/xpm.h>
  101. #include "bufioI.h"
  102. #include "Access.h"
  103. #include "AccessI.h"
  104. #include "GraphicsP.h"
  105. #include "GraphicsI.h"
  106. #include "StringFuncsI.h"
  107. #include "GifUtilsI.h"
  108. #include "XbmUtilsI.h"
  109. #include "JpegUtilsI.h"
  110. #include "Lock.h"
  111. /*****************************************************************************/
  112. /* Private data */
  113. /*****************************************************************************/
  114. #define GR_SUCCESS 0 /* everything okay */
  115. #define GR_DRAWABLE_ERR 1 /* couldn't get drawable attr/geom */
  116. #define GR_FILE_ERR 2 /* file open/read/write problem */
  117. #define GR_REQUEST_ERR 3 /* bad placement or size */
  118. #define GR_ALLOC_ERR 4 /* memory allocation failure */
  119. #define GR_HEADER_ERR 5 /* file header version/size problem */
  120. #define MAX_GREY_COLORS 8
  121. #define RES_TOLERANCE 25 /* resolution scaling tolerance */
  122. static short GreyAllocated = False;
  123. static Pixel GreyScalePixels[MAX_GREY_COLORS];
  124. static char *GreyScaleColors[MAX_GREY_COLORS] =
  125. {
  126. "#212121212121",
  127. "#424242424242",
  128. "#636363636363",
  129. "#737373737373",
  130. "#949494949494",
  131. "#adadadadadad",
  132. "#bdbdbdbdbdbd",
  133. "#dededededede",
  134. };
  135. #define Missing_bm_width 75
  136. #define Missing_bm_height 47
  137. static unsigned char Missing_bm_bits[] = {
  138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  139. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  140. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff,
  141. 0xff, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  142. 0xff, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00,
  143. 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00,
  144. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00,
  145. 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x04, 0x01, 0x00, 0x00, 0x00,
  146. 0x00, 0x00, 0xc0, 0x00, 0x18, 0x04, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
  147. 0xc0, 0x00, 0x18, 0x8c, 0x21, 0x00, 0x00, 0x20, 0x00, 0x00, 0xc0, 0x00,
  148. 0x18, 0x54, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x24,
  149. 0x31, 0xf0, 0xf0, 0x30, 0xe8, 0xf0, 0xc1, 0x00, 0x18, 0x04, 0x21, 0x08,
  150. 0x08, 0x20, 0x18, 0x09, 0xc1, 0x00, 0x18, 0x04, 0x21, 0xf0, 0xf0, 0x20,
  151. 0x08, 0x09, 0xc1, 0x00, 0x18, 0x04, 0x21, 0x00, 0x01, 0x21, 0x08, 0x09,
  152. 0xc1, 0x00, 0x18, 0x04, 0x21, 0x08, 0x09, 0x21, 0x08, 0x09, 0xc1, 0x00,
  153. 0x18, 0x04, 0x71, 0xf0, 0xf0, 0x70, 0x08, 0xf1, 0xc1, 0x00, 0x18, 0x00,
  154. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc1, 0x00, 0x18, 0x00, 0x00, 0x00,
  155. 0x00, 0x00, 0x00, 0x00, 0xc1, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
  156. 0x00, 0xf0, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  157. 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00,
  158. 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0xf0,
  159. 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x08, 0x01, 0x00,
  160. 0x00, 0x08, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x08, 0x00, 0x00, 0x00, 0x08,
  161. 0x20, 0x00, 0xc0, 0x00, 0x18, 0x08, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
  162. 0xc0, 0x00, 0x18, 0x08, 0xd8, 0xf0, 0xe8, 0xf8, 0x30, 0xf0, 0xc0, 0x00,
  163. 0x18, 0xc8, 0x31, 0x01, 0x19, 0x09, 0x21, 0x08, 0xc1, 0x00, 0x18, 0x08,
  164. 0x11, 0xf0, 0x09, 0x09, 0x21, 0x08, 0xc0, 0x00, 0x18, 0x08, 0x11, 0x08,
  165. 0x09, 0x09, 0x21, 0x08, 0xc0, 0x00, 0x18, 0x08, 0x11, 0x08, 0x09, 0x09,
  166. 0x21, 0x08, 0xc1, 0x00, 0x18, 0xf0, 0x10, 0xf0, 0xf9, 0x08, 0x71, 0xf0,
  167. 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0xc0, 0x00,
  168. 0x18, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00,
  169. 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00,
  170. 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00,
  171. 0x00, 0x00, 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  172. 0xc0, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00,
  173. 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xf8, 0xff,
  174. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
  175. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  176. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  177. 0x00, 0x00};
  178. /* Used to hold image type converter registry info */
  179. typedef struct {
  180. char *image_type;
  181. _DtGrLoadProc convert_proc;
  182. _DtGrDestroyContextProc destroy_context_proc;
  183. } _DtGrRegistryRec;
  184. /*
  185. * This array maps image file name extensions to the corresponding
  186. * CDE data type. It is used by the DetermineImageType function.
  187. * The array must be NULL-terminated.
  188. */
  189. static char * img_extensions[] = {
  190. "xpm", "PM",
  191. "pm", "PM",
  192. "gif", "GIF",
  193. "jpeg", "JPEG",
  194. "jpg", "JPEG",
  195. "xbm", "BM",
  196. "bm", "BM",
  197. "tiff", "TIFF",
  198. "tif", "TIFF",
  199. "xwd", "XWD",
  200. "cgm", "CGM",
  201. NULL
  202. };
  203. /*****************************************************************************/
  204. /* Private declarations */
  205. /*****************************************************************************/
  206. static int Do_Direct(
  207. Display *dpy,
  208. int screen,
  209. XWDFileHeader *header,
  210. Colormap colormap,
  211. int ncolors,
  212. XColor *colors,
  213. enum _DtGrColorModel force,
  214. XImage *in_image,
  215. XImage *out_image,
  216. unsigned long **ret_colors,
  217. int *ret_number );
  218. static int Do_Pseudo(
  219. Display *dpy,
  220. int screen,
  221. Colormap colormap,
  222. int ncolors,
  223. XColor *colors,
  224. enum _DtGrColorModel force,
  225. XImage *in_image,
  226. XImage *out_image,
  227. unsigned long **ret_colors,
  228. int *ret_number );
  229. static int GreyScale (
  230. Display *dpy,
  231. int screen,
  232. Colormap cmap,
  233. XImage *in_image,
  234. XImage *out_image,
  235. XColor *colors,
  236. int ncolors,
  237. enum _DtGrColorModel force,
  238. int rshift,
  239. int gshift,
  240. int bshift,
  241. Pixel rmask,
  242. Pixel gmask,
  243. Pixel bmask );
  244. static unsigned int Image_Size(
  245. XImage *image );
  246. static void Perform_Dither(
  247. Display *dpy,
  248. int screen,
  249. XImage *image,
  250. int *greyscale );
  251. static enum _DtGrLoadStatus processBitmap(
  252. _DtGrStream *image,
  253. Screen *screen,
  254. int depth,
  255. Colormap colormap,
  256. Visual *visual,
  257. Pixel foreground,
  258. Pixel background,
  259. GC gc,
  260. enum _DtGrColorModel color_model,
  261. Boolean allow_reduced_colors,
  262. Dimension *in_out_width,
  263. Dimension *in_out_height,
  264. unsigned short media_resolution,
  265. Pixmap *ret_pixmap,
  266. Pixmap *ret_mask,
  267. Pixel **ret_colors,
  268. int *ret_num_colors,
  269. _DtGrContext *context);
  270. #ifndef STUB
  271. static enum _DtGrLoadStatus processTiff(
  272. _DtGrStream *stream,
  273. Screen *screen,
  274. int depth,
  275. Colormap colormap,
  276. Visual *visual,
  277. Pixel foreground,
  278. Pixel background,
  279. GC gc,
  280. enum _DtGrColorModel color_model,
  281. Boolean allow_reduced_colors,
  282. Dimension *in_out_width,
  283. Dimension *in_out_height,
  284. unsigned short media_resolution,
  285. Pixmap *ret_pixmap,
  286. Pixmap *ret_mask,
  287. Pixel **ret_colors,
  288. int *ret_num_colors,
  289. _DtGrContext *context);
  290. static void destroyTiffContext(
  291. _DtGrContext *context);
  292. #endif
  293. static enum _DtGrLoadStatus processXwd(
  294. _DtGrStream *stream,
  295. Screen *screen,
  296. int depth,
  297. Colormap colormap,
  298. Visual *visual,
  299. Pixel foreground,
  300. Pixel background,
  301. GC gc,
  302. enum _DtGrColorModel color_model,
  303. Boolean allow_reduced_colors,
  304. Dimension *in_out_width,
  305. Dimension *in_out_height,
  306. unsigned short media_resolution,
  307. Pixmap *ret_pixmap,
  308. Pixmap *ret_mask,
  309. Pixel **ret_colors,
  310. int *ret_num_colors,
  311. _DtGrContext *context);
  312. static void _swaplong (
  313. register char *bp,
  314. register unsigned n );
  315. static void _swapshort (
  316. register char *bp,
  317. register unsigned n );
  318. static int XwdFileToPixmap (
  319. Display *dpy,
  320. int screen,
  321. int depth,
  322. Pixmap pixmap,
  323. Colormap colormap,
  324. Visual *visual,
  325. GC gc,
  326. enum _DtGrColorModel color_model,
  327. int src_x,
  328. int src_y,
  329. int dst_x,
  330. int dst_y,
  331. int width,
  332. int height,
  333. _DtGrStream *stream,
  334. unsigned long **ret_colors,
  335. int *ret_number );
  336. static enum _DtGrLoadStatus processXpm(
  337. _DtGrStream *stream,
  338. Screen *screen,
  339. int depth,
  340. Colormap colormap,
  341. Visual *visual,
  342. Pixel foreground,
  343. Pixel background,
  344. GC gc,
  345. enum _DtGrColorModel color_model,
  346. Boolean allow_reduced_colors,
  347. Dimension *in_out_width,
  348. Dimension *in_out_height,
  349. unsigned short media_resolution,
  350. Pixmap *ret_pixmap,
  351. Pixmap *ret_mask,
  352. Pixel **ret_colors,
  353. int *ret_num_colors,
  354. _DtGrContext *context);
  355. static enum _DtGrLoadStatus processGIF(
  356. _DtGrStream *image,
  357. Screen *screen,
  358. int depth,
  359. Colormap colormap,
  360. Visual *visual,
  361. Pixel foreground,
  362. Pixel background,
  363. GC gc,
  364. enum _DtGrColorModel color_model,
  365. Boolean allow_reduced_colors,
  366. Dimension *in_out_width,
  367. Dimension *in_out_height,
  368. unsigned short media_resolution,
  369. Pixmap *ret_pixmap,
  370. Pixmap *ret_mask,
  371. Pixel **ret_colors,
  372. int *ret_num_colors,
  373. _DtGrContext *context);
  374. static enum _DtGrLoadStatus processJPEG(
  375. _DtGrStream *image,
  376. Screen *screen,
  377. int depth,
  378. Colormap colormap,
  379. Visual *visual,
  380. Pixel foreground,
  381. Pixel background,
  382. GC gc,
  383. enum _DtGrColorModel color_model,
  384. Boolean allow_reduced_colors,
  385. Dimension *in_out_width,
  386. Dimension *in_out_height,
  387. unsigned short media_resolution,
  388. Pixmap *ret_pixmap,
  389. Pixmap *ret_mask,
  390. Pixel **ret_colors,
  391. int *ret_num_colors,
  392. _DtGrContext *context);
  393. static enum _DtGrLoadStatus DetermineImageType(
  394. _DtGrStream *stream,
  395. char *image_type);
  396. static enum _DtGrLoadStatus GetConverterAndDestructor(
  397. char *image_type,
  398. _DtGrLoadProc *converter,
  399. _DtGrDestroyContextProc *destructor);
  400. static float ComputeRatio(int media_res, int image_res);
  401. /*****************************************************************************
  402. * Image type registry declarations
  403. *****************************************************************************/
  404. /* Registry of converters and destructors for default image types */
  405. static _DtGrRegistryRec registry[] =
  406. {
  407. {
  408. "PM",
  409. processXpm,
  410. NULL
  411. },
  412. {
  413. "BM",
  414. processBitmap,
  415. NULL
  416. },
  417. {
  418. "GIF",
  419. processGIF,
  420. NULL
  421. },
  422. {
  423. "JPEG",
  424. processJPEG,
  425. NULL
  426. },
  427. {
  428. "TIFF",
  429. processTiff,
  430. destroyTiffContext
  431. },
  432. {
  433. "XWD",
  434. processXwd,
  435. NULL
  436. },
  437. };
  438. static int registry_count = XtNumber(registry);
  439. /* Registry of converters and destructors for non-default image types */
  440. static _DtGrRegistryRec *new_registry = NULL;
  441. static int new_registry_count = 0;
  442. /*****************************************************************************
  443. * Private Routines
  444. *****************************************************************************/
  445. /*****************************************************************************
  446. * taken straight out of xwud and modified.
  447. *****************************************************************************/
  448. /* Copyright 1985, 1986, 1988 Massachusetts Institute of Technology */
  449. static void
  450. _swapshort (
  451. register char *bp,
  452. register unsigned n )
  453. {
  454. register char c;
  455. register char *ep = bp + n;
  456. while (bp < ep) {
  457. c = *bp;
  458. *bp = *(bp + 1);
  459. bp++;
  460. *bp++ = c;
  461. }
  462. }
  463. static void
  464. _swaplong (
  465. register char *bp,
  466. register unsigned n )
  467. {
  468. register char c;
  469. register char *ep = bp + n;
  470. register char *sp;
  471. while (bp < ep) {
  472. sp = bp + 3;
  473. c = *sp;
  474. *sp = *bp;
  475. *bp++ = c;
  476. sp = bp + 1;
  477. c = *sp;
  478. *sp = *bp;
  479. *bp++ = c;
  480. bp += 2;
  481. }
  482. }
  483. static float
  484. ComputeRatio(int media_res, int image_res)
  485. {
  486. float ratio;
  487. int iratio, dx;
  488. if (media_res >= image_res) {
  489. ratio = (float)media_res / (float)image_res;
  490. if (media_res % image_res == 0)
  491. return ratio;
  492. iratio = ratio + 0.5;
  493. dx = media_res - image_res * iratio;
  494. if (dx >= -RES_TOLERANCE && dx <= RES_TOLERANCE)
  495. ratio = (float)iratio;
  496. } else {
  497. ratio = (float)image_res / (float)media_res;
  498. if (image_res % media_res == 0)
  499. return 1.0 / ratio;
  500. iratio = ratio + 0.5;
  501. dx = image_res - media_res * iratio;
  502. if (dx >= -RES_TOLERANCE && dx <= RES_TOLERANCE)
  503. ratio = (float)iratio;
  504. ratio = 1.0 / ratio;
  505. }
  506. return ratio;
  507. }
  508. /*****************************************************************************
  509. * Function: GreyScale
  510. *
  511. * Turn a color image into a 8 grey color image
  512. * If it can't create a grey scale image, GreyScale will call
  513. * Perform_Dither to create a bi-tonal image.
  514. *
  515. *****************************************************************************
  516. * The GreyScale code was ported from xgedit and changed to fit our needs
  517. *****************************************************************************/
  518. static int
  519. GreyScale (
  520. Display *dpy,
  521. int screen,
  522. Colormap cmap,
  523. XImage *in_image,
  524. XImage *out_image,
  525. XColor *colors,
  526. int ncolors,
  527. enum _DtGrColorModel force,
  528. int rshift,
  529. int gshift,
  530. int bshift,
  531. Pixel rmask,
  532. Pixel gmask,
  533. Pixel bmask )
  534. {
  535. int i, j, x;
  536. int inc;
  537. int count;
  538. int width = in_image->width;
  539. int height = in_image->height;
  540. int value;
  541. int *grey_scale;
  542. Pixel valueArray[256];
  543. Pixel n;
  544. XColor ret_color;
  545. /*--- allocate an array big enough to map each pixel in the image ---*/
  546. /*--- into a corresponding greyscale value (in the range [0-255]). ---*/
  547. grey_scale = (int *) calloc(width*height, sizeof(int));
  548. if (grey_scale == NULL) {
  549. return GR_ALLOC_ERR;
  550. }
  551. /*--- < ESTABLISH THE GREYSCALE IMAGE > ---*/
  552. /*--- The NTSC formula for converting an RGB value into the ---*/
  553. /*--- corresponding greyscale value is: ---*/
  554. /*--- luminosity = .299 red + .587 green + .114 blue ---*/
  555. /*
  556. * zero the flag array
  557. */
  558. for (i = 0; i < 256; i++)
  559. valueArray [i] = 0;
  560. for (j=0, x = 0; j<height; j++)
  561. for (i=0; i<width; i++, x++) {
  562. n = XGetPixel(in_image, i, j);
  563. if (rshift)
  564. {
  565. ret_color.red = (n >> rshift) & rmask;
  566. ret_color.green = (n >> gshift) & gmask;
  567. ret_color.blue = (n >> bshift) & bmask;
  568. if (ncolors)
  569. {
  570. ret_color.red = colors[ret_color.red ].red;
  571. ret_color.green = colors[ret_color.green].green;
  572. ret_color.blue = colors[ret_color.blue ].blue;
  573. }
  574. else
  575. {
  576. ret_color.red = (((Pixel) ret_color.red ) * 65535) / rmask;
  577. ret_color.green = (((Pixel) ret_color.green) * 65535) / gmask;
  578. ret_color.blue = (((Pixel) ret_color.blue ) * 65535) / bmask;
  579. }
  580. value = (((int)(ret_color.red*299) + (int)(ret_color.green*587) +
  581. (int)(ret_color.blue*114)) / 1000) >> 8;
  582. }
  583. else
  584. value = (((int)(colors[n].red*299) + (int)(colors[n].green*587) +
  585. (int)(colors[n].blue*114)) / 1000) >> 8;
  586. grey_scale[x] = value;
  587. valueArray[value]++;
  588. } /* for(i...) */
  589. /*
  590. * Find out if we can/have allocate the pre-defined grey colors.
  591. */
  592. _DtHelpProcessLock();
  593. if (!GreyAllocated && force != _DtGrBITONAL)
  594. {
  595. for (i = 0; !GreyAllocated && i < MAX_GREY_COLORS; i++)
  596. {
  597. if (!XParseColor (dpy, cmap, GreyScaleColors[i], &ret_color) ||
  598. !XAllocColor (dpy, cmap, &ret_color))
  599. {
  600. /*
  601. * Have a problem allocating one of the pre-defined
  602. * grey colors. Free the already allocated pixels
  603. * and set the flag.
  604. */
  605. if (i)
  606. XFreeColors (dpy, cmap, GreyScalePixels, i, 0);
  607. GreyAllocated = -1;
  608. }
  609. else
  610. GreyScalePixels[i] = ret_color.pixel;
  611. }
  612. if (!GreyAllocated)
  613. GreyAllocated = True;
  614. }
  615. /*
  616. * Had a problem allocating the pre-defined grey colors.
  617. * Try to dither into black and white.
  618. */
  619. if (force == _DtGrBITONAL || GreyAllocated == -1)
  620. {
  621. Perform_Dither (dpy, screen, out_image, grey_scale);
  622. free(grey_scale);
  623. _DtHelpProcessUnlock();
  624. return GR_SUCCESS;
  625. }
  626. _DtHelpProcessUnlock();
  627. /*
  628. * Find out how many grey scale colors there are.
  629. */
  630. for (count = 0, i = 0; i < 256; i++)
  631. if (valueArray[i])
  632. count++;
  633. /*
  634. * If we have less than maximum grey colors we want to spread the load
  635. * between the colors
  636. */
  637. if (count < MAX_GREY_COLORS)
  638. inc = count;
  639. else
  640. inc = MAX_GREY_COLORS;
  641. /*
  642. * This is rather esoteric code.
  643. * The line
  644. * valueArray[n] = GreyScalePixels[i * MAX_GREY_COLORS / inc]
  645. *
  646. * causes the colors to be evenly distributed if the total number
  647. * of calculated grey shades is less than the number of pre-defined
  648. * grey colors.
  649. *
  650. * The if stmt
  651. * if (j >= count / (MAX_GREY_COLORS - i))
  652. *
  653. * uniformly reduces the calculated grey shades into the pre-defined
  654. * grey colors, if the total number of grey shades is greater than
  655. * the number of pre-defined grey colors.
  656. *
  657. * If after reading the following code, the reader still doesn't
  658. * understand it, pick a couple of numbers between 1-255 for 'count'
  659. * and walk each of them thru the 'for' loop. Hopefully it will help.
  660. * (Suggestion - pick a large number for one value and pick
  661. * a number less than 8 for the other value).
  662. */
  663. for (i = 0, n = 0, j = 0; n < 256 && count && i < MAX_GREY_COLORS; n++)
  664. {
  665. if (valueArray[n])
  666. {
  667. _DtHelpProcessLock();
  668. valueArray[n] = GreyScalePixels[i * MAX_GREY_COLORS / inc];
  669. _DtHelpProcessUnlock();
  670. j++;
  671. if (j >= count / (MAX_GREY_COLORS - i))
  672. {
  673. count -= j;
  674. i++;
  675. j = 0;
  676. }
  677. }
  678. }
  679. /*
  680. * Now replace the image pixels with the grey pixels
  681. */
  682. for (j = 0, x = 0; j < height; j++)
  683. for (i = 0; i < width; i++, x++) {
  684. XPutPixel (out_image, i, j, valueArray[grey_scale[x]]);
  685. } /* for(i...) */
  686. free(grey_scale);
  687. return GR_SUCCESS;
  688. }
  689. /***************************************************************************
  690. * *
  691. * Routine: Perform_Dither *
  692. * *
  693. * Purpose: Given a color XImage and a greyscale representation of it, *
  694. * for each pixel in the image, determine whether it should be *
  695. * coverted to Black or White base on the weighted average of *
  696. * the greyscale value of it and the pixels surrounding it. *
  697. * Be sure to do bounds checking for pixels that are at the *
  698. * edge of the image. *
  699. * *
  700. * The dithering is done using the Floyd-Steinberg error *
  701. * diffusion algorithm, which incorporates a Stucki error *
  702. * filter (specifics can be found in Chapter 8, "Dithering *
  703. * with Blue Noise", of the book "Digital Halftoning" by *
  704. * Robert Ulichney (MIT Press, 1988). *
  705. * *
  706. *X11***********************************************************************/
  707. static void
  708. Perform_Dither(
  709. Display *dpy,
  710. int screen,
  711. XImage *image,
  712. int *greyscale )
  713. {
  714. int i, j, width, height;
  715. int result, error, max_lum;
  716. Pixel blackPixel = XBlackPixel (dpy, screen);
  717. Pixel whitePixel = XWhitePixel (dpy, screen);
  718. width = image->width;
  719. height = image->height;
  720. max_lum = 256;
  721. for (j=0; j<height; j++) /* rows */
  722. for (i=0; i<width; i++) { /* columns */
  723. if (greyscale[(j*width)+i] < (max_lum/2))
  724. result = 0;
  725. else
  726. result = max_lum - 1;
  727. error = greyscale[(j*width)+i] - result;
  728. if (i+1 < width) /*--- G [j] [i+1] ---*/
  729. greyscale[(j*width)+i+1] += (error * 8)/42;
  730. if (i+2 < width) /*--- G [j] [i+2] ---*/
  731. greyscale[(j*width)+i+2] += (error * 4)/42;
  732. if (j+1 < height) {
  733. if (i-2 >= 0) /*--- G [j+1] [i-2] ---*/
  734. greyscale[((j+1)*width)+i-2] += (error * 2)/42;
  735. if (i-1 >= 0) /*--- G [j+1] [i-1] ---*/
  736. greyscale[((j+1)*width)+i-1] += (error * 4)/42;
  737. /*--- G [j+1] [i] ---*/
  738. greyscale[((j+1)*width)+i] += (error * 8)/42;
  739. if (i+1 < width) /*--- G [j+1] [i+1] ---*/
  740. greyscale[((j+1)*width)+i+1] += (error * 4)/42;
  741. if (i+2 < width) /*--- G [j+1] [i+2] ---*/
  742. greyscale[((j+1)*width)+i+2] += (error * 2)/42;
  743. }
  744. if (j+2 < height) {
  745. if (i-2 >= 0) /*--- G [j+2] [i-2] ---*/
  746. greyscale[((j+2)*width)+i-2] += error/42;
  747. if (i-1 >= 0) /*--- G [j+2] [i-1] ---*/
  748. greyscale[((j+2)*width)+i-1] += (error * 2)/42;
  749. /*--- G [j+2] [i] ---*/
  750. greyscale[((j+2)*width)+i] += (error * 4)/42;
  751. if (i+1 < width) /*--- G [j+2] [i+1] ---*/
  752. greyscale[((j+2)*width)+i+1] += (error * 2)/42;
  753. if (i+2 < width) /*--- G [j+2] [i+2] ---*/
  754. greyscale[((j+2)*width)+i+2] += error/42;
  755. }
  756. if (result)
  757. XPutPixel(image, i, j, whitePixel);
  758. else
  759. XPutPixel(image, i, j, blackPixel);
  760. } /* for(i...) */
  761. }
  762. static int
  763. Do_Pseudo(
  764. Display *dpy,
  765. int screen,
  766. Colormap colormap,
  767. int ncolors,
  768. XColor *colors,
  769. enum _DtGrColorModel force,
  770. XImage *in_image,
  771. XImage *out_image,
  772. unsigned long **ret_colors,
  773. int *ret_number )
  774. {
  775. register int i, x, y, colorCount = 0;
  776. register XColor *color;
  777. int result = 0;
  778. Pixel pixel;
  779. for (i = 0; i < ncolors; i++)
  780. colors[i].flags = 0;
  781. *ret_colors = NULL;
  782. *ret_number = 0;
  783. /*
  784. * beware 'result'.
  785. * It is set to one upon entering this routine.
  786. * The only way it can be modified is by the call to XAllocColor.
  787. */
  788. if (force == _DtGrCOLOR)
  789. result = 1;
  790. for (y = 0; result && y < in_image->height; y++)
  791. {
  792. for (x = 0; result && x < in_image->width; x++)
  793. {
  794. pixel = XGetPixel(in_image, x, y);
  795. color = &colors[pixel];
  796. if (!color->flags)
  797. {
  798. color->flags = DoRed | DoGreen | DoBlue;
  799. result = XAllocColor(dpy, colormap, color);
  800. if (!result)
  801. color->flags = 0;
  802. else
  803. colorCount++;
  804. }
  805. if (result)
  806. XPutPixel(out_image, x, y, color->pixel);
  807. }
  808. }
  809. /*
  810. * If result == 0, a call to XAllocColor failed
  811. * Try to grey scale the image.
  812. */
  813. if (!result)
  814. {
  815. if (colorCount)
  816. {
  817. for (i = 0; i < ncolors; i++)
  818. {
  819. if (colors[i].flags)
  820. {
  821. XFreeColors (dpy, colormap, &(colors[i].pixel), 1, 0);
  822. colors[i].flags = 0;
  823. }
  824. }
  825. }
  826. result = GreyScale (dpy, screen, colormap, in_image, out_image, colors,
  827. ncolors, force, 0, 0, 0, 0, 0, 0);
  828. }
  829. else if (colorCount)
  830. {
  831. result = GR_SUCCESS;
  832. *ret_colors = (unsigned long *) malloc (
  833. sizeof (unsigned long) * colorCount);
  834. if (*ret_colors == NULL)
  835. {
  836. colorCount = 0;
  837. result = GR_ALLOC_ERR;
  838. }
  839. for (i = 0, x = 0; i < ncolors && x < colorCount; i++)
  840. if (colors[i].flags)
  841. (*ret_colors)[x++] = colors[i].pixel;
  842. *ret_number = colorCount;
  843. }
  844. /*
  845. * result was set to a XHPIF value via the 'else' stmt or
  846. * returned from GreyScale routine.
  847. */
  848. return result;
  849. }
  850. static int
  851. Do_Direct(
  852. Display *dpy,
  853. int screen,
  854. XWDFileHeader *header,
  855. Colormap colormap,
  856. int ncolors,
  857. XColor *colors,
  858. enum _DtGrColorModel force,
  859. XImage *in_image,
  860. XImage *out_image,
  861. unsigned long **ret_colors,
  862. int *ret_number )
  863. {
  864. register int x, y;
  865. XColor color;
  866. unsigned long rmask, gmask, bmask;
  867. int rshift = 0, gshift = 0, bshift = 0;
  868. int i;
  869. int result;
  870. int pixMax = 256;
  871. int pixI = 0;
  872. Pixel pix;
  873. Pixel *oldPixels;
  874. Pixel *newPixels;
  875. oldPixels = (Pixel *) malloc (sizeof (Pixel) * pixMax);
  876. newPixels = (Pixel *) malloc (sizeof (Pixel) * pixMax);
  877. if (oldPixels == NULL || newPixels == NULL)
  878. {
  879. if (oldPixels)
  880. free (oldPixels);
  881. if (newPixels)
  882. free (newPixels);
  883. return GR_ALLOC_ERR;
  884. }
  885. rmask = header->red_mask;
  886. while (!(rmask & 1)) {
  887. rmask >>= 1;
  888. rshift++;
  889. }
  890. gmask = header->green_mask;
  891. while (!(gmask & 1)) {
  892. gmask >>= 1;
  893. gshift++;
  894. }
  895. bmask = header->blue_mask;
  896. while (!(bmask & 1)) {
  897. bmask >>= 1;
  898. bshift++;
  899. }
  900. if (in_image->depth <= 12)
  901. pix = 1 << in_image->depth;
  902. if (force == _DtGrCOLOR)
  903. color.flags = DoRed | DoGreen | DoBlue;
  904. else
  905. color.flags = 0;
  906. for (y = 0; color.flags && y < in_image->height; y++)
  907. {
  908. for (x = 0; color.flags && x < in_image->width; x++)
  909. {
  910. pix = XGetPixel(in_image, x, y);
  911. i = 0;
  912. while (i < pixI && oldPixels[i] != pix)
  913. i++;
  914. if (i == pixI)
  915. {
  916. color.red = (pix >> rshift) & rmask;
  917. color.green = (pix >> gshift) & gmask;
  918. color.blue = (pix >> bshift) & bmask;
  919. if (ncolors) {
  920. color.red = colors[color.red].red;
  921. color.green = colors[color.green].green;
  922. color.blue = colors[color.blue].blue;
  923. } else {
  924. color.red = ((unsigned long)color.red * 65535) / rmask;
  925. color.green = ((unsigned long)color.green * 65535) / gmask;
  926. color.blue = ((unsigned long)color.blue * 65535) / bmask;
  927. }
  928. if (!XAllocColor(dpy, colormap, &color))
  929. color.flags = 0;
  930. else
  931. {
  932. if (pixI >= pixMax)
  933. {
  934. pixMax += 128;
  935. oldPixels = (Pixel *) realloc ((void *) oldPixels,
  936. (sizeof (Pixel) * pixMax));
  937. newPixels = (Pixel *) realloc ((void *) newPixels,
  938. (sizeof (Pixel) * pixMax));
  939. /*
  940. * check the realloc
  941. */
  942. if (oldPixels == NULL || newPixels == NULL)
  943. {
  944. if (oldPixels)
  945. free (oldPixels);
  946. if (newPixels)
  947. {
  948. XFreeColors(dpy, colormap, newPixels, pixI, 0);
  949. free (newPixels);
  950. }
  951. return GR_ALLOC_ERR;
  952. }
  953. }
  954. oldPixels[pixI] = pix;
  955. newPixels[pixI++] = color.pixel;
  956. }
  957. }
  958. if (color.flags)
  959. XPutPixel(out_image, x, y, newPixels[i]);
  960. }
  961. }
  962. if (color.flags)
  963. {
  964. result = GR_SUCCESS;
  965. if (pixI < pixMax)
  966. {
  967. newPixels = (Pixel *) realloc ((void *) newPixels,
  968. (sizeof (Pixel) * pixI));
  969. if (newPixels == NULL)
  970. result = GR_ALLOC_ERR;
  971. }
  972. free (oldPixels);
  973. *ret_colors = newPixels;
  974. *ret_number = pixI;
  975. }
  976. else
  977. {
  978. if (pixI)
  979. XFreeColors (dpy, colormap, newPixels, pixI, 0);
  980. free (oldPixels);
  981. free (newPixels);
  982. result = GreyScale(dpy, screen, colormap, in_image, out_image, colors,
  983. ncolors, force, rshift, gshift, bshift, rmask, gmask, bmask);
  984. }
  985. return result;
  986. }
  987. static unsigned int
  988. Image_Size(
  989. XImage *image )
  990. {
  991. if (image->format != ZPixmap)
  992. return(image->bytes_per_line * image->height * image->depth);
  993. return((unsigned)image->bytes_per_line * image->height);
  994. }
  995. static int
  996. XwdFileToPixmap (
  997. Display *dpy,
  998. int screen,
  999. int depth,
  1000. Pixmap pixmap,
  1001. Colormap colormap,
  1002. Visual *visual,
  1003. GC gc,
  1004. enum _DtGrColorModel color_model,
  1005. int src_x,
  1006. int src_y,
  1007. int dst_x,
  1008. int dst_y,
  1009. int width,
  1010. int height,
  1011. _DtGrStream *stream,
  1012. unsigned long **ret_colors,
  1013. int *ret_number )
  1014. {
  1015. int result;
  1016. register int i;
  1017. XImage in_image, *out_image;
  1018. register char *buffer;
  1019. unsigned long swaptest = 1;
  1020. int count;
  1021. unsigned buffer_size;
  1022. int ncolors;
  1023. Bool rawbits = False;
  1024. XColor *colors = NULL;
  1025. #ifdef __alpha
  1026. /* Use a different structure for compatibility with 32-bit platform */
  1027. XWDColor xwd_color;
  1028. #endif /* __alpha */
  1029. XWDFileHeader header;
  1030. /* Reset the pointer to the beginning of the stream */
  1031. _DtGrSeek(stream, 0, SEEK_SET);
  1032. /*
  1033. * Read in header information.
  1034. */
  1035. if(_DtGrRead((char *)&header, sizeof(header), 1, stream) != 1)
  1036. {
  1037. return GR_HEADER_ERR;
  1038. }
  1039. if (*(char *) &swaptest)
  1040. _swaplong((char *) &header, sizeof(header));
  1041. /* check to see if the dump file is in the proper format */
  1042. if (header.file_version != XWD_FILE_VERSION)
  1043. {
  1044. return GR_HEADER_ERR;
  1045. }
  1046. if (header.header_size < sizeof(header))
  1047. {
  1048. return GR_HEADER_ERR;
  1049. }
  1050. /*
  1051. * skip the window name
  1052. */
  1053. if (_DtGrSeek(stream, (header.header_size - sizeof(header)), 1))
  1054. {
  1055. return GR_FILE_ERR;
  1056. }
  1057. /*
  1058. * initialize the input image
  1059. */
  1060. in_image.width = (int) header.pixmap_width;
  1061. in_image.height = (int) header.pixmap_height;
  1062. in_image.xoffset = (int) header.xoffset;
  1063. in_image.format = (int) header.pixmap_format;
  1064. in_image.byte_order = (int) header.byte_order;
  1065. in_image.bitmap_unit = (int) header.bitmap_unit;
  1066. in_image.bitmap_bit_order = (int) header.bitmap_bit_order;
  1067. in_image.bitmap_pad = (int) header.bitmap_pad;
  1068. in_image.depth = (int) header.pixmap_depth;
  1069. in_image.bits_per_pixel = (int) header.bits_per_pixel;
  1070. in_image.bytes_per_line = (int) header.bytes_per_line;
  1071. in_image.red_mask = header.red_mask;
  1072. in_image.green_mask = header.green_mask;
  1073. in_image.blue_mask = header.blue_mask;
  1074. in_image.obdata = NULL;
  1075. _XInitImageFuncPtrs(&in_image);
  1076. /* read in the color map buffer */
  1077. ncolors = header.ncolors;
  1078. if (ncolors) {
  1079. colors = (XColor *)malloc((unsigned) ncolors * sizeof(XColor));
  1080. if (!colors)
  1081. {
  1082. return GR_ALLOC_ERR;
  1083. }
  1084. #ifdef __alpha
  1085. /* Use XWDColor instead of XColor. Byte-swapping if it is necessary.
  1086. * Move values back into Xcolor structure.
  1087. */
  1088. for (i = 0; i < ncolors; i++) {
  1089. if (_DtGrRead( (char *) &xwd_color, sizeof(XWDColor), 1 , stream) != 1 )
  1090. {
  1091. XFree ((char *) colors);
  1092. return GR_FILE_ERR;
  1093. }
  1094. if (*(char *) &swaptest) {
  1095. _swaplong((char *) &xwd_color.pixel, sizeof(xwd_color.pixel));
  1096. _swapshort((char *) &xwd_color.red, 3 * sizeof(xwd_color.red));
  1097. }
  1098. colors[i].pixel = xwd_color.pixel;
  1099. colors[i].red = xwd_color.red;
  1100. colors[i].green = xwd_color.green;
  1101. colors[i].blue = xwd_color.blue;
  1102. colors[i].flags = xwd_color.flags;
  1103. }
  1104. #else
  1105. if(_DtGrRead((char *) colors, sizeof(XColor), ncolors, stream) != ncolors)
  1106. {
  1107. XFree ((char *) colors);
  1108. return GR_FILE_ERR;
  1109. }
  1110. if (*(char *) &swaptest) {
  1111. for (i = 0; i < ncolors; i++) {
  1112. _swaplong((char *) &colors[i].pixel, sizeof(long));
  1113. _swapshort((char *) &colors[i].red, 3 * sizeof(short));
  1114. }
  1115. }
  1116. #endif /* __alpha */
  1117. }
  1118. /*
  1119. * alloc the pixel buffer
  1120. */
  1121. buffer_size = Image_Size(&in_image);
  1122. buffer = (char *) malloc(buffer_size);
  1123. if (buffer == NULL)
  1124. {
  1125. if (NULL != colors)
  1126. XFree ((char *) colors);
  1127. return GR_ALLOC_ERR;
  1128. }
  1129. /* read in the image data */
  1130. count = _DtGrRead(buffer, sizeof(char), (int)buffer_size, stream);
  1131. if (count != buffer_size)
  1132. {
  1133. if (NULL != colors)
  1134. XFree ((char *) colors);
  1135. XFree (buffer);
  1136. return GR_FILE_ERR;
  1137. }
  1138. if (in_image.depth == 1) {
  1139. in_image.format = XYBitmap;
  1140. rawbits = True;
  1141. }
  1142. in_image.data = buffer;
  1143. /* create the output image */
  1144. result = GR_SUCCESS;
  1145. if (rawbits) {
  1146. out_image = &in_image;
  1147. } else {
  1148. out_image = XCreateImage(dpy, visual, depth,
  1149. (depth == 1) ? XYBitmap : in_image.format,
  1150. in_image.xoffset, NULL,
  1151. in_image.width, in_image.height,
  1152. XBitmapPad(dpy), 0);
  1153. out_image->data = (char *) malloc(Image_Size(out_image));
  1154. if ((header.visual_class == TrueColor) ||
  1155. (header.visual_class == DirectColor))
  1156. result = Do_Direct(dpy, screen, &header, colormap, ncolors, colors,
  1157. color_model,
  1158. &in_image, out_image, ret_colors, ret_number);
  1159. else
  1160. result = Do_Pseudo(dpy, screen, colormap, ncolors, colors, color_model,
  1161. &in_image, out_image, ret_colors, ret_number);
  1162. }
  1163. if (result != GR_ALLOC_ERR)
  1164. _XmPutScaledImage(dpy, pixmap, gc, out_image,
  1165. src_x, src_y, dst_x, dst_y,
  1166. in_image.width, in_image.height,
  1167. width, height);
  1168. /*
  1169. * free the buffers
  1170. */
  1171. if (NULL != colors)
  1172. XFree ((char *) colors);
  1173. XFree (buffer);
  1174. if (!rawbits)
  1175. XDestroyImage (out_image);
  1176. return result;
  1177. }
  1178. /*****************************************************************************/
  1179. /* General functions for changing a file into a pixmap */
  1180. /* */
  1181. /*****************************************************************************/
  1182. /***********
  1183. *
  1184. * Function processBitmap
  1185. *
  1186. * takes a string.
  1187. * Make a bitmap into the pixmap for the graphic.
  1188. *
  1189. ***********/
  1190. static enum _DtGrLoadStatus processBitmap(
  1191. _DtGrStream *stream,
  1192. Screen *screen,
  1193. int depth,
  1194. Colormap colormap,
  1195. Visual *visual,
  1196. Pixel foreground,
  1197. Pixel background,
  1198. GC gc,
  1199. enum _DtGrColorModel color_model,
  1200. Boolean allow_reduced_colors,
  1201. Dimension *in_out_width,
  1202. Dimension *in_out_height,
  1203. unsigned short media_resolution,
  1204. Pixmap *ret_pixmap,
  1205. Pixmap *ret_mask,
  1206. Pixel **ret_colors,
  1207. int *ret_num_colors,
  1208. _DtGrContext *context
  1209. )
  1210. {
  1211. int result;
  1212. int junk;
  1213. unsigned int width, height;
  1214. unsigned char *data;
  1215. Pixmap scaled_pixmap = 0;
  1216. Display *dpy = DisplayOfScreen(screen);
  1217. Drawable drawable = RootWindowOfScreen(screen);
  1218. XImage ximage;
  1219. float ratio = 1.0;
  1220. if (media_resolution == 0)
  1221. return(_DtGrCONVERT_FAILURE);
  1222. if (*in_out_width == 0 && *in_out_height == 0)
  1223. ratio = ComputeRatio(media_resolution, 100);
  1224. if (stream->type == _DtGrNONE)
  1225. return(_DtGrCONVERT_FAILURE);
  1226. result = _DtGrReadBitmapStreamData (stream,
  1227. &width, &height, &data, &junk, &junk);
  1228. /* Be sure to implement XvpCopyPlane later */
  1229. if (result == BitmapSuccess)
  1230. {
  1231. ximage.height = height;
  1232. ximage.width = width;
  1233. ximage.depth = 1;
  1234. ximage.bits_per_pixel = 1;
  1235. ximage.xoffset = 0;
  1236. ximage.format = XYBitmap;
  1237. ximage.data = (char *)data;
  1238. ximage.byte_order = LSBFirst;
  1239. ximage.bitmap_unit = 8;
  1240. ximage.bitmap_bit_order = LSBFirst;
  1241. ximage.bitmap_pad = 8;
  1242. ximage.bytes_per_line = (width+7)/8;
  1243. XInitImage(&ximage);
  1244. *in_out_width = (Dimension) width * ratio + 0.5;
  1245. *in_out_height = (Dimension) height * ratio + 0.5;
  1246. if (*in_out_width == 0)
  1247. *in_out_width = 1;
  1248. if (*in_out_height == 0)
  1249. *in_out_height = 1;
  1250. scaled_pixmap = XCreatePixmap (dpy, drawable, (*in_out_width),
  1251. (*in_out_height), depth);
  1252. _XmPutScaledImage(dpy, scaled_pixmap, gc, &ximage,0, 0, 0, 0,
  1253. width,height,(*in_out_width),(*in_out_height));
  1254. XFree((char *)data);
  1255. *ret_pixmap = scaled_pixmap;
  1256. }
  1257. if (result == BitmapSuccess)
  1258. return(_DtGrSUCCESS);
  1259. else if (result == BitmapOpenFailed)
  1260. return(_DtGrOPEN_FAILED);
  1261. else if (result == BitmapFileInvalid)
  1262. return(_DtGrFILE_INVALID);
  1263. else if (result == BitmapNoMemory)
  1264. return(_DtGrNO_MEMORY);
  1265. else
  1266. return(_DtGrCONVERT_FAILURE);
  1267. }
  1268. #ifndef STUB
  1269. /***********
  1270. *
  1271. * Function processTiff
  1272. *
  1273. * takes a string.
  1274. * Make a tiff into the pixmap for the graphic.
  1275. *
  1276. ***********/
  1277. static enum _DtGrLoadStatus processTiff(
  1278. _DtGrStream *stream,
  1279. Screen *screen,
  1280. int depth,
  1281. Colormap colormap,
  1282. Visual *visual,
  1283. Pixel foreground,
  1284. Pixel background,
  1285. GC gc,
  1286. enum _DtGrColorModel color_model,
  1287. Boolean allow_reduced_colors,
  1288. Dimension *in_out_width,
  1289. Dimension *in_out_height,
  1290. unsigned short media_resolution,
  1291. Pixmap *ret_pixmap,
  1292. Pixmap *ret_mask,
  1293. Pixel **ret_colors,
  1294. int *ret_num_colors,
  1295. _DtGrContext *context
  1296. )
  1297. {
  1298. int result = -1;
  1299. Pixmap pixmap = 0;
  1300. ilFile inFile;
  1301. ilPipe inPipe;
  1302. ilFileImage inImage;
  1303. const ilImageDes *inDes;
  1304. static ilContext IlContext = NULL;
  1305. Display *dpy = DisplayOfScreen(screen);
  1306. Drawable drawable = RootWindowOfScreen(screen);
  1307. ilXWC *tiff_xwc;
  1308. float ratio = 1.0;
  1309. _DtHelpProcessLock();
  1310. if ((context == NULL) || (stream->type == _DtGrNONE))
  1311. {
  1312. _DtHelpProcessUnlock();
  1313. return (_DtGrCONVERT_FAILURE);
  1314. }
  1315. if (media_resolution == 0)
  1316. {
  1317. _DtHelpProcessUnlock();
  1318. return(_DtGrCONVERT_FAILURE);
  1319. }
  1320. tiff_xwc = (ilXWC *) &(context->context);
  1321. if (IlContext == NULL)
  1322. {
  1323. if (IL_CREATE_CONTEXT (&IlContext, 0))
  1324. {
  1325. IlContext = NULL;
  1326. _DtHelpProcessUnlock();
  1327. return (_DtGrCONVERT_FAILURE);
  1328. }
  1329. }
  1330. if (*tiff_xwc == NULL)
  1331. {
  1332. /* Fill in the context record fields */
  1333. *tiff_xwc = ilCreateXWC (IlContext, dpy, visual, colormap, gc, 0, 0);
  1334. if (*tiff_xwc == NULL)
  1335. {
  1336. _DtHelpProcessUnlock();
  1337. return(_DtGrCONVERT_FAILURE);
  1338. }
  1339. context->image_type = XtNewString("TIFF");
  1340. }
  1341. inFile = ilConnectFile (IlContext, stream, 0, 0);
  1342. if (inFile)
  1343. {
  1344. inImage = ilListFileImages (inFile, 0);
  1345. if (inImage)
  1346. {
  1347. int image_resolution = 100 ; /* assume that if not in file */
  1348. if (inImage->xRes != 0) {
  1349. image_resolution = inImage->xRes ; /* dpi from the file */
  1350. }
  1351. if (*in_out_width == 0 && *in_out_height == 0)
  1352. ratio = ComputeRatio(media_resolution, image_resolution);
  1353. *in_out_width = inImage->width * ratio + 0.5;
  1354. *in_out_height = inImage->height * ratio + 0.5;
  1355. if (*in_out_width == 0)
  1356. *in_out_width = 1;
  1357. if (*in_out_height == 0)
  1358. *in_out_height = 1;
  1359. pixmap = XCreatePixmap (dpy, drawable, (*in_out_width),
  1360. (*in_out_height), depth);
  1361. if (pixmap)
  1362. {
  1363. inPipe = ilCreatePipe (IlContext, 0);
  1364. if (inPipe)
  1365. {
  1366. if (color_model == _DtGrGRAY_SCALE)
  1367. inDes = IL_DES_GRAY;
  1368. else if (color_model == _DtGrBITONAL)
  1369. inDes = IL_DES_BITONAL;
  1370. result = 0;
  1371. if (!ilReadFileImage (inPipe, inImage, (ilRect *) NULL, 0))
  1372. result = -1;
  1373. if (result == 0 && color_model != _DtGrCOLOR &&
  1374. ilConvert (inPipe, inDes, ((ilImageFormat *) NULL), 0, NULL) != True)
  1375. result = -1;
  1376. if (result == 0 &&
  1377. ilWriteXDrawable (inPipe, pixmap, *tiff_xwc,
  1378. (ilRect *) NULL, 0, 0, 0))
  1379. {
  1380. ilExecutePipe (inPipe, 0, ratio);
  1381. if (IlContext->error != 0)
  1382. result = -1;
  1383. }
  1384. ilDestroyObject (inPipe);
  1385. }
  1386. if (result == -1)
  1387. {
  1388. XFreePixmap (dpy, pixmap);
  1389. pixmap = 0;
  1390. }
  1391. }
  1392. }
  1393. ilDestroyObject (inFile);
  1394. }
  1395. *ret_pixmap = pixmap;
  1396. if (result < 0)
  1397. {
  1398. if ((IlContext->error == IL_ERROR_FILE_IO) ||
  1399. (IlContext->error == IL_ERROR_FILE_NOT_TIFF))
  1400. {
  1401. _DtHelpProcessUnlock();
  1402. return(_DtGrFILE_INVALID);
  1403. }
  1404. else if (IlContext->error == IL_ERROR_MALLOC)
  1405. {
  1406. _DtHelpProcessUnlock();
  1407. return(_DtGrNO_MEMORY);
  1408. }
  1409. else
  1410. {
  1411. _DtHelpProcessUnlock();
  1412. return(_DtGrCONVERT_FAILURE);
  1413. }
  1414. }
  1415. else
  1416. {
  1417. _DtHelpProcessUnlock();
  1418. return(_DtGrSUCCESS);
  1419. }
  1420. }
  1421. #endif
  1422. /***********
  1423. *
  1424. * Function processXwd
  1425. *
  1426. * takes a string.
  1427. * Make an XWD file into the pixmap for the graphic.
  1428. *
  1429. ***********/
  1430. static enum _DtGrLoadStatus processXwd(
  1431. _DtGrStream *stream,
  1432. Screen *screen,
  1433. int depth,
  1434. Colormap colormap,
  1435. Visual *visual,
  1436. Pixel foreground,
  1437. Pixel background,
  1438. GC gc,
  1439. enum _DtGrColorModel color_model,
  1440. Boolean allow_reduced_colors,
  1441. Dimension *in_out_width,
  1442. Dimension *in_out_height,
  1443. unsigned short media_resolution,
  1444. Pixmap *ret_pixmap,
  1445. Pixmap *ret_mask,
  1446. Pixel **ret_colors,
  1447. int *ret_num_colors,
  1448. _DtGrContext *context
  1449. )
  1450. {
  1451. Pixmap pixmap;
  1452. XWDFileHeader header;
  1453. int result;
  1454. unsigned long swaptest = TRUE;
  1455. Display *dpy = DisplayOfScreen(screen);
  1456. Drawable drawable = RootWindowOfScreen(screen);
  1457. int screen_num = XScreenNumberOfScreen(screen);
  1458. float ratio = 1.0;
  1459. if (media_resolution == 0)
  1460. return(_DtGrCONVERT_FAILURE);
  1461. if (stream->type == _DtGrNONE)
  1462. return(_DtGrCONVERT_FAILURE);
  1463. /* Read in XWDFileHeader structure */
  1464. result = _DtGrRead((char *)&header, sizeof(header), 1, stream);
  1465. if (result == 1 && *(char *) &swaptest)
  1466. _swaplong((char *) &header, sizeof(header));
  1467. if (result != 1 || header.file_version != XWD_FILE_VERSION)
  1468. return (_DtGrFILE_INVALID);
  1469. if (*in_out_width == 0 && *in_out_height == 0)
  1470. ratio = ComputeRatio(media_resolution, 100);
  1471. *in_out_width = header.pixmap_width * ratio + 0.5;
  1472. *in_out_height = header.pixmap_height * ratio + 0.5;
  1473. if (*in_out_width == 0)
  1474. *in_out_width = 1;
  1475. if (*in_out_height == 0)
  1476. *in_out_height = 1;
  1477. pixmap = XCreatePixmap (dpy, drawable, (*in_out_width),
  1478. (*in_out_height),depth);
  1479. if (pixmap) {
  1480. if ((result=XwdFileToPixmap (dpy, screen_num, depth, pixmap, colormap,
  1481. visual, gc, color_model, 0, 0, 0, 0,
  1482. (*in_out_width), (*in_out_height), stream,
  1483. ret_colors, ret_num_colors))
  1484. == GR_SUCCESS) {
  1485. *ret_pixmap = pixmap;
  1486. return (_DtGrSUCCESS);
  1487. }
  1488. else if (result == GR_HEADER_ERR)
  1489. return (_DtGrFILE_INVALID);
  1490. else if (result == GR_FILE_ERR)
  1491. return (_DtGrFILE_INVALID);
  1492. else if (result == GR_ALLOC_ERR)
  1493. return (_DtGrNO_MEMORY);
  1494. else
  1495. return (_DtGrCONVERT_FAILURE);
  1496. }
  1497. else
  1498. return (_DtGrNO_MEMORY);
  1499. }
  1500. /************************************
  1501. * myXpmReadFileToPixmap
  1502. ***********************************/
  1503. static int
  1504. myXpmReadFileToPixmap(
  1505. Display *display,
  1506. Screen *screen,
  1507. Drawable d,
  1508. _DtGrStream *stream,
  1509. Pixmap *pixmap_return,
  1510. Pixmap *shapemask_return,
  1511. XpmAttributes *attributes,
  1512. GC gc,
  1513. Pixel bg,
  1514. Pixel fg,
  1515. int depth,
  1516. float ratio
  1517. )
  1518. {
  1519. XImage *image = NULL;
  1520. XImage **imageptr = 0;
  1521. XImage *shapeimage, **shapeimageptr = NULL;
  1522. int ErrorStatus;
  1523. int switchFlag = 0;
  1524. int scaledWidth, scaledHeight;
  1525. /*
  1526. * initialize return values
  1527. */
  1528. if (pixmap_return) {
  1529. *pixmap_return = 0;
  1530. imageptr = &image;
  1531. }
  1532. if (shapemask_return) {
  1533. *shapemask_return = 0;
  1534. shapeimageptr = &shapeimage;
  1535. }
  1536. /*
  1537. * create the images
  1538. */
  1539. if (stream->type == _DtGrFILE)
  1540. {
  1541. if (stream->source.file.uncompressed_filename != NULL)
  1542. ErrorStatus = _DtXpmReadFileToImage(display,
  1543. stream->source.file.uncompressed_filename,
  1544. imageptr, shapeimageptr, attributes);
  1545. else
  1546. ErrorStatus = _DtXpmReadFileToImage(display,
  1547. stream->source.file.filename,
  1548. imageptr, shapeimageptr, attributes);
  1549. }
  1550. else if (stream->type == _DtGrBUFFER)
  1551. {
  1552. ErrorStatus = XpmCreateImageFromBuffer(display,
  1553. (char *) stream->source.buffer.base,
  1554. imageptr, shapeimageptr, attributes);
  1555. }
  1556. else
  1557. ErrorStatus = XpmFileInvalid; /* stream type of _DtGrNONE */
  1558. if (ErrorStatus < 0)
  1559. return (ErrorStatus);
  1560. /*
  1561. * Check to see if we will need to switch the foreground and
  1562. * background colors. When forced to a depth of 1, the Xpm call
  1563. * returns a ZPixmap ready to place in pixmap of depth 1.
  1564. * Unfortunately, usually the pixmap is of a different depth. This
  1565. * causes a depth mismatch for the XPutImage. Therefore, we change
  1566. * the format type to XYBitmap and XPutImage is happy. But on
  1567. * servers where BlackPixel is not at pixel 1 in the colormap, this
  1568. * causes the image to be inverted. So by switching the foreground
  1569. * and background in the gc, the image is corrected for these
  1570. * systems.
  1571. *
  1572. * The other way of doing this, is to create a pixmap of depth 1 and
  1573. * do an XPutImage to it. Then do a XCopyPlane to a pixmap of the
  1574. * correct depth. But this is a major performance hit compared to
  1575. * switching the colors.
  1576. *
  1577. * The downside of switching colors is that it works only when
  1578. * BlackPixel and WhitePixel fill colormap entries 0 and 1. But
  1579. * since this is the 99.9% case, we'll go with it.
  1580. */
  1581. if (image->depth == 1 && BlackPixelOfScreen(screen) != 1L)
  1582. switchFlag = 1;
  1583. /*
  1584. * create the pixmaps
  1585. */
  1586. if (imageptr && image) {
  1587. scaledWidth = image->width * ratio + 0.5;
  1588. scaledHeight = image->height * ratio + 0.5;
  1589. if (scaledWidth == 0)
  1590. scaledWidth = 1;
  1591. if (scaledHeight == 0)
  1592. scaledHeight = 1;
  1593. *pixmap_return = XCreatePixmap(display, d, scaledWidth,
  1594. scaledHeight, depth);
  1595. if (image->depth == 1)
  1596. image->format = XYBitmap;
  1597. if (switchFlag)
  1598. {
  1599. XSetBackground (display, gc, fg);
  1600. XSetForeground (display, gc, bg);
  1601. }
  1602. _XmPutScaledImage(display, *pixmap_return, gc, image,
  1603. 0, 0, 0, 0,
  1604. image->width, image->height,
  1605. scaledWidth, scaledHeight);
  1606. if (switchFlag)
  1607. {
  1608. XSetBackground (display, gc, bg);
  1609. XSetForeground (display, gc, fg);
  1610. }
  1611. XDestroyImage(image);
  1612. }
  1613. if (shapeimageptr && shapeimage)
  1614. XDestroyImage(shapeimage);
  1615. return (ErrorStatus);
  1616. }
  1617. static XpmColorSymbol colorSymbol = {"none", NULL, 0};
  1618. /***********
  1619. *
  1620. * Function processXpm
  1621. *
  1622. * takes a string.
  1623. * Make an XPM file into the pixmap for the graphic.
  1624. *
  1625. ***********/
  1626. static enum _DtGrLoadStatus processXpm(
  1627. _DtGrStream *stream,
  1628. Screen *screen,
  1629. int depth,
  1630. Colormap colormap,
  1631. Visual *visual,
  1632. Pixel foreground,
  1633. Pixel background,
  1634. GC gc,
  1635. enum _DtGrColorModel color_model,
  1636. Boolean allow_reduced_colors,
  1637. Dimension *in_out_width,
  1638. Dimension *in_out_height,
  1639. unsigned short media_resolution,
  1640. Pixmap *ret_pixmap,
  1641. Pixmap *ret_mask,
  1642. Pixel **ret_colors,
  1643. int *ret_num_colors,
  1644. _DtGrContext *context
  1645. )
  1646. {
  1647. int i, j;
  1648. int result;
  1649. short done;
  1650. XpmAttributes xpmAttr;
  1651. Visual vis2;
  1652. float ratio = 1.0;
  1653. enum _DtGrLoadStatus status = _DtGrSUCCESS;
  1654. if (media_resolution == 0)
  1655. return(_DtGrCONVERT_FAILURE);
  1656. if (stream->type == _DtGrNONE)
  1657. return(_DtGrCONVERT_FAILURE); /* stream type of _DtGrNONE */
  1658. _DtHelpProcessLock();
  1659. do
  1660. {
  1661. colorSymbol.pixel = background;
  1662. xpmAttr.valuemask = XpmVisual | XpmReturnPixels |
  1663. XpmColorSymbols | XpmColormap | XpmDepth;
  1664. xpmAttr.visual = visual;
  1665. xpmAttr.colorsymbols = &colorSymbol;
  1666. xpmAttr.colormap = colormap;
  1667. xpmAttr.numsymbols = 1;
  1668. xpmAttr.depth = depth;
  1669. xpmAttr.pixels = NULL;
  1670. /*
  1671. * If not color, force to black and white.
  1672. */
  1673. if (color_model != _DtGrCOLOR)
  1674. {
  1675. memcpy (&vis2, xpmAttr.visual, sizeof(Visual));
  1676. vis2.class = StaticGray;
  1677. vis2.map_entries = 2;
  1678. xpmAttr.depth = 1;
  1679. xpmAttr.visual = &vis2;
  1680. }
  1681. if (*in_out_width == 0 && *in_out_height == 0)
  1682. ratio = ComputeRatio(media_resolution, 100);
  1683. result = myXpmReadFileToPixmap (DisplayOfScreen(screen),
  1684. screen,
  1685. RootWindowOfScreen(screen),
  1686. stream, ret_pixmap,
  1687. ret_mask, &xpmAttr, gc,
  1688. background, foreground, depth,
  1689. ratio);
  1690. done = True;
  1691. /*
  1692. * if we did not successfully get our icon, force the color
  1693. * to black and white.
  1694. */
  1695. if (result == XpmColorFailed && color_model != _DtGrBITONAL)
  1696. {
  1697. if (allow_reduced_colors)
  1698. {
  1699. color_model = _DtGrBITONAL;
  1700. status = _DtGrCOLOR_REDUCE;
  1701. done = False;
  1702. }
  1703. else
  1704. status = _DtGrCOLOR_FAILED;
  1705. }
  1706. } while (done == False);
  1707. if (result == XpmSuccess || result == XpmColorError)
  1708. {
  1709. *in_out_width = xpmAttr.width * ratio + 0.5;
  1710. *in_out_height = xpmAttr.height * ratio + 0.5;
  1711. if (*in_out_width == 0)
  1712. *in_out_width = 1;
  1713. if (*in_out_height == 0)
  1714. *in_out_height = 1;
  1715. *ret_colors = xpmAttr.pixels;
  1716. *ret_num_colors = xpmAttr.npixels;
  1717. /*
  1718. * squeeze out the pixel used for the transparent color since we
  1719. * don't want to free it when we free the other colors
  1720. */
  1721. i = 0;
  1722. while (i < xpmAttr.npixels && xpmAttr.pixels[i] != colorSymbol.pixel)
  1723. i++;
  1724. if (i < xpmAttr.npixels)
  1725. {
  1726. for (j = i, i = i + 1; i < xpmAttr.npixels; i++, j++)
  1727. xpmAttr.pixels[j] = xpmAttr.pixels[i];
  1728. *ret_num_colors = j;
  1729. }
  1730. }
  1731. _DtHelpProcessUnlock();
  1732. if (result == XpmOpenFailed)
  1733. status = _DtGrOPEN_FAILED;
  1734. else if (result == XpmFileInvalid)
  1735. status = _DtGrFILE_INVALID;
  1736. else if (result == XpmNoMemory)
  1737. status = _DtGrNO_MEMORY;
  1738. else if (result == XpmColorFailed)
  1739. status = _DtGrCOLOR_FAILED;
  1740. return(status);
  1741. }
  1742. /******************************************************************************
  1743. *
  1744. * Function processGIF
  1745. *
  1746. * Default gif converter, creates a pixmap from a file-associated or
  1747. * buffer-associated stream of gif data.
  1748. *
  1749. *****************************************************************************/
  1750. static enum _DtGrLoadStatus processGIF(
  1751. _DtGrStream *stream,
  1752. Screen *screen,
  1753. int depth,
  1754. Colormap colormap,
  1755. Visual *visual,
  1756. Pixel foreground,
  1757. Pixel background,
  1758. GC gc,
  1759. enum _DtGrColorModel color_model,
  1760. Boolean allow_reduced_colors,
  1761. Dimension *in_out_width,
  1762. Dimension *in_out_height,
  1763. unsigned short media_resolution,
  1764. Pixmap *ret_pixmap,
  1765. Pixmap *ret_mask,
  1766. Pixel **ret_colors,
  1767. int *ret_num_colors,
  1768. _DtGrContext *context
  1769. )
  1770. {
  1771. struct stat stbuf;
  1772. char *buffer;
  1773. GifObj g;
  1774. enum _DtGrLoadStatus status;
  1775. Display *display = DisplayOfScreen(screen);
  1776. Drawable drawable = RootWindowOfScreen(screen);
  1777. int i, size=0;
  1778. float ratio = 1.0;
  1779. if (media_resolution == 0)
  1780. return(_DtGrCONVERT_FAILURE);
  1781. if (stream->type == _DtGrNONE)
  1782. return(_DtGrCONVERT_FAILURE); /* stream type of _DtGrNONE */
  1783. *ret_pixmap = 0; /* Initialize the return pixmap to zero */
  1784. /*
  1785. ** The gif-to-pixmap utilities operate only on in-memory buffers,
  1786. ** so if this is a buffer-based stream, simply pass the buffer, and
  1787. ** if this is a file-based stream, read the file contents into a buffer
  1788. ** and pass that.
  1789. */
  1790. if (stream->type == _DtGrBUFFER)
  1791. {
  1792. buffer = (char *) stream->source.buffer.base;
  1793. size = stream->source.buffer.size;
  1794. }
  1795. else
  1796. {
  1797. if (stream->source.file.uncompressed_filename != NULL)
  1798. stat (stream->source.file.uncompressed_filename, &stbuf);
  1799. else
  1800. stat (stream->source.file.filename, &stbuf);
  1801. size = stbuf.st_size;
  1802. buffer = XtMalloc(size);
  1803. fread (buffer, 1, size, stream->source.file.fileptr);
  1804. }
  1805. /* Initialize the gif object */
  1806. status = InitGifObject (&g, display, drawable, screen, depth, colormap,
  1807. visual, gc, color_model, allow_reduced_colors);
  1808. if (*in_out_width == 0 && *in_out_height == 0)
  1809. ratio = ComputeRatio(media_resolution, 100);
  1810. /* Create an X pixmap from the gif object */
  1811. if ((status == _DtGrSUCCESS) || (status == _DtGrCOLOR_REDUCE))
  1812. *ret_pixmap = gif_to_pixmap (&g, (unsigned char *) buffer, size,
  1813. in_out_width, in_out_height,
  1814. g.f_black, g.f_white, ratio);
  1815. /* Set the returned colors parameters */
  1816. if (*ret_pixmap != 0)
  1817. {
  1818. if (g.f_do_visual == DO_COLOR)
  1819. {
  1820. *ret_num_colors = g.total_colors;
  1821. *ret_colors = (Pixel *) malloc(*ret_num_colors * sizeof(Pixel));
  1822. for (i=0; i<*ret_num_colors; i++)
  1823. (*ret_colors)[i] = g.GifCMap[i].pixel;
  1824. }
  1825. else /* DO_GREY */
  1826. {
  1827. *ret_num_colors = g.f_total_greys;
  1828. *ret_colors = (Pixel *) malloc(*ret_num_colors * sizeof(Pixel));
  1829. for (i=0; i<*ret_num_colors; i++)
  1830. (*ret_colors)[i] = g.GifGMap[i];
  1831. }
  1832. }
  1833. /* Free up any resources associated with the gif object */
  1834. DeleteGifObjectResources (&g);
  1835. /* If we allocated a buffer, free it */
  1836. if (stream->type == _DtGrFILE)
  1837. XtFree(buffer);
  1838. /* Return the status */
  1839. return (status);
  1840. }
  1841. /******************************************************************************
  1842. *
  1843. * Function processJPEG
  1844. *
  1845. * Default jpeg converter, creates a pixmap from a file-associated or
  1846. * buffer-associated stream of jpeg data.
  1847. *
  1848. * The function first converts the jpeg stream to an XImage with a virtual
  1849. * colormap using the jpeg_to_ximage call, then generates a new XImage that
  1850. * uses the X colormap by calling the Do_Pseudo routine. The Do_Pseudo
  1851. * routine, which is also used by the XWD converter, automatically handles X
  1852. * color allocation and color model degradation where necessary. Finally,
  1853. * a pixmap is generated from the XImage returned by Do_Pseudo.
  1854. *
  1855. *****************************************************************************/
  1856. static enum _DtGrLoadStatus processJPEG(
  1857. _DtGrStream *stream,
  1858. Screen *screen,
  1859. int depth,
  1860. Colormap colormap,
  1861. Visual *visual,
  1862. Pixel foreground,
  1863. Pixel background,
  1864. GC gc,
  1865. enum _DtGrColorModel color_model,
  1866. Boolean allow_reduced_colors,
  1867. Dimension *in_out_width,
  1868. Dimension *in_out_height,
  1869. unsigned short media_resolution,
  1870. Pixmap *ret_pixmap,
  1871. Pixmap *ret_mask,
  1872. Pixel **ret_colors,
  1873. int *ret_num_colors,
  1874. _DtGrContext *context
  1875. )
  1876. {
  1877. enum _DtGrLoadStatus status;
  1878. XImage *in_image, *out_image;
  1879. XColor *colors;
  1880. int ncolors;
  1881. int xres;
  1882. int result = GR_SUCCESS;
  1883. Display *dpy = DisplayOfScreen(screen);
  1884. int screen_num = XScreenNumberOfScreen(screen);
  1885. Drawable drawable = RootWindowOfScreen(screen);
  1886. float ratio = 1.0;
  1887. unsigned int scaledWidth,scaledHeight;
  1888. if (media_resolution == 0)
  1889. return(_DtGrCONVERT_FAILURE);
  1890. if (*in_out_width == 0 && *in_out_height == 0)
  1891. ratio = 0.0;
  1892. if (stream->type == _DtGrNONE)
  1893. return(_DtGrCONVERT_FAILURE); /* stream type of _DtGrNONE */
  1894. /*
  1895. ** Convert the stream to an XImage with a virtual colormap
  1896. */
  1897. status = jpeg_to_ximage (stream, screen, visual, in_out_width,
  1898. in_out_height, &in_image, &colors, &ncolors,
  1899. &xres);
  1900. if (ratio == 0.0) {
  1901. if (!xres)
  1902. xres = 100;
  1903. ratio = ComputeRatio(media_resolution, xres);
  1904. }
  1905. if (status == _DtGrSUCCESS)
  1906. {
  1907. if (in_image->depth == 1)
  1908. {
  1909. /*
  1910. ** Bitmap, no further image transformation necessary
  1911. */
  1912. out_image = in_image;
  1913. result = GR_SUCCESS;
  1914. }
  1915. else
  1916. {
  1917. /*
  1918. ** Create an output image and have Do_Pseudo allocate the needed
  1919. ** pixels and store image data that uses these pixels into
  1920. ** the output image.
  1921. */
  1922. out_image = XCreateImage(dpy, visual, depth,
  1923. (depth == 1) ? XYBitmap : in_image->format,
  1924. in_image->xoffset, NULL,
  1925. in_image->width, in_image->height,
  1926. XBitmapPad(dpy), 0);
  1927. out_image->data = (char *) malloc(Image_Size(out_image));
  1928. result = Do_Pseudo(dpy, screen_num, colormap, ncolors, colors,
  1929. color_model, in_image, out_image,
  1930. ret_colors, ret_num_colors);
  1931. }
  1932. /*
  1933. ** If we've got a good XImage, go ahead and make a pixmap out of it
  1934. */
  1935. if (result != GR_ALLOC_ERR)
  1936. {
  1937. /*
  1938. ** Create a pixmap the same size as the XImage
  1939. */
  1940. scaledWidth = out_image->width * ratio + 0.5;
  1941. scaledHeight = out_image->height * ratio + 0.5;
  1942. if (scaledWidth == 0)
  1943. scaledWidth = 1;
  1944. if (scaledHeight == 0)
  1945. scaledHeight = 1;
  1946. *ret_pixmap = XCreatePixmap (dpy, drawable, scaledWidth,
  1947. scaledHeight, depth);
  1948. if (*ret_pixmap)
  1949. {
  1950. /*
  1951. ** Copy the XImage into the pixmap and set the other
  1952. ** return parameters.
  1953. */
  1954. _XmPutScaledImage(dpy, *ret_pixmap, gc, out_image, 0, 0, 0, 0,
  1955. out_image->width, out_image->height,
  1956. scaledWidth, scaledHeight);
  1957. *in_out_width = scaledWidth;
  1958. *in_out_height = scaledHeight;
  1959. status = _DtGrSUCCESS;
  1960. }
  1961. else
  1962. status = _DtGrNO_MEMORY;
  1963. if (out_image != in_image)
  1964. XDestroyImage (out_image);
  1965. }
  1966. else
  1967. status = _DtGrNO_MEMORY;
  1968. /*
  1969. ** Free the colors array and the initial XImage
  1970. */
  1971. XFree ((char *) colors);
  1972. XDestroyImage (in_image);
  1973. }
  1974. return (status);
  1975. }
  1976. /******************************************************************************
  1977. *
  1978. * Function destroyTiffContext
  1979. *
  1980. * Default tiff context destructor, destroys the ilXWC object pointed to
  1981. * by context->context and frees the context->image_type string.
  1982. *
  1983. *****************************************************************************/
  1984. static void destroyTiffContext(
  1985. _DtGrContext *context)
  1986. {
  1987. if (context != NULL)
  1988. {
  1989. if (context->context != NULL)
  1990. {
  1991. _DtHelpProcessLock();
  1992. ilDestroyObject ((ilObject) context->context);
  1993. _DtHelpProcessUnlock();
  1994. }
  1995. if (context->image_type != NULL)
  1996. XtFree(context->image_type);
  1997. }
  1998. }
  1999. /******************************************************************************
  2000. *
  2001. * Function DetermineImageType
  2002. *
  2003. * Determines the image type of the specified stream and copies the
  2004. * string representing that image type into the image_type buffer.
  2005. *
  2006. * If the stream is a buffer, the buffer is passed to DtDtsBufferToData
  2007. * in hopes that the file might be identified through content criteria.
  2008. *
  2009. * If the stream is a file, the following checks are made:
  2010. *
  2011. * 1. The file name extension is checked against a list of known image file
  2012. * type extensions.
  2013. * 2. The filename is passed to DtDtsFileToDataType in hopes that the file
  2014. * type might be identified through filename extension or content
  2015. * criteria.
  2016. * 3. If the file was uncompressed into a temporary file, the temporary
  2017. * filename is passed to DtDtsFileToDataType in hopes that the file
  2018. * type might be identified through content criteria.
  2019. *
  2020. * If none of these checks are successful, _DtCONVERT_FAILURE is returned.
  2021. * Otherwise, _DtGrSUCCESS is returned.
  2022. *
  2023. *****************************************************************************/
  2024. static enum _DtGrLoadStatus DetermineImageType(
  2025. _DtGrStream *stream,
  2026. char *image_type)
  2027. {
  2028. int i=0;
  2029. char *ext;
  2030. if (stream->type == _DtGrFILE)
  2031. {
  2032. /* First try to match the file name extension to a known mapping */
  2033. if (stream->source.file.filename != NULL)
  2034. {
  2035. /* Get the filename extension */
  2036. if (_DtHelpCeStrrchr(stream->source.file.filename, ".",
  2037. MB_CUR_MAX, &ext) == 0)
  2038. {
  2039. ext++; /* Increment past the dot */
  2040. while (img_extensions[i] != NULL)
  2041. {
  2042. if (strcmp(ext, img_extensions[i]) == 0)
  2043. {
  2044. /* Found a match */
  2045. strcpy (image_type,img_extensions[++i]);
  2046. return (_DtGrSUCCESS);
  2047. }
  2048. /* Skip two ahead to the next file extension name */
  2049. i += 2;
  2050. }
  2051. }
  2052. }
  2053. /*
  2054. * Didn't work, see if CDE datatyping can determine the image type.
  2055. * First try the filename, then try the uncompressed temporary
  2056. * filename, if it exists.
  2057. */
  2058. if ((ext = DtDtsFileToDataType(stream->source.file.filename)) == NULL)
  2059. if (stream->source.file.uncompressed_filename != NULL)
  2060. ext = DtDtsFileToDataType(
  2061. stream->source.file.uncompressed_filename);
  2062. /* If successful, save the image type and return */
  2063. if (ext != NULL)
  2064. {
  2065. strcpy (image_type, ext);
  2066. DtDtsFreeDataType(ext);
  2067. return (_DtGrSUCCESS);
  2068. }
  2069. }
  2070. else if (stream->type == _DtGrBUFFER)
  2071. {
  2072. /* See if the CDE datatyping mechanism can determine the image type */
  2073. ext = DtDtsBufferToDataType (stream->source.buffer.base,
  2074. stream->source.buffer.size,
  2075. NULL);
  2076. /* If successful, save the image type and return */
  2077. if (ext != NULL)
  2078. {
  2079. strcpy (image_type, ext);
  2080. DtDtsFreeDataType(ext);
  2081. return (_DtGrSUCCESS);
  2082. }
  2083. }
  2084. /* Couldn't figure out the type, return failure */
  2085. return (_DtGrCONVERT_FAILURE);
  2086. }
  2087. /******************************************************************************
  2088. *
  2089. * Function GetConverterAndDestructor
  2090. *
  2091. * This function returns the converter and context destructor functions for
  2092. * the specified image type. The registry of default image types is searched
  2093. * first, followed by the registry of new image types, until a match is made.
  2094. * The function returns _DtGrSUCCESS if the image_type has been registered
  2095. * and _DtGrCONVERT_FAILURE if it has not.
  2096. *
  2097. *****************************************************************************/
  2098. static enum _DtGrLoadStatus GetConverterAndDestructor(
  2099. char *image_type,
  2100. _DtGrLoadProc *converter,
  2101. _DtGrDestroyContextProc *destructor)
  2102. {
  2103. int i;
  2104. /* No image_type string, return failure */
  2105. if (image_type == NULL)
  2106. return (_DtGrCONVERT_FAILURE);
  2107. /* Search the default image type registry for the specified image type */
  2108. _DtHelpProcessLock();
  2109. for (i=0; i<registry_count; i++)
  2110. {
  2111. if (strcmp (registry[i].image_type, image_type) == 0)
  2112. {
  2113. /* Found it, return the converter and destructor */
  2114. *converter = registry[i].convert_proc;
  2115. *destructor = registry[i].destroy_context_proc;
  2116. _DtHelpProcessUnlock();
  2117. return (_DtGrSUCCESS);
  2118. }
  2119. }
  2120. /* Search the new image type registry for the specified image type */
  2121. for (i=0; i<new_registry_count; i++)
  2122. {
  2123. if (strcmp (new_registry[i].image_type, image_type) == 0)
  2124. {
  2125. /* Found it, return the converter */
  2126. *converter = new_registry[i].convert_proc;
  2127. *destructor = registry[i].destroy_context_proc;
  2128. _DtHelpProcessUnlock();
  2129. return (_DtGrSUCCESS);
  2130. }
  2131. }
  2132. _DtHelpProcessUnlock();
  2133. /* Image type wasn't registered, return failure */
  2134. return (_DtGrCONVERT_FAILURE);
  2135. }
  2136. /******************************************************************************
  2137. *
  2138. * Public Functions
  2139. *
  2140. *****************************************************************************/
  2141. /******************************************************************************
  2142. *
  2143. * Function _DtHelpProcessGraphic
  2144. *
  2145. * takes a string.
  2146. * Get the pixmap for the graphic.
  2147. *
  2148. *****************************************************************************/
  2149. Pixmap
  2150. _DtHelpProcessGraphic(
  2151. Display *dpy,
  2152. Drawable drawable,
  2153. int screen,
  2154. int depth,
  2155. GC gc,
  2156. Pixmap *def_pix,
  2157. Dimension *def_pix_width,
  2158. Dimension *def_pix_height,
  2159. _DtGrContext *context,
  2160. Colormap colormap,
  2161. Visual *visual,
  2162. Pixel fore_ground,
  2163. Pixel back_ground,
  2164. char *filename,
  2165. unsigned short media_resolution,
  2166. Dimension *width,
  2167. Dimension *height,
  2168. Pixmap *ret_mask,
  2169. Pixel **ret_colors,
  2170. int *ret_number )
  2171. {
  2172. unsigned int pixWidth = 0;
  2173. unsigned int pixHeight = 0;
  2174. Dimension pWidth, pHeight;
  2175. Pixmap pix = 0, mask = 0;
  2176. _DtGrStream stream;
  2177. Screen *scrptr = ScreenOfDisplay (dpy, screen);
  2178. static enum _DtGrColorModel ForceColor = _DtGrCOLOR;
  2179. static Boolean first_time = TRUE;
  2180. /*
  2181. * initialize the return values
  2182. */
  2183. *ret_colors = NULL;
  2184. *ret_number = 0;
  2185. *ret_mask = None;
  2186. /*
  2187. * Initialize the Force Variable if this is the first time
  2188. */
  2189. _DtHelpProcessLock();
  2190. if (first_time)
  2191. {
  2192. char *ptr;
  2193. char *ptr2;
  2194. char *xrmName = NULL;
  2195. char *xrmClass = NULL;
  2196. char *retStrType;
  2197. XrmValue retValue;
  2198. ForceColor = _DtGrCOLOR;
  2199. XtGetApplicationNameAndClass (dpy, &ptr, &ptr2);
  2200. xrmName = malloc (strlen (ptr) + 14);
  2201. xrmClass = malloc (strlen (ptr2) + 14);
  2202. if (xrmName && xrmClass)
  2203. {
  2204. strcpy (xrmName , ptr);
  2205. strcat (xrmName , ".helpColorUse");
  2206. strcpy (xrmClass, ptr2);
  2207. strcat (xrmClass, ".HelpColorUse");
  2208. if (XrmGetResource (XtDatabase (dpy), xrmName, xrmClass,
  2209. &retStrType, &retValue) == True)
  2210. {
  2211. ptr = (char *) retValue.addr;
  2212. /*
  2213. * check for GreyScale
  2214. */
  2215. if (*ptr == 'G' || *ptr == 'g')
  2216. ForceColor = _DtGrGRAY_SCALE;
  2217. /*
  2218. * check for BlackWhite
  2219. */
  2220. else if (*ptr == 'B' || *ptr == 'b')
  2221. ForceColor = _DtGrBITONAL;
  2222. }
  2223. free (xrmName);
  2224. free (xrmClass);
  2225. }
  2226. /*
  2227. * choose the correct visual type to use
  2228. */
  2229. if (ForceColor == _DtGrCOLOR &&
  2230. (visual->class == GrayScale || visual->class == StaticGray))
  2231. ForceColor = _DtGrGRAY_SCALE;
  2232. if (ForceColor != _DtGrBITONAL && depth == 1)
  2233. ForceColor = _DtGrBITONAL;
  2234. first_time = FALSE;
  2235. }
  2236. _DtHelpProcessUnlock();
  2237. if (filename != NULL)
  2238. {
  2239. int result;
  2240. /* Open the file and associate with a stream */
  2241. result = _DtGrOpenFile(&stream, filename);
  2242. if (result != 0)
  2243. pix = 0;
  2244. else
  2245. {
  2246. /* Create a pixmap from the image data stream */
  2247. _DtGrLoad(&stream, NULL, scrptr, depth, colormap, visual,
  2248. fore_ground, back_ground, gc, ForceColor, TRUE, &pWidth,
  2249. &pHeight, media_resolution, &pix, &mask, ret_colors,
  2250. ret_number, context);
  2251. pixWidth = pWidth;
  2252. pixHeight = pHeight;
  2253. /* Close the stream and the file associated with it */
  2254. _DtGrCloseStream( &stream );
  2255. }
  2256. }
  2257. if (pix == 0)
  2258. {
  2259. /*
  2260. * Try to get a localized pixmap
  2261. * NOTE....
  2262. * This is a cached pixmap.....
  2263. * Make sure XFreePixmaps does not free this one.
  2264. */
  2265. if (*def_pix == 0)
  2266. {
  2267. *def_pix = XmGetPixmap(scrptr, "Dthgraphic", fore_ground, back_ground);
  2268. if (*def_pix != XmUNSPECIFIED_PIXMAP)
  2269. {
  2270. Window root_id;
  2271. int x, y;
  2272. unsigned int border;
  2273. unsigned int depth;
  2274. if (XGetGeometry(dpy, *def_pix, &root_id, &x, &y,
  2275. &pixWidth, &pixHeight, &border, &depth) == 0)
  2276. {
  2277. XmDestroyPixmap(scrptr, *def_pix);
  2278. *def_pix = 0;
  2279. }
  2280. }
  2281. else
  2282. *def_pix = 0;
  2283. /*
  2284. * couldn't get a localized pixmap, go with a build in default
  2285. */
  2286. if (*def_pix == 0)
  2287. {
  2288. pixWidth = Missing_bm_width;
  2289. pixHeight = Missing_bm_height;
  2290. *def_pix = XCreatePixmapFromBitmapData ( dpy, drawable,
  2291. (char *) Missing_bm_bits,
  2292. pixWidth, pixHeight,
  2293. fore_ground, back_ground, depth);
  2294. }
  2295. *def_pix_width = (Dimension) pixWidth;
  2296. *def_pix_height = (Dimension) pixHeight;
  2297. }
  2298. pix = *def_pix;
  2299. pixWidth = *def_pix_width;
  2300. pixHeight = *def_pix_height;
  2301. }
  2302. *width = (Dimension) pixWidth;
  2303. *height = (Dimension) pixHeight;
  2304. return pix;
  2305. }
  2306. /******************************************************************************
  2307. *
  2308. * Function _DtGrLoad
  2309. *
  2310. * Determines the image type of the specified stream and calls the appropriate
  2311. * imagetype-to-pixmap converter function. If the conversion is successful
  2312. * and the caller passed the address of a pointer to NULL in the image_type
  2313. * parameter, a copy of the image type string is allocated and stored at this
  2314. * address. The caller is responsible for freeing this string. The caller
  2315. * is also responsible for freeing the values passed back in the ret_pixmap,
  2316. * ret_mask, ret_colors, and context parameters. The _DtGrLoad function
  2317. * returns _DtGrCONVERT_FAILURE if the appropriate converter could not be
  2318. * determined, otherwise it returns the value returned by the converter.
  2319. *
  2320. *****************************************************************************/
  2321. enum _DtGrLoadStatus _DtGrLoad(
  2322. _DtGrStream *stream,
  2323. char **image_type,
  2324. Screen *screen,
  2325. int depth,
  2326. Colormap colormap,
  2327. Visual *visual,
  2328. Pixel foreground,
  2329. Pixel background,
  2330. GC gc,
  2331. enum _DtGrColorModel color_model,
  2332. Boolean allow_reduced_colors,
  2333. Dimension *in_out_width,
  2334. Dimension *in_out_height,
  2335. unsigned short media_resolution,
  2336. Pixmap *ret_pixmap,
  2337. Pixmap *ret_mask,
  2338. Pixel **ret_colors,
  2339. int *ret_num_colors,
  2340. _DtGrContext *context
  2341. )
  2342. {
  2343. enum _DtGrLoadStatus status;
  2344. _DtGrLoadProc converter;
  2345. _DtGrDestroyContextProc destructor;
  2346. char buf[20],*itype;
  2347. /* Determine the image type */
  2348. if ((image_type != NULL) && (*image_type != NULL))
  2349. itype = *image_type; /* Caller specified image type */
  2350. else
  2351. {
  2352. /* Image type not specified by caller, try to figure it out */
  2353. if ((status = DetermineImageType(stream, buf)) != _DtGrSUCCESS)
  2354. return (status); /* Return failure if image type is unknown */
  2355. itype = &buf[0];
  2356. }
  2357. /* Look up the proper converter for this image type */
  2358. if ((status=GetConverterAndDestructor(itype, &converter, &destructor)) ==
  2359. _DtGrSUCCESS)
  2360. {
  2361. /* Call the converter */
  2362. if (converter != NULL)
  2363. {
  2364. status = (*converter)(stream, screen, depth, colormap, visual,
  2365. foreground, background, gc, color_model,
  2366. allow_reduced_colors, in_out_width,
  2367. in_out_height, media_resolution, ret_pixmap,
  2368. ret_mask, ret_colors, ret_num_colors,
  2369. context);
  2370. }
  2371. else
  2372. status = _DtGrCONVERT_FAILURE;
  2373. }
  2374. /* Return a copy of the image type string if successful */
  2375. if (status == _DtGrSUCCESS || status == _DtGrCOLOR_REDUCE)
  2376. if ((image_type != NULL) && (*image_type == NULL) && (itype != NULL))
  2377. *image_type = XtNewString(itype);
  2378. /* Return the conversion status */
  2379. return (status);
  2380. }
  2381. /******************************************************************************
  2382. *
  2383. * Function _DtGrDestroyContext
  2384. *
  2385. * Obtains and calls the context destructor function for the image type
  2386. * indicated by the context parameter.
  2387. *
  2388. *****************************************************************************/
  2389. void _DtGrDestroyContext(
  2390. _DtGrContext *context)
  2391. {
  2392. _DtGrLoadProc converter;
  2393. _DtGrDestroyContextProc destructor;
  2394. if (GetConverterAndDestructor(context->image_type, &converter,
  2395. &destructor) == _DtGrSUCCESS)
  2396. {
  2397. if (destructor != NULL)
  2398. (*destructor)(context);
  2399. }
  2400. }
  2401. /******************************************************************************
  2402. *
  2403. * Function _DtGrRegisterConverter
  2404. *
  2405. * Registers a converter and context destructor function for the specified
  2406. * image type. If the image_type has not been registered, a new record for
  2407. * it is created in the new image type registry. If the image_type has been
  2408. * registered, the current converter and context destructor functions are
  2409. * replaced by those specified by the caller. If the caller passes valid
  2410. * addresses in the current_convert_proc and current_destroy_proc parameters,
  2411. * the current converter and context destructor functions will be saved there
  2412. * first so that the caller may restore them at a later time.
  2413. *
  2414. *****************************************************************************/
  2415. void _DtGrRegisterConverter(
  2416. char *image_type,
  2417. _DtGrLoadProc convert_proc,
  2418. _DtGrDestroyContextProc destroy_context_proc,
  2419. _DtGrLoadProc *current_convert_proc,
  2420. _DtGrDestroyContextProc *current_destroy_proc)
  2421. {
  2422. int i;
  2423. if (image_type == NULL)
  2424. return;
  2425. /* Search the default converter registry for the specified image type */
  2426. _DtHelpProcessLock();
  2427. for (i=0; i<registry_count; i++)
  2428. {
  2429. if (strcmp (registry[i].image_type, image_type) == 0)
  2430. {
  2431. /* Found it, save the current procs, then replace them */
  2432. /* with the new ones and return */
  2433. if (current_convert_proc != NULL)
  2434. *current_convert_proc = registry[i].convert_proc;
  2435. if (current_destroy_proc != NULL)
  2436. *current_destroy_proc = registry[i].destroy_context_proc;
  2437. registry[i].convert_proc = convert_proc;
  2438. registry[i].destroy_context_proc = destroy_context_proc;
  2439. _DtHelpProcessUnlock();
  2440. return;
  2441. }
  2442. }
  2443. /* Search the new converter registry for the specified image type */
  2444. for (i=0; i<new_registry_count; i++)
  2445. {
  2446. if (strcmp (new_registry[i].image_type, image_type) == 0)
  2447. {
  2448. /* Found it, save the current procs, then replace them */
  2449. /* with the new ones and return */
  2450. if (current_convert_proc != NULL)
  2451. *current_convert_proc = new_registry[i].convert_proc;
  2452. if (current_destroy_proc != NULL)
  2453. *current_destroy_proc = new_registry[i].destroy_context_proc;
  2454. new_registry[i].convert_proc = convert_proc;
  2455. new_registry[i].destroy_context_proc = destroy_context_proc;
  2456. _DtHelpProcessUnlock();
  2457. return;
  2458. }
  2459. }
  2460. /* If we make it here, we've got a new image type to register */
  2461. new_registry = (_DtGrRegistryRec *) XtRealloc ((char *) new_registry,
  2462. sizeof(_DtGrRegistryRec) * (new_registry_count + 1));
  2463. new_registry[new_registry_count].image_type = XtNewString (image_type);
  2464. new_registry[new_registry_count].convert_proc = convert_proc;
  2465. new_registry[new_registry_count].destroy_context_proc =
  2466. destroy_context_proc;
  2467. new_registry_count++;
  2468. _DtHelpProcessUnlock();
  2469. /* Newly registered type, so return NULL for current function params */
  2470. if (current_convert_proc != NULL)
  2471. *current_convert_proc = NULL;
  2472. if (current_destroy_proc != NULL)
  2473. *current_destroy_proc = NULL;
  2474. return;
  2475. }
  2476. /******************************************************************************
  2477. *
  2478. * Input stream functions
  2479. *
  2480. * These functions allow the creation and manipulation of a stream that
  2481. * can be associated with either a file or a buffer. They are intended to
  2482. * be used by image data to pixmap converter functions that need to handle
  2483. * both image files and in-memory buffers containing file data.
  2484. *
  2485. *****************************************************************************/
  2486. /******************************************************************************
  2487. *
  2488. * Function _DtGrOpenFile
  2489. *
  2490. * Opens a file for reading and associates it with the specified stream. If
  2491. * the file is compressed, the function uncompresses it prior to opening.
  2492. * Returns 0 for success, -1 or the value of errno as set by fopen for
  2493. * failure.
  2494. *
  2495. *****************************************************************************/
  2496. int _DtGrOpenFile(
  2497. _DtGrStream *stream,
  2498. const char *path)
  2499. {
  2500. char *fname = NULL;
  2501. if (stream == NULL)
  2502. return (-1); /* Failure */
  2503. /* Uncompress the file if necessary and obtain the new filename */
  2504. if (_DtHelpCeGetUncompressedFileName(path, &fname) == -1)
  2505. {
  2506. stream->type = _DtGrNONE;
  2507. return (-1); /* Failure */
  2508. }
  2509. /* Open the file */
  2510. stream->source.file.fileptr = fopen(fname,"r");
  2511. if (stream->source.file.fileptr == NULL)
  2512. {
  2513. /* Failure, couldn't open the file */
  2514. stream->type = _DtGrNONE;
  2515. if ((fname != path) && (fname != NULL))
  2516. {
  2517. unlink (fname);
  2518. free (fname);
  2519. }
  2520. return(errno);
  2521. }
  2522. /* Set the stream type and copy the filename */
  2523. stream->type = _DtGrFILE;
  2524. stream->source.file.filename = XtNewString(path);
  2525. /* If the file was uncompressed and renamed, save the new name */
  2526. if (fname == path)
  2527. stream->source.file.uncompressed_filename = NULL;
  2528. else
  2529. stream->source.file.uncompressed_filename = fname;
  2530. return(0); /* Success */
  2531. }
  2532. /******************************************************************************
  2533. *
  2534. * Function _DtGrOpenBuffer
  2535. *
  2536. * Associates the specified in-memory buffer with the specified stream.
  2537. * The function returns 0 for success and the EINVAL error code for failure.
  2538. *
  2539. *****************************************************************************/
  2540. int _DtGrOpenBuffer(
  2541. _DtGrStream *stream,
  2542. const char *buffer,
  2543. int buffer_size)
  2544. {
  2545. if ((stream == NULL) || (buffer == NULL) || (buffer_size < 0))
  2546. {
  2547. if (stream != NULL)
  2548. stream->type = _DtGrNONE;
  2549. return(EINVAL); /* Failure */
  2550. }
  2551. /* Set the appropriate stream fields */
  2552. stream->type = _DtGrBUFFER;
  2553. stream->source.buffer.base = buffer;
  2554. stream->source.buffer.size = buffer_size;
  2555. stream->source.buffer.current = (char *)buffer;
  2556. stream->source.buffer.end = (char *)(buffer + buffer_size - 1);
  2557. return(0); /* Success */
  2558. }
  2559. /******************************************************************************
  2560. *
  2561. * Function _DtGrCloseStream
  2562. *
  2563. * The function closes the specified stream by setting the stream type to
  2564. * _DtGrNONE. If the stream is associated with a file, then the file is
  2565. * closed and the filename in the stream structure is freed. If the file
  2566. * required decompression, then the uncompressed file is unlinked and the
  2567. * uncompressed filename is freed.
  2568. *
  2569. *****************************************************************************/
  2570. int _DtGrCloseStream(
  2571. _DtGrStream *stream)
  2572. {
  2573. int status;
  2574. if ((stream == NULL) || (stream->type == _DtGrNONE))
  2575. return(EOF); /* Failure */
  2576. if (stream->type == _DtGrFILE)
  2577. {
  2578. /* Close the file and free the filename */
  2579. status = fclose(stream->source.file.fileptr);
  2580. if (stream->source.file.filename != NULL)
  2581. XtFree(stream->source.file.filename);
  2582. if (stream->source.file.uncompressed_filename != NULL)
  2583. {
  2584. /* Unlink the uncompressed file and free the filename */
  2585. unlink(stream->source.file.uncompressed_filename);
  2586. free(stream->source.file.uncompressed_filename);
  2587. }
  2588. }
  2589. else if (stream->type == _DtGrBUFFER)
  2590. {
  2591. status = 0; /* Success */
  2592. }
  2593. stream->type = _DtGrNONE;
  2594. return (status);
  2595. }
  2596. /******************************************************************************
  2597. *
  2598. * Function _DtGrRead
  2599. *
  2600. * Stream version of fread, reads data from a stream into a buffer. If the
  2601. * stream is file-associated, a call to fread is made. If the stream is
  2602. * buffer-associated, an analogous operation is performed on the stream
  2603. * buffer. The number of items read from the stream is returned to the caller.
  2604. *
  2605. *****************************************************************************/
  2606. size_t _DtGrRead(
  2607. void *buffer,
  2608. size_t size,
  2609. size_t num_items,
  2610. _DtGrStream *stream)
  2611. {
  2612. unsigned nleft;
  2613. int n;
  2614. if ((stream == NULL) || (stream->type == _DtGrNONE))
  2615. return(0);
  2616. if (stream->type == _DtGrFILE)
  2617. {
  2618. return (fread(buffer, size, num_items, stream->source.file.fileptr));
  2619. }
  2620. else if (stream->type == _DtGrBUFFER)
  2621. {
  2622. /* This code mirrors that of fread in libc */
  2623. if (size <= 0 || num_items <= 0)
  2624. return (0);
  2625. for (nleft = num_items * size; ; )
  2626. {
  2627. if (stream->source.buffer.current > stream->source.buffer.end)
  2628. {
  2629. /* past end of stream */
  2630. if (stream->source.buffer.current ==
  2631. stream->source.buffer.end + 1)
  2632. return (num_items - (nleft + size - 1)/size);
  2633. stream->source.buffer.current--;
  2634. }
  2635. n = (nleft < stream->source.buffer.end -
  2636. stream->source.buffer.current + 1 ? nleft :
  2637. stream->source.buffer.end -
  2638. stream->source.buffer.current + 1);
  2639. /* Copy the items into the caller-supplied buffer */
  2640. buffer = (char *)memcpy(buffer,
  2641. (void *) stream->source.buffer.current,
  2642. (size_t)n) + n;
  2643. stream->source.buffer.current += n;
  2644. if ((nleft -= n) == 0)
  2645. return (num_items);
  2646. }
  2647. }
  2648. }
  2649. /******************************************************************************
  2650. *
  2651. * Function _DtGrSeek
  2652. *
  2653. * Stream version of fseek, repositions the file or buffer pointer of a
  2654. * stream. If the stream is file-associated, the return value is the value
  2655. * returned by fseek. If the stream is buffer-associated, 0 is returned if
  2656. * the requested position is inside the buffer, -1 if it is not.
  2657. *
  2658. *****************************************************************************/
  2659. int _DtGrSeek(
  2660. _DtGrStream *stream,
  2661. long offset,
  2662. int whence)
  2663. {
  2664. long newpos;
  2665. if ((stream == NULL) || (stream->type == _DtGrNONE))
  2666. return(-1); /* Failure */
  2667. if (stream->type == _DtGrFILE)
  2668. {
  2669. return (fseek(stream->source.file.fileptr,offset,whence));
  2670. }
  2671. else if (stream->type == _DtGrBUFFER)
  2672. {
  2673. switch (whence)
  2674. {
  2675. case SEEK_SET:
  2676. newpos = (long)stream->source.buffer.base + offset;
  2677. break;
  2678. case SEEK_CUR:
  2679. newpos = (long)stream->source.buffer.current + offset;
  2680. break;
  2681. case SEEK_END:
  2682. newpos = (long)stream->source.buffer.end + 1 + offset;
  2683. break;
  2684. default:
  2685. return (-1);
  2686. }
  2687. if ((newpos >= (long)stream->source.buffer.base) &&
  2688. (newpos <= (long)stream->source.buffer.end))
  2689. {
  2690. /* New position is within buffer, reposition pointer */
  2691. stream->source.buffer.current = (char *)newpos;
  2692. return(0); /* Success */
  2693. }
  2694. else
  2695. return(-1); /* Failure */
  2696. }
  2697. }
  2698. /******************************************************************************
  2699. *
  2700. * Function _DtGrGetChar
  2701. *
  2702. * Stream version of fgetc, reads a character from a stream and advances the
  2703. * stream position. The next byte in the stream is returned, or EOF if an
  2704. * error occurs or the end of the stream is reached.
  2705. *
  2706. *****************************************************************************/
  2707. int _DtGrGetChar(
  2708. _DtGrStream *stream)
  2709. {
  2710. if ((stream == NULL) || (stream->type == _DtGrNONE))
  2711. return(EOF);
  2712. if (stream->type == _DtGrFILE)
  2713. {
  2714. return (fgetc(stream->source.file.fileptr));
  2715. }
  2716. else if (stream->type == _DtGrBUFFER)
  2717. {
  2718. if (stream->source.buffer.current > stream->source.buffer.end)
  2719. return (EOF);
  2720. else
  2721. return ((unsigned char) *(stream->source.buffer.current++));
  2722. }
  2723. }
  2724. /******************************************************************************
  2725. *
  2726. * Function _DtGrGetString
  2727. *
  2728. * Stream version of fgets, reads a string from a stream and advances the
  2729. * stream position. If an error occurs or the end of the stream is
  2730. * encountered and no characters have been read, no characters are transferred
  2731. * to the buffer and a NULL pointer is returned. Otherwise, the buffer is
  2732. * returned.
  2733. *
  2734. *****************************************************************************/
  2735. char *_DtGrGetString(
  2736. char *buffer,
  2737. int num_bytes,
  2738. _DtGrStream *stream)
  2739. {
  2740. char *p, *save = buffer;
  2741. int i;
  2742. if ((stream == NULL) || (stream->type == _DtGrNONE))
  2743. return(NULL); /* Failure */
  2744. if (stream->type == _DtGrFILE)
  2745. {
  2746. return (fgets(buffer,num_bytes,stream->source.file.fileptr));
  2747. }
  2748. else if (stream->type == _DtGrBUFFER)
  2749. {
  2750. /* This code mirrors that of fgets in libc */
  2751. for (num_bytes--; num_bytes > 0; num_bytes -= i)
  2752. {
  2753. if (stream->source.buffer.current > stream->source.buffer.end)
  2754. {
  2755. if (stream->source.buffer.current ==
  2756. stream->source.buffer.end + 1)
  2757. {
  2758. if (save == buffer)
  2759. return (NULL);
  2760. break; /* no more data */
  2761. }
  2762. stream->source.buffer.current--;
  2763. }
  2764. i = (num_bytes < stream->source.buffer.end -
  2765. stream->source.buffer.current + 1 ? num_bytes :
  2766. stream->source.buffer.end -
  2767. stream->source.buffer.current + 1);
  2768. /* Copy the data into the buffer */
  2769. if ((p = memccpy((void *)buffer,
  2770. (void *)stream->source.buffer.current,
  2771. (int)'\n',(size_t)i)) != NULL)
  2772. i = p - buffer;
  2773. buffer += i;
  2774. stream->source.buffer.current += i;
  2775. if (p != NULL)
  2776. break; /* found '\n' in buffer */
  2777. }
  2778. *buffer = '\0';
  2779. return (save);
  2780. }
  2781. }