gdevcdj.c 111 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810
  1. /* Copyright (C) 1991, 1995, 1996, 1997, 1998, 1999, 2000 Aladdin Enterprises. All rights reserved.
  2. This file is part of AFPL Ghostscript.
  3. AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND. No author or
  4. distributor accepts any responsibility for the consequences of using it, or
  5. for whether it serves any particular purpose or works at all, unless he or
  6. she says so in writing. Refer to the Aladdin Free Public License (the
  7. "License") for full details.
  8. Every copy of AFPL Ghostscript must include a copy of the License, normally
  9. in a plain ASCII text file named PUBLIC. The License grants you the right
  10. to copy, modify and redistribute AFPL Ghostscript, but only under certain
  11. conditions described in the License. Among other things, the License
  12. requires that the copyright notice and this notice be preserved on all
  13. copies.
  14. */
  15. /*$Id: gdevcdj.c,v 1.5 2001/03/13 06:51:39 ghostgum Exp $*/
  16. /* H-P and Canon colour printer drivers */
  17. /****************************************************************
  18. * The code in this file was contributed by the authors whose names and/or
  19. * e-mail addresses appear below: Aladdin Enterprises takes no
  20. * responsibility for it. In the past, we have tried to keep it working,
  21. * but too many users have made too many "improvements" without regard to
  22. * the overall structure; the last three "improvements" required me to spend
  23. * several hours fixing each one so that the code worked again at all. For
  24. * this reason, no further changes to this file will be accepted. We are
  25. * planning eventually to get these drivers rewritten from scratch.
  26. *
  27. * L. Peter Deutsch
  28. * Aladdin Enterprises
  29. * February 28, 1996
  30. ****************************************************************/
  31. /*
  32. * Change history:
  33. * 2000-08-20 Jonathan Kamens <jik@kamens.brookline.ma.us>:
  34. * change to support printers with different X and Y resolution.
  35. */
  36. /*
  37. * Important compilation notes (YA).
  38. *
  39. * You may also try the cdj550cmyk driver after having defined
  40. * USE_CDJ550_CMYK and added the needed definition in devs.mak. Not tried!
  41. * (I have a BJC!) Also note that modes descriptions of CMYK printing
  42. * is done under the BJC section of devices.doc.
  43. *
  44. * CMYK to RGB conversion is made a la GhostScript unless you define
  45. * the preprocessor symbol USE_ADOBE_CMYK_RGB.
  46. *
  47. * Ghostscript: R = (1.0 - C) * (1.0 - K)
  48. * Adobe: R = 1.0 - min(1.0, C + K)
  49. *
  50. * (and similarly for G and B). Ghostscript claims its method achieves
  51. * better results.
  52. *
  53. * For the BJC drivers, define BJC_DEFAULT_CENTEREDAREA if you want to
  54. * have the same top and bottom margins (default to use the tallest
  55. * imageable area available, usually with a top margin smaller than
  56. * the bottom one). Defining USE_RECOMMENDED_MARGINS has the same
  57. * effect and also sets these margins to 12.4 mm. Other compilation
  58. * defines are explained in devices.doc.
  59. *
  60. * You can also define BJC_INIT_800_AS_600 to not use BJC-800-specific code
  61. * in the page initialization sequence (normally not useful to you at all,
  62. * just for my debugging of the driver margins).
  63. *
  64. */
  65. #include "std.h" /* to stop stdlib.h redefining types */
  66. #include <stdlib.h> /* for rand() */
  67. #include "gdevprn.h"
  68. #include "gdevpcl.h"
  69. #include "gsparam.h"
  70. #include "gsstate.h"
  71. /* Conversion stuff. */
  72. #include "gxlum.h"
  73. /* Canon stuff */
  74. #include "gdevbjc.h"
  75. /***
  76. *** This file contains multiple drivers. The main body of code, and all
  77. *** but the DesignJet driver, were contributed by George Cameron;
  78. *** please contact g.cameron@biomed.abdn.ac.uk if you have questions.
  79. * 1 - cdj500: HP DeskJet 500C
  80. * 2 - cdj550: HP DeskJet 550C
  81. * 3 - pjxl300: HP PaintJet XL300
  82. * 4 - pj: HP PaintJet
  83. * 5 - pjxl: HP PaintJet XL
  84. * 6 - declj250: DEC LJ250
  85. *** The DesignJet 650C driver was contributed by Koert Zeilstra;
  86. *** please contact koert@zen.cais.com if you have questions.
  87. * 7 - dnj650c HP DesignJet 650C
  88. *** The LaserJet 4 driver with dithering was contributed by Eckhard
  89. *** Rueggeberg; please contact eckhard@ts.go.dlr.de if you have questions.
  90. * 8 - lj4dith: HP LaserJet 4 with dithering
  91. *** The ESC/P driver (for Epson ESC/P compatible printers) was written by
  92. *** Yoshio Kuniyoshi <yoshio@nak.math.keio.ac.jp>, but is not maintained at
  93. *** the moment.
  94. * 9 - esc/p: Epson ESC/P-compatible printers
  95. *** The BJC600 driver (which also works for BJC4000) was written first
  96. *** by Yoshio Kuniyoshi <yoshio@nak.math.keio.ac.jp> and later modified by
  97. *** Yves Arrouye <yves.arrouye@usa.net>. The current driver has been
  98. *** completely rewritten by me (YA) for good color handling.
  99. * 10 - bjc600: BJC 600//4000 printers
  100. *** The BJC800 driver is based on the bjc600 one. By YA too.
  101. * 11 - bjc800: BJC 800 printer
  102. ***/
  103. /*
  104. * All of the HP-like drivers have 8-bit (monochrome), 16-bit and 24-bit
  105. * (colour) and for the DJ 550C 32-bit, (colour, cmyk mode)
  106. * options in addition to the usual 1-bit and 3-bit modes
  107. * It is also possible to set various printer-specific parameters
  108. * from the gs command line, eg.
  109. *
  110. * gs -sDEVICE=cdj550 -dBitsPerPixel=16 -dDepletion=1 -dShingling=2 tiger.ps
  111. *
  112. * Please consult the appropriate section in the devices.doc file for
  113. * further details on all these drivers.
  114. *
  115. * All of the BJC-like drivers have 1-bit and 8-bit monochrome modes, 8-bit,
  116. * 16-bit, 24-bit and 32-bit colour cmyk mode (the 8-bit monochrome mode
  117. * is called "4-bit". If you want to add a CMYK printer, look at the
  118. * bjc6000/bjc800 devices declarations and initialization.
  119. *
  120. * If you want to support different color components for the same depth
  121. * on a non-CMYK printer, look how this is done for CMYK printers in
  122. * cdj_set_bpp.
  123. *
  124. */
  125. /*
  126. * This taken from gsdparam.c. I hope it will be useable directly some day.
  127. *
  128. */
  129. #define BEGIN_ARRAY_PARAM(pread, pname, pa, psize, e)\
  130. switch ( ncode = pread(plist, (oname = pname), &pa) )\
  131. {\
  132. case 0:\
  133. if ( pa.size != psize )\
  134. code = gs_error_rangecheck;\
  135. else {
  136. /* The body of the processing code goes here. */
  137. /* If it succeeds, it should do a 'break'; */
  138. /* if it fails, it should set ecode and fall through. */
  139. #define END_PARAM(pa, e)\
  140. }\
  141. goto e;\
  142. default:\
  143. code = ncode;\
  144. e: param_signal_error(plist, oname, code);\
  145. case 1:\
  146. pa.data = 0; /* mark as not filled */\
  147. }
  148. private int cdj_param_check_bytes(P5(gs_param_list *, gs_param_name, const byte *, uint, bool));
  149. private int cdj_param_check_float(P4(gs_param_list *, gs_param_name, floatp, bool));
  150. #define cdj_param_check_string(plist, pname, str, defined)\
  151. cdj_param_check_bytes(plist, pname, (const byte *)str, strlen(str), defined)
  152. /*
  153. * Drivers stuff.
  154. *
  155. */
  156. #define DESKJET_PRINT_LIMIT 0.04 /* 'real' top margin? */
  157. #define PAINTJET_PRINT_LIMIT 0.0 /* This is a guess.. */
  158. #define ESC_P_PRINT_LIMIT 0.335
  159. /* Margins are left, bottom, right, top. */
  160. #define DESKJET_MARGINS_LETTER 0.25, 0.50, 0.25, 0.167
  161. #define DESKJET_MARGINS_A4 0.125, 0.50, 0.143, 0.167
  162. #define LJET4_MARGINS 0.26, 0.0, 0.0, 0.0
  163. /* The PaintJet and DesignJet seem to have the same margins */
  164. /* regardless of paper size. */
  165. #define PAINTJET_MARGINS 0.167, 0.167, 0.167, 0.167
  166. #define DESIGNJET_MARGINS 0.167, 0.167, 0.167, 0.167
  167. /*
  168. * With ESC/P commands, BJC-600 can print no more than 8 inches width.
  169. * So it can't use full width of letter size paper. Since non printable
  170. * left side area is 0.134 inch, we set up margins as follows.
  171. *
  172. * Note to readers: the bjc drivers series do *not* use ESC/P commands
  173. * but raster ops. Configuration of these drivers can be done through
  174. * the gdevbjc.h file.
  175. *
  176. */
  177. #define ESC_P_MARGINS_LETTER 0.134, 0.276+0.2, 0.366+0.01, 0.335
  178. #define ESC_P_MARGINS_A4 0.134, 0.276+0.2, 0.166+0.01, 0.335
  179. /* Define bits-per-pixel for generic drivers - default is 24-bit mode */
  180. #ifndef BITSPERPIXEL
  181. # define BITSPERPIXEL 24
  182. #endif
  183. /*
  184. * The following use of size_of rather than sizeof is required to work
  185. * around a bug in Microsoft Visual C++ 5.0, which considers the THRESHOLD
  186. * value (128 << SHIFT) to be unsigned because SHIFT is unsigned (because
  187. * sizeof() is unsigned).
  188. */
  189. #define W size_of(word)
  190. #define I size_of(int)
  191. #define invert_word(v)\
  192. ((v) >> 24) + (((v) >> 8) & 0xff00L) +\
  193. (((word)(v) << 8) & 0xff0000L) + ((word)(v) << 24)
  194. /* Printer types */
  195. #define DJ500C 0
  196. #define DJ550C 1
  197. #define PJXL300 2
  198. #define PJ180 3
  199. #define PJXL180 4
  200. #define DECLJ250 5
  201. #define DNJ650C 6
  202. #define LJ4DITH 7
  203. #define ESC_P 8
  204. #define BJC600 9
  205. #define BJC800 10
  206. /* No. of ink jets (used to minimise head movements) */
  207. #define HEAD_ROWS_MONO 50
  208. #define HEAD_ROWS_COLOUR 16
  209. /* Colour mapping procedures */
  210. private dev_proc_map_cmyk_color (gdev_cmyk_map_cmyk_color);
  211. private dev_proc_map_rgb_color (gdev_cmyk_map_rgb_color);
  212. private dev_proc_map_color_rgb (gdev_cmyk_map_color_rgb);
  213. private dev_proc_map_rgb_color (gdev_pcl_map_rgb_color);
  214. private dev_proc_map_color_rgb (gdev_pcl_map_color_rgb);
  215. /* Print-page, parameters and miscellaneous procedures */
  216. private dev_proc_open_device(dj500c_open);
  217. private dev_proc_open_device(dj550c_open);
  218. private dev_proc_open_device(dnj650c_open);
  219. private dev_proc_open_device(lj4dith_open);
  220. private dev_proc_open_device(pj_open);
  221. private dev_proc_open_device(pjxl_open);
  222. private dev_proc_open_device(pjxl300_open);
  223. private dev_proc_open_device(escp_open);
  224. private dev_proc_open_device(bjc_open);
  225. private dev_proc_print_page(declj250_print_page);
  226. private dev_proc_print_page(dj500c_print_page);
  227. private dev_proc_print_page(dj550c_print_page);
  228. private dev_proc_print_page(dnj650c_print_page);
  229. private dev_proc_print_page(lj4dith_print_page);
  230. private dev_proc_print_page(pj_print_page);
  231. private dev_proc_print_page(pjxl_print_page);
  232. private dev_proc_print_page(pjxl300_print_page);
  233. private dev_proc_print_page(escp_print_page);
  234. private dev_proc_print_page(bjc_print_page);
  235. private dev_proc_get_params(cdj_get_params);
  236. private dev_proc_get_params(pjxl_get_params);
  237. private dev_proc_get_params(bjc_get_params);
  238. #define ep_get_params cdj_get_params
  239. private dev_proc_put_params(cdj_put_params);
  240. private dev_proc_put_params(pj_put_params);
  241. private dev_proc_put_params(pjxl_put_params);
  242. private dev_proc_put_params(bjc_put_params);
  243. #define ep_put_params cdj_put_params
  244. /* The device descriptors */
  245. #define gx_prn_colour_device_common \
  246. gx_prn_device_common; \
  247. short cmyk; /* 0: not CMYK-capable, > 0: printing CMYK, */ \
  248. /* < 0 : CMYK-capable, not printing CMYK */ \
  249. uint default_depth; /* Used only for CMYK-capable printers now. */ \
  250. uint correction
  251. typedef struct gx_device_cdj_s gx_device_cdj;
  252. struct gx_device_cdj_s {
  253. gx_device_common;
  254. gx_prn_colour_device_common;
  255. int shingling; /* Interlaced, multi-pass printing */
  256. int depletion; /* 'Intelligent' dot-removal */
  257. };
  258. typedef struct gx_device_pjxl_s gx_device_pjxl;
  259. struct gx_device_pjxl_s {
  260. gx_device_common;
  261. gx_prn_colour_device_common;
  262. int printqual; /* Mechanical print quality */
  263. int rendertype; /* Driver or printer dithering control */
  264. };
  265. typedef struct gx_device_hp_s gx_device_hp;
  266. struct gx_device_hp_s {
  267. gx_device_common;
  268. gx_prn_colour_device_common;
  269. };
  270. typedef struct gx_device_hp_s gx_device_pj;
  271. typedef struct gx_device_bjc600_s gx_device_bjc600;
  272. typedef struct gx_device_bjc800_s gx_device_bjc800;
  273. typedef struct gx_device_bjc800_s gx_device_bjc;
  274. #define bjc_params_common \
  275. bool manualFeed; /* Use manual feed */ \
  276. int mediaType; /* Cf. strings below */ \
  277. bool mediaWeight_isSet; /* Say if weight is an integer or null */ \
  278. int mediaWeight; /* Weigth of the media */ \
  279. int printQuality; /* Cf. strings below */ \
  280. bool ditheringType; /* Do dithering */ \
  281. int colorComponents; /* The number of *desired* color comps */ \
  282. int printColors /* 0: Transparent, \
  283. 1: C, 2: M, 4: Y, 7: K (Color decomp). \
  284. if > 8, print in black ink. */
  285. typedef struct {
  286. bjc_params_common;
  287. bool monochromePrint; /* Print with black only */
  288. } bjc600_params;
  289. typedef struct {
  290. bjc_params_common;
  291. } bjc_params;
  292. typedef bjc_params bjc800_params;
  293. #define gx_bjc_device_common \
  294. gx_device_common; \
  295. gx_prn_colour_device_common; \
  296. int ptype; \
  297. float printLimit
  298. struct gx_device_bjc600_s {
  299. gx_bjc_device_common;
  300. bjc600_params bjc_p;
  301. };
  302. struct gx_device_bjc800_s {
  303. gx_bjc_device_common;
  304. bjc800_params bjc_p;
  305. };
  306. typedef struct {
  307. gx_device_common;
  308. gx_prn_colour_device_common;
  309. } gx_device_colour_prn;
  310. /* Use the cprn_device macro to access generic fields (like cmyk,
  311. default_depth and correction), and specific macros for specific
  312. devices. */
  313. #define cprn_device ((gx_device_colour_prn*) pdev)
  314. #define cdj ((gx_device_cdj *)pdev)
  315. #define pjxl ((gx_device_pjxl *)pdev)
  316. #define pj ((gx_device_pj *)pdev)
  317. #define bjc ((gx_device_bjc*) pdev)
  318. #define bjc600 ((gx_device_bjc600*) pdev)
  319. #define bjc800 ((gx_device_bjc800*) pdev)
  320. #define bjcparams (bjc->bjc_p)
  321. #define bjc600params (bjc600->bjc_p)
  322. #define bjc800params (bjc800->bjc_p)
  323. #define bjcversion(p) (((gx_device_bjc*) pdev)->ptype == BJC800 ? \
  324. BJC_BJC800_VERSION : BJC_BJC600_VERSION)
  325. #define bjcversionstring(p) (((gx_device_bjc*) pdev)->ptype == BJC800 ? \
  326. BJC_BJC800_VERSIONSTR : BJC_BJC600_VERSIONSTR)
  327. #define bjcthickpaper(l) \
  328. (bjcparams.mediaWeight_isSet && bjcparams.mediaWeight > l)
  329. #define bjc600thickpaper() bjcthickpaper(BJC600_MEDIAWEIGHT_THICKLIMIT)
  330. #define bjc800thickpaper() bjcthickpaper(BJC800_MEDIAWEIGHT_THICKLIMIT)
  331. /* The basic structure for all printers. Note the presence of the cmyk, depth
  332. and correct fields even if soem are not used by all printers. */
  333. #define prn_colour_device_body(dtype, procs, dname, w10, h10, xdpi, ydpi, lm, bm, rm, tm, ncomp, depth, mg, mc, dg, dc, print_page, cmyk, correct)\
  334. prn_device_body(dtype, procs, dname, w10, h10, xdpi, ydpi, lm, bm, rm, tm, ncomp, depth, mg, mc, dg, dc, print_page), cmyk, depth /* default */, correct
  335. /* Note: the computation of color_info values here must match */
  336. /* the computation in the cdj_set_bpp procedure below. */
  337. #define prn_hp_colour_device(dtype, procs, dev_name, x_dpi, y_dpi, bpp, print_page, correct)\
  338. prn_colour_device_body(dtype, procs, dev_name,\
  339. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0,\
  340. (bpp == 32 ? 4 : (bpp == 1 || bpp == 8) ? 1 : 3), bpp,\
  341. (bpp >= 8 ? 255 : 1), (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0),\
  342. (bpp >= 8 ? 5 : 2), (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0),\
  343. print_page, 0 /* cmyk */, correct)
  344. #define prn_cmyk_colour_device(dtype, procs, dev_name, x_dpi, y_dpi, bpp, print_page, correct)\
  345. prn_colour_device_body(dtype, procs, dev_name,\
  346. DEFAULT_WIDTH_10THS, DEFAULT_HEIGHT_10THS, x_dpi, y_dpi, 0, 0, 0, 0,\
  347. ((bpp == 1 || bpp == 4) ? 1 : 4), bpp,\
  348. (bpp > 8 ? 255 : 1), (1 << (bpp >> 2)) - 1, /* max_gray, max_color */\
  349. (bpp > 8 ? 5 : 2), (bpp > 8 ? 5 : bpp > 1 ? 2 : 0),\
  350. print_page, 1 /* cmyk */, correct)
  351. #define bjc_device(dtype, p, d, x, y, b, pp, c) \
  352. prn_cmyk_colour_device(dtype, p, d, x, y, b, pp, c)
  353. #define cdj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, correction, shingling, depletion)\
  354. { prn_hp_colour_device(gx_device_cdj, procs, dev_name, x_dpi, y_dpi, bpp, print_page, correction),\
  355. shingling,\
  356. depletion\
  357. }
  358. #define pjxl_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, printqual, rendertype)\
  359. { prn_hp_colour_device(gx_device_pjxl, procs, dev_name, x_dpi, y_dpi, bpp, print_page, 0), \
  360. printqual,\
  361. rendertype\
  362. }
  363. #define pj_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page)\
  364. { prn_hp_colour_device(gx_device_pj, procs, dev_name, x_dpi, y_dpi, bpp, print_page, 0) }
  365. #define bjc600_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, t, mf, mt, mws, mw, pq, dt, cc, pc, mp) \
  366. { bjc_device(gx_device_bjc600, procs, dev_name, x_dpi, y_dpi, bpp, print_page, 0),\
  367. t, 0., { mf, mt, mws, mw, pq, dt, cc, pc, mp }\
  368. }
  369. #define bjc800_device(procs, dev_name, x_dpi, y_dpi, bpp, print_page, t, mf, mt, mws, mw, pq, dt, cc, pc) \
  370. { bjc_device(gx_device_bjc800, procs, dev_name, x_dpi, y_dpi, bpp, print_page, 0),\
  371. t, 0., { mf, mt, mws, mw, pq, dt, cc, pc }\
  372. }
  373. #define hp_colour_procs(proc_colour_open, proc_get_params, proc_put_params) {\
  374. proc_colour_open,\
  375. gx_default_get_initial_matrix,\
  376. gx_default_sync_output,\
  377. gdev_prn_output_page,\
  378. gdev_prn_close,\
  379. gdev_pcl_map_rgb_color,\
  380. gdev_pcl_map_color_rgb,\
  381. NULL, /* fill_rectangle */\
  382. NULL, /* tile_rectangle */\
  383. NULL, /* copy_mono */\
  384. NULL, /* copy_color */\
  385. NULL, /* draw_line */\
  386. gx_default_get_bits,\
  387. proc_get_params,\
  388. proc_put_params\
  389. }
  390. #define cmyk_colour_procs(proc_colour_open, proc_get_params, proc_put_params) {\
  391. proc_colour_open,\
  392. gx_default_get_initial_matrix,\
  393. gx_default_sync_output,\
  394. gdev_prn_output_page,\
  395. gdev_prn_close,\
  396. NULL /* map_rgb_color */,\
  397. gdev_cmyk_map_color_rgb,\
  398. NULL /* fill_rectangle */,\
  399. NULL /* tile_rectangle */,\
  400. NULL /* copy_mono */,\
  401. NULL /* copy_color */,\
  402. NULL /* draw_line */,\
  403. gx_default_get_bits,\
  404. proc_get_params,\
  405. proc_put_params,\
  406. gdev_cmyk_map_cmyk_color\
  407. }
  408. private gx_device_procs cdj500_procs =
  409. hp_colour_procs(dj500c_open, cdj_get_params, cdj_put_params);
  410. private gx_device_procs cdj550_procs =
  411. hp_colour_procs(dj550c_open, cdj_get_params, cdj_put_params);
  412. #ifdef USE_CDJ550_CMYK
  413. private gx_device_procs cdj550cmyk_procs =
  414. cmyk_colour_procs(dj550c_open, cdj_get_params, cdj_put_params);
  415. #endif
  416. private gx_device_procs dnj650c_procs =
  417. hp_colour_procs(dnj650c_open, cdj_get_params, cdj_put_params);
  418. private gx_device_procs lj4dith_procs =
  419. hp_colour_procs(lj4dith_open, cdj_get_params, cdj_put_params);
  420. private gx_device_procs pj_procs =
  421. hp_colour_procs(pj_open, gdev_prn_get_params, pj_put_params);
  422. private gx_device_procs pjxl_procs =
  423. hp_colour_procs(pjxl_open, pjxl_get_params, pjxl_put_params);
  424. private gx_device_procs pjxl300_procs =
  425. hp_colour_procs(pjxl300_open, pjxl_get_params, pjxl_put_params);
  426. private gx_device_procs bjc_procs =
  427. cmyk_colour_procs(bjc_open, bjc_get_params, bjc_put_params);
  428. private gx_device_procs escp_procs =
  429. hp_colour_procs(escp_open, ep_get_params, ep_put_params);
  430. gx_device_cdj far_data gs_cdjmono_device =
  431. cdj_device(cdj500_procs, "cdjmono", 300, 300, 1,
  432. dj500c_print_page, 4, 0, 1);
  433. gx_device_cdj far_data gs_cdeskjet_device =
  434. cdj_device(cdj500_procs, "cdeskjet", 300, 300, 3,
  435. dj500c_print_page, 4, 2, 1);
  436. gx_device_cdj far_data gs_cdjcolor_device =
  437. cdj_device(cdj500_procs, "cdjcolor", 300, 300, 24,
  438. dj500c_print_page, 4, 2, 1);
  439. gx_device_cdj far_data gs_cdj500_device =
  440. cdj_device(cdj500_procs, "cdj500", 300, 300, BITSPERPIXEL,
  441. dj500c_print_page, 4, 2, 1);
  442. gx_device_cdj far_data gs_cdj550_device =
  443. cdj_device(cdj550_procs, "cdj550", 300, 300, BITSPERPIXEL,
  444. dj550c_print_page, 0, 2, 1);
  445. #ifdef USE_CDJ550_CMYK
  446. gx_device_cdj far_data gs_cdj550cmyk_device = {
  447. prn_cmyk_colour_device(cdj550cmyk_procs, "cdj550cmyk", 300, 300,
  448. BITSPERPIXEL, dj550c_print_page, 0), 2, 1
  449. };
  450. #endif
  451. gx_device_pj far_data gs_declj250_device =
  452. pj_device(pj_procs, "declj250", 180, 180, BITSPERPIXEL,
  453. declj250_print_page);
  454. gx_device_cdj far_data gs_dnj650c_device =
  455. cdj_device(dnj650c_procs, "dnj650c", 300, 300, BITSPERPIXEL,
  456. dnj650c_print_page, 0, 2, 1);
  457. gx_device_cdj far_data gs_lj4dith_device =
  458. cdj_device(lj4dith_procs, "lj4dith", 600, 600, 8,
  459. lj4dith_print_page, 4, 0, 1);
  460. gx_device_pj far_data gs_pj_device =
  461. pj_device(pj_procs, "pj", 180, 180, BITSPERPIXEL,
  462. pj_print_page);
  463. gx_device_pjxl far_data gs_pjxl_device =
  464. pjxl_device(pjxl_procs, "pjxl", 180, 180, BITSPERPIXEL,
  465. pjxl_print_page, 0, 0);
  466. gx_device_pjxl far_data gs_pjxl300_device =
  467. pjxl_device(pjxl300_procs, "pjxl300", 300, 300, BITSPERPIXEL,
  468. pjxl300_print_page, 0, 0);
  469. gx_device_cdj far_data gs_escp_device =
  470. cdj_device(escp_procs, "escp", 360, 360, 8,
  471. escp_print_page, 0, 0, 1);
  472. gx_device_cdj far_data gs_escpc_device =
  473. cdj_device(escp_procs, "escpc", 360, 360, 24,
  474. escp_print_page, 0, 0, 1);
  475. /* Args of bjc drivers are manualFeed, mediaType, printQuality, printColor,
  476. mediaWeight_isSet, mediaWeight, (monochromePrint) */
  477. gx_device_bjc600 far_data gs_bjc600_device =
  478. bjc600_device(
  479. bjc_procs,
  480. BJC_BJC600,
  481. BJC600_DEFAULT_RESOLUTION,
  482. BJC600_DEFAULT_RESOLUTION,
  483. BJC600_DEFAULT_BITSPERPIXEL,
  484. bjc_print_page,
  485. BJC600,
  486. BJC600_DEFAULT_MANUALFEED,
  487. BJC600_DEFAULT_MEDIATYPE,
  488. BJC600_DEFAULT_SETMEDIAWEIGHT,
  489. BJC600_DEFAULT_MEDIAWEIGHT,
  490. BJC600_DEFAULT_PRINTQUALITY,
  491. BJC600_DEFAULT_DITHERINGTYPE,
  492. BJC600_DEFAULT_COLORCOMPONENTS,
  493. BJC600_DEFAULT_PRINTCOLORS,
  494. BJC600_DEFAULT_MONOCHROMEPRINT);
  495. gx_device_bjc800 far_data gs_bjc800_device =
  496. bjc800_device(
  497. bjc_procs,
  498. BJC_BJC800,
  499. BJC800_DEFAULT_RESOLUTION,
  500. BJC800_DEFAULT_RESOLUTION,
  501. BJC800_DEFAULT_BITSPERPIXEL,
  502. bjc_print_page,
  503. BJC800,
  504. BJC800_DEFAULT_MANUALFEED,
  505. BJC800_DEFAULT_MEDIATYPE,
  506. BJC800_DEFAULT_SETMEDIAWEIGHT,
  507. BJC800_DEFAULT_MEDIAWEIGHT,
  508. BJC800_DEFAULT_PRINTQUALITY,
  509. BJC800_DEFAULT_DITHERINGTYPE,
  510. BJC600_DEFAULT_COLORCOMPONENTS,
  511. BJC800_DEFAULT_PRINTCOLORS);
  512. /* Forward references */
  513. private int gdev_pcl_mode1compress(P3(const byte *, const byte *, byte *));
  514. private int hp_colour_open(P2(gx_device *, int));
  515. private int hp_colour_print_page(P3(gx_device_printer *, FILE *, int));
  516. private int cdj_put_param_int(P6(gs_param_list *, gs_param_name, int *, int, int, int));
  517. private uint gdev_prn_rasterwidth(P2(const gx_device_printer *, int));
  518. private int cdj_put_param_bpp(P5(gx_device *, gs_param_list *, int, int, int));
  519. private int cdj_set_bpp(P3(gx_device *, int, int));
  520. private void cdj_expand_line(P5(word *, int, short, int, int));
  521. private int bjc_fscmyk(P5(byte**, byte*[4][4], int**, int, int));
  522. /* String parameters manipulation */
  523. typedef struct {
  524. const char* p_name;
  525. int p_value;
  526. } stringParamDescription;
  527. private const byte* paramValueToString(P2(const stringParamDescription*, int));
  528. private int paramStringValue(P4(const stringParamDescription*,
  529. const byte*, int, int*));
  530. private int put_param_string(P6(gs_param_list*, const byte*,
  531. gs_param_string*, const stringParamDescription*, int *, int));
  532. private int get_param_string(P7(gs_param_list*, const byte*,
  533. gs_param_string*, const stringParamDescription*, int, bool, int));
  534. /* Open the printer and set up the margins. */
  535. private int
  536. dj500c_open(gx_device *pdev)
  537. { return hp_colour_open(pdev, DJ500C);
  538. }
  539. private int
  540. dj550c_open(gx_device *pdev)
  541. { return hp_colour_open(pdev, DJ550C);
  542. }
  543. private int
  544. dnj650c_open(gx_device *pdev)
  545. { return hp_colour_open(pdev, DNJ650C);
  546. }
  547. private int
  548. lj4dith_open(gx_device *pdev)
  549. { return hp_colour_open(pdev, LJ4DITH);
  550. }
  551. private int
  552. pjxl300_open(gx_device *pdev)
  553. { return hp_colour_open(pdev, PJXL300);
  554. }
  555. private int
  556. pj_open(gx_device *pdev)
  557. { return hp_colour_open(pdev, PJ180);
  558. }
  559. private int
  560. pjxl_open(gx_device *pdev)
  561. { return hp_colour_open(pdev, PJXL180);
  562. }
  563. private int
  564. escp_open(gx_device *pdev)
  565. { return hp_colour_open(pdev, ESC_P);
  566. }
  567. private int
  568. bjc_open(gx_device *pdev)
  569. { return hp_colour_open(pdev, bjc->ptype);
  570. }
  571. private int
  572. hp_colour_open(gx_device *pdev, int ptype)
  573. { /* Change the margins if necessary. */
  574. static const float dj_a4[4] = { DESKJET_MARGINS_A4 };
  575. static const float dj_letter[4] = { DESKJET_MARGINS_LETTER };
  576. static const float lj4_all[4] = { LJET4_MARGINS };
  577. static const float pj_all[4] = { PAINTJET_MARGINS };
  578. static const float dnj_all[4] = { DESIGNJET_MARGINS };
  579. static const float ep_a4[4] = { ESC_P_MARGINS_A4 };
  580. static const float ep_letter[4] = { ESC_P_MARGINS_LETTER };
  581. static float bjc_a3[4] = { BJC_MARGINS_A3 }; /* Not const! */
  582. static float bjc_letter[4] = { BJC_MARGINS_LETTER }; /* Not const! */
  583. static float bjc_a4[4] = { BJC_MARGINS_A4 }; /* Not const! */
  584. const float *m = (float *) 0;
  585. /* Set up colour params if put_params has not already done so */
  586. if (pdev->color_info.num_components == 0)
  587. { int code = cdj_set_bpp(pdev, pdev->color_info.depth,
  588. pdev->color_info.num_components);
  589. if ( code < 0 )
  590. return code;
  591. }
  592. switch (ptype) {
  593. case DJ500C:
  594. case DJ550C:
  595. m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? dj_a4 :
  596. dj_letter);
  597. break;
  598. case DNJ650C:
  599. m = dnj_all;
  600. break;
  601. case LJ4DITH:
  602. m = lj4_all;
  603. break;
  604. case PJ180:
  605. case PJXL300:
  606. case PJXL180:
  607. m = pj_all;
  608. break;
  609. case ESC_P:
  610. m = (gdev_pcl_paper_size(pdev) == PAPER_SIZE_A4 ? ep_a4 :
  611. ep_letter);
  612. break;
  613. case BJC600:
  614. case BJC800:
  615. switch (gdev_pcl_paper_size(pdev)) {
  616. case PAPER_SIZE_LEGAL:
  617. case PAPER_SIZE_LETTER:
  618. m = bjc_letter;
  619. break;
  620. case PAPER_SIZE_A0:
  621. case PAPER_SIZE_A1:
  622. case PAPER_SIZE_A3:
  623. m = bjc_a3;
  624. break;
  625. default:
  626. m = bjc_a4;
  627. }
  628. #ifndef USE_FIXED_MARGINS
  629. if (ptype == BJC800) {
  630. ((float *) m)[1] = BJC_HARD_LOWER_LIMIT;
  631. }
  632. #endif
  633. bjc->printLimit = m[3]; /* The real hardware limit. */
  634. #ifdef BJC_DEFAULT_CENTEREDAREA
  635. if (m[3] < m[1]) {
  636. ((float *) m)[3] = m[1]; /* Top margin = bottom one. */
  637. } else {
  638. ((float *) m)[1] = m[3]; /* Bottom margin = top one. */
  639. }
  640. #endif
  641. break;
  642. /*NOTREACHED*/
  643. /*
  644. * The margins must be set so that the resulting page length will be
  645. * expressed exactly as a multiple of tenthes of inches.
  646. *
  647. */
  648. /**/ {
  649. float *bjcm = (float *) m;
  650. byte pdimen = (byte)
  651. (pdev->height / pdev->y_pixels_per_inch * 10.
  652. - bjcm[3] * 10. - bjcm[1] * 10. + .5) + 1;
  653. do {
  654. --pdimen;
  655. bjcm[1] = pdev->height / pdev->y_pixels_per_inch
  656. - bjcm[3] - (float) pdimen / 10.;
  657. } while (bjcm[1] < BJC_LOWER_LIMIT);
  658. }
  659. break;
  660. }
  661. gx_device_set_margins(pdev, m, true);
  662. return gdev_prn_open(pdev);
  663. }
  664. /* Added parameters for DeskJet 5xxC */
  665. /* Get parameters. In addition to the standard and printer
  666. * parameters, we supply shingling and depletion parameters,
  667. * and control over the bits-per-pixel used in output rendering */
  668. private int
  669. cdj_get_params(gx_device *pdev, gs_param_list *plist)
  670. { int code = gdev_prn_get_params(pdev, plist);
  671. if ( code < 0 ||
  672. (code = param_write_int(plist, "BlackCorrect", (int *)&cdj->correction)) < 0 ||
  673. (code = param_write_int(plist, "Shingling", &cdj->shingling)) < 0 ||
  674. (code = param_write_int(plist, "Depletion", &cdj->depletion)) < 0
  675. )
  676. return code;
  677. return code;
  678. }
  679. /* Put parameters. */
  680. private int
  681. cdj_put_params(gx_device *pdev, gs_param_list *plist)
  682. { int correction = cdj->correction;
  683. int shingling = cdj->shingling;
  684. int depletion = cdj->depletion;
  685. int bpp = 0;
  686. int code = 0;
  687. code = cdj_put_param_int(plist, "BlackCorrect", &correction, 0, 9, code);
  688. code = cdj_put_param_int(plist, "Shingling", &shingling, 0, 2, code);
  689. code = cdj_put_param_int(plist, "Depletion", &depletion, 1, 3, code);
  690. code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
  691. if ( code < 0 )
  692. return code;
  693. code = cdj_put_param_bpp(pdev, plist, bpp, bpp, 0);
  694. if ( code < 0 )
  695. return code;
  696. cdj->correction = correction;
  697. cdj->shingling = shingling;
  698. cdj->depletion = depletion;
  699. return 0;
  700. }
  701. /* Added parameters for PaintJet XL and PaintJet XL300 */
  702. /* Get parameters. In addition to the standard and printer
  703. * parameters, we supply print_quality and render_type
  704. * parameters, together with bpp control. */
  705. private int
  706. pjxl_get_params(gx_device *pdev, gs_param_list *plist)
  707. { int code = gdev_prn_get_params(pdev, plist);
  708. if ( code < 0 ||
  709. (code = param_write_int(plist, "PrintQuality", &pjxl->printqual)) < 0 ||
  710. (code = param_write_int(plist, "RenderType", &pjxl->rendertype)) < 0
  711. )
  712. return code;
  713. return code;
  714. }
  715. /* Put parameters. */
  716. private int
  717. pjxl_put_params(gx_device *pdev, gs_param_list *plist)
  718. { int printqual = pjxl->printqual;
  719. int rendertype = pjxl->rendertype;
  720. int bpp = 0, real_bpp = 0;
  721. int code = 0;
  722. code = cdj_put_param_int(plist, "PrintQuality", &printqual, -1, 1, code);
  723. code = cdj_put_param_int(plist, "RenderType", &rendertype, 0, 10, code);
  724. code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, code);
  725. if ( code < 0 )
  726. return code;
  727. real_bpp = bpp;
  728. if ( rendertype > 0 )
  729. { /* If printer is doing the dithering, we must have a
  730. * true-colour mode, ie. 16 or 24 bits per pixel */
  731. if ( bpp > 0 && bpp < 16 )
  732. real_bpp = 24;
  733. }
  734. code = cdj_put_param_bpp(pdev, plist, bpp, real_bpp, 0);
  735. if ( code < 0 )
  736. return code;
  737. pjxl->printqual = printqual;
  738. pjxl->rendertype = rendertype;
  739. return 0;
  740. }
  741. /* Added parameters for PaintJet */
  742. /* Put parameters. In addition to the standard and printer */
  743. /* parameters, we allow control of the bits-per-pixel */
  744. private int
  745. pj_put_params(gx_device *pdev, gs_param_list *plist)
  746. { int bpp = 0;
  747. int code = cdj_put_param_int(plist, "BitsPerPixel", &bpp, 1, 32, 0);
  748. if ( code < 0 )
  749. return code;
  750. return cdj_put_param_bpp(pdev, plist, bpp, bpp, 0);
  751. }
  752. private stringParamDescription bjc_processColorsStrings[] = {
  753. { "DeviceGray", 1 },
  754. { "DeviceRGB", 3 },
  755. { "DeviceCMYK", 4 },
  756. { 0 }
  757. };
  758. private stringParamDescription bjc_mediaTypeStrings[] = {
  759. { "PlainPaper", BJC_MEDIA_PLAINPAPER },
  760. { "CoatedPaper", BJC_MEDIA_COATEDPAPER },
  761. { "TransparencyFilm", BJC_MEDIA_TRANSPARENCYFILM },
  762. { "Envelope", BJC_MEDIA_ENVELOPE },
  763. { "Card", BJC_MEDIA_CARD},
  764. { "Other", BJC_MEDIA_OTHER },
  765. { 0 }
  766. };
  767. private stringParamDescription bjc600_printQualityStrings[] = {
  768. { "Normal", 0 },
  769. { "High", 1 },
  770. { "Draft", 2 },
  771. { 0 }
  772. };
  773. private stringParamDescription bjc800_printQualityStrings[] = {
  774. { "Normal", 0 },
  775. { "High", 1 },
  776. { "Low", 3 },
  777. { "Draft", 4 },
  778. { 0 },
  779. };
  780. private stringParamDescription bjc_ditheringTypeStrings[] = {
  781. { "None", BJC_DITHER_NONE },
  782. { "Floyd-Steinberg", BJC_DITHER_FS },
  783. { 0 }
  784. };
  785. private int
  786. bjc_get_params(gx_device *pdev, gs_param_list *plist)
  787. {
  788. int code = gdev_prn_get_params(pdev, plist);
  789. int ncode;
  790. gs_param_string pmedia;
  791. gs_param_string pquality;
  792. gs_param_string dithering;
  793. if (code < 0) return_error(code);
  794. if ((ncode = param_write_bool(plist, BJC_OPTION_MANUALFEED,
  795. &bjcparams.manualFeed)) < 0) {
  796. code = ncode;
  797. }
  798. code = get_param_string(plist, (unsigned char *)BJC_OPTION_MEDIATYPE, &pmedia,
  799. bjc_mediaTypeStrings, bjcparams.mediaType, true, code);
  800. code = get_param_string(plist, (unsigned char *)BJC_OPTION_PRINTQUALITY, &pquality,
  801. (bjc->ptype == BJC800 ? bjc800_printQualityStrings :
  802. bjc600_printQualityStrings), bjcparams.printQuality,
  803. true, code);
  804. code = get_param_string(plist, (unsigned char *)BJC_OPTION_DITHERINGTYPE, &dithering,
  805. bjc_ditheringTypeStrings, bjcparams.ditheringType, true, code);
  806. if ((ncode = param_write_int(plist, BJC_OPTION_PRINTCOLORS,
  807. &bjcparams.printColors)) < 0) {
  808. code = ncode;
  809. }
  810. if ((ncode = (bjcparams.mediaWeight_isSet ?
  811. param_write_int(plist, BJC_OPTION_MEDIAWEIGHT,
  812. &bjcparams.mediaWeight) :
  813. param_write_null(plist, BJC_OPTION_MEDIAWEIGHT))) < 0) {
  814. code = ncode;
  815. }
  816. if (bjc->ptype != BJC800) {
  817. if ((ncode = param_write_bool(plist, BJC_OPTION_MONOCHROMEPRINT,
  818. &bjc600params.monochromePrint)) < 0) {
  819. code = ncode;
  820. }
  821. }
  822. /**/ {
  823. float version;
  824. gs_param_string versionString;
  825. bool bTrue = true;
  826. version = bjcversion(pdev);
  827. versionString.data = (byte *)bjcversionstring(pdev);
  828. versionString.size = strlen((char *)versionString.data);
  829. versionString.persistent = true;
  830. if ((ncode = param_write_float(plist, BJC_DEVINFO_VERSION,
  831. &version)) < 0) {
  832. code = ncode;
  833. }
  834. if ((ncode = param_write_string(plist, BJC_DEVINFO_VERSIONSTRING,
  835. &versionString)) < 0) {
  836. code = ncode;
  837. }
  838. if ((ncode = param_write_bool(plist, BJC_DEVINFO_OUTPUTFACEUP,
  839. &bTrue)) < 0) {
  840. code = ncode;
  841. }
  842. }
  843. return code;
  844. }
  845. /* Put properties for the bjc drivers. */
  846. private int
  847. bjc_put_params(gx_device *pdev, gs_param_list *plist)
  848. {
  849. int bpp = 0, ccomps = 0;
  850. int code = 0;
  851. int ncode;
  852. bool aBool = true;
  853. const char* oname = (const char*) 0;
  854. bjc600_params new600Params;
  855. bjc800_params new800Params;
  856. bjc_params* params;
  857. gs_param_string pprocesscolors;
  858. gs_param_string pmedia;
  859. gs_param_string pquality;
  860. gs_param_float_array hwra;
  861. if (bjc->ptype != BJC800) {
  862. new600Params = bjc600params;
  863. params = (bjc_params*) &new600Params;
  864. } else {
  865. new800Params = bjc800params;
  866. params = (bjc_params*) &new800Params;
  867. }
  868. if ((code = cdj_put_param_int(plist, "BitsPerPixel",
  869. &bpp, 1, 32, code)) != 1) {
  870. bpp = pdev->color_info.depth;
  871. }
  872. if ((code = put_param_string(plist, (unsigned char *)"ProcessColorModel",
  873. &pprocesscolors, bjc_processColorsStrings, &ccomps, code)) != 1) {
  874. ccomps = pdev->color_info.num_components;
  875. }
  876. if ((ncode = param_read_bool(plist, oname = BJC_OPTION_MANUALFEED,
  877. &params->manualFeed)) < 0) {
  878. param_signal_error(plist, oname, code = ncode);
  879. }
  880. code = put_param_string(plist, (unsigned char *)BJC_OPTION_MEDIATYPE, &pmedia,
  881. bjc_mediaTypeStrings, &params->mediaType, code);
  882. code = cdj_put_param_int(plist, BJC_OPTION_PRINTCOLORS,
  883. &params->printColors, 0, 15, code);
  884. code = put_param_string(plist, (unsigned char *)BJC_OPTION_PRINTQUALITY, &pquality,
  885. (bjc->ptype == BJC800 ? bjc800_printQualityStrings :
  886. bjc600_printQualityStrings), &params->printQuality, code);
  887. switch (ncode = param_read_int(plist,
  888. oname = BJC_OPTION_MEDIAWEIGHT, &params->mediaWeight)) {
  889. case 0:
  890. if (params->mediaWeight <= 0) {
  891. ncode = gs_error_rangecheck;
  892. } else {
  893. params->mediaWeight_isSet = 1;
  894. break;
  895. }
  896. goto mwe;
  897. default:
  898. if ((ncode = param_read_null(plist, oname)) == 0) {
  899. params->mediaWeight_isSet = 0;
  900. break;
  901. }
  902. mwe: param_signal_error(plist, oname, code = ncode);
  903. case 1:
  904. break;
  905. }
  906. if (bjc->ptype != BJC800) {
  907. bjc600_params* params600 = (bjc600_params*) params;
  908. if ((ncode = param_read_bool(plist,
  909. oname = BJC_OPTION_MONOCHROMEPRINT,
  910. &params600->monochromePrint)) < 0) {
  911. param_signal_error(plist, oname, code = ncode);
  912. }
  913. }
  914. if ((ncode = cdj_param_check_float(plist, BJC_DEVINFO_VERSION,
  915. bjcversion(pdev), true)) < 0) {
  916. code = ncode;
  917. }
  918. if ((ncode = cdj_param_check_string(plist, BJC_DEVINFO_VERSIONSTRING,
  919. bjcversionstring(pdev), true)) < 0) {
  920. code = ncode;
  921. }
  922. if ((ncode = param_read_bool(plist, oname = BJC_DEVINFO_OUTPUTFACEUP,
  923. &aBool)) < 0) {
  924. param_signal_error(plist, oname, code = ncode);
  925. } else if (aBool != true) {
  926. param_signal_error(plist, oname, code = ncode = gs_error_rangecheck);
  927. }
  928. /* Check for invalid resolution. The array macros are taken from
  929. gsdparam.c and modified to use oname, ncode and code instead
  930. of param_name, code and ecode respectively. */
  931. BEGIN_ARRAY_PARAM(param_read_float_array, "HWResolution", hwra, 2, hwre)
  932. if ( hwra.data[0] <= 0 || hwra.data[1] <= 0 ||
  933. hwra.data[0] != hwra.data[1] )
  934. ncode = gs_error_rangecheck;
  935. else {
  936. #ifdef BJC_STRICT
  937. if (hwra.data[0] != BJC_RESOLUTION_LOW &&
  938. hwra.data[0] != BJC_RESOLUTION_NORMAL &&
  939. hwra.data[0] != BJC_RESOLUTION_HIGH) {
  940. ncode = gs_error_rangecheck;
  941. }
  942. #else
  943. /* A small hack for checking resolution without logarithms. */
  944. /**/ {
  945. int n;
  946. for (n = 0; n < 8 * sizeof(n) / BJC_RESOLUTION_BASE; ++n) {
  947. float res = BJC_RESOLUTION_BASE * (1 << n);
  948. if (res == hwra.data[0]) break;
  949. if (res > hwra.data[0]) {
  950. ncode = gs_error_rangecheck;
  951. }
  952. }
  953. if (n == 8 * sizeof(n)) {
  954. ncode = gs_error_rangecheck;
  955. }
  956. }
  957. #endif
  958. if (ncode < 0) {
  959. code = ncode;
  960. } else {
  961. break;
  962. }
  963. }
  964. END_PARAM(hwra, hwre)
  965. if ((ncode = cdj_put_param_bpp(pdev, plist, bpp, bpp, ccomps)) < 0) {
  966. code = ncode;
  967. }
  968. if (code < 0)
  969. return code;
  970. if (bpp == 1) {
  971. params->ditheringType = BJC_DITHER_NONE;
  972. }
  973. /* Write values that did change */
  974. if (bjc->ptype != BJC800) {
  975. bjc600params = new600Params;
  976. } else {
  977. bjc800params = new800Params;
  978. }
  979. return code;
  980. }
  981. /* ------ Internal routines ------ */
  982. /* The DeskJet500C can compress (mode 9) */
  983. private int
  984. dj500c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  985. {
  986. return hp_colour_print_page(pdev, prn_stream, DJ500C);
  987. }
  988. /* The DeskJet550C can compress (mode 9) */
  989. private int
  990. dj550c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  991. {
  992. return hp_colour_print_page(pdev, prn_stream, DJ550C);
  993. }
  994. /* The DesignJet650C can compress (mode 1) */
  995. private int
  996. dnj650c_print_page(gx_device_printer * pdev, FILE * prn_stream)
  997. {
  998. return hp_colour_print_page(pdev, prn_stream, DNJ650C);
  999. }
  1000. private int
  1001. lj4dith_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1002. {
  1003. return hp_colour_print_page(pdev, prn_stream, LJ4DITH);
  1004. }
  1005. /* The PJXL300 can compress (modes 2 & 3) */
  1006. private int
  1007. pjxl300_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1008. { int ret_code;
  1009. /* Ensure we're operating in PCL mode */
  1010. fputs("\033%-12345X@PJL enter language = PCL\n", prn_stream);
  1011. ret_code = hp_colour_print_page(pdev, prn_stream, PJXL300);
  1012. /* Reenter switch-configured language */
  1013. fputs("\033%-12345X", prn_stream);
  1014. return ret_code;
  1015. }
  1016. /* The PaintJet XL can compress (modes 2 & 3) */
  1017. private int
  1018. pjxl_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1019. {
  1020. return hp_colour_print_page(pdev, prn_stream, PJXL180);
  1021. }
  1022. /* The PaintJet can compress (mode 1) */
  1023. private int
  1024. pj_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1025. {
  1026. return hp_colour_print_page(pdev, prn_stream, PJ180);
  1027. }
  1028. /* The LJ250 can compress (mode 1) */
  1029. private int
  1030. declj250_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1031. { int ret_code;
  1032. fputs("\033%8", prn_stream); /* Enter PCL emulation mode */
  1033. ret_code = hp_colour_print_page(pdev, prn_stream, DECLJ250);
  1034. fputs("\033%@", prn_stream); /* Exit PCL emulation mode */
  1035. return ret_code;
  1036. }
  1037. /* The BJC-600 cannot compress w/o raster image commands. */
  1038. private int
  1039. escp_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1040. {
  1041. return hp_colour_print_page(pdev, prn_stream, ESC_P);
  1042. }
  1043. /* The BJC-600 can compress w/ raster image commands. */
  1044. private int
  1045. bjc_print_page(gx_device_printer * pdev, FILE * prn_stream)
  1046. {
  1047. return hp_colour_print_page(pdev, prn_stream, bjc->ptype);
  1048. }
  1049. /* MACROS FOR DITHERING (we use macros for compact source and faster code) */
  1050. /* Floyd-Steinberg dithering. Often results in a dramatic improvement in
  1051. * subjective image quality, but can also produce dramatic increases in
  1052. * amount of printer data generated and actual printing time!! Mode 9 2D
  1053. * compression is still useful for fairly flat colour or blank areas but its
  1054. * compression is much less effective in areas where the dithering has
  1055. * effectively randomised the dot distribution. */
  1056. #define SHIFT ((I * 8) - 13)
  1057. #define RSHIFT ((I * 8) - 16)
  1058. #define RANDOM (((rand() << RSHIFT) % (MAXVALUE / 2)) - MAXVALUE / 4);
  1059. #define MINVALUE 0
  1060. #define MAXVALUE (255 << SHIFT)
  1061. #define THRESHOLD (128 << SHIFT)
  1062. #define C 8
  1063. #define FSdither(inP, out, errP, Err, Bit, Offset, Element)\
  1064. oldErr = Err;\
  1065. Err = (errP[Element] + ((Err * 7 + C) >> 4) + ((int)inP[Element] << SHIFT));\
  1066. if (Err > THRESHOLD) {\
  1067. out |= Bit;\
  1068. Err -= MAXVALUE;\
  1069. }\
  1070. errP[Element + Offset] += ((Err * 3 + C) >> 4);\
  1071. errP[Element] = ((Err * 5 + oldErr + C) >> 4);
  1072. /* Here we rely on compiler optimisation to remove lines of the form
  1073. * (if (1 >= 4) {...}, ie. the constant boolean expressions */
  1074. /* The original code is in the #else part. Since by default NEW_DITHER
  1075. is not defined, the old code is used. No enhancement is visible for the
  1076. bjc600 drivers with the new code, anyway :-( */
  1077. #ifdef NEW_DITHER
  1078. #define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
  1079. {\
  1080. if (scan == 0) { /* going_up */\
  1081. for (i = 0; i < plane_size; i++) {\
  1082. byte c, y, m, k, bitmask;\
  1083. int oldErr;\
  1084. bitmask = 0x80;\
  1085. for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
  1086. if (n >= 4) {\
  1087. FSdither(dp, k, ep, kErr, bitmask, -n, 0);\
  1088. }\
  1089. if (n >= 3) {\
  1090. FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
  1091. FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
  1092. }\
  1093. FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
  1094. dp += n, ep += n;\
  1095. }\
  1096. if (n >= 4)\
  1097. *kP++ = k;\
  1098. if (n >= 3) {\
  1099. *cP++ = c;\
  1100. *mP++ = m;\
  1101. }\
  1102. *yP++ = y;\
  1103. }\
  1104. } else { /* going_down */\
  1105. for (i = 0; i < plane_size; i++) {\
  1106. byte c, y, m, k, bitmask;\
  1107. int oldErr;\
  1108. bitmask = 0x01;\
  1109. for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
  1110. dp -= n, ep -= n;\
  1111. FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
  1112. if (n >= 3) {\
  1113. FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
  1114. FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
  1115. }\
  1116. if (n >= 4) {\
  1117. FSdither(dp, k, ep, kErr, bitmask, n, 0);\
  1118. }\
  1119. }\
  1120. *--yP = y;\
  1121. if (n >= 3)\
  1122. { *--mP = m;\
  1123. *--cP = c;\
  1124. }\
  1125. if (n >= 4)\
  1126. *--kP = k;\
  1127. }\
  1128. }\
  1129. }
  1130. #else
  1131. #define FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr, cP, mP, yP, kP, n)\
  1132. {\
  1133. if (scan == 0) { /* going_up */\
  1134. for (i = 0; i < plane_size; i++) {\
  1135. byte c, y, m, k, bitmask;\
  1136. int oldErr;\
  1137. bitmask = 0x80;\
  1138. for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
  1139. if (n >= 4) {\
  1140. if (*dp) {\
  1141. FSdither(dp, k, ep, kErr, bitmask, -n, 0);\
  1142. cErr = mErr = yErr = 0;\
  1143. } else {\
  1144. FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
  1145. FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
  1146. FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
  1147. }\
  1148. } else {\
  1149. if (n >= 3) {\
  1150. FSdither(dp, c, ep, cErr, bitmask, -n, n - 3);\
  1151. FSdither(dp, m, ep, mErr, bitmask, -n, n - 2);\
  1152. }\
  1153. FSdither(dp, y, ep, yErr, bitmask, -n, n - 1);\
  1154. }\
  1155. dp += n, ep += n;\
  1156. }\
  1157. if (n >= 4)\
  1158. *kP++ = k;\
  1159. if (n >= 3) {\
  1160. *cP++ = c;\
  1161. *mP++ = m;\
  1162. }\
  1163. *yP++ = y;\
  1164. }\
  1165. } else { /* going_down */\
  1166. for (i = 0; i < plane_size; i++) {\
  1167. byte c, y, m, k, bitmask;\
  1168. int oldErr;\
  1169. bitmask = 0x01;\
  1170. for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
  1171. dp -= n, ep -= n;\
  1172. if (n >= 4) {\
  1173. if (*dp) {\
  1174. FSdither(dp, k, ep, kErr, bitmask, n, 0);\
  1175. cErr = mErr = yErr = 0;\
  1176. } else {\
  1177. FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
  1178. FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
  1179. FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
  1180. }\
  1181. } else {\
  1182. FSdither(dp, y, ep, yErr, bitmask, n, n - 1);\
  1183. if (n >= 3) {\
  1184. FSdither(dp, m, ep, mErr, bitmask, n, n - 2);\
  1185. FSdither(dp, c, ep, cErr, bitmask, n, n - 3);\
  1186. }\
  1187. }\
  1188. }\
  1189. *--yP = y;\
  1190. if (n >= 3)\
  1191. { *--mP = m;\
  1192. *--cP = c;\
  1193. }\
  1194. if (n >= 4)\
  1195. *--kP = k;\
  1196. }\
  1197. }\
  1198. }
  1199. #endif
  1200. /* END MACROS FOR DITHERING */
  1201. #define CPbit(inP, out, Bit, Element)\
  1202. if (inP[Element]) {\
  1203. out |= Bit;\
  1204. }
  1205. #define COPYline(scan, i, j, plane_size, cP, mP, yP, kP, n)\
  1206. {\
  1207. if (scan == 0) { /* going_up */\
  1208. for (i = 0; i < plane_size; i++) {\
  1209. byte c, y, m, k, bitmask;\
  1210. bitmask = 0x80;\
  1211. for (c = m = y = k = 0; bitmask != 0; bitmask >>= 1) {\
  1212. if (n >= 4) {\
  1213. CPbit(dp, k, bitmask, 0);\
  1214. } \
  1215. if (n >= 3) {\
  1216. CPbit(dp, c, bitmask, n - 3);\
  1217. CPbit(dp, m, bitmask, n - 2);\
  1218. }\
  1219. CPbit(dp, y, bitmask, n - 1);\
  1220. dp += n, ep += n;\
  1221. }\
  1222. if (n >= 4)\
  1223. *kP++ = k;\
  1224. if (n >= 3) {\
  1225. *cP++ = c;\
  1226. *mP++ = m;\
  1227. }\
  1228. *yP++ = y;\
  1229. }\
  1230. } else { /* going_down */\
  1231. for (i = 0; i < plane_size; i++) {\
  1232. byte c, y, m, k, bitmask;\
  1233. bitmask = 0x01;\
  1234. for (c = m = y = k = 0; bitmask != 0; bitmask <<= 1) {\
  1235. dp -= n, ep -= n;\
  1236. if (n >= 4) {\
  1237. CPbit(dp, k, bitmask, 0);\
  1238. }\
  1239. if (n >= 3) {\
  1240. CPbit(dp, m, bitmask, n - 2);\
  1241. CPbit(dp, c, bitmask, n - 3);\
  1242. }\
  1243. CPbit(dp, y, bitmask, n - 1);\
  1244. }\
  1245. *--yP = y;\
  1246. if (n >= 3)\
  1247. { *--mP = m;\
  1248. *--cP = c;\
  1249. }\
  1250. if (n >= 4)\
  1251. *--kP = k;\
  1252. }\
  1253. }\
  1254. }
  1255. /* Some convenient shorthand .. */
  1256. #define x_dpi (pdev->x_pixels_per_inch)
  1257. #define y_dpi (pdev->y_pixels_per_inch)
  1258. #define CONFIG_16BIT "\033*v6W\000\003\000\005\006\005"
  1259. #define CONFIG_24BIT "\033*v6W\000\003\000\010\010\010"
  1260. /* To calculate buffer size as next greater multiple of both parameter and W */
  1261. #define calc_buffsize(a, b) (((((a) + ((b) * W) - 1) / ((b) * W))) * W)
  1262. /*
  1263. * Miscellaneous functions for Canon BJC-600 printers in raster command mode.
  1264. */
  1265. #define fputshort(n, f) fputc((n)%256,f);fputc((n)/256,f)
  1266. private int
  1267. bjc_cmd(byte cmd, int argsize, byte* arg, gx_device_printer* pdev,
  1268. FILE* stream)
  1269. {
  1270. fputs("\033(", stream);
  1271. putc(cmd, stream);
  1272. fputshort(argsize, stream);
  1273. fwrite(arg, sizeof(byte), argsize, stream);
  1274. return 0;
  1275. }
  1276. private int
  1277. bjc_raster_cmd_sub(char c, int rastsize, byte* data, FILE* stream)
  1278. {
  1279. fputs("\033(A", stream);
  1280. fputshort(rastsize + 1, stream);
  1281. putc(c, stream);
  1282. fwrite(data, sizeof(byte), rastsize, stream);
  1283. putc('\015', stream);
  1284. return 0;
  1285. }
  1286. private int
  1287. bjc_raster_cmd(int c_id, int rastsize, byte* data, gx_device_printer* pdev,
  1288. FILE* stream)
  1289. {
  1290. if (bjcparams.printColors == BJC_COLOR_ALLBLACK) {
  1291. bjc_raster_cmd_sub('K', rastsize, data, stream);
  1292. } else if (pdev->color_info.num_components == 1) {
  1293. if (bjcparams.printColors & BJC_COLOR_BLACK) {
  1294. bjc_raster_cmd_sub('K', rastsize, data, stream);
  1295. } else {
  1296. if (bjcparams.printColors & BJC_COLOR_YELLOW)
  1297. bjc_raster_cmd_sub('Y', rastsize, data, stream);
  1298. if (bjcparams.printColors & BJC_COLOR_MAGENTA)
  1299. bjc_raster_cmd_sub('M', rastsize, data, stream);
  1300. if (bjcparams.printColors & BJC_COLOR_CYAN)
  1301. bjc_raster_cmd_sub('C', rastsize, data, stream);
  1302. }
  1303. }else { /* Color decomposition */
  1304. private byte ymckCodes[] = {
  1305. BJC_COLOR_YELLOW,
  1306. BJC_COLOR_MAGENTA,
  1307. BJC_COLOR_CYAN,
  1308. BJC_COLOR_BLACK,
  1309. };
  1310. if (bjcparams.printColors & (int) ymckCodes[c_id]) {
  1311. bjc_raster_cmd_sub("YMCK"[c_id], rastsize, data, stream);
  1312. }
  1313. }
  1314. return 0;
  1315. }
  1316. private int
  1317. bjc_init_page(gx_device_printer* pdev, FILE* stream)
  1318. {
  1319. byte pagemargins[3], resolution[4], paperloading[2];
  1320. /* Compute page margins. */
  1321. pagemargins[0] = (byte) ((float) pdev->height / pdev->y_pixels_per_inch
  1322. * 10 + .5);
  1323. pagemargins[1] = (byte) 1;
  1324. pagemargins[2] = (byte) ((pdev->width / pdev->x_pixels_per_inch * 10) -
  1325. pdev->HWMargins[0] / 7.2 - pdev->HWMargins[2] / 7.2 + .5);
  1326. /* Cheat to keep margins into bounds (while waiting to have the right
  1327. margins for big papers. */
  1328. switch (bjc->ptype) {
  1329. case BJC800:
  1330. if (pagemargins[2] > 114) pagemargins[2] = 114;
  1331. break;
  1332. default:
  1333. if (pagemargins[2] > 80) pagemargins[2] = 80;
  1334. break;
  1335. }
  1336. /* Initialize resolution argument. */
  1337. resolution[0] = (byte) ((int)pdev->y_pixels_per_inch / 256);
  1338. resolution[1] = (byte) ((int)pdev->y_pixels_per_inch % 256);
  1339. resolution[2] = (byte) ((int)pdev->x_pixels_per_inch / 256);
  1340. resolution[3] = (byte) ((int)pdev->x_pixels_per_inch % 256);
  1341. /* Initialize paper loading argument. */
  1342. paperloading[0] = 0x10 + ((1 - bjcparams.manualFeed) << 2);
  1343. paperloading[1] = bjcparams.mediaType << 4;
  1344. /* Reinitialize printer in raster mode. */
  1345. fputs("\033[K", stream);
  1346. fputshort(2, stream);
  1347. fputc(0x00, stream);
  1348. fputc(0x0f, stream);
  1349. /* Set page mode on (ignore data at end of page) */
  1350. bjc_cmd('a', 1, (byte*) "\001", pdev, stream);
  1351. /* Set page margins */
  1352. bjc_cmd('g', 3, pagemargins, pdev, stream);
  1353. /* Set compression on (this is PackBits compression a la TIFF/Mac) */
  1354. bjc_cmd('b', 1, (byte*) "\001", pdev, stream);
  1355. /* Set paper loading. */
  1356. bjc_cmd('l', 2, paperloading, pdev, stream);
  1357. /* Set printing method. */
  1358. #ifndef BJC_INIT_800_AS_600
  1359. if (bjc->ptype == BJC800) {
  1360. #else
  1361. if (0) {
  1362. #endif
  1363. byte printmode[2];
  1364. printmode[0] = bjcparams.printQuality;
  1365. /* Modes not used are 3 (CN, Color Normal) and 2 (TP+ (?)) */
  1366. switch (bjcparams.printQuality) {
  1367. case BJC_QUALITY_DRAFT:
  1368. printmode[0] = 4; /* Draft */
  1369. break;
  1370. }
  1371. printmode[1] = (bjcparams.mediaType >= BJC_MEDIA_ENVELOPE ? 1 :
  1372. bjc800thickpaper());
  1373. bjc_cmd('c', 2, printmode, pdev, stream);
  1374. } else /* BJC600 */ {
  1375. byte printmeth[3];
  1376. printmeth[0] = 0x10 + ((1 - bjcparams.manualFeed) << 2);
  1377. printmeth[1] = (bjcparams.mediaType << 4) + bjcparams.printQuality;
  1378. printmeth[2] = (bjcparams.printQuality == BJC_QUALITY_HIGH ?
  1379. 0x10 : 0) + (bjcparams.mediaType >= BJC_MEDIA_ENVELOPE ? 1 :
  1380. bjc600thickpaper());
  1381. bjc_cmd('c', 3, printmeth, pdev, stream);
  1382. }
  1383. /* Set raster resolution */
  1384. bjc_cmd('d', 4, resolution, pdev, stream);
  1385. return 0;
  1386. }
  1387. private int
  1388. bjc_v_skip(int n, gx_device_printer* pdev, FILE* stream)
  1389. {
  1390. if (n) {
  1391. fputs("\033(e", stream);
  1392. putc(2, stream);
  1393. putc(0, stream);
  1394. putc(n / 256, stream);
  1395. putc(n % 256, stream);
  1396. }
  1397. return 0;
  1398. }
  1399. private int
  1400. bjc_finish_page(gx_device_printer* pdev, FILE* stream)
  1401. {
  1402. bjc_cmd('a', 1, (byte*) "\000", pdev, stream);
  1403. bjc_cmd('b', 1, (byte*) "\000", pdev, stream);
  1404. fputc('\014', stream);
  1405. fputs("\033@", stream);
  1406. return 0;
  1407. }
  1408. /* 1D runlength compression for BJC-600
  1409. * this code is borrowed from gdevpcl.c:gdev_pcl_mode2compress.
  1410. */
  1411. private int
  1412. bjc_compress(const byte *row, const byte *end_row, byte *compressed)
  1413. {
  1414. register const byte *exam = row;
  1415. register byte *cptr = compressed; /* output pointer into compressed bytes */
  1416. while ( exam < end_row ) {
  1417. /* Search ahead in the input looking for a run */
  1418. /* of at least 4 identical bytes. */
  1419. const byte *compr = exam;
  1420. const byte *end_dis;
  1421. const byte *next;
  1422. register byte test, test2;
  1423. test = *exam;
  1424. while ( exam < end_row ) {
  1425. test2 = *++exam;
  1426. if ( test == test2 )
  1427. break;
  1428. test = test2;
  1429. }
  1430. /* Find out how long the run is */
  1431. end_dis = exam - 1;
  1432. if ( exam == end_row ) { /* no run */
  1433. next = --end_row;
  1434. } else {
  1435. next = exam + 1;
  1436. while ( next < end_row && *next == test ) next++;
  1437. }
  1438. /* Now [compr..end_dis) should be encoded as dissimilar, */
  1439. /* and [end_dis..next) should be encoded as similar. */
  1440. /* Note that either of these ranges may be empty. */
  1441. for ( ; ; ) { /* Encode up to 128 dissimilar bytes */
  1442. uint count = end_dis - compr; /* uint for faster switch */
  1443. switch ( count ) { /* Use memcpy only if it's worthwhile. */
  1444. case 6: cptr[6] = compr[5];
  1445. case 5: cptr[5] = compr[4];
  1446. case 4: cptr[4] = compr[3];
  1447. case 3: cptr[3] = compr[2];
  1448. case 2: cptr[2] = compr[1];
  1449. case 1: cptr[1] = compr[0];
  1450. *cptr = count - 1;
  1451. cptr += count + 1;
  1452. case 0: /* all done */
  1453. break;
  1454. default:
  1455. if ( count > 128 ) count = 128;
  1456. *cptr++ = count - 1;
  1457. memcpy(cptr, compr, count);
  1458. cptr += count, compr += count;
  1459. continue;
  1460. }
  1461. break;
  1462. }
  1463. { /* Encode up to 128 similar bytes. */
  1464. /* Note that count may be <0 at end of row. */
  1465. int count = next - end_dis;
  1466. if (next < end_row || test != 0)
  1467. while ( count > 0 ) {
  1468. int this = (count > 128 ? 128 : count);
  1469. *cptr++ = 257 - this;
  1470. *cptr++ = (byte)test;
  1471. count -= this;
  1472. }
  1473. exam = next;
  1474. }
  1475. }
  1476. return cptr - compressed;
  1477. }
  1478. /*
  1479. * For the ESC/P mode, resolution is fixed as 360dpi and we must transform
  1480. * image data to serialized data.
  1481. */
  1482. private word *ep_storage;
  1483. private uint ep_storage_size_words;
  1484. private byte *ep_raster_buf[4][BJC_HEAD_ROWS], *ep_print_buf;
  1485. private int ep_num_comps, ep_plane_size, img_rows=BJC_HEAD_ROWS;
  1486. #define row_bytes (img_rows / 8)
  1487. #define row_words (row_bytes / sizeof(word))
  1488. #define min_rows (32) /* for optimization of text image printing */
  1489. private int
  1490. ep_print_image(FILE *prn_stream, char cmd, byte *data, int size)
  1491. {
  1492. static int ln_idx=0, vskip1=0, vskip2=0, real_rows;
  1493. int i;
  1494. static const char color[4] = {4,1,2,0};
  1495. switch (cmd) {
  1496. case 3: /* Black */
  1497. case 2: /* Cyan */
  1498. case 1: /* Magenta */
  1499. case 0: /* Yellow */
  1500. memcpy(ep_raster_buf[((int) cmd)][ln_idx+vskip2], data, size);
  1501. return 0;
  1502. case 'B': /* blank line skip */
  1503. if (!ln_idx) {
  1504. vskip1 += size;
  1505. } else if (size >= img_rows - (ln_idx+vskip2) || ln_idx+vskip2 >= min_rows) {
  1506. /* The 'I' cmd must precede 'B' cmd! */
  1507. vskip2 += size;
  1508. ep_print_image(prn_stream, 'F', 0, 0); /* flush and reset status */
  1509. } else {
  1510. vskip2 += size;
  1511. }
  1512. return 0;
  1513. case 'I': /* Increment index */
  1514. ln_idx += vskip2 + 1;
  1515. vskip2 = 0;
  1516. if (ln_idx < img_rows) return 0;
  1517. /* if ep_raster_buf filled up, then fall through here and flush buffer */
  1518. case 'F': /* flush print buffer */
  1519. if (!ln_idx) return 0; /* The end of the page. */
  1520. /* before print the image, perform vertical skip. */
  1521. while (vskip1 >= (255*2)) {
  1522. fputs("\033J\377", prn_stream); /* n/180in. feeding */
  1523. vskip1 -= (255*2);
  1524. }
  1525. if (vskip1 > 255) {
  1526. fputs("\033J\200", prn_stream);
  1527. vskip1 -= 256;
  1528. }
  1529. if (vskip1) {
  1530. /* n/360in. feeding */
  1531. fputs("\033|J", prn_stream); putc(0, prn_stream); putc(vskip1, prn_stream);
  1532. }
  1533. /* Optimize the number of nozzles to be used. */
  1534. if (ln_idx > 56) { /* use 64 nozzles */
  1535. real_rows = 64;
  1536. } else if (ln_idx > 48) { /* use 56 nozzles */
  1537. real_rows = 56;
  1538. } else if (ln_idx > 32) { /* use 48 nozzles */
  1539. real_rows = 48;
  1540. } else { /* use 32 nozzles */
  1541. real_rows = 32;
  1542. }
  1543. for (i = 0; i < ep_num_comps; i++) {
  1544. int lnum, hskip, print_size, img_rows;
  1545. byte *p0, *p1, *p2, *p3;
  1546. byte *inp, *inbuf, *outp, *outbuf;
  1547. img_rows = real_rows; /* Note that this img_rows is not the one that
  1548. * defined out of this function. */
  1549. outbuf = ep_print_buf;
  1550. /* Transpose raster image for serial printer image */
  1551. for (lnum=0; lnum < img_rows; lnum+=8, outbuf++) {
  1552. inbuf = inp = ep_raster_buf[i][lnum];
  1553. for (outp = outbuf; inp < inbuf+ep_plane_size; inp++, outp += img_rows) {
  1554. memflip8x8(inp, ep_plane_size, outp, row_bytes);
  1555. }
  1556. }
  1557. /* Set color */
  1558. if (ep_num_comps == 1) {
  1559. /* Don't set color (to enable user setting). */
  1560. putc('\015', prn_stream);
  1561. } else {
  1562. /* set color to one of CMYK. */
  1563. fputs("\015\033r", prn_stream);
  1564. putc(color[i], prn_stream);
  1565. }
  1566. *(outp = ep_print_buf + ep_plane_size * img_rows) = 1; /* sentinel */
  1567. p0 = p3 = ep_print_buf;
  1568. /* print image p0 to p1 and h skip p1 to p2 if p2<outp,
  1569. * then make p0=p2 and continue */
  1570. while (p0 < outp) {
  1571. static const word zeros[8] = {0,0,0,0,0,0,0,0};
  1572. if (p3 < outp) {
  1573. /* p1 is the head of running zeros. */
  1574. /* note that h skip unit is 1/180inch */
  1575. for (p1 = p3; !memcmp(p3, zeros, row_bytes*2); p3 += row_bytes*2);
  1576. /* p2 is the head of non zero image. */
  1577. p2 = p3;
  1578. redo:
  1579. for (p3 += row_bytes; memcmp(p3, zeros, row_bytes); p3 += row_bytes);
  1580. if (p3 < outp && memcmp(p3+row_bytes, zeros, row_bytes)) goto redo;
  1581. } else p1 = p2 = outp;
  1582. if (p0 < p1) { /* print the image between p0 and p1 */
  1583. print_size = ((p1 < outp) ? p1 : outp) - p0;
  1584. fputs("\033|B", prn_stream); putc(img_rows, prn_stream);
  1585. fputshort(print_size, prn_stream);
  1586. fwrite(p0, sizeof(byte), print_size, prn_stream);
  1587. }
  1588. if (p1 < p2) { /* skip running zeros from p1 to p2 */
  1589. hskip = (((p2 < outp) ? p2 : outp) - p1) / row_bytes / 2;
  1590. fputs("\033\\", prn_stream);
  1591. fputshort(hskip, prn_stream);
  1592. }
  1593. p0 = p2;
  1594. }
  1595. }
  1596. return ep_print_image(prn_stream, 'R', 0, vskip2 + ln_idx);
  1597. case 'R': /* Reset status */
  1598. ln_idx = 0;
  1599. vskip1 = size;
  1600. vskip2 = 0;
  1601. memset(ep_storage, 0, ep_storage_size_words * W);
  1602. return 0;
  1603. default: /* This should not happen */
  1604. errprintf("ep_print_image: illegal command character `%c'.\n", cmd);
  1605. return 1;
  1606. }
  1607. /* NOT REACHED */
  1608. }
  1609. /* Send the page to the printer. Compress each scan line. */
  1610. private int
  1611. hp_colour_print_page(gx_device_printer * pdev, FILE * prn_stream, int ptype)
  1612. {
  1613. uint raster_width = gdev_prn_rasterwidth(pdev, 1);
  1614. /* int line_size = gdev_prn_rasterwidth(pdev, 0); */
  1615. int line_size = gdev_prn_raster(pdev);
  1616. int line_size_words = (line_size + W - 1) / W;
  1617. int paper_size = gdev_pcl_paper_size((gx_device *)pdev);
  1618. int num_comps = pdev->color_info.num_components;
  1619. int bits_per_pixel = pdev->color_info.depth;
  1620. int storage_bpp = bits_per_pixel;
  1621. int expanded_bpp = bits_per_pixel;
  1622. int plane_size, databuff_size;
  1623. int combined_escapes = 1;
  1624. int errbuff_size = 0;
  1625. int outbuff_size = 0;
  1626. int compression = 0;
  1627. int scan = 0;
  1628. int *errors[2];
  1629. const char *cid_string = (const char*) 0;
  1630. byte *data[4], *plane_data[4][4], *out_data;
  1631. byte *out_row, *out_row_alt;
  1632. word *storage;
  1633. uint storage_size_words;
  1634. /* Tricks and cheats ... */
  1635. switch (ptype) {
  1636. case DJ550C:
  1637. if (num_comps == 3 && !cprn_device->cmyk)
  1638. num_comps = 4; /* 4-component printing */
  1639. break;
  1640. case ESC_P:
  1641. if (bits_per_pixel == 24) /* prefer 3-component printing for bpp=24. */
  1642. num_comps = 3;
  1643. else
  1644. if (num_comps != 1)
  1645. num_comps = 4;
  1646. break;
  1647. case PJXL300:
  1648. case PJXL180:
  1649. if (pjxl->rendertype > 0) {
  1650. if (bits_per_pixel < 16)
  1651. pjxl->rendertype = 0;
  1652. else {
  1653. /* Control codes for CID sequence */
  1654. cid_string = (bits_per_pixel == 16) ? CONFIG_16BIT : CONFIG_24BIT;
  1655. /* Pretend we're a monobit device so we send the data out unchanged */
  1656. bits_per_pixel = storage_bpp = expanded_bpp = 1;
  1657. num_comps = 1;
  1658. }
  1659. }
  1660. break;
  1661. }
  1662. if (cprn_device->cmyk <= 0) {
  1663. if (storage_bpp == 8 && num_comps >= 3)
  1664. bits_per_pixel = expanded_bpp = 3; /* Only 3 bits of each byte used */
  1665. }
  1666. plane_size = calc_buffsize(line_size, storage_bpp);
  1667. ep_plane_size = plane_size;
  1668. if (bits_per_pixel == 1) { /* Data printed direct from i/p */
  1669. databuff_size = 0; /* so no data buffer required, */
  1670. outbuff_size = plane_size * 4; /* but need separate output buffers */
  1671. }
  1672. if (bits_per_pixel > 4) { /* Error buffer for FS dithering */
  1673. storage_bpp = expanded_bpp =
  1674. num_comps * 8; /* 8, 24 or 32 bits */
  1675. if (cprn_device->cmyk > 0) { /* Use CMYK dithering algorithm. */
  1676. errbuff_size = 4 * (5 + 1 + 1 + line_size + 1 + 2) * I;
  1677. } else { /* Use original (RGB) dithering. */
  1678. errbuff_size = /* 4n extra values for line ends */
  1679. calc_buffsize((plane_size * expanded_bpp + num_comps * 4) * I, 1);
  1680. }
  1681. }
  1682. databuff_size = plane_size * storage_bpp;
  1683. storage_size_words = ((plane_size + plane_size) * num_comps +
  1684. databuff_size + errbuff_size + outbuff_size) / W;
  1685. storage = (ulong *) gs_malloc(storage_size_words, W, "hp_colour_print_page");
  1686. ep_storage_size_words = (plane_size * (num_comps + 1)) / W * img_rows
  1687. + 16; /* Redundant space for sentinel and aligning. */
  1688. ep_storage = (word *) gs_malloc(ep_storage_size_words, W, "ep_print_buffer");
  1689. /*
  1690. * The principal data pointers are stored as pairs of values, with
  1691. * the selection being made by the 'scan' variable. The function of the
  1692. * scan variable is overloaded, as it controls both the alternating
  1693. * raster scan direction used in the Floyd-Steinberg dithering and also
  1694. * the buffer alternation required for line-difference compression.
  1695. *
  1696. * Thus, the number of pointers required is as follows:
  1697. *
  1698. * errors: 2 (scan direction only)
  1699. * data: 4 (scan direction and alternating buffers)
  1700. * plane_data: 4 (scan direction and alternating buffers)
  1701. */
  1702. if (storage == 0 || ep_storage == 0) /* can't allocate working area */
  1703. return_error(gs_error_VMerror);
  1704. else {
  1705. int i, j;
  1706. byte *p = out_data = out_row = (byte *)storage;
  1707. byte *ep_p = (byte *)ep_storage;
  1708. data[0] = data[1] = data[2] = p;
  1709. data[3] = p + databuff_size;
  1710. out_row_alt = out_row + plane_size * 2;
  1711. if (bits_per_pixel > 1) {
  1712. p += databuff_size;
  1713. }
  1714. if (bits_per_pixel > 4) {
  1715. errors[0] = (int *)p + num_comps * 2;
  1716. errors[1] = errors[0] + databuff_size;
  1717. p += errbuff_size;
  1718. }
  1719. for (i = 0; i < num_comps; i++) {
  1720. plane_data[0][i] = plane_data[2][i] = p;
  1721. p += plane_size;
  1722. }
  1723. for (i = 0; i < num_comps; i++) {
  1724. plane_data[1][i] = p;
  1725. plane_data[3][i] = p + plane_size;
  1726. p += plane_size;
  1727. }
  1728. if (bits_per_pixel == 1) {
  1729. out_data = out_row = p; /* size is outbuff_size * 4 */
  1730. out_row_alt = out_row + plane_size * 2;
  1731. data[1] += databuff_size; /* coincides with plane_data pointers */
  1732. data[3] += databuff_size;
  1733. }
  1734. for (i = 0; i < num_comps; i++) {
  1735. for (j = 0; j < img_rows; j++) {
  1736. ep_raster_buf[i][j] = ep_p;
  1737. ep_p += plane_size;
  1738. }
  1739. /* Make a sentinel and align to word size. */
  1740. ep_print_buf = (byte *)((word)(ep_p + sizeof(word)) & ~(sizeof(word)-1));
  1741. }
  1742. ep_num_comps = num_comps;
  1743. }
  1744. /* Initialize printer. */
  1745. if (ptype == BJC600 || ptype == BJC800) {
  1746. bjc_init_page(pdev, prn_stream);
  1747. } else {
  1748. if (ptype == LJ4DITH) {
  1749. fputs("\033*rB", prn_stream);
  1750. } else {
  1751. fputs("\033*rbC", prn_stream); /* End raster graphics */
  1752. }
  1753. fprintf(prn_stream, "\033*t%dR", (int)x_dpi);
  1754. /* Set resolution */
  1755. }
  1756. /* Clear temp storage */
  1757. memset(storage, 0, storage_size_words * W);
  1758. #define DOFFSET (dev_t_margin(pdev) - DESKJET_PRINT_LIMIT) /* Print position */
  1759. #define POFFSET (dev_t_margin(pdev) - PAINTJET_PRINT_LIMIT)
  1760. #define EOFFSET (dev_t_margin(pdev) - ESC_P_PRINT_LIMIT)
  1761. #define BOFFSET (dev_t_margin(pdev) - bjc->printLimit)
  1762. switch (ptype) {
  1763. case LJ4DITH:
  1764. /* Page size, orientation, top margin & perforation skip */
  1765. fprintf(prn_stream, "\033&l26A\033&l0o0e0L\033*r0F" );
  1766. fprintf(prn_stream, "\033*p0x0Y" ); /* These Offsets are hacked ! */
  1767. fprintf(prn_stream, "\033&u600D\033*r1A" );
  1768. /* Select data compression */
  1769. compression = 3;
  1770. combined_escapes = 0;
  1771. break;
  1772. case DJ500C:
  1773. case DJ550C:
  1774. /* Page size, orientation, top margin & perforation skip */
  1775. fprintf(prn_stream, "\033&l%daolE", paper_size);
  1776. /* Set depletion and shingling levels */
  1777. fprintf(prn_stream, "\033*o%dd%dQ", cdj->depletion, cdj->shingling);
  1778. /* Move to top left of printed area */
  1779. fprintf(prn_stream, "\033*p%dY", (int)(300 * DOFFSET));
  1780. /* Set number of planes ((-)1 is mono, (-)3 is (cmy)rgb, -4 is cmyk),
  1781. * and raster width, then start raster graphics */
  1782. fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  1783. /* Select data compression */
  1784. compression = 9;
  1785. break;
  1786. case DNJ650C:
  1787. if (pdev->x_pixels_per_inch == 600) {
  1788. /* set resolution to 600dpi 1st through PJL command */
  1789. fprintf(prn_stream,"\033%%-12345X@PJL SET RESOLUTION = 600\n");
  1790. }
  1791. fprintf (prn_stream, "\033%%0B"); /* Enter HPGL/2 mode */
  1792. fprintf (prn_stream, "BP5,1"); /* Turn off autorotation */
  1793. fprintf (prn_stream, "PS%d,%d",
  1794. (int)((pdev->height/pdev->y_pixels_per_inch)*1016),
  1795. (int)((pdev->width/pdev->x_pixels_per_inch)*1016)); /* Set length/width of page */
  1796. fprintf (prn_stream, "PU"); /* Pen up */
  1797. fprintf (prn_stream, "PA%d,%d", 0, 0); /* Move pen to upper-left */
  1798. fprintf (prn_stream, "\033%%1A"); /* Enter HP-RTL mode */
  1799. fprintf (prn_stream, "\033&a1N"); /* No negative motion - allow plotting
  1800. while receiving */
  1801. if (pdev->x_pixels_per_inch == 600)
  1802. fprintf (prn_stream, "\033*t600R"); /* request 600dpi via HP RTL */
  1803. { static const char temp[] = {
  1804. 033, '*', 'v', '6', 'W',
  1805. 000 /* color model */,
  1806. 000 /* pixel encoding mode */,
  1807. 003 /* number of bits per index */,
  1808. 010 /* bits red */,
  1809. 010 /* bits green */,
  1810. 010 /* bits blue */
  1811. };
  1812. fwrite (temp, 1, sizeof(temp), prn_stream);
  1813. }
  1814. /* Set raster width */
  1815. fprintf(prn_stream, "\033*r%dS", raster_width);
  1816. /* Start raster graphics */
  1817. fprintf(prn_stream, "\033*r1A");
  1818. /* Select data compression */
  1819. compression = 1;
  1820. /* No combined escapes for raster transfers */
  1821. combined_escapes = 0;
  1822. break;
  1823. case PJXL300:
  1824. /* Page size, orientation, top margin & perforation skip */
  1825. fprintf(prn_stream, "\033&l%daolE", paper_size);
  1826. /* Set no-negative-motion mode, for faster (unbuffered) printing */
  1827. fprintf(prn_stream, "\033&a1N");
  1828. /* Set print quality */
  1829. fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  1830. /* Move to top left of printed area */
  1831. fprintf(prn_stream, "\033*p%dY", (int)(300 * POFFSET));
  1832. /* Configure colour setup */
  1833. if (pjxl->rendertype > 0) {
  1834. /* Set render type */
  1835. fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  1836. /* Configure image data */
  1837. fputs(cid_string, prn_stream);
  1838. /* Set raster width, then start raster graphics */
  1839. fprintf(prn_stream, "\033*r%ds1A", raster_width);
  1840. } else {
  1841. /* Set number of planes (1 is mono, 3 is rgb),
  1842. * and raster width, then start raster graphics */
  1843. fprintf(prn_stream, "\033*r%ds-%du0A", raster_width, num_comps);
  1844. }
  1845. /* No combined escapes for raster transfers */
  1846. combined_escapes = 0;
  1847. break;
  1848. case PJXL180:
  1849. /* Page size, orientation, top margin & perforation skip */
  1850. fprintf(prn_stream, "\033&l%daolE", paper_size);
  1851. /* Set print quality */
  1852. fprintf(prn_stream, "\033*o%dQ", pjxl->printqual);
  1853. /* Move to top left of printed area */
  1854. fprintf(prn_stream, "\033*p%dY", (int)(180 * POFFSET));
  1855. /* Configure colour setup */
  1856. if (pjxl->rendertype > 0) {
  1857. /* Set render type */
  1858. fprintf(prn_stream, "\033*t%dJ", pjxl->rendertype);
  1859. /* Configure image data */
  1860. fputs(cid_string, prn_stream);
  1861. /* Set raster width, then start raster graphics */
  1862. fprintf(prn_stream, "\033*r%ds1A", raster_width);
  1863. } else {
  1864. /* Set number of planes (1 is mono, 3 is rgb),
  1865. * and raster width, then start raster graphics */
  1866. fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  1867. }
  1868. break;
  1869. case PJ180:
  1870. case DECLJ250:
  1871. /* Disable perforation skip */
  1872. fprintf(prn_stream, "\033&lL");
  1873. /* Move to top left of printed area */
  1874. fprintf(prn_stream, "\033&a%dV", (int)(720 * POFFSET));
  1875. /* Set number of planes (1 is mono, 3 is rgb),
  1876. * and raster width, then start raster graphics */
  1877. fprintf(prn_stream, "\033*r%ds%du0A", raster_width, num_comps);
  1878. if (ptype == DECLJ250) {
  1879. /* No combined escapes for raster transfers */
  1880. combined_escapes = 0;
  1881. /* From here on, we're a standard Paintjet .. */
  1882. ptype = PJ180;
  1883. }
  1884. /* Select data compression */
  1885. compression = 1;
  1886. break;
  1887. case ESC_P:
  1888. /* Move to top left of printed area (must be modified for large movement(YK))*/
  1889. if ((int)(EOFFSET*360)) fprintf(prn_stream, "\033|J%c%c", 0, (int)(360*EOFFSET));
  1890. combined_escapes = 0;
  1891. break;
  1892. case BJC600:
  1893. case BJC800:
  1894. /* Move to top left of printed area */
  1895. bjc_v_skip((int)(pdev->HWResolution[1] * BOFFSET), pdev, prn_stream);
  1896. combined_escapes = 0;
  1897. compression = 2; /* BJC600 uses the same method as mode 2 compression */
  1898. break;
  1899. }
  1900. /* Unfortunately, the Paintjet XL300 PCL interpreter introduces a
  1901. * version of the PCL language which is different to all earlier HP
  1902. * colour and mono inkjets, in that it loses the very useful ability
  1903. * to use combined escape sequences with the raster transfer
  1904. * commands. In this respect, it is incompatible even with the older
  1905. * 180 dpi PaintJet and PaintJet XL printers! Another regrettable
  1906. * omission is that 'mode 9' compression is not supported, as this
  1907. * mode can give both computational and PCL file size advantages. */
  1908. if (combined_escapes) {
  1909. /* From now on, all escape commands start with \033*b, so we
  1910. * combine them (if the printer supports this). */
  1911. fputs("\033*b", prn_stream);
  1912. /* Set compression if the mode has been defined. */
  1913. if (compression)
  1914. fprintf(prn_stream, "%dm", compression);
  1915. }
  1916. else if (ptype == BJC600 || ptype == BJC800)
  1917. ; /* Currently, nothing to do. */
  1918. else
  1919. if (compression)
  1920. fprintf(prn_stream, "\033*b%dM", compression);
  1921. /* Send each scan line in turn */
  1922. {
  1923. int cErr, mErr, yErr, kErr;
  1924. int this_pass, lnum, i;
  1925. int start_rows;
  1926. int lend, num_blank_lines = 0;
  1927. word rmask = ~(word) 0 << ((-pdev->width * storage_bpp) & (W * 8 - 1));
  1928. lend = pdev->height - (dev_t_margin(pdev) + dev_b_margin(pdev)) * y_dpi;
  1929. switch (ptype) {
  1930. case BJC600:
  1931. case BJC800:
  1932. start_rows = BJC_HEAD_ROWS;
  1933. break;
  1934. /* Inhibit blank line printing for RGB-only printers, since in
  1935. * this case 'blank' means black! Also disabled for XL300 due to
  1936. * an obscure bug in the printer's firmware */
  1937. case PJ180:
  1938. case PJXL180:
  1939. case PJXL300:
  1940. start_rows = -1;
  1941. break;
  1942. default:
  1943. start_rows = (num_comps == 1) ? HEAD_ROWS_MONO - 1 :
  1944. HEAD_ROWS_COLOUR - 1;
  1945. break;
  1946. }
  1947. cErr = mErr = yErr = kErr = 0;
  1948. if (bits_per_pixel > 4) { /* Randomly seed initial error buffer */
  1949. if (cprn_device->cmyk > 0 && expanded_bpp == 32) {
  1950. bjc_fscmyk(data, plane_data, errors, plane_size, -1);
  1951. } else {
  1952. int *ep = errors[0];
  1953. for (i = 0; i < databuff_size; i++) {
  1954. *ep++ = RANDOM;
  1955. }
  1956. }
  1957. }
  1958. this_pass = start_rows;
  1959. for (lnum = 0; lnum < lend; lnum++) {
  1960. word *data_words = (word *)data[scan];
  1961. register word *end_data = data_words + line_size_words;
  1962. gdev_prn_copy_scan_lines(pdev, lnum, data[scan], line_size);
  1963. /* Mask off 1-bits beyond the line width. */
  1964. end_data[-1] &= rmask;
  1965. /* Remove trailing 0s. */
  1966. while (end_data > data_words && end_data[-1] == 0)
  1967. end_data--;
  1968. if (ptype != DNJ650C) /* DesignJet can't skip blank lines ? ? */
  1969. if (end_data == data_words) { /* Blank line */
  1970. num_blank_lines++;
  1971. continue;
  1972. }
  1973. /* Skip blank lines if any */
  1974. if (num_blank_lines > 0) {
  1975. if (ptype == ESC_P) {
  1976. ep_print_image(prn_stream, 'B', 0, num_blank_lines);
  1977. } else if (ptype == BJC600 || ptype == BJC800) {
  1978. bjc_v_skip(num_blank_lines, pdev, prn_stream);
  1979. } else if (num_blank_lines < this_pass) {
  1980. /* Moving down from current position
  1981. * causes head motion on the DeskJets, so
  1982. * if the number of lines is within the
  1983. * current pass of the print head, we're
  1984. * better off printing blanks. */
  1985. this_pass -= num_blank_lines;
  1986. if (combined_escapes) {
  1987. fputc('y', prn_stream); /* Clear current and seed rows */
  1988. for (; num_blank_lines; num_blank_lines--)
  1989. fputc('w', prn_stream);
  1990. } else {
  1991. #if 0
  1992. /**************** The following code has been proposed ****************/
  1993. /**************** as a replacement: ****************/
  1994. fputs("\033*b1Y", prn_stream); /* Clear current and seed rows */
  1995. if ( num_blank_lines > 1 )
  1996. fprintf(prn_stream, "\033*b%dY", num_blank_lines - 1);
  1997. num_blank_lines = 0;
  1998. #else
  1999. fputs("\033*bY", prn_stream); /* Clear current and seed rows */
  2000. if (ptype == DNJ650C) {
  2001. fprintf (prn_stream, "\033*b%dY", num_blank_lines);
  2002. num_blank_lines = 0;
  2003. }
  2004. else {
  2005. for (; num_blank_lines; num_blank_lines--)
  2006. fputs("\033*bW", prn_stream);
  2007. }
  2008. #endif
  2009. }
  2010. } else {
  2011. if (combined_escapes)
  2012. fprintf(prn_stream, "%dy", num_blank_lines);
  2013. else
  2014. fprintf(prn_stream, "\033*b%dY", num_blank_lines);
  2015. }
  2016. memset(plane_data[1 - scan][0], 0, plane_size * num_comps);
  2017. num_blank_lines = 0;
  2018. this_pass = start_rows;
  2019. }
  2020. { /* Printing non-blank lines */
  2021. register byte *kP = plane_data[scan + 2][3];
  2022. register byte *cP = plane_data[scan + 2][2];
  2023. register byte *mP = plane_data[scan + 2][1];
  2024. register byte *yP = plane_data[scan + 2][0];
  2025. register byte *dp = data[scan + 2];
  2026. register int *ep = errors[scan];
  2027. int zero_row_count;
  2028. int i, j;
  2029. byte *odp;
  2030. if (this_pass)
  2031. this_pass--;
  2032. else
  2033. this_pass = start_rows;
  2034. if (expanded_bpp > bits_per_pixel) { /* Expand line if required */
  2035. cdj_expand_line(data_words, line_size,
  2036. cprn_device->cmyk,
  2037. bits_per_pixel, expanded_bpp);
  2038. }
  2039. /* In colour modes, we have some bit-shuffling to do before
  2040. * we can print the data; in FS mode we also have the
  2041. * dithering to take care of. */
  2042. switch (expanded_bpp) { /* Can be 1, 3, 8, 24 or 32 */
  2043. case 3:
  2044. /* Transpose the data to get pixel planes. */
  2045. for (i = 0, odp = plane_data[scan][0]; i < databuff_size;
  2046. i += 8, odp++) { /* The following is for 16-bit
  2047. * machines */
  2048. #define spread3(c)\
  2049. { 0, c, c*0x100, c*0x101, c*0x10000L, c*0x10001L, c*0x10100L, c*0x10101L }
  2050. static ulong spr40[8] = spread3(0x40);
  2051. static ulong spr08[8] = spread3(8);
  2052. static ulong spr02[8] = spread3(2);
  2053. register byte *dp = data[scan] + i;
  2054. register ulong pword =
  2055. (spr40[dp[0]] << 1) +
  2056. (spr40[dp[1]]) +
  2057. (spr40[dp[2]] >> 1) +
  2058. (spr08[dp[3]] << 1) +
  2059. (spr08[dp[4]]) +
  2060. (spr08[dp[5]] >> 1) +
  2061. (spr02[dp[6]]) +
  2062. (spr02[dp[7]] >> 1);
  2063. odp[0] = (byte) (pword >> 16);
  2064. odp[plane_size] = (byte) (pword >> 8);
  2065. odp[plane_size * 2] = (byte) (pword);
  2066. }
  2067. break;
  2068. case 8:
  2069. switch (ptype) {
  2070. case BJC600:
  2071. case BJC800:
  2072. if (bjcparams.ditheringType == BJC_DITHER_NONE) {
  2073. COPYline(scan, i, j, plane_size, cP, mP, yP, kP, 1);
  2074. break;
  2075. }
  2076. default:
  2077. FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  2078. cP, mP, yP, kP, 1);
  2079. }
  2080. break;
  2081. case 24:
  2082. FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  2083. cP, mP, yP, kP, 3);
  2084. break;
  2085. case 32:
  2086. if (cprn_device->cmyk > 0) {
  2087. bjc_fscmyk(data, plane_data, errors, plane_size, scan);
  2088. } else {
  2089. FSDline(scan, i, j, plane_size, cErr, mErr, yErr, kErr,
  2090. cP, mP, yP, kP, 4);
  2091. }
  2092. break;
  2093. } /* switch(expanded_bpp) */
  2094. /* Make sure all black is in the k plane */
  2095. if (num_comps == 4 && (cprn_device->cmyk <= 0 || expanded_bpp != 32)) {
  2096. register word *kp = (word *)plane_data[scan][3];
  2097. register word *cp = (word *)plane_data[scan][2];
  2098. register word *mp = (word *)plane_data[scan][1];
  2099. register word *yp = (word *)plane_data[scan][0];
  2100. if (bits_per_pixel > 4) { /* Done as 4 planes */
  2101. for (i = 0; i < plane_size / W; i++) {
  2102. word bits = *cp & *mp & *yp;
  2103. *kp++ |= bits;
  2104. bits = ~bits;
  2105. *cp++ &= bits;
  2106. *mp++ &= bits;
  2107. *yp++ &= bits;
  2108. }
  2109. } else { /* This has really been done as 3 planes */
  2110. for (i = 0; i < plane_size / W; i++) {
  2111. word bits = *cp & *mp & *yp;
  2112. *kp++ = bits;
  2113. bits = ~bits;
  2114. *cp++ &= bits;
  2115. *mp++ &= bits;
  2116. *yp++ &= bits;
  2117. }
  2118. }
  2119. }
  2120. /* Transfer raster graphics in the order (K), C, M, Y */
  2121. for (zero_row_count = 0, i = num_comps - 1; i >= 0; i--) {
  2122. int output_plane = 1;
  2123. int out_count = 0;
  2124. switch (ptype) {
  2125. case DJ500C: /* Always compress using mode 9 */
  2126. case DJ550C:
  2127. out_count = gdev_pcl_mode9compress(plane_size,
  2128. plane_data[scan][i],
  2129. plane_data[1 - scan][i],
  2130. out_data);
  2131. /* This optimisation allows early termination of the
  2132. * row, but this doesn't work correctly in an alternating
  2133. * mode 2 / mode 3 regime, so we only use it with mode 9
  2134. * compression */
  2135. if (out_count == 0)
  2136. { output_plane = 0; /* No further output for this plane */
  2137. if (i == 0)
  2138. fputc('w', prn_stream);
  2139. else
  2140. zero_row_count++;
  2141. }
  2142. else
  2143. { for (; zero_row_count; zero_row_count--)
  2144. fputc('v', prn_stream);
  2145. }
  2146. break;
  2147. case PJ180:
  2148. case DNJ650C:
  2149. if (num_comps > 1)
  2150. { word *wp = (word *)plane_data[scan][i];
  2151. for (j = 0; j < plane_size / W; j++, wp++)
  2152. *wp = ~*wp;
  2153. }
  2154. out_count = gdev_pcl_mode1compress((const byte *)
  2155. plane_data[scan][i],
  2156. (const byte *)
  2157. plane_data[scan][i] + plane_size - 1,
  2158. out_data);
  2159. break;
  2160. case PJXL180: /* Need to invert data as CMY not supported */
  2161. if (num_comps > 1)
  2162. { word *wp = (word *)plane_data[scan][i];
  2163. for (j = 0; j < plane_size / W; j++, wp++)
  2164. *wp = ~*wp;
  2165. }
  2166. /* fall through .. */
  2167. case PJXL300: /* Compression modes 2 and 3 are both
  2168. * available. Try both and see which one
  2169. * produces the least output data. */
  2170. case LJ4DITH:
  2171. { const byte *plane = plane_data[scan][i];
  2172. byte *prev_plane = plane_data[1 - scan][i];
  2173. const word *row = (word *)plane;
  2174. const word *end_row = row + plane_size/W;
  2175. int count2 = gdev_pcl_mode2compress(row, end_row, out_row_alt);
  2176. int count3 = gdev_pcl_mode3compress(plane_size, plane, prev_plane, out_row);
  2177. int penalty = combined_escapes ? strlen("#m") : strlen("\033*b#M");
  2178. int penalty2 = (compression == 2 ? 0 : penalty);
  2179. int penalty3 = (compression == 3 ? 0 : penalty);
  2180. if (count3 + penalty3 < count2 + penalty2)
  2181. { if ( compression != 3 ) {
  2182. if (combined_escapes)
  2183. fputs("3m", prn_stream);
  2184. else
  2185. fputs("\033*b3M", prn_stream);
  2186. compression = 3;
  2187. }
  2188. out_data = out_row;
  2189. out_count = count3;
  2190. }
  2191. else
  2192. { if ( compression != 2 ) {
  2193. if (combined_escapes)
  2194. fputs("2m", prn_stream);
  2195. else
  2196. fputs("\033*b2M", prn_stream);
  2197. compression = 2;
  2198. }
  2199. out_data = out_row_alt;
  2200. out_count = count2;
  2201. }
  2202. }
  2203. break;
  2204. case BJC600:
  2205. case BJC800:
  2206. { const byte *plane = (byte *)plane_data[scan][i];
  2207. int count2 = bjc_compress(plane, plane + plane_size, out_row_alt);
  2208. out_data = out_row_alt;
  2209. out_count = count2;
  2210. }
  2211. break;
  2212. }
  2213. if (output_plane) {
  2214. if (combined_escapes)
  2215. fprintf(prn_stream, "%d%c", out_count, "wvvv"[i]);
  2216. else if (ptype == BJC600 || ptype == BJC800) {
  2217. if (out_count)
  2218. bjc_raster_cmd(num_comps == 1 ? 3 : i,
  2219. out_count, out_data, pdev, prn_stream);
  2220. if (i == 0) bjc_v_skip(1, pdev, prn_stream);
  2221. } else if (ptype == ESC_P)
  2222. ep_print_image(prn_stream, i, plane_data[scan][i], plane_size);
  2223. else
  2224. fprintf(prn_stream, "\033*b%d%c", out_count, "WVVV"[i]);
  2225. if (ptype < ESC_P)
  2226. fwrite(out_data, sizeof(byte), out_count, prn_stream);
  2227. }
  2228. } /* Transfer Raster Graphics ... */
  2229. if (ptype == ESC_P)
  2230. ep_print_image(prn_stream, 'I', 0, 0); /* increment line index */
  2231. scan = 1 - scan; /* toggle scan direction */
  2232. } /* Printing non-blank lines */
  2233. } /* for lnum ... */
  2234. } /* send each scan line in turn */
  2235. if (combined_escapes)
  2236. fputs("0M", prn_stream);
  2237. /* end raster graphics */
  2238. if (ptype == BJC600 || ptype == BJC800) {
  2239. bjc_finish_page(pdev, prn_stream);
  2240. }
  2241. else if (ptype != ESC_P)
  2242. fputs("\033*rbC\033E", prn_stream);
  2243. /* eject page */
  2244. if (ptype == PJ180)
  2245. fputc('\f', prn_stream);
  2246. else if (ptype == DNJ650C)
  2247. fputs ("\033*rC\033%0BPG;", prn_stream);
  2248. else if (ptype == BJC600 || ptype == BJC800)
  2249. ; /* Already done */
  2250. else if (ptype == ESC_P) {
  2251. ep_print_image(prn_stream, 'F', 0, 0); /* flush print buffer */
  2252. fputs("\014\033@", prn_stream); /* reset after eject page */
  2253. } else
  2254. fputs("\033&l0H", prn_stream);
  2255. /* free temporary storage */
  2256. gs_free((char *) ep_storage, ep_storage_size_words, W, "ep_print_buffer");
  2257. gs_free((char *) storage, storage_size_words, W, "hp_colour_print_page");
  2258. return 0;
  2259. }
  2260. /*
  2261. * Row compression for the H-P PaintJet.
  2262. * Compresses data from row up to end_row, storing the result
  2263. * starting at compressed. Returns the number of bytes stored.
  2264. * The compressed format consists of a byte N followed by a
  2265. * data byte that is to be repeated N+1 times.
  2266. * In the worst case, the `compressed' representation is
  2267. * twice as large as the input.
  2268. * We complement the bytes at the same time, because
  2269. * we accumulated the image in complemented form.
  2270. */
  2271. private int
  2272. gdev_pcl_mode1compress(const byte *row, const byte *end_row, byte *compressed)
  2273. { register const byte *in = row;
  2274. register byte *out = compressed;
  2275. while ( in < end_row )
  2276. { byte test = *in++;
  2277. const byte *run = in;
  2278. while ( in < end_row && *in == test ) in++;
  2279. /* Note that in - run + 1 is the repetition count. */
  2280. while ( in - run > 255 )
  2281. { *out++ = 255;
  2282. *out++ = test;
  2283. run += 256;
  2284. }
  2285. *out++ = in - run;
  2286. *out++ = test;
  2287. }
  2288. return out - compressed;
  2289. }
  2290. /*
  2291. * Map a CMYK color to a color index. We just use depth / 4 bits per color
  2292. * to produce the color index.
  2293. *
  2294. * Important note: CMYK values are stored in the order K, C, M, Y because of
  2295. * the way the HP drivers work.
  2296. *
  2297. */
  2298. #define gx_color_value_to_bits(cv, b) \
  2299. ((cv) >> (gx_color_value_bits - (b)))
  2300. #define gx_bits_to_color_value(cv, b) \
  2301. ((cv) << (gx_color_value_bits - (b)))
  2302. #define gx_cmyk_value_bits(c, m, y, k, b) \
  2303. ((gx_color_value_to_bits((k), (b)) << (3 * (b))) | \
  2304. (gx_color_value_to_bits((c), (b)) << (2 * (b))) | \
  2305. (gx_color_value_to_bits((m), (b)) << (b)) | \
  2306. (gx_color_value_to_bits((y), (b))))
  2307. #define gx_value_cmyk_bits(v, c, m, y, k, b) \
  2308. (k) = gx_bits_to_color_value(((v) >> (3 * (b))) & ((1 << (b)) - 1), (b)), \
  2309. (c) = gx_bits_to_color_value(((v) >> (2 * (b))) & ((1 << (b)) - 1), (b)), \
  2310. (m) = gx_bits_to_color_value(((v) >> (b)) & ((1 << (b)) - 1), (b)), \
  2311. (y) = gx_bits_to_color_value((v) & ((1 << (b)) - 1), (b))
  2312. private gx_color_index
  2313. gdev_cmyk_map_cmyk_color(gx_device* pdev,
  2314. gx_color_value cyan, gx_color_value magenta, gx_color_value yellow,
  2315. gx_color_value black) {
  2316. gx_color_index color;
  2317. switch (pdev->color_info.depth) {
  2318. case 1:
  2319. color = (cyan | magenta | yellow | black) > gx_max_color_value / 2 ?
  2320. (gx_color_index) 1 : (gx_color_index) 0;
  2321. break;
  2322. default: {
  2323. int nbits = pdev->color_info.depth;
  2324. if (cyan == magenta && magenta == yellow) {
  2325. /* Convert CMYK to gray -- Red Book 6.2.2 */
  2326. float bpart = ((float) cyan) * (lum_red_weight / 100.) +
  2327. ((float) magenta) * (lum_green_weight / 100.) +
  2328. ((float) yellow) * (lum_blue_weight / 100.) +
  2329. (float) black;
  2330. cyan = magenta = yellow = (gx_color_index) 0;
  2331. black = (gx_color_index) (bpart > gx_max_color_value ?
  2332. gx_max_color_value : bpart);
  2333. }
  2334. color = gx_cmyk_value_bits(cyan, magenta, yellow, black,
  2335. nbits >> 2);
  2336. }
  2337. }
  2338. return color;
  2339. }
  2340. /* Mapping of RGB colors to gray values. */
  2341. private gx_color_index
  2342. gdev_cmyk_map_rgb_color(gx_device *pdev, gx_color_value r, gx_color_value g, gx_color_value b)
  2343. {
  2344. if (gx_color_value_to_byte(r & g & b) == 0xff) {
  2345. return (gx_color_index) 0; /* White */
  2346. } else {
  2347. gx_color_value c = gx_max_color_value - r;
  2348. gx_color_value m = gx_max_color_value - g;
  2349. gx_color_value y = gx_max_color_value - b;
  2350. switch (pdev->color_info.depth) {
  2351. case 1:
  2352. return (c | m | y) > gx_max_color_value / 2 ?
  2353. (gx_color_index) 1 : (gx_color_index) 0;
  2354. /*NOTREACHED*/
  2355. break;
  2356. case 8:
  2357. return ((ulong) c * lum_red_weight * 10
  2358. + (ulong) m * lum_green_weight * 10
  2359. + (ulong) y * lum_blue_weight * 10)
  2360. >> (gx_color_value_bits + 2);
  2361. /*NOTREACHED*/
  2362. break;
  2363. }
  2364. }
  2365. return (gx_color_index) 0; /* This should never happen. */
  2366. }
  2367. /* Mapping of CMYK colors. */
  2368. private int
  2369. gdev_cmyk_map_color_rgb(gx_device *pdev, gx_color_index color, gx_color_value prgb[3])
  2370. {
  2371. switch (pdev->color_info.depth) {
  2372. case 1:
  2373. prgb[0] = prgb[1] = prgb[2] = gx_max_color_value * (1 - color);
  2374. break;
  2375. case 8:
  2376. if (pdev->color_info.num_components == 1) {
  2377. gx_color_value value = (gx_color_value) color ^ 0xff;
  2378. prgb[0] = prgb[1] = prgb[2] = (value << 8) + value;
  2379. break;
  2380. }
  2381. default: {
  2382. unsigned long bcyan, bmagenta, byellow, black;
  2383. int nbits = pdev->color_info.depth;
  2384. gx_value_cmyk_bits(color, bcyan, bmagenta, byellow, black,
  2385. nbits >> 2);
  2386. #ifdef USE_ADOBE_CMYK_RGB
  2387. /* R = 1.0 - min(1.0, C + K), etc. */
  2388. bcyan += black, bmagenta += black, byellow += black;
  2389. prgb[0] = (bcyan > gx_max_color_value ? (gx_color_value) 0 :
  2390. gx_max_color_value - bcyan);
  2391. prgb[1] = (bmagenta > gx_max_color_value ? (gx_color_value) 0 :
  2392. gx_max_color_value - bmagenta);
  2393. prgb[2] = (byellow > gx_max_color_value ? (gx_color_value) 0 :
  2394. gx_max_color_value - byellow);
  2395. #else
  2396. /* R = (1.0 - C) * (1.0 - K), etc. */
  2397. prgb[0] = (gx_color_value)
  2398. ((ulong)(gx_max_color_value - bcyan) *
  2399. (gx_max_color_value - black) / gx_max_color_value);
  2400. prgb[1] = (gx_color_value)
  2401. ((ulong)(gx_max_color_value - bmagenta) *
  2402. (gx_max_color_value - black) / gx_max_color_value);
  2403. prgb[2] = (gx_color_value)
  2404. ((ulong)(gx_max_color_value - byellow) *
  2405. (gx_max_color_value - black) / gx_max_color_value);
  2406. #endif
  2407. }
  2408. }
  2409. return 0;
  2410. }
  2411. /*
  2412. * Map a r-g-b color to a color index.
  2413. * We complement the colours, since we're using cmy anyway, and
  2414. * because the buffering routines expect white to be zero.
  2415. * Includes colour balancing, following HP recommendations, to try
  2416. * and correct the greenish cast resulting from an equal mix of the
  2417. * c, m, y, inks by reducing the cyan component to give a truer black.
  2418. */
  2419. /* Simple black generation/under-color removal with BG(k) = UG(k) = k. YA. */
  2420. #define bg_and_ucr(c, c_v, m, m_v, y, y_v, k) \
  2421. do { \
  2422. register byte cv = c_v, mv = m_v, yv = y_v, kv; \
  2423. \
  2424. kv = (cv > mv ? mv : cv); \
  2425. kv = (yv > k ? k : y); \
  2426. y = yv - kv; m = mv - kv; c = cv -kv; k = kv; \
  2427. } while (0)
  2428. private gx_color_index
  2429. gdev_pcl_map_rgb_color(gx_device *pdev, gx_color_value r,
  2430. gx_color_value g, gx_color_value b)
  2431. {
  2432. if (gx_color_value_to_byte(r & g & b) == 0xff)
  2433. return (gx_color_index)0; /* white */
  2434. else {
  2435. int correction = cprn_device->correction;
  2436. gx_color_value c = gx_max_color_value - r;
  2437. gx_color_value m = gx_max_color_value - g;
  2438. gx_color_value y = gx_max_color_value - b;
  2439. /* Colour correction for better blacks when using the colour ink
  2440. * cartridge (on the DeskJet 500C only). We reduce the cyan component
  2441. * by some fraction (eg. 4/5) to correct the slightly greenish cast
  2442. * resulting from an equal mix of the three inks */
  2443. if (correction) {
  2444. ulong maxval, minval, range;
  2445. maxval = c >= m ? (c >= y ? c : y) : (m >= y ? m : y);
  2446. if (maxval > 0) {
  2447. minval = c <= m ? (c <= y ? c : y) : (m <= y? m : y);
  2448. range = maxval - minval;
  2449. #define shift (gx_color_value_bits - 12)
  2450. c = ((c >> shift) * (range + (maxval * correction))) /
  2451. ((maxval * (correction + 1)) >> shift);
  2452. }
  2453. }
  2454. switch (pdev->color_info.depth) {
  2455. case 1:
  2456. return ((c | m | y) > gx_max_color_value / 2 ?
  2457. (gx_color_index)1 : (gx_color_index)0);
  2458. case 8:
  2459. if (pdev->color_info.num_components >= 3)
  2460. #define gx_color_value_to_1bit(cv) ((cv) >> (gx_color_value_bits - 1))
  2461. return (gx_color_value_to_1bit(c) +
  2462. (gx_color_value_to_1bit(m) << 1) +
  2463. (gx_color_value_to_1bit(y) << 2));
  2464. else
  2465. #define red_weight 306
  2466. #define green_weight 601
  2467. #define blue_weight 117
  2468. return ((((ulong)c * red_weight +
  2469. (ulong)m * green_weight +
  2470. (ulong)y * blue_weight)
  2471. >> (gx_color_value_bits + 2)));
  2472. case 16:
  2473. #define gx_color_value_to_5bits(cv) ((cv) >> (gx_color_value_bits - 5))
  2474. #define gx_color_value_to_6bits(cv) ((cv) >> (gx_color_value_bits - 6))
  2475. return (gx_color_value_to_5bits(y) +
  2476. (gx_color_value_to_6bits(m) << 5) +
  2477. (gx_color_value_to_5bits(c) << 11));
  2478. case 24:
  2479. return (gx_color_value_to_byte(y) +
  2480. (gx_color_value_to_byte(m) << 8) +
  2481. ((ulong)gx_color_value_to_byte(c) << 16));
  2482. case 32:
  2483. { return ((c == m && c == y) ? ((ulong)gx_color_value_to_byte(c) << 24)
  2484. : (gx_color_value_to_byte(y) +
  2485. (gx_color_value_to_byte(m) << 8) +
  2486. ((ulong)gx_color_value_to_byte(c) << 16)));
  2487. }
  2488. }
  2489. }
  2490. return (gx_color_index)0; /* This never happens */
  2491. }
  2492. /* Map a color index to a r-g-b color. */
  2493. private int
  2494. gdev_pcl_map_color_rgb(gx_device *pdev, gx_color_index color,
  2495. gx_color_value prgb[3])
  2496. {
  2497. /* For the moment, we simply ignore any black correction */
  2498. switch (pdev->color_info.depth) {
  2499. case 1:
  2500. prgb[0] = prgb[1] = prgb[2] = -((gx_color_value)color ^ 1);
  2501. break;
  2502. case 8:
  2503. if (pdev->color_info.num_components >= 3)
  2504. { gx_color_value c = (gx_color_value)color ^ 7;
  2505. prgb[0] = -(c & 1);
  2506. prgb[1] = -((c >> 1) & 1);
  2507. prgb[2] = -(c >> 2);
  2508. }
  2509. else
  2510. { gx_color_value value = (gx_color_value)color ^ 0xff;
  2511. prgb[0] = prgb[1] = prgb[2] = (value << 8) + value;
  2512. }
  2513. break;
  2514. case 16:
  2515. { gx_color_value c = (gx_color_value)color ^ 0xffff;
  2516. ushort value = c >> 11;
  2517. prgb[0] = ((value << 11) + (value << 6) + (value << 1) +
  2518. (value >> 4)) >> (16 - gx_color_value_bits);
  2519. value = (c >> 6) & 0x3f;
  2520. prgb[1] = ((value << 10) + (value << 4) + (value >> 2))
  2521. >> (16 - gx_color_value_bits);
  2522. value = c & 0x1f;
  2523. prgb[2] = ((value << 11) + (value << 6) + (value << 1) +
  2524. (value >> 4)) >> (16 - gx_color_value_bits);
  2525. }
  2526. break;
  2527. case 24:
  2528. { gx_color_value c = (gx_color_value)color ^ 0xffffff;
  2529. prgb[0] = gx_color_value_from_byte(c >> 16);
  2530. prgb[1] = gx_color_value_from_byte((c >> 8) & 0xff);
  2531. prgb[2] = gx_color_value_from_byte(c & 0xff);
  2532. }
  2533. break;
  2534. case 32:
  2535. #define gx_maxcol gx_color_value_from_byte(gx_color_value_to_byte(gx_max_color_value))
  2536. { gx_color_value w = gx_maxcol - gx_color_value_from_byte(color >> 24);
  2537. prgb[0] = w - gx_color_value_from_byte((color >> 16) & 0xff);
  2538. prgb[1] = w - gx_color_value_from_byte((color >> 8) & 0xff);
  2539. prgb[2] = w - gx_color_value_from_byte(color & 0xff);
  2540. }
  2541. break;
  2542. }
  2543. return 0;
  2544. }
  2545. /*
  2546. * Convert and expand scanlines:
  2547. *
  2548. * For devices with 3 components:
  2549. *
  2550. * (a) 16 -> 24 bit (1-stage)
  2551. * (b) 16 -> 32 bit (2-stage)
  2552. * or (c) 24 -> 32 bit (1-stage)
  2553. *
  2554. * For devices with 4 components:
  2555. *
  2556. * (a) 16 -> 32 bit (1-stage)
  2557. * (b) 8 -> 32 bit (2-stage)
  2558. * or (c) 24 -> 32 bit (1-stage)
  2559. *
  2560. */
  2561. private void
  2562. cdj_expand_line(word *line, int linesize, short cmyk, int bpp, int ebpp)
  2563. {
  2564. int endline = linesize;
  2565. byte *start = (byte *)line;
  2566. register byte *in, *out;
  2567. if (cmyk > 0) {
  2568. if (bpp == 8) {
  2569. in = start + endline;
  2570. out = start + (endline *= 2);
  2571. while (in > start) {
  2572. register byte b0;
  2573. register byte bs0, bs1, bs2, bs3;
  2574. b0 = *--in;
  2575. bs0 = b0 & 0x03;
  2576. bs1 = (b0 >> 2) & 0x03;
  2577. bs2 = (b0 >> 4) & 0x03;
  2578. bs3 = (b0 >> 6) & 0x03;
  2579. *--out = (bs0 << 2) + bs0 + (bs1 << 6) + (bs1 << 4);
  2580. *--out = (bs2 << 2) + bs2 + (bs3 << 6) + (bs3 << 4);
  2581. }
  2582. }
  2583. if (bpp == 24) {
  2584. endline = (endline + 2) / 3;
  2585. in = start + endline * 3;
  2586. out = start + endline * 4;
  2587. while (in > start) {
  2588. register byte b0, b1, b2;
  2589. b0 = *--in;
  2590. b1 = *--in;
  2591. b2 = *--in;
  2592. *--out = (b0 << 2) + ((b0 >> 4) & 0x03);
  2593. *--out = ((b1 & 0x0f) << 4) + ((b0 >> 6) << 2)
  2594. + ((b1 >> 2) & 0x03);
  2595. *--out = ((b2 & 0x03) << 6) + ((b1 >> 4) << 2) + (b2 & 0x03);
  2596. *--out = (b2 & 0xfc) + ((b2 >> 6) & 0x03);
  2597. }
  2598. } else if (ebpp == 32) {
  2599. endline = (endline + 1) / 2;
  2600. in = start + endline * 2;
  2601. out = start + (endline *= 4);
  2602. while (in > start) {
  2603. register byte b0, b1;
  2604. b0 = *--in;
  2605. b1 = *--in;
  2606. *--out = (b0 << 4) + ((b0 >> 4) & 0x07);
  2607. *--out = (b0 & 0xf0) + ((b0 >> 4) & 0xf);
  2608. *--out = (b1 << 4) + ((b1 >> 4) & 0x0f);
  2609. *--out = (b1 & 0xf0) + ((b1 >> 4) & 0xf);
  2610. }
  2611. }
  2612. } else /* cmyk > 0 */ {
  2613. if (bpp == 16) /* 16 to 24 (cmy) if required */
  2614. { register byte b0, b1;
  2615. endline = ((endline + 1) / 2);
  2616. in = start + endline * 2;
  2617. out = start + (endline *= 3);
  2618. while (in > start)
  2619. { b0 = *--in;
  2620. b1 = *--in;
  2621. *--out = (b0 << 3) + ((b0 >> 2) & 0x7);
  2622. *--out = (b1 << 5) + ((b0 >> 3) & 0x1c) + ((b1 >> 1) & 0x3);
  2623. *--out = (b1 & 0xf8) + (b1 >> 5);
  2624. }
  2625. }
  2626. if (ebpp == 32) /* 24/32 (cmy) to 32 (cmyk) if required */
  2627. { register byte c, m, y;
  2628. endline = ((endline + 2) / 3);
  2629. in = start + endline * 3;
  2630. out = start + endline * 4;
  2631. while (in > start)
  2632. {
  2633. y = *--in;
  2634. m = *--in;
  2635. c = *--in;
  2636. if (c == y && c == m) {
  2637. *--out = 0, *--out = 0, *--out = 0;
  2638. *--out = c;
  2639. } else {
  2640. *--out = y, *--out = m, *--out = c;
  2641. *--out = 0;
  2642. }
  2643. }
  2644. }
  2645. }
  2646. }
  2647. private int
  2648. cdj_put_param_int(gs_param_list *plist, gs_param_name pname, int *pvalue,
  2649. int minval, int maxval, int ecode)
  2650. { int code, value;
  2651. switch ( code = param_read_int(plist, pname, &value) )
  2652. {
  2653. default:
  2654. return code;
  2655. case 1:
  2656. return ecode;
  2657. case 0:
  2658. if ( value < minval || value > maxval )
  2659. param_signal_error(plist, pname, gs_error_rangecheck);
  2660. *pvalue = value;
  2661. return (ecode < 0 ? ecode : 1);
  2662. }
  2663. }
  2664. private int
  2665. cdj_set_bpp(gx_device *pdev, int bpp, int ccomps)
  2666. { gx_device_color_info *ci = &pdev->color_info;
  2667. if (ccomps && bpp == 0) {
  2668. if (cprn_device->cmyk) {
  2669. switch (ccomps) {
  2670. default:
  2671. return gs_error_rangecheck;
  2672. /*NOTREACHED*/
  2673. break;
  2674. case 1:
  2675. bpp = 1;
  2676. break;
  2677. case 3:
  2678. bpp = 24;
  2679. break;
  2680. case 4:
  2681. switch (ci->depth) {
  2682. case 8:
  2683. case 16:
  2684. case 24:
  2685. case 32:
  2686. break;
  2687. default:
  2688. bpp = cprn_device->default_depth;
  2689. break;
  2690. }
  2691. break;
  2692. }
  2693. }
  2694. }
  2695. if (bpp == 0) {
  2696. bpp = ci->depth; /* Use the current setting. */
  2697. }
  2698. if (cprn_device->cmyk < 0) {
  2699. /* Reset procedures because we may have been in another mode. */
  2700. dev_proc(pdev, map_cmyk_color) = gdev_cmyk_map_cmyk_color;
  2701. dev_proc(pdev, map_rgb_color) = NULL;
  2702. dev_proc(pdev, map_color_rgb) = gdev_cmyk_map_color_rgb;
  2703. if (pdev->is_open) gs_closedevice(pdev);
  2704. }
  2705. /* Check for valid bpp values */
  2706. switch ( bpp )
  2707. {
  2708. case 16:
  2709. case 32:
  2710. if (cprn_device->cmyk && ccomps && ccomps != 4) goto bppe;
  2711. break;
  2712. case 24:
  2713. if (!cprn_device->cmyk || ccomps == 0 || ccomps == 4) {
  2714. break;
  2715. } else if (ccomps == 1) {
  2716. goto bppe;
  2717. } else {
  2718. /* 3 components 24 bpp printing for CMYK device. */
  2719. cprn_device->cmyk = -1;
  2720. }
  2721. break;
  2722. case 8:
  2723. if (cprn_device->cmyk) {
  2724. if (ccomps) {
  2725. if (ccomps == 3) {
  2726. cprn_device->cmyk = -1;
  2727. bpp = 3;
  2728. } else if (ccomps != 1 && ccomps != 4) {
  2729. goto bppe;
  2730. }
  2731. }
  2732. if (ccomps != 1) break;
  2733. } else {
  2734. break;
  2735. }
  2736. case 1:
  2737. if (ccomps != 1) goto bppe;
  2738. if (cprn_device->cmyk && bpp != pdev->color_info.depth) {
  2739. dev_proc(pdev, map_cmyk_color) = NULL;
  2740. dev_proc(pdev, map_rgb_color) = gdev_cmyk_map_rgb_color;
  2741. if (pdev->is_open) {
  2742. gs_closedevice(pdev);
  2743. }
  2744. }
  2745. break;
  2746. case 3:
  2747. if (!cprn_device->cmyk) {
  2748. break;
  2749. }
  2750. default:
  2751. bppe: return gs_error_rangecheck;
  2752. }
  2753. if (cprn_device->cmyk == -1) {
  2754. dev_proc(pdev, map_cmyk_color) = NULL;
  2755. dev_proc(pdev, map_rgb_color) = gdev_pcl_map_rgb_color;
  2756. dev_proc(pdev, map_color_rgb) = gdev_pcl_map_color_rgb;
  2757. if (pdev->is_open) {
  2758. gs_closedevice(pdev);
  2759. }
  2760. }
  2761. switch (ccomps) {
  2762. case 0:
  2763. break;
  2764. case 1:
  2765. if (bpp != 1 && bpp != 8) goto cce;
  2766. break;
  2767. case 4:
  2768. if (cprn_device->cmyk) {
  2769. if (bpp >= 8) break;
  2770. }
  2771. case 3:
  2772. if (bpp == 1 || bpp == 3 || bpp == 8 || bpp == 16
  2773. || bpp == 24 || bpp == 32) {
  2774. break;
  2775. }
  2776. cce: default: return gs_error_rangecheck;
  2777. }
  2778. if (cprn_device->cmyk) {
  2779. if (cprn_device->cmyk > 0) {
  2780. ci->num_components = ccomps ? ccomps : (bpp < 8 ? 1 : 4);
  2781. } else {
  2782. ci->num_components = ccomps ? ccomps : (bpp < 8 ? 1 : 3);
  2783. }
  2784. if (bpp != 1 && ci->num_components == 1) { /* We do dithered grays. */
  2785. bpp = bpp < 8 ? 8 : bpp;
  2786. }
  2787. ci->max_color = (1 << (bpp >> 2)) - 1;
  2788. ci->max_gray = (bpp >= 8 ? 255 : 1);
  2789. if (ci->num_components == 1) {
  2790. ci->dither_grays = (bpp >= 8 ? 5 : 2);
  2791. ci->dither_colors = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0);
  2792. } else {
  2793. ci->dither_grays = (bpp > 8 ? 5 : 2);
  2794. ci->dither_colors = (bpp > 8 ? 5 : bpp > 1 ? 2 : 0);
  2795. }
  2796. } else {
  2797. ci->num_components = (bpp == 1 || bpp == 8 ? 1 : 3);
  2798. ci->max_color = (bpp >= 8 ? 255 : bpp > 1 ? 1 : 0);
  2799. ci->max_gray = (bpp >= 8 ? 255 : 1);
  2800. ci->dither_grays = (bpp >= 8 ? 5 : 2);
  2801. ci->dither_colors = (bpp >= 8 ? 5 : bpp > 1 ? 2 : 0);
  2802. }
  2803. ci->depth = ((bpp > 1) && (bpp < 8) ? 8 : bpp);
  2804. return 0;
  2805. }
  2806. /* new_bpp == save_bpp or new_bpp == 0 means don't change bpp.
  2807. ccomps == 0 means don't change number of color comps.
  2808. If new_bpp != 0, it must be the value of the BitsPerPixel element of
  2809. the plist; real_bpp may differ from new_bpp.
  2810. */
  2811. private int
  2812. cdj_put_param_bpp(gx_device *pdev, gs_param_list *plist, int new_bpp,
  2813. int real_bpp, int ccomps)
  2814. {
  2815. if (new_bpp == 0 && ccomps == 0)
  2816. return gdev_prn_put_params(pdev, plist);
  2817. else
  2818. {
  2819. gx_device_color_info save_info;
  2820. int save_bpp;
  2821. int code;
  2822. save_info = pdev->color_info;
  2823. save_bpp = save_info.depth;
  2824. #define save_ccomps save_info.num_components
  2825. if ( save_bpp == 8 && save_ccomps == 3 && !cprn_device->cmyk)
  2826. save_bpp = 3;
  2827. code = cdj_set_bpp(pdev, real_bpp, ccomps);
  2828. if ( code < 0 ) {
  2829. param_signal_error(plist, "BitsPerPixel", code);
  2830. param_signal_error(plist, "ProcessColorModel", code);
  2831. return code;
  2832. }
  2833. pdev->color_info.depth = new_bpp; /* cdj_set_bpp maps 3/6 to 8 */
  2834. code = gdev_prn_put_params(pdev, plist);
  2835. if ( code < 0 )
  2836. { cdj_set_bpp(pdev, save_bpp, save_ccomps);
  2837. return code;
  2838. }
  2839. cdj_set_bpp(pdev, real_bpp, ccomps); /* reset depth if needed */
  2840. if ((cdj->color_info.depth != save_bpp ||
  2841. (ccomps != 0 && ccomps != save_ccomps))
  2842. && pdev->is_open )
  2843. return gs_closedevice(pdev);
  2844. return 0;
  2845. #undef save_ccomps
  2846. }
  2847. }
  2848. /* This returns either the number of pixels in a scan line, or the number
  2849. * of bytes required to store the line, both clipped to the page margins */
  2850. private uint
  2851. gdev_prn_rasterwidth(const gx_device_printer *pdev, int pixelcount)
  2852. {
  2853. ulong raster_width =
  2854. pdev->width - pdev->x_pixels_per_inch * (dev_l_margin(pdev) + dev_r_margin(pdev));
  2855. return (pixelcount ?
  2856. (uint)raster_width :
  2857. (uint)((raster_width * pdev->color_info.depth + 7) >> 3));
  2858. }
  2859. /* Functions for manipulation params strings */
  2860. private const byte*
  2861. paramValueToString(const stringParamDescription* params, int value)
  2862. {
  2863. for (; params->p_name; ++params) {
  2864. if (params->p_value == value) {
  2865. return (const byte *)params->p_name;
  2866. }
  2867. }
  2868. return (const byte*) 0;
  2869. }
  2870. private int
  2871. paramStringValue(const stringParamDescription* params,
  2872. const byte* name, int namelen, int* value)
  2873. {
  2874. for (; params->p_name; ++params) {
  2875. if (strncmp(params->p_name, (char *)name, namelen) == 0 &&
  2876. params->p_name[namelen] == 0) {
  2877. *value = params->p_value;
  2878. return 1;
  2879. }
  2880. }
  2881. return 0;
  2882. }
  2883. private int
  2884. put_param_string(gs_param_list* plist,
  2885. const byte* pname, gs_param_string* pstring,
  2886. const stringParamDescription* params, int *pvalue, int code)
  2887. {
  2888. int ncode;
  2889. if ((ncode = param_read_string(plist, (char *)pname, pstring)) < 0) {
  2890. param_signal_error(plist, (char *)pname, code = ncode);
  2891. } else if (ncode == 1) {
  2892. pstring->data = 0, pstring->size = 0;
  2893. } else {
  2894. int value = 0;
  2895. if (paramStringValue(params, pstring->data, pstring->size,
  2896. &value) == 0) {
  2897. param_signal_error(plist, (char *)pname, code = gs_error_rangecheck);
  2898. } else {
  2899. *pvalue = value;
  2900. }
  2901. }
  2902. return code;
  2903. }
  2904. private int
  2905. get_param_string(gs_param_list* plist,
  2906. const byte* pname, gs_param_string* pstring,
  2907. const stringParamDescription* params, int pvalue, bool persist, int code)
  2908. {
  2909. int ncode;
  2910. pstring->data = paramValueToString(params, pvalue);
  2911. if (pstring->data == (byte*) 0) {
  2912. param_signal_error(plist, (char *)pname, ncode = gs_error_unknownerror);
  2913. } else {
  2914. pstring->size = strlen((char *)pstring->data);
  2915. pstring->persistent = persist;
  2916. }
  2917. if ((ncode = param_write_string(plist, (char *)pname, pstring)) < 0) {
  2918. code = ncode;
  2919. }
  2920. return code;
  2921. }
  2922. /*
  2923. * This taken from gsdparam.c. I hope it will be useable directly some day.
  2924. *
  2925. */
  2926. private int
  2927. cdj_param_check_bytes(gs_param_list *plist, gs_param_name pname, const byte *str,
  2928. uint size, bool defined)
  2929. { int code;
  2930. gs_param_string new_value;
  2931. switch ( code = param_read_string(plist, pname, &new_value) )
  2932. {
  2933. case 0:
  2934. if ( defined && new_value.size == size &&
  2935. !memcmp((const char *)str, (const char *)new_value.data,
  2936. size)
  2937. )
  2938. break;
  2939. code = gs_note_error(gs_error_rangecheck);
  2940. goto e;
  2941. default:
  2942. if ( param_read_null(plist, pname) == 0 )
  2943. return 1;
  2944. e: param_signal_error(plist, pname, code);
  2945. case 1:
  2946. ;
  2947. }
  2948. return code;
  2949. }
  2950. /* This is original code. */
  2951. private int
  2952. cdj_param_check_float(gs_param_list *plist, gs_param_name pname, floatp fval,
  2953. bool defined)
  2954. { int code;
  2955. float new_value;
  2956. switch ( code = param_read_float(plist, pname, &new_value) )
  2957. {
  2958. case 0:
  2959. if ( defined && new_value == fval)
  2960. break;
  2961. code = gs_note_error(gs_error_rangecheck);
  2962. goto e;
  2963. default:
  2964. if ( param_read_null(plist, pname) == 0 )
  2965. return 1;
  2966. e: param_signal_error(plist, pname, code);
  2967. case 1:
  2968. ;
  2969. }
  2970. return code;
  2971. }
  2972. /* The following dithering algorithm has been kindly given to me (YA) by
  2973. * Klaus-Gunther Hess, I just adapted it for use with the code here. */
  2974. /*
  2975. (From KGH:)
  2976. Just about the features of the code:
  2977. - Stored Color-Values are BYTES in the order C-M-Y-K.
  2978. (Indices need to change with gdevcdj.c)
  2979. - There are individual THRESHOLDs and SPOTSIZEs for
  2980. the color-components. The following relation should
  2981. be maintained:
  2982. SPOTSIZE = 2 * THRESHOLD + 1
  2983. (The internal calculation is dedicated for limiting
  2984. ink-density at the 720x720DpI-Resolution of the
  2985. Epson-Printers, without loss of dynamic color-range)
  2986. - In addition to that there are EMIN & EMAX-Values
  2987. for the components. The Values are computed from
  2988. the dithering-algorithm and can be replaced by
  2989. constants, if neither the implementation nor
  2990. THRESHOLD and SPOTSIZE can change.
  2991. - The algorithm is tuned for speed. (K-only, if gray-
  2992. levels are detected, with EMIN/EMAX-clipping of
  2993. stored CMY-Errors. [Notice: cerr, merr, yerr are
  2994. *not* reset to zero! Clearing them would cause
  2995. regular patterns & "Halos" to appear!])
  2996. */
  2997. /*
  2998. * Macros, that represent the undisturbed dithering-algorithm
  2999. *
  3000. * FSerror: compute the desired Value
  3001. * FSdecide: decision based on the value computed by FSerror
  3002. * FSdiffuse: distribute remaining error among pixels
  3003. */
  3004. #define FSerror(Val,Erow,Ecol) (Val + Erow + ((7 * Ecol)>>4))
  3005. #define FSdecide(Error,Threshold,Spotsize,Pixel,Bit) \
  3006. if(Error > Threshold) {\
  3007. Pixel |= Bit;\
  3008. Error -= Spotsize;\
  3009. }
  3010. #define FSdiffuse(Error,Erow,Ecol,Eprev)\
  3011. Eprev += (3 * Error + 8)>>4;\
  3012. Erow = (5 * Error + Ecol + 8)>>4;\
  3013. Ecol = Error;
  3014. /*
  3015. * some aliases for values from the device-structure
  3016. */
  3017. #define DIRECTION direction[0]
  3018. #define CMYK_THRESHOLD(I) threshold[I]
  3019. #define SPOTSIZE(I) spotsize[I]
  3020. #define EMIN(I) emin[I]
  3021. #define EMAX(I) emax[I]
  3022. #define NPIXEL (plane_size * 8)
  3023. #define IDX_C 1
  3024. #define IDX_M 2
  3025. #define IDX_Y 3
  3026. #define IDX_K 0
  3027. #define ODX_C 2
  3028. #define ODX_M 1
  3029. #define ODX_Y 0
  3030. #define ODX_K 3
  3031. private int
  3032. bjc_fscmyk(byte** inplanes, byte* outplanes[4][4], int** errplanes,
  3033. int plane_size, int scan) {
  3034. byte* err = (byte*) errplanes[0];
  3035. /* =========================================================== */
  3036. if(scan < 0) { /* scan < 0 -> initialize private buffer */
  3037. /* =========================================================== */
  3038. int p,i,v;
  3039. int *direction,*threshold,*spotsize,*emin,*emax;
  3040. int *errv,*errc;
  3041. /*
  3042. * allocate the error-buffer
  3043. */
  3044. /*KGHorig
  3045. i = 4 * (5 + 1 + 1 + sd->stc.prt_pixels + 1) * sizeof(errv[0]);
  3046. if((sd->stc.err_size < i) || (NULL == sd->stc.err)) {
  3047. if(NULL != sd->stc.err)
  3048. gs_free(sd->stc.err,sd->stc.err_size,1,"stcm/err");
  3049. sd->stc.err_size = i;
  3050. sd->stc.err = gs_malloc(sd->stc.err_size,1,"stcm/err");
  3051. if(sd->stc.err == NULL) return_error(gs_error_VMerror);
  3052. }
  3053. */
  3054. direction = (int *) err;
  3055. threshold = direction + 4;
  3056. spotsize = threshold + 4;
  3057. emin = spotsize + 4;
  3058. emax = emin + 4;
  3059. errc = emax + 4;
  3060. errv = errc + 2*4;
  3061. /*
  3062. * compute initial values
  3063. */
  3064. DIRECTION = -1;
  3065. for(i = 0; i < 4; ++i) {
  3066. int j;
  3067. float maxv = 1.0;
  3068. /*KGHorig
  3069. if((sd->stc.xfer[i].size < 1) || (sd->stc.xfer[i].data == NULL)) {
  3070. maxv = 1.0;
  3071. } else {
  3072. maxv = 1.0/255.0;
  3073. for(j = 0; j < sd->stc.xfer[i].size; ++j)
  3074. if(maxv < sd->stc.xfer[i].data[j])
  3075. maxv = sd->stc.xfer[i].data[j];
  3076. }
  3077. */
  3078. CMYK_THRESHOLD(i) = 127.0 / maxv + 0.5;
  3079. SPOTSIZE(i) = ((int) CMYK_THRESHOLD(i)<<1)+1;
  3080. j = CMYK_THRESHOLD(i); /* Maximum Error-Value */
  3081. errc[3] = 0;
  3082. FSdiffuse(CMYK_THRESHOLD(i),errv[0],errc[0],errv[-4]);
  3083. FSdiffuse(CMYK_THRESHOLD(i),errv[0],errc[0],errv[-4]);
  3084. EMAX(i) = errv[0];
  3085. errc[0] = 0;
  3086. FSdiffuse((-CMYK_THRESHOLD(i)),errv[0],errc[0],errv[-4]);
  3087. FSdiffuse((-CMYK_THRESHOLD(i)),errv[0],errc[0],errv[-4]);
  3088. EMIN(i) = errv[0];
  3089. }
  3090. #ifdef CDJ_DEBUG_FS
  3091. for(i = 0; i < 4; ++i) errprintf(
  3092. "CMYK_THRESHOLD(%d)=%5d, spotsize(%d)=%5d, emin(%d)=%5d, emax(%d)=%5d\n",
  3093. i,CMYK_THRESHOLD(i),i,SPOTSIZE(i),i,EMIN(i),i,EMAX(i));
  3094. #endif
  3095. for(i = 0; i < 4; ++i) errc[i] = 0;
  3096. for(p = 0; p < NPIXEL; ++p) {
  3097. for(i = 0; i < 4; ++i) {
  3098. /*KHGOrig
  3099. if(sd->stc.flags & STCDFLAG0) v = 0;
  3100. */
  3101. if (0) v = 0; /* Must provide a default for that. */
  3102. else v = (rand() % SPOTSIZE(i)) - CMYK_THRESHOLD(i);
  3103. FSdiffuse(v,errv[i],errc[i],errv[i-4]);
  3104. }
  3105. errv += i;
  3106. }
  3107. /* =========================================================== */
  3108. } else { /* scan >= 0 -> scanline-processing */
  3109. /* =========================================================== */
  3110. int w,p,dir,thedir;
  3111. byte *out[4],pixel[4],bit;
  3112. /*KGHorig
  3113. int *width = outplanes[scan];
  3114. */
  3115. int *direction = (int *) err;
  3116. int *threshold = direction + 4;
  3117. int *spotsize = threshold + 4;
  3118. int *emin = spotsize + 4;
  3119. int *emax = emin + 4;
  3120. int *errc = emax + 4;
  3121. int *errv = errc + 2*4;
  3122. int kerr,cerr,merr,yerr;
  3123. byte* in;
  3124. /*KGHorig
  3125. if(sd->stc.flags & STCDFLAG1) {
  3126. */
  3127. if (0) { /* Eventually will provide a flag for this. */
  3128. cerr = merr = yerr = kerr = 0;
  3129. } else {
  3130. cerr = errc[0];
  3131. merr = errc[1];
  3132. yerr = errc[2];
  3133. kerr = errc[3];
  3134. }
  3135. out[0] = outplanes[scan + 2][ODX_C];
  3136. out[1] = outplanes[scan + 2][ODX_M];
  3137. out[2] = outplanes[scan + 2][ODX_Y];
  3138. out[3] = outplanes[scan + 2][ODX_K];
  3139. pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0;
  3140. if(DIRECTION < 0) { /* scan == 0, run backward */
  3141. w = NPIXEL;
  3142. in = inplanes[2] + 4 * (NPIXEL - 1);
  3143. errv += (w-1)<<2;
  3144. dir = -4;
  3145. /*KGHorig
  3146. if(w > 8) for(p = 0; p < 4; ++p) out[p] += (w-1)>>3;
  3147. */
  3148. thedir = -1;
  3149. for (p = 0; p < 4; ++p) {
  3150. out[p] += plane_size - 1;
  3151. }
  3152. } else { /* run forward */
  3153. w = 1;
  3154. in = inplanes[3] - 4 * NPIXEL;
  3155. dir = 4;
  3156. thedir = 1;
  3157. for (p = 0; p < 4; ++p) {
  3158. out[p] -= plane_size;
  3159. }
  3160. } /* run backward/forward */
  3161. /*KGHorig
  3162. if(0 == (sd->stc.flags & STCDFLAG1)) DIRECTION = -DIRECTION;
  3163. */
  3164. if (1) DIRECTION = -DIRECTION; /* Scan in other direction. */
  3165. bit = 0x80>>((w-1) & 7);
  3166. w = (w+7)>>3;
  3167. for(p = NPIXEL; p; --p) { /* loop over pixels */
  3168. int cmy = in[IDX_C] | in[IDX_M] | in[IDX_Y];
  3169. int kv = FSerror(in[IDX_K],errv[3],kerr);
  3170. int cv;
  3171. FSdecide(kv,CMYK_THRESHOLD(3),SPOTSIZE(3),pixel[3],bit);
  3172. if(cmy) {
  3173. if(pixel[3] & bit) { /* black known to fire */
  3174. FSdiffuse(kv,errv[3],kerr,errv[3-dir]);
  3175. cv = FSerror(in[IDX_C],errv[0],cerr);
  3176. cv -= SPOTSIZE(0);
  3177. if ((cv+CMYK_THRESHOLD(0)) < 0) cv = -CMYK_THRESHOLD(0);
  3178. FSdiffuse(cv,errv[0],cerr,errv[0-dir]);
  3179. cv = FSerror(in[IDX_M],errv[1],merr);
  3180. cv -= SPOTSIZE(1);
  3181. if ((cv+CMYK_THRESHOLD(1)) < 0) cv = -CMYK_THRESHOLD(1);
  3182. FSdiffuse(cv,errv[1],merr,errv[1-dir]);
  3183. cv = FSerror(in[IDX_Y],errv[2],yerr);
  3184. cv -= SPOTSIZE(2);
  3185. if ((cv+CMYK_THRESHOLD(2)) < 0) cv = -CMYK_THRESHOLD(2);
  3186. FSdiffuse(cv,errv[2],yerr,errv[2-dir]);
  3187. } else {
  3188. cv = FSerror(in[IDX_C],errv[0],cerr);
  3189. FSdecide(cv,CMYK_THRESHOLD(0),SPOTSIZE(0),pixel[0],bit);
  3190. FSdiffuse(cv,errv[0],cerr,errv[0-dir]);
  3191. cv = FSerror(in[IDX_M],errv[1],merr);
  3192. FSdecide(cv,CMYK_THRESHOLD(1),SPOTSIZE(1),pixel[1],bit);
  3193. FSdiffuse(cv,errv[1],merr,errv[1-dir]);
  3194. cv = FSerror(in[IDX_Y],errv[2],yerr);
  3195. FSdecide(cv,CMYK_THRESHOLD(2),SPOTSIZE(2),pixel[2],bit);
  3196. FSdiffuse(cv,errv[2],yerr,errv[2-dir]);
  3197. if(pixel[0] & pixel[1] & pixel[2] & bit) {
  3198. pixel[0] &= ~bit;
  3199. pixel[1] &= ~bit;
  3200. pixel[2] &= ~bit;
  3201. pixel[3] |= bit;
  3202. kv -= SPOTSIZE(3);
  3203. if ((kv+CMYK_THRESHOLD(3)) < 0) kv = -CMYK_THRESHOLD(0);
  3204. FSdiffuse(kv,errv[3],kerr,errv[3-dir]);
  3205. }
  3206. }
  3207. } else {
  3208. FSdiffuse(kv,errv[3],kerr,errv[3-dir]);
  3209. if( errv[0] > EMAX(0)) errv[0] = EMAX(0);
  3210. else if(errv[0] < EMIN(0)) errv[0] = EMIN(0);
  3211. if( errv[1] > EMAX(1)) errv[1] = EMAX(1);
  3212. else if(errv[1] < EMIN(1)) errv[1] = EMIN(1);
  3213. if( errv[2] > EMAX(2)) errv[2] = EMAX(2);
  3214. else if(errv[2] < EMIN(2)) errv[2] = EMIN(2);
  3215. }
  3216. /*
  3217. * Adjust indices
  3218. */
  3219. bit = dir > 0 ? (bit>>1) : (bit<<1);
  3220. if(bit == 0) {
  3221. /*KGHorig
  3222. if(((*out[0] = pixel[0]) != 0) && (width[0] < w)) width[0] = w;
  3223. if(((*out[1] = pixel[1]) != 0) && (width[1] < w)) width[1] = w;
  3224. if(((*out[2] = pixel[2]) != 0) && (width[2] < w)) width[2] = w;
  3225. if(((*out[3] = pixel[3]) != 0) && (width[3] < w)) width[3] = w;
  3226. */
  3227. *out[0] = pixel[0];
  3228. *out[1] = pixel[1];
  3229. *out[2] = pixel[2];
  3230. *out[3] = pixel[3];
  3231. out[0] += thedir; out[1] += thedir;
  3232. out[2] += thedir; out[3] += thedir;
  3233. pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0;
  3234. if(dir > 0) bit = 0x80;
  3235. else bit = 0x01;
  3236. w += dir>>2;
  3237. }
  3238. in += dir;
  3239. errv += dir;
  3240. } /* loop over pixels */
  3241. /*KGHorig
  3242. if(0 == (sd->stc.flags & STCDFLAG1)) {
  3243. */
  3244. if (1) {
  3245. cerr = errc[0] = cerr;
  3246. merr = errc[1] = merr;
  3247. yerr = errc[2] = yerr;
  3248. kerr = errc[3] = kerr;
  3249. }
  3250. /* =========================================================== */
  3251. } /* initialization or scanline-Processing */
  3252. /* =========================================================== */
  3253. return 0;
  3254. }