_imaging.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989
  1. /*
  2. * The Python Imaging Library.
  3. *
  4. * the imaging library bindings
  5. *
  6. * history:
  7. * 1995-09-24 fl Created
  8. * 1996-03-24 fl Ready for first public release (release 0.0)
  9. * 1996-03-25 fl Added fromstring (for Jack's "img" library)
  10. * 1996-03-28 fl Added channel operations
  11. * 1996-03-31 fl Added point operation
  12. * 1996-04-08 fl Added new/new_block/new_array factories
  13. * 1996-04-13 fl Added decoders
  14. * 1996-05-04 fl Added palette hack
  15. * 1996-05-12 fl Compile cleanly as C++
  16. * 1996-05-19 fl Added matrix conversions, gradient fills
  17. * 1996-05-27 fl Added display_mode
  18. * 1996-07-22 fl Added getbbox, offset
  19. * 1996-07-23 fl Added sequence semantics
  20. * 1996-08-13 fl Added logical operators, point mode
  21. * 1996-08-16 fl Modified paste interface
  22. * 1996-09-06 fl Added putdata methods, use abstract interface
  23. * 1996-11-01 fl Added xbm encoder
  24. * 1996-11-04 fl Added experimental path stuff, draw_lines, etc
  25. * 1996-12-10 fl Added zip decoder, crc32 interface
  26. * 1996-12-14 fl Added modulo arithmetics
  27. * 1996-12-29 fl Added zip encoder
  28. * 1997-01-03 fl Added fli and msp decoders
  29. * 1997-01-04 fl Added experimental sun_rle and tga_rle decoders
  30. * 1997-01-05 fl Added gif encoder, getpalette hack
  31. * 1997-02-23 fl Added histogram mask
  32. * 1997-05-12 fl Minor tweaks to match the IFUNC95 interface
  33. * 1997-05-21 fl Added noise generator, spread effect
  34. * 1997-06-05 fl Added mandelbrot generator
  35. * 1997-08-02 fl Modified putpalette to coerce image mode if necessary
  36. * 1998-01-11 fl Added INT32 support
  37. * 1998-01-22 fl Fixed draw_points to draw the last point too
  38. * 1998-06-28 fl Added getpixel, getink, draw_ink
  39. * 1998-07-12 fl Added getextrema
  40. * 1998-07-17 fl Added point conversion to arbitrary formats
  41. * 1998-09-21 fl Added support for resampling filters
  42. * 1998-09-22 fl Added support for quad transform
  43. * 1998-12-29 fl Added support for arcs, chords, and pieslices
  44. * 1999-01-10 fl Added some experimental arrow graphics stuff
  45. * 1999-02-06 fl Added draw_bitmap, font acceleration stuff
  46. * 2001-04-17 fl Fixed some egcs compiler nits
  47. * 2001-09-17 fl Added screen grab primitives (win32)
  48. * 2002-03-09 fl Added stretch primitive
  49. * 2002-03-10 fl Fixed filter handling in rotate
  50. * 2002-06-06 fl Added I, F, and RGB support to putdata
  51. * 2002-06-08 fl Added rankfilter
  52. * 2002-06-09 fl Added support for user-defined filter kernels
  53. * 2002-11-19 fl Added clipboard grab primitives (win32)
  54. * 2002-12-11 fl Added draw context
  55. * 2003-04-26 fl Tweaks for Python 2.3 beta 1
  56. * 2003-05-21 fl Added createwindow primitive (win32)
  57. * 2003-09-13 fl Added thread section hooks
  58. * 2003-09-15 fl Added expand helper
  59. * 2003-09-26 fl Added experimental LA support
  60. * 2004-02-21 fl Handle zero-size images in quantize
  61. * 2004-06-05 fl Added ptr attribute (used to access Imaging objects)
  62. * 2004-06-05 fl Don't crash when fetching pixels from zero-wide images
  63. * 2004-09-17 fl Added getcolors
  64. * 2004-10-04 fl Added modefilter
  65. * 2005-10-02 fl Added access proxy
  66. * 2006-06-18 fl Always draw last point in polyline
  67. *
  68. * Copyright (c) 1997-2006 by Secret Labs AB
  69. * Copyright (c) 1995-2006 by Fredrik Lundh
  70. *
  71. * See the README file for information on usage and redistribution.
  72. */
  73. #define PY_SSIZE_T_CLEAN
  74. #include "Python.h"
  75. #ifdef HAVE_LIBJPEG
  76. #include "jconfig.h"
  77. #endif
  78. #ifdef HAVE_LIBZ
  79. #include "zlib.h"
  80. #endif
  81. #include "Imaging.h"
  82. #include "py3.h"
  83. #define _USE_MATH_DEFINES
  84. #include <math.h>
  85. /* Configuration stuff. Feel free to undef things you don't need. */
  86. #define WITH_IMAGECHOPS /* ImageChops support */
  87. #define WITH_IMAGEDRAW /* ImageDraw support */
  88. #define WITH_MAPPING /* use memory mapping to read some file formats */
  89. #define WITH_IMAGEPATH /* ImagePath stuff */
  90. #define WITH_ARROW /* arrow graphics stuff (experimental) */
  91. #define WITH_EFFECTS /* special effects */
  92. #define WITH_QUANTIZE /* quantization support */
  93. #define WITH_RANKFILTER /* rank filter */
  94. #define WITH_MODEFILTER /* mode filter */
  95. #define WITH_THREADING /* "friendly" threading support */
  96. #define WITH_UNSHARPMASK /* Kevin Cazabon's unsharpmask module */
  97. #undef VERBOSE
  98. #define B16(p, i) ((((int)p[(i)]) << 8) + p[(i)+1])
  99. #define L16(p, i) ((((int)p[(i)+1]) << 8) + p[(i)])
  100. #define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
  101. /* -------------------------------------------------------------------- */
  102. /* OBJECT ADMINISTRATION */
  103. /* -------------------------------------------------------------------- */
  104. typedef struct {
  105. PyObject_HEAD
  106. Imaging image;
  107. ImagingAccess access;
  108. } ImagingObject;
  109. static PyTypeObject Imaging_Type;
  110. #ifdef WITH_IMAGEDRAW
  111. typedef struct
  112. {
  113. /* to write a character, cut out sxy from glyph data, place
  114. at current position plus dxy, and advance by (dx, dy) */
  115. int dx, dy;
  116. int dx0, dy0, dx1, dy1;
  117. int sx0, sy0, sx1, sy1;
  118. } Glyph;
  119. typedef struct {
  120. PyObject_HEAD
  121. ImagingObject* ref;
  122. Imaging bitmap;
  123. int ysize;
  124. int baseline;
  125. Glyph glyphs[256];
  126. } ImagingFontObject;
  127. static PyTypeObject ImagingFont_Type;
  128. typedef struct {
  129. PyObject_HEAD
  130. ImagingObject* image;
  131. UINT8 ink[4];
  132. int blend;
  133. } ImagingDrawObject;
  134. static PyTypeObject ImagingDraw_Type;
  135. #endif
  136. typedef struct {
  137. PyObject_HEAD
  138. ImagingObject* image;
  139. int readonly;
  140. } PixelAccessObject;
  141. static PyTypeObject PixelAccess_Type;
  142. PyObject*
  143. PyImagingNew(Imaging imOut)
  144. {
  145. ImagingObject* imagep;
  146. if (!imOut)
  147. return NULL;
  148. imagep = PyObject_New(ImagingObject, &Imaging_Type);
  149. if (imagep == NULL) {
  150. ImagingDelete(imOut);
  151. return NULL;
  152. }
  153. #ifdef VERBOSE
  154. printf("imaging %p allocated\n", imagep);
  155. #endif
  156. imagep->image = imOut;
  157. imagep->access = ImagingAccessNew(imOut);
  158. return (PyObject*) imagep;
  159. }
  160. static void
  161. _dealloc(ImagingObject* imagep)
  162. {
  163. #ifdef VERBOSE
  164. printf("imaging %p deleted\n", imagep);
  165. #endif
  166. if (imagep->access)
  167. ImagingAccessDelete(imagep->image, imagep->access);
  168. ImagingDelete(imagep->image);
  169. PyObject_Del(imagep);
  170. }
  171. #define PyImaging_Check(op) (Py_TYPE(op) == &Imaging_Type)
  172. Imaging PyImaging_AsImaging(PyObject *op)
  173. {
  174. if (!PyImaging_Check(op)) {
  175. PyErr_BadInternalCall();
  176. return NULL;
  177. }
  178. return ((ImagingObject *)op)->image;
  179. }
  180. /* -------------------------------------------------------------------- */
  181. /* THREAD HANDLING */
  182. /* -------------------------------------------------------------------- */
  183. void ImagingSectionEnter(ImagingSectionCookie* cookie)
  184. {
  185. #ifdef WITH_THREADING
  186. *cookie = (PyThreadState *) PyEval_SaveThread();
  187. #endif
  188. }
  189. void ImagingSectionLeave(ImagingSectionCookie* cookie)
  190. {
  191. #ifdef WITH_THREADING
  192. PyEval_RestoreThread((PyThreadState*) *cookie);
  193. #endif
  194. }
  195. /* -------------------------------------------------------------------- */
  196. /* BUFFER HANDLING */
  197. /* -------------------------------------------------------------------- */
  198. /* Python compatibility API */
  199. int PyImaging_CheckBuffer(PyObject* buffer)
  200. {
  201. #if PY_VERSION_HEX >= 0x03000000
  202. return PyObject_CheckBuffer(buffer);
  203. #else
  204. return PyObject_CheckBuffer(buffer) || PyObject_CheckReadBuffer(buffer);
  205. #endif
  206. }
  207. int PyImaging_GetBuffer(PyObject* buffer, Py_buffer *view)
  208. {
  209. /* must call check_buffer first! */
  210. #if PY_VERSION_HEX >= 0x03000000
  211. return PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
  212. #else
  213. /* Use new buffer protocol if available
  214. (mmap doesn't support this in 2.7, go figure) */
  215. if (PyObject_CheckBuffer(buffer)) {
  216. int success = PyObject_GetBuffer(buffer, view, PyBUF_SIMPLE);
  217. if (!success) { return success; }
  218. PyErr_Clear();
  219. }
  220. /* Pretend we support the new protocol; PyBuffer_Release happily ignores
  221. calling bf_releasebuffer on objects that don't support it */
  222. view->buf = NULL;
  223. view->len = 0;
  224. view->readonly = 1;
  225. view->format = NULL;
  226. view->ndim = 0;
  227. view->shape = NULL;
  228. view->strides = NULL;
  229. view->suboffsets = NULL;
  230. view->itemsize = 0;
  231. view->internal = NULL;
  232. Py_INCREF(buffer);
  233. view->obj = buffer;
  234. return PyObject_AsReadBuffer(buffer, (void *) &view->buf, &view->len);
  235. #endif
  236. }
  237. /* -------------------------------------------------------------------- */
  238. /* EXCEPTION REROUTING */
  239. /* -------------------------------------------------------------------- */
  240. /* error messages */
  241. static const char* must_be_sequence = "argument must be a sequence";
  242. static const char* must_be_two_coordinates =
  243. "coordinate list must contain exactly 2 coordinates";
  244. static const char* wrong_mode = "unrecognized image mode";
  245. static const char* wrong_raw_mode = "unrecognized raw mode";
  246. static const char* outside_image = "image index out of range";
  247. static const char* outside_palette = "palette index out of range";
  248. static const char* wrong_palette_size = "invalid palette size";
  249. static const char* no_palette = "image has no palette";
  250. static const char* readonly = "image is readonly";
  251. /* static const char* no_content = "image has no content"; */
  252. void *
  253. ImagingError_IOError(void)
  254. {
  255. PyErr_SetString(PyExc_IOError, "error when accessing file");
  256. return NULL;
  257. }
  258. void *
  259. ImagingError_MemoryError(void)
  260. {
  261. return PyErr_NoMemory();
  262. }
  263. void *
  264. ImagingError_Mismatch(void)
  265. {
  266. PyErr_SetString(PyExc_ValueError, "images do not match");
  267. return NULL;
  268. }
  269. void *
  270. ImagingError_ModeError(void)
  271. {
  272. PyErr_SetString(PyExc_ValueError, "image has wrong mode");
  273. return NULL;
  274. }
  275. void *
  276. ImagingError_ValueError(const char *message)
  277. {
  278. PyErr_SetString(
  279. PyExc_ValueError,
  280. (message) ? (char*) message : "unrecognized argument value"
  281. );
  282. return NULL;
  283. }
  284. void
  285. ImagingError_Clear(void)
  286. {
  287. PyErr_Clear();
  288. }
  289. /* -------------------------------------------------------------------- */
  290. /* HELPERS */
  291. /* -------------------------------------------------------------------- */
  292. static int
  293. getbands(const char* mode)
  294. {
  295. Imaging im;
  296. int bands;
  297. /* FIXME: add primitive to libImaging to avoid extra allocation */
  298. im = ImagingNew(mode, 0, 0);
  299. if (!im)
  300. return -1;
  301. bands = im->bands;
  302. ImagingDelete(im);
  303. return bands;
  304. }
  305. #define TYPE_UINT8 (0x100|sizeof(UINT8))
  306. #define TYPE_INT32 (0x200|sizeof(INT32))
  307. #define TYPE_FLOAT16 (0x500|sizeof(FLOAT16))
  308. #define TYPE_FLOAT32 (0x300|sizeof(FLOAT32))
  309. #define TYPE_DOUBLE (0x400|sizeof(double))
  310. static void*
  311. getlist(PyObject* arg, Py_ssize_t* length, const char* wrong_length, int type)
  312. {
  313. /* - allocates and returns a c array of the items in the
  314. python sequence arg.
  315. - the size of the returned array is in length
  316. - all of the arg items must be numeric items of the type
  317. specified in type
  318. - sequence length is checked against the length parameter IF
  319. an error parameter is passed in wrong_length
  320. - caller is responsible for freeing the memory
  321. */
  322. Py_ssize_t i, n;
  323. int itemp;
  324. double dtemp;
  325. FLOAT32 ftemp;
  326. UINT8* list;
  327. PyObject* seq;
  328. PyObject* op;
  329. if ( ! PySequence_Check(arg)) {
  330. PyErr_SetString(PyExc_TypeError, must_be_sequence);
  331. return NULL;
  332. }
  333. n = PySequence_Size(arg);
  334. if (length && wrong_length && n != *length) {
  335. PyErr_SetString(PyExc_ValueError, wrong_length);
  336. return NULL;
  337. }
  338. /* malloc check ok, type & ff is just a sizeof(something)
  339. calloc checks for overflow */
  340. list = calloc(n, type & 0xff);
  341. if ( ! list)
  342. return PyErr_NoMemory();
  343. seq = PySequence_Fast(arg, must_be_sequence);
  344. if ( ! seq) {
  345. free(list);
  346. return NULL;
  347. }
  348. for (i = 0; i < n; i++) {
  349. op = PySequence_Fast_GET_ITEM(seq, i);
  350. // DRY, branch prediction is going to work _really_ well
  351. // on this switch. And 3 fewer loops to copy/paste.
  352. switch (type) {
  353. case TYPE_UINT8:
  354. itemp = PyInt_AsLong(op);
  355. list[i] = CLIP8(itemp);
  356. break;
  357. case TYPE_INT32:
  358. itemp = PyInt_AsLong(op);
  359. memcpy(list + i * sizeof(INT32), &itemp, sizeof(itemp));
  360. break;
  361. case TYPE_FLOAT32:
  362. ftemp = (FLOAT32)PyFloat_AsDouble(op);
  363. memcpy(list + i * sizeof(ftemp), &ftemp, sizeof(ftemp));
  364. break;
  365. case TYPE_DOUBLE:
  366. dtemp = PyFloat_AsDouble(op);
  367. memcpy(list + i * sizeof(dtemp), &dtemp, sizeof(dtemp));
  368. break;
  369. }
  370. }
  371. Py_DECREF(seq);
  372. if (PyErr_Occurred()) {
  373. free(list);
  374. return NULL;
  375. }
  376. if (length)
  377. *length = n;
  378. return list;
  379. }
  380. FLOAT32
  381. float16tofloat32(const FLOAT16 in) {
  382. UINT32 t1;
  383. UINT32 t2;
  384. UINT32 t3;
  385. FLOAT32 out[1] = {0};
  386. t1 = in & 0x7fff; // Non-sign bits
  387. t2 = in & 0x8000; // Sign bit
  388. t3 = in & 0x7c00; // Exponent
  389. t1 <<= 13; // Align mantissa on MSB
  390. t2 <<= 16; // Shift sign bit into position
  391. t1 += 0x38000000; // Adjust bias
  392. t1 = (t3 == 0 ? 0 : t1); // Denormals-as-zero
  393. t1 |= t2; // Re-insert sign bit
  394. memcpy(out, &t1, 4);
  395. return out[0];
  396. }
  397. static inline PyObject*
  398. getpixel(Imaging im, ImagingAccess access, int x, int y)
  399. {
  400. union {
  401. UINT8 b[4];
  402. UINT16 h;
  403. INT32 i;
  404. FLOAT32 f;
  405. } pixel;
  406. if (x < 0) {
  407. x = im->xsize + x;
  408. }
  409. if (y < 0) {
  410. y = im->ysize + y;
  411. }
  412. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
  413. PyErr_SetString(PyExc_IndexError, outside_image);
  414. return NULL;
  415. }
  416. access->get_pixel(im, x, y, &pixel);
  417. switch (im->type) {
  418. case IMAGING_TYPE_UINT8:
  419. switch (im->bands) {
  420. case 1:
  421. return PyInt_FromLong(pixel.b[0]);
  422. case 2:
  423. return Py_BuildValue("BB", pixel.b[0], pixel.b[1]);
  424. case 3:
  425. return Py_BuildValue("BBB", pixel.b[0], pixel.b[1], pixel.b[2]);
  426. case 4:
  427. return Py_BuildValue("BBBB", pixel.b[0], pixel.b[1], pixel.b[2], pixel.b[3]);
  428. }
  429. break;
  430. case IMAGING_TYPE_INT32:
  431. return PyInt_FromLong(pixel.i);
  432. case IMAGING_TYPE_FLOAT32:
  433. return PyFloat_FromDouble(pixel.f);
  434. case IMAGING_TYPE_SPECIAL:
  435. if (strncmp(im->mode, "I;16", 4) == 0)
  436. return PyInt_FromLong(pixel.h);
  437. break;
  438. }
  439. /* unknown type */
  440. Py_INCREF(Py_None);
  441. return Py_None;
  442. }
  443. static char*
  444. getink(PyObject* color, Imaging im, char* ink)
  445. {
  446. int g=0, b=0, a=0;
  447. double f=0;
  448. /* Windows 64 bit longs are 32 bits, and 0xFFFFFFFF (white) is a
  449. python long (not int) that raises an overflow error when trying
  450. to return it into a 32 bit C long
  451. */
  452. PY_LONG_LONG r = 0;
  453. FLOAT32 ftmp;
  454. INT32 itmp;
  455. /* fill ink buffer (four bytes) with something that can
  456. be cast to either UINT8 or INT32 */
  457. int rIsInt = 0;
  458. if (im->type == IMAGING_TYPE_UINT8 ||
  459. im->type == IMAGING_TYPE_INT32 ||
  460. im->type == IMAGING_TYPE_SPECIAL) {
  461. #if PY_VERSION_HEX >= 0x03000000
  462. if (PyLong_Check(color)) {
  463. r = PyLong_AsLongLong(color);
  464. #else
  465. if (PyInt_Check(color) || PyLong_Check(color)) {
  466. if (PyInt_Check(color))
  467. r = PyInt_AS_LONG(color);
  468. else
  469. r = PyLong_AsLongLong(color);
  470. #endif
  471. rIsInt = 1;
  472. }
  473. if (r == -1 && PyErr_Occurred()) {
  474. rIsInt = 0;
  475. }
  476. }
  477. switch (im->type) {
  478. case IMAGING_TYPE_UINT8:
  479. /* unsigned integer */
  480. if (im->bands == 1) {
  481. /* unsigned integer, single layer */
  482. if (rIsInt != 1) {
  483. if (!PyArg_ParseTuple(color, "L", &r)) {
  484. return NULL;
  485. }
  486. }
  487. ink[0] = (char) CLIP8(r);
  488. ink[1] = ink[2] = ink[3] = 0;
  489. } else {
  490. a = 255;
  491. if (rIsInt) {
  492. /* compatibility: ABGR */
  493. a = (UINT8) (r >> 24);
  494. b = (UINT8) (r >> 16);
  495. g = (UINT8) (r >> 8);
  496. r = (UINT8) r;
  497. } else {
  498. if (im->bands == 2) {
  499. if (!PyArg_ParseTuple(color, "L|i", &r, &a))
  500. return NULL;
  501. g = b = r;
  502. } else {
  503. if (!PyArg_ParseTuple(color, "Lii|i", &r, &g, &b, &a))
  504. return NULL;
  505. }
  506. }
  507. ink[0] = (char) CLIP8(r);
  508. ink[1] = (char) CLIP8(g);
  509. ink[2] = (char) CLIP8(b);
  510. ink[3] = (char) CLIP8(a);
  511. }
  512. return ink;
  513. case IMAGING_TYPE_INT32:
  514. /* signed integer */
  515. if (rIsInt != 1)
  516. return NULL;
  517. itmp = r;
  518. memcpy(ink, &itmp, sizeof(itmp));
  519. return ink;
  520. case IMAGING_TYPE_FLOAT32:
  521. /* floating point */
  522. f = PyFloat_AsDouble(color);
  523. if (f == -1.0 && PyErr_Occurred())
  524. return NULL;
  525. ftmp = f;
  526. memcpy(ink, &ftmp, sizeof(ftmp));
  527. return ink;
  528. case IMAGING_TYPE_SPECIAL:
  529. if (strncmp(im->mode, "I;16", 4) == 0) {
  530. if (rIsInt != 1)
  531. return NULL;
  532. ink[0] = (UINT8) r;
  533. ink[1] = (UINT8) (r >> 8);
  534. ink[2] = ink[3] = 0;
  535. return ink;
  536. }
  537. }
  538. PyErr_SetString(PyExc_ValueError, wrong_mode);
  539. return NULL;
  540. }
  541. /* -------------------------------------------------------------------- */
  542. /* FACTORIES */
  543. /* -------------------------------------------------------------------- */
  544. static PyObject*
  545. _fill(PyObject* self, PyObject* args)
  546. {
  547. char* mode;
  548. int xsize, ysize;
  549. PyObject* color;
  550. char buffer[4];
  551. Imaging im;
  552. xsize = ysize = 256;
  553. color = NULL;
  554. if (!PyArg_ParseTuple(args, "s|(ii)O", &mode, &xsize, &ysize, &color))
  555. return NULL;
  556. im = ImagingNewDirty(mode, xsize, ysize);
  557. if (!im)
  558. return NULL;
  559. buffer[0] = buffer[1] = buffer[2] = buffer[3] = 0;
  560. if (color) {
  561. if (!getink(color, im, buffer)) {
  562. ImagingDelete(im);
  563. return NULL;
  564. }
  565. }
  566. (void) ImagingFill(im, buffer);
  567. return PyImagingNew(im);
  568. }
  569. static PyObject*
  570. _new(PyObject* self, PyObject* args)
  571. {
  572. char* mode;
  573. int xsize, ysize;
  574. if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize))
  575. return NULL;
  576. return PyImagingNew(ImagingNew(mode, xsize, ysize));
  577. }
  578. static PyObject*
  579. _new_block(PyObject* self, PyObject* args)
  580. {
  581. char* mode;
  582. int xsize, ysize;
  583. if (!PyArg_ParseTuple(args, "s(ii)", &mode, &xsize, &ysize))
  584. return NULL;
  585. return PyImagingNew(ImagingNewBlock(mode, xsize, ysize));
  586. }
  587. static PyObject*
  588. _linear_gradient(PyObject* self, PyObject* args)
  589. {
  590. char* mode;
  591. if (!PyArg_ParseTuple(args, "s", &mode))
  592. return NULL;
  593. return PyImagingNew(ImagingFillLinearGradient(mode));
  594. }
  595. static PyObject*
  596. _radial_gradient(PyObject* self, PyObject* args)
  597. {
  598. char* mode;
  599. if (!PyArg_ParseTuple(args, "s", &mode))
  600. return NULL;
  601. return PyImagingNew(ImagingFillRadialGradient(mode));
  602. }
  603. static PyObject*
  604. _alpha_composite(ImagingObject* self, PyObject* args)
  605. {
  606. ImagingObject* imagep1;
  607. ImagingObject* imagep2;
  608. if (!PyArg_ParseTuple(args, "O!O!",
  609. &Imaging_Type, &imagep1,
  610. &Imaging_Type, &imagep2))
  611. return NULL;
  612. return PyImagingNew(ImagingAlphaComposite(imagep1->image, imagep2->image));
  613. }
  614. static PyObject*
  615. _blend(ImagingObject* self, PyObject* args)
  616. {
  617. ImagingObject* imagep1;
  618. ImagingObject* imagep2;
  619. double alpha;
  620. alpha = 0.5;
  621. if (!PyArg_ParseTuple(args, "O!O!|d",
  622. &Imaging_Type, &imagep1,
  623. &Imaging_Type, &imagep2,
  624. &alpha))
  625. return NULL;
  626. return PyImagingNew(ImagingBlend(imagep1->image, imagep2->image,
  627. (float) alpha));
  628. }
  629. /* -------------------------------------------------------------------- */
  630. /* METHODS */
  631. /* -------------------------------------------------------------------- */
  632. static INT16*
  633. _prepare_lut_table(PyObject* table, Py_ssize_t table_size)
  634. {
  635. int i;
  636. Py_buffer buffer_info;
  637. INT32 data_type = TYPE_FLOAT32;
  638. float item = 0;
  639. void* table_data = NULL;
  640. int free_table_data = 0;
  641. INT16* prepared;
  642. /* NOTE: This value should be the same as in ColorLUT.c */
  643. #define PRECISION_BITS (16 - 8 - 2)
  644. const char* wrong_size = ("The table should have table_channels * "
  645. "size1D * size2D * size3D float items.");
  646. if (PyObject_CheckBuffer(table)) {
  647. if ( ! PyObject_GetBuffer(table, &buffer_info,
  648. PyBUF_CONTIG_RO | PyBUF_FORMAT)) {
  649. if (buffer_info.ndim == 1 && buffer_info.shape[0] == table_size) {
  650. if (strlen(buffer_info.format) == 1) {
  651. switch (buffer_info.format[0]) {
  652. case 'e':
  653. data_type = TYPE_FLOAT16;
  654. table_data = buffer_info.buf;
  655. break;
  656. case 'f':
  657. data_type = TYPE_FLOAT32;
  658. table_data = buffer_info.buf;
  659. break;
  660. case 'd':
  661. data_type = TYPE_DOUBLE;
  662. table_data = buffer_info.buf;
  663. break;
  664. }
  665. }
  666. }
  667. PyBuffer_Release(&buffer_info);
  668. }
  669. }
  670. if ( ! table_data) {
  671. free_table_data = 1;
  672. table_data = getlist(table, &table_size, wrong_size, TYPE_FLOAT32);
  673. if ( ! table_data) {
  674. return NULL;
  675. }
  676. }
  677. /* malloc check ok, max is 2 * 4 * 65**3 = 2197000 */
  678. prepared = (INT16*) malloc(sizeof(INT16) * table_size);
  679. if ( ! prepared) {
  680. if (free_table_data)
  681. free(table_data);
  682. return (INT16*) PyErr_NoMemory();
  683. }
  684. for (i = 0; i < table_size; i++) {
  685. FLOAT16 htmp;
  686. double dtmp;
  687. switch (data_type) {
  688. case TYPE_FLOAT16:
  689. memcpy(&htmp, ((char*) table_data) + i * sizeof(htmp), sizeof(htmp));
  690. item = float16tofloat32(htmp);
  691. break;
  692. case TYPE_FLOAT32:
  693. memcpy(&item, ((char*) table_data) + i * sizeof(FLOAT32), sizeof(FLOAT32));
  694. break;
  695. case TYPE_DOUBLE:
  696. memcpy(&dtmp, ((char*) table_data) + i * sizeof(dtmp), sizeof(dtmp));
  697. item = (FLOAT32) dtmp;
  698. break;
  699. }
  700. /* Max value for INT16 */
  701. if (item >= (0x7fff - 0.5) / (255 << PRECISION_BITS)) {
  702. prepared[i] = 0x7fff;
  703. continue;
  704. }
  705. /* Min value for INT16 */
  706. if (item <= (-0x8000 + 0.5) / (255 << PRECISION_BITS)) {
  707. prepared[i] = -0x8000;
  708. continue;
  709. }
  710. if (item < 0) {
  711. prepared[i] = item * (255 << PRECISION_BITS) - 0.5;
  712. } else {
  713. prepared[i] = item * (255 << PRECISION_BITS) + 0.5;
  714. }
  715. }
  716. #undef PRECISION_BITS
  717. if (free_table_data) {
  718. free(table_data);
  719. }
  720. return prepared;
  721. }
  722. static PyObject*
  723. _color_lut_3d(ImagingObject* self, PyObject* args)
  724. {
  725. char* mode;
  726. int filter;
  727. int table_channels;
  728. int size1D, size2D, size3D;
  729. PyObject* table;
  730. INT16* prepared_table;
  731. Imaging imOut;
  732. if ( ! PyArg_ParseTuple(args, "siiiiiO:color_lut_3d", &mode, &filter,
  733. &table_channels, &size1D, &size2D, &size3D,
  734. &table)) {
  735. return NULL;
  736. }
  737. /* actually, it is trilinear */
  738. if (filter != IMAGING_TRANSFORM_BILINEAR) {
  739. PyErr_SetString(PyExc_ValueError,
  740. "Only LINEAR filter is supported.");
  741. return NULL;
  742. }
  743. if (1 > table_channels || table_channels > 4) {
  744. PyErr_SetString(PyExc_ValueError,
  745. "table_channels should be from 1 to 4");
  746. return NULL;
  747. }
  748. if (2 > size1D || size1D > 65 ||
  749. 2 > size2D || size2D > 65 ||
  750. 2 > size3D || size3D > 65
  751. ) {
  752. PyErr_SetString(PyExc_ValueError,
  753. "Table size in any dimension should be from 2 to 65");
  754. return NULL;
  755. }
  756. prepared_table = _prepare_lut_table(
  757. table, table_channels * size1D * size2D * size3D);
  758. if ( ! prepared_table) {
  759. return NULL;
  760. }
  761. imOut = ImagingNewDirty(mode, self->image->xsize, self->image->ysize);
  762. if ( ! imOut) {
  763. free(prepared_table);
  764. return NULL;
  765. }
  766. if ( ! ImagingColorLUT3D_linear(imOut, self->image,
  767. table_channels, size1D, size2D, size3D,
  768. prepared_table)) {
  769. free(prepared_table);
  770. ImagingDelete(imOut);
  771. return NULL;
  772. }
  773. free(prepared_table);
  774. return PyImagingNew(imOut);
  775. }
  776. static PyObject*
  777. _convert(ImagingObject* self, PyObject* args)
  778. {
  779. char* mode;
  780. int dither = 0;
  781. ImagingObject *paletteimage = NULL;
  782. if (!PyArg_ParseTuple(args, "s|iO", &mode, &dither, &paletteimage))
  783. return NULL;
  784. if (paletteimage != NULL) {
  785. if (!PyImaging_Check(paletteimage)) {
  786. PyObject_Print((PyObject *)paletteimage, stderr, 0);
  787. PyErr_SetString(PyExc_ValueError, "palette argument must be image with mode 'P'");
  788. return NULL;
  789. }
  790. if (paletteimage->image->palette == NULL) {
  791. PyErr_SetString(PyExc_ValueError, "null palette");
  792. return NULL;
  793. }
  794. }
  795. return PyImagingNew(ImagingConvert(self->image, mode, paletteimage ? paletteimage->image->palette : NULL, dither));
  796. }
  797. static PyObject*
  798. _convert2(ImagingObject* self, PyObject* args)
  799. {
  800. ImagingObject* imagep1;
  801. ImagingObject* imagep2;
  802. if (!PyArg_ParseTuple(args, "O!O!",
  803. &Imaging_Type, &imagep1,
  804. &Imaging_Type, &imagep2))
  805. return NULL;
  806. if (!ImagingConvert2(imagep1->image, imagep2->image))
  807. return NULL;
  808. Py_INCREF(Py_None);
  809. return Py_None;
  810. }
  811. static PyObject*
  812. _convert_matrix(ImagingObject* self, PyObject* args)
  813. {
  814. char* mode;
  815. float m[12];
  816. if (!PyArg_ParseTuple(args, "s(ffff)", &mode, m+0, m+1, m+2, m+3)) {
  817. PyErr_Clear();
  818. if (!PyArg_ParseTuple(args, "s(ffffffffffff)", &mode,
  819. m+0, m+1, m+2, m+3,
  820. m+4, m+5, m+6, m+7,
  821. m+8, m+9, m+10, m+11)){
  822. return NULL;
  823. }
  824. }
  825. return PyImagingNew(ImagingConvertMatrix(self->image, mode, m));
  826. }
  827. static PyObject*
  828. _convert_transparent(ImagingObject* self, PyObject* args)
  829. {
  830. char* mode;
  831. int r,g,b;
  832. if (PyArg_ParseTuple(args, "s(iii)", &mode, &r, &g, &b)) {
  833. return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, g, b));
  834. }
  835. PyErr_Clear();
  836. if (PyArg_ParseTuple(args, "si", &mode, &r)) {
  837. return PyImagingNew(ImagingConvertTransparent(self->image, mode, r, 0, 0));
  838. }
  839. return NULL;
  840. }
  841. static PyObject*
  842. _copy(ImagingObject* self, PyObject* args)
  843. {
  844. if (!PyArg_ParseTuple(args, ""))
  845. return NULL;
  846. return PyImagingNew(ImagingCopy(self->image));
  847. }
  848. static PyObject*
  849. _crop(ImagingObject* self, PyObject* args)
  850. {
  851. int x0, y0, x1, y1;
  852. if (!PyArg_ParseTuple(args, "(iiii)", &x0, &y0, &x1, &y1))
  853. return NULL;
  854. return PyImagingNew(ImagingCrop(self->image, x0, y0, x1, y1));
  855. }
  856. static PyObject*
  857. _expand_image(ImagingObject* self, PyObject* args)
  858. {
  859. int x, y;
  860. int mode = 0;
  861. if (!PyArg_ParseTuple(args, "ii|i", &x, &y, &mode))
  862. return NULL;
  863. return PyImagingNew(ImagingExpand(self->image, x, y, mode));
  864. }
  865. static PyObject*
  866. _filter(ImagingObject* self, PyObject* args)
  867. {
  868. PyObject* imOut;
  869. Py_ssize_t kernelsize;
  870. FLOAT32* kerneldata;
  871. int xsize, ysize, i;
  872. float divisor, offset;
  873. PyObject* kernel = NULL;
  874. if (!PyArg_ParseTuple(args, "(ii)ffO", &xsize, &ysize,
  875. &divisor, &offset, &kernel))
  876. return NULL;
  877. /* get user-defined kernel */
  878. kerneldata = getlist(kernel, &kernelsize, NULL, TYPE_FLOAT32);
  879. if (!kerneldata)
  880. return NULL;
  881. if (kernelsize != (Py_ssize_t) xsize * (Py_ssize_t) ysize) {
  882. free(kerneldata);
  883. return ImagingError_ValueError("bad kernel size");
  884. }
  885. for (i = 0; i < kernelsize; ++i) {
  886. kerneldata[i] /= divisor;
  887. }
  888. imOut = PyImagingNew(
  889. ImagingFilter(self->image, xsize, ysize, kerneldata, offset)
  890. );
  891. free(kerneldata);
  892. return imOut;
  893. }
  894. #ifdef WITH_UNSHARPMASK
  895. static PyObject*
  896. _gaussian_blur(ImagingObject* self, PyObject* args)
  897. {
  898. Imaging imIn;
  899. Imaging imOut;
  900. float radius = 0;
  901. int passes = 3;
  902. if (!PyArg_ParseTuple(args, "f|i", &radius, &passes))
  903. return NULL;
  904. imIn = self->image;
  905. imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
  906. if (!imOut)
  907. return NULL;
  908. if (!ImagingGaussianBlur(imOut, imIn, radius, passes)) {
  909. ImagingDelete(imOut);
  910. return NULL;
  911. }
  912. return PyImagingNew(imOut);
  913. }
  914. #endif
  915. static PyObject*
  916. _getpalette(ImagingObject* self, PyObject* args)
  917. {
  918. PyObject* palette;
  919. int palettesize = 256;
  920. int bits;
  921. ImagingShuffler pack;
  922. char* mode = "RGB";
  923. char* rawmode = "RGB";
  924. if (!PyArg_ParseTuple(args, "|ss", &mode, &rawmode))
  925. return NULL;
  926. if (!self->image->palette) {
  927. PyErr_SetString(PyExc_ValueError, no_palette);
  928. return NULL;
  929. }
  930. pack = ImagingFindPacker(mode, rawmode, &bits);
  931. if (!pack) {
  932. PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
  933. return NULL;
  934. }
  935. palette = PyBytes_FromStringAndSize(NULL, palettesize * bits / 8);
  936. if (!palette)
  937. return NULL;
  938. pack((UINT8*) PyBytes_AsString(palette),
  939. self->image->palette->palette, palettesize);
  940. return palette;
  941. }
  942. static PyObject*
  943. _getpalettemode(ImagingObject* self, PyObject* args)
  944. {
  945. if (!self->image->palette) {
  946. PyErr_SetString(PyExc_ValueError, no_palette);
  947. return NULL;
  948. }
  949. return PyUnicode_FromString(self->image->palette->mode);
  950. }
  951. static inline int
  952. _getxy(PyObject* xy, int* x, int *y)
  953. {
  954. PyObject* value;
  955. if (!PyTuple_Check(xy) || PyTuple_GET_SIZE(xy) != 2)
  956. goto badarg;
  957. value = PyTuple_GET_ITEM(xy, 0);
  958. if (PyInt_Check(value))
  959. *x = PyInt_AS_LONG(value);
  960. else if (PyFloat_Check(value))
  961. *x = (int) PyFloat_AS_DOUBLE(value);
  962. else
  963. goto badval;
  964. value = PyTuple_GET_ITEM(xy, 1);
  965. if (PyInt_Check(value))
  966. *y = PyInt_AS_LONG(value);
  967. else if (PyFloat_Check(value))
  968. *y = (int) PyFloat_AS_DOUBLE(value);
  969. else
  970. goto badval;
  971. return 0;
  972. badarg:
  973. PyErr_SetString(
  974. PyExc_TypeError,
  975. "argument must be sequence of length 2"
  976. );
  977. return -1;
  978. badval:
  979. PyErr_SetString(
  980. PyExc_TypeError,
  981. "an integer is required"
  982. );
  983. return -1;
  984. }
  985. static PyObject*
  986. _getpixel(ImagingObject* self, PyObject* args)
  987. {
  988. PyObject* xy;
  989. int x, y;
  990. if (PyTuple_GET_SIZE(args) != 1) {
  991. PyErr_SetString(
  992. PyExc_TypeError,
  993. "argument 1 must be sequence of length 2"
  994. );
  995. return NULL;
  996. }
  997. xy = PyTuple_GET_ITEM(args, 0);
  998. if (_getxy(xy, &x, &y))
  999. return NULL;
  1000. if (self->access == NULL) {
  1001. Py_INCREF(Py_None);
  1002. return Py_None;
  1003. }
  1004. return getpixel(self->image, self->access, x, y);
  1005. }
  1006. union hist_extrema {
  1007. UINT8 u[2];
  1008. INT32 i[2];
  1009. FLOAT32 f[2];
  1010. };
  1011. static union hist_extrema*
  1012. parse_histogram_extremap(ImagingObject* self, PyObject* extremap,
  1013. union hist_extrema* ep)
  1014. {
  1015. int i0, i1;
  1016. double f0, f1;
  1017. if (extremap) {
  1018. switch (self->image->type) {
  1019. case IMAGING_TYPE_UINT8:
  1020. if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1))
  1021. return NULL;
  1022. ep->u[0] = CLIP8(i0);
  1023. ep->u[1] = CLIP8(i1);
  1024. break;
  1025. case IMAGING_TYPE_INT32:
  1026. if (!PyArg_ParseTuple(extremap, "ii", &i0, &i1))
  1027. return NULL;
  1028. ep->i[0] = i0;
  1029. ep->i[1] = i1;
  1030. break;
  1031. case IMAGING_TYPE_FLOAT32:
  1032. if (!PyArg_ParseTuple(extremap, "dd", &f0, &f1))
  1033. return NULL;
  1034. ep->f[0] = (FLOAT32) f0;
  1035. ep->f[1] = (FLOAT32) f1;
  1036. break;
  1037. default:
  1038. return NULL;
  1039. }
  1040. } else {
  1041. return NULL;
  1042. }
  1043. return ep;
  1044. }
  1045. static PyObject*
  1046. _histogram(ImagingObject* self, PyObject* args)
  1047. {
  1048. ImagingHistogram h;
  1049. PyObject* list;
  1050. int i;
  1051. union hist_extrema extrema;
  1052. union hist_extrema* ep;
  1053. PyObject* extremap = NULL;
  1054. ImagingObject* maskp = NULL;
  1055. if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp))
  1056. return NULL;
  1057. /* Using a var to avoid allocations. */
  1058. ep = parse_histogram_extremap(self, extremap, &extrema);
  1059. h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep);
  1060. if (!h)
  1061. return NULL;
  1062. /* Build an integer list containing the histogram */
  1063. list = PyList_New(h->bands * 256);
  1064. for (i = 0; i < h->bands * 256; i++) {
  1065. PyObject* item;
  1066. item = PyInt_FromLong(h->histogram[i]);
  1067. if (item == NULL) {
  1068. Py_DECREF(list);
  1069. list = NULL;
  1070. break;
  1071. }
  1072. PyList_SetItem(list, i, item);
  1073. }
  1074. /* Destroy the histogram structure */
  1075. ImagingHistogramDelete(h);
  1076. return list;
  1077. }
  1078. static PyObject*
  1079. _entropy(ImagingObject* self, PyObject* args)
  1080. {
  1081. ImagingHistogram h;
  1082. int idx, length;
  1083. long sum;
  1084. double entropy, fsum, p;
  1085. union hist_extrema extrema;
  1086. union hist_extrema* ep;
  1087. PyObject* extremap = NULL;
  1088. ImagingObject* maskp = NULL;
  1089. if (!PyArg_ParseTuple(args, "|OO!", &extremap, &Imaging_Type, &maskp))
  1090. return NULL;
  1091. /* Using a local var to avoid allocations. */
  1092. ep = parse_histogram_extremap(self, extremap, &extrema);
  1093. h = ImagingGetHistogram(self->image, (maskp) ? maskp->image : NULL, ep);
  1094. if (!h)
  1095. return NULL;
  1096. /* Calculate the histogram entropy */
  1097. /* First, sum the histogram data */
  1098. length = h->bands * 256;
  1099. sum = 0;
  1100. for (idx = 0; idx < length; idx++) {
  1101. sum += h->histogram[idx];
  1102. }
  1103. /* Next, normalize the histogram data, */
  1104. /* using the histogram sum value */
  1105. fsum = (double)sum;
  1106. entropy = 0.0;
  1107. for (idx = 0; idx < length; idx++) {
  1108. p = (double)h->histogram[idx] / fsum;
  1109. if (p != 0.0) {
  1110. entropy += p * log(p) * M_LOG2E;
  1111. }
  1112. }
  1113. /* Destroy the histogram structure */
  1114. ImagingHistogramDelete(h);
  1115. return PyFloat_FromDouble(-entropy);
  1116. }
  1117. #ifdef WITH_MODEFILTER
  1118. static PyObject*
  1119. _modefilter(ImagingObject* self, PyObject* args)
  1120. {
  1121. int size;
  1122. if (!PyArg_ParseTuple(args, "i", &size))
  1123. return NULL;
  1124. return PyImagingNew(ImagingModeFilter(self->image, size));
  1125. }
  1126. #endif
  1127. static PyObject*
  1128. _offset(ImagingObject* self, PyObject* args)
  1129. {
  1130. int xoffset, yoffset;
  1131. if (!PyArg_ParseTuple(args, "ii", &xoffset, &yoffset))
  1132. return NULL;
  1133. return PyImagingNew(ImagingOffset(self->image, xoffset, yoffset));
  1134. }
  1135. static PyObject*
  1136. _paste(ImagingObject* self, PyObject* args)
  1137. {
  1138. int status;
  1139. char ink[4];
  1140. PyObject* source;
  1141. int x0, y0, x1, y1;
  1142. ImagingObject* maskp = NULL;
  1143. if (!PyArg_ParseTuple(args, "O(iiii)|O!",
  1144. &source,
  1145. &x0, &y0, &x1, &y1,
  1146. &Imaging_Type, &maskp))
  1147. return NULL;
  1148. if (PyImaging_Check(source))
  1149. status = ImagingPaste(
  1150. self->image, PyImaging_AsImaging(source),
  1151. (maskp) ? maskp->image : NULL,
  1152. x0, y0, x1, y1
  1153. );
  1154. else {
  1155. if (!getink(source, self->image, ink))
  1156. return NULL;
  1157. status = ImagingFill2(
  1158. self->image, ink,
  1159. (maskp) ? maskp->image : NULL,
  1160. x0, y0, x1, y1
  1161. );
  1162. }
  1163. if (status < 0)
  1164. return NULL;
  1165. Py_INCREF(Py_None);
  1166. return Py_None;
  1167. }
  1168. static PyObject*
  1169. _point(ImagingObject* self, PyObject* args)
  1170. {
  1171. static const char* wrong_number = "wrong number of lut entries";
  1172. Py_ssize_t n;
  1173. int i, bands;
  1174. Imaging im;
  1175. PyObject* list;
  1176. char* mode;
  1177. if (!PyArg_ParseTuple(args, "Oz", &list, &mode))
  1178. return NULL;
  1179. if (mode && !strcmp(mode, "F")) {
  1180. FLOAT32* data;
  1181. /* map from 8-bit data to floating point */
  1182. n = 256;
  1183. data = getlist(list, &n, wrong_number, TYPE_FLOAT32);
  1184. if (!data)
  1185. return NULL;
  1186. im = ImagingPoint(self->image, mode, (void*) data);
  1187. free(data);
  1188. } else if (!strcmp(self->image->mode, "I") && mode && !strcmp(mode, "L")) {
  1189. UINT8* data;
  1190. /* map from 16-bit subset of 32-bit data to 8-bit */
  1191. /* FIXME: support arbitrary number of entries (requires API change) */
  1192. n = 65536;
  1193. data = getlist(list, &n, wrong_number, TYPE_UINT8);
  1194. if (!data)
  1195. return NULL;
  1196. im = ImagingPoint(self->image, mode, (void*) data);
  1197. free(data);
  1198. } else {
  1199. INT32* data;
  1200. UINT8 lut[1024];
  1201. if (mode) {
  1202. bands = getbands(mode);
  1203. if (bands < 0)
  1204. return NULL;
  1205. } else
  1206. bands = self->image->bands;
  1207. /* map to integer data */
  1208. n = 256 * bands;
  1209. data = getlist(list, &n, wrong_number, TYPE_INT32);
  1210. if (!data)
  1211. return NULL;
  1212. if (mode && !strcmp(mode, "I"))
  1213. im = ImagingPoint(self->image, mode, (void*) data);
  1214. else if (mode && bands > 1) {
  1215. for (i = 0; i < 256; i++) {
  1216. lut[i*4] = CLIP8(data[i]);
  1217. lut[i*4+1] = CLIP8(data[i+256]);
  1218. lut[i*4+2] = CLIP8(data[i+512]);
  1219. if (n > 768)
  1220. lut[i*4+3] = CLIP8(data[i+768]);
  1221. }
  1222. im = ImagingPoint(self->image, mode, (void*) lut);
  1223. } else {
  1224. /* map individual bands */
  1225. for (i = 0; i < n; i++)
  1226. lut[i] = CLIP8(data[i]);
  1227. im = ImagingPoint(self->image, mode, (void*) lut);
  1228. }
  1229. free(data);
  1230. }
  1231. return PyImagingNew(im);
  1232. }
  1233. static PyObject*
  1234. _point_transform(ImagingObject* self, PyObject* args)
  1235. {
  1236. double scale = 1.0;
  1237. double offset = 0.0;
  1238. if (!PyArg_ParseTuple(args, "|dd", &scale, &offset))
  1239. return NULL;
  1240. return PyImagingNew(ImagingPointTransform(self->image, scale, offset));
  1241. }
  1242. static PyObject*
  1243. _putdata(ImagingObject* self, PyObject* args)
  1244. {
  1245. Imaging image;
  1246. // i & n are # pixels, require py_ssize_t. x can be as large as n. y, just because.
  1247. Py_ssize_t n, i, x, y;
  1248. PyObject* data;
  1249. PyObject* seq = NULL;
  1250. PyObject* op;
  1251. double scale = 1.0;
  1252. double offset = 0.0;
  1253. if (!PyArg_ParseTuple(args, "O|dd", &data, &scale, &offset))
  1254. return NULL;
  1255. if (!PySequence_Check(data)) {
  1256. PyErr_SetString(PyExc_TypeError, must_be_sequence);
  1257. return NULL;
  1258. }
  1259. image = self->image;
  1260. n = PyObject_Length(data);
  1261. if (n > (Py_ssize_t)image->xsize * (Py_ssize_t)image->ysize) {
  1262. PyErr_SetString(PyExc_TypeError, "too many data entries");
  1263. return NULL;
  1264. }
  1265. if (image->image8) {
  1266. if (PyBytes_Check(data)) {
  1267. unsigned char* p;
  1268. p = (unsigned char*) PyBytes_AS_STRING(data);
  1269. if (scale == 1.0 && offset == 0.0)
  1270. /* Plain string data */
  1271. for (i = y = 0; i < n; i += image->xsize, y++) {
  1272. x = n - i;
  1273. if (x > (int) image->xsize)
  1274. x = image->xsize;
  1275. memcpy(image->image8[y], p+i, x);
  1276. }
  1277. else
  1278. /* Scaled and clipped string data */
  1279. for (i = x = y = 0; i < n; i++) {
  1280. image->image8[y][x] = CLIP8((int) (p[i] * scale + offset));
  1281. if (++x >= (int) image->xsize)
  1282. x = 0, y++;
  1283. }
  1284. } else {
  1285. seq = PySequence_Fast(data, must_be_sequence);
  1286. if (!seq) {
  1287. PyErr_SetString(PyExc_TypeError, must_be_sequence);
  1288. return NULL;
  1289. }
  1290. if (scale == 1.0 && offset == 0.0) {
  1291. /* Clipped data */
  1292. for (i = x = y = 0; i < n; i++) {
  1293. op = PySequence_Fast_GET_ITEM(seq, i);
  1294. image->image8[y][x] = (UINT8) CLIP8(PyInt_AsLong(op));
  1295. if (++x >= (int) image->xsize){
  1296. x = 0, y++;
  1297. }
  1298. }
  1299. } else {
  1300. /* Scaled and clipped data */
  1301. for (i = x = y = 0; i < n; i++) {
  1302. PyObject *op = PySequence_Fast_GET_ITEM(seq, i);
  1303. image->image8[y][x] = CLIP8(
  1304. (int) (PyFloat_AsDouble(op) * scale + offset));
  1305. if (++x >= (int) image->xsize){
  1306. x = 0, y++;
  1307. }
  1308. }
  1309. }
  1310. PyErr_Clear(); /* Avoid weird exceptions */
  1311. }
  1312. } else {
  1313. /* 32-bit images */
  1314. seq = PySequence_Fast(data, must_be_sequence);
  1315. if (!seq) {
  1316. PyErr_SetString(PyExc_TypeError, must_be_sequence);
  1317. return NULL;
  1318. }
  1319. switch (image->type) {
  1320. case IMAGING_TYPE_INT32:
  1321. for (i = x = y = 0; i < n; i++) {
  1322. op = PySequence_Fast_GET_ITEM(seq, i);
  1323. IMAGING_PIXEL_INT32(image, x, y) =
  1324. (INT32) (PyFloat_AsDouble(op) * scale + offset);
  1325. if (++x >= (int) image->xsize){
  1326. x = 0, y++;
  1327. }
  1328. }
  1329. PyErr_Clear(); /* Avoid weird exceptions */
  1330. break;
  1331. case IMAGING_TYPE_FLOAT32:
  1332. for (i = x = y = 0; i < n; i++) {
  1333. op = PySequence_Fast_GET_ITEM(seq, i);
  1334. IMAGING_PIXEL_FLOAT32(image, x, y) =
  1335. (FLOAT32) (PyFloat_AsDouble(op) * scale + offset);
  1336. if (++x >= (int) image->xsize){
  1337. x = 0, y++;
  1338. }
  1339. }
  1340. PyErr_Clear(); /* Avoid weird exceptions */
  1341. break;
  1342. default:
  1343. for (i = x = y = 0; i < n; i++) {
  1344. union {
  1345. char ink[4];
  1346. INT32 inkint;
  1347. } u;
  1348. u.inkint = 0;
  1349. op = PySequence_Fast_GET_ITEM(seq, i);
  1350. if (!op || !getink(op, image, u.ink)) {
  1351. Py_DECREF(seq);
  1352. return NULL;
  1353. }
  1354. /* FIXME: what about scale and offset? */
  1355. image->image32[y][x] = u.inkint;
  1356. if (++x >= (int) image->xsize){
  1357. x = 0, y++;
  1358. }
  1359. }
  1360. PyErr_Clear(); /* Avoid weird exceptions */
  1361. break;
  1362. }
  1363. }
  1364. Py_XDECREF(seq);
  1365. Py_INCREF(Py_None);
  1366. return Py_None;
  1367. }
  1368. #ifdef WITH_QUANTIZE
  1369. static PyObject*
  1370. _quantize(ImagingObject* self, PyObject* args)
  1371. {
  1372. int colours = 256;
  1373. int method = 0;
  1374. int kmeans = 0;
  1375. if (!PyArg_ParseTuple(args, "|iii", &colours, &method, &kmeans))
  1376. return NULL;
  1377. if (!self->image->xsize || !self->image->ysize) {
  1378. /* no content; return an empty image */
  1379. return PyImagingNew(
  1380. ImagingNew("P", self->image->xsize, self->image->ysize)
  1381. );
  1382. }
  1383. return PyImagingNew(ImagingQuantize(self->image, colours, method, kmeans));
  1384. }
  1385. #endif
  1386. static PyObject*
  1387. _putpalette(ImagingObject* self, PyObject* args)
  1388. {
  1389. ImagingShuffler unpack;
  1390. int bits;
  1391. char* rawmode;
  1392. UINT8* palette;
  1393. Py_ssize_t palettesize;
  1394. if (!PyArg_ParseTuple(args, "s"PY_ARG_BYTES_LENGTH, &rawmode, &palette, &palettesize))
  1395. return NULL;
  1396. if (strcmp(self->image->mode, "L") && strcmp(self->image->mode, "LA") &&
  1397. strcmp(self->image->mode, "P") && strcmp(self->image->mode, "PA")) {
  1398. PyErr_SetString(PyExc_ValueError, wrong_mode);
  1399. return NULL;
  1400. }
  1401. unpack = ImagingFindUnpacker("RGB", rawmode, &bits);
  1402. if (!unpack) {
  1403. PyErr_SetString(PyExc_ValueError, wrong_raw_mode);
  1404. return NULL;
  1405. }
  1406. if ( palettesize * 8 / bits > 256) {
  1407. PyErr_SetString(PyExc_ValueError, wrong_palette_size);
  1408. return NULL;
  1409. }
  1410. ImagingPaletteDelete(self->image->palette);
  1411. strcpy(self->image->mode, strlen(self->image->mode) == 2 ? "PA" : "P");
  1412. self->image->palette = ImagingPaletteNew("RGB");
  1413. unpack(self->image->palette->palette, palette, palettesize * 8 / bits);
  1414. Py_INCREF(Py_None);
  1415. return Py_None;
  1416. }
  1417. static PyObject*
  1418. _putpalettealpha(ImagingObject* self, PyObject* args)
  1419. {
  1420. int index;
  1421. int alpha = 0;
  1422. if (!PyArg_ParseTuple(args, "i|i", &index, &alpha))
  1423. return NULL;
  1424. if (!self->image->palette) {
  1425. PyErr_SetString(PyExc_ValueError, no_palette);
  1426. return NULL;
  1427. }
  1428. if (index < 0 || index >= 256) {
  1429. PyErr_SetString(PyExc_ValueError, outside_palette);
  1430. return NULL;
  1431. }
  1432. strcpy(self->image->palette->mode, "RGBA");
  1433. self->image->palette->palette[index*4+3] = (UINT8) alpha;
  1434. Py_INCREF(Py_None);
  1435. return Py_None;
  1436. }
  1437. static PyObject*
  1438. _putpalettealphas(ImagingObject* self, PyObject* args)
  1439. {
  1440. int i;
  1441. UINT8 *values;
  1442. Py_ssize_t length;
  1443. if (!PyArg_ParseTuple(args, PY_ARG_BYTES_LENGTH, &values, &length))
  1444. return NULL;
  1445. if (!self->image->palette) {
  1446. PyErr_SetString(PyExc_ValueError, no_palette);
  1447. return NULL;
  1448. }
  1449. if (length > 256) {
  1450. PyErr_SetString(PyExc_ValueError, outside_palette);
  1451. return NULL;
  1452. }
  1453. strcpy(self->image->palette->mode, "RGBA");
  1454. for (i=0; i<length; i++) {
  1455. self->image->palette->palette[i*4+3] = (UINT8) values[i];
  1456. }
  1457. Py_INCREF(Py_None);
  1458. return Py_None;
  1459. }
  1460. static PyObject*
  1461. _putpixel(ImagingObject* self, PyObject* args)
  1462. {
  1463. Imaging im;
  1464. char ink[4];
  1465. int x, y;
  1466. PyObject* color;
  1467. if (!PyArg_ParseTuple(args, "(ii)O", &x, &y, &color))
  1468. return NULL;
  1469. im = self->image;
  1470. if (x < 0) {
  1471. x = im->xsize + x;
  1472. }
  1473. if (y < 0) {
  1474. y = im->ysize + y;
  1475. }
  1476. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
  1477. PyErr_SetString(PyExc_IndexError, outside_image);
  1478. return NULL;
  1479. }
  1480. if (!getink(color, im, ink))
  1481. return NULL;
  1482. if (self->access)
  1483. self->access->put_pixel(im, x, y, ink);
  1484. Py_INCREF(Py_None);
  1485. return Py_None;
  1486. }
  1487. #ifdef WITH_RANKFILTER
  1488. static PyObject*
  1489. _rankfilter(ImagingObject* self, PyObject* args)
  1490. {
  1491. int size, rank;
  1492. if (!PyArg_ParseTuple(args, "ii", &size, &rank))
  1493. return NULL;
  1494. return PyImagingNew(ImagingRankFilter(self->image, size, rank));
  1495. }
  1496. #endif
  1497. static PyObject*
  1498. _resize(ImagingObject* self, PyObject* args)
  1499. {
  1500. Imaging imIn;
  1501. Imaging imOut;
  1502. int xsize, ysize;
  1503. int filter = IMAGING_TRANSFORM_NEAREST;
  1504. float box[4] = {0, 0, 0, 0};
  1505. imIn = self->image;
  1506. box[2] = imIn->xsize;
  1507. box[3] = imIn->ysize;
  1508. if (!PyArg_ParseTuple(args, "(ii)|i(ffff)", &xsize, &ysize, &filter,
  1509. &box[0], &box[1], &box[2], &box[3]))
  1510. return NULL;
  1511. if (xsize < 1 || ysize < 1) {
  1512. return ImagingError_ValueError("height and width must be > 0");
  1513. }
  1514. if (box[0] < 0 || box[1] < 0) {
  1515. return ImagingError_ValueError("box offset can't be negative");
  1516. }
  1517. if (box[2] > imIn->xsize || box[3] > imIn->ysize) {
  1518. return ImagingError_ValueError("box can't exceed original image size");
  1519. }
  1520. if (box[2] - box[0] < 0 || box[3] - box[1] < 0) {
  1521. return ImagingError_ValueError("box can't be empty");
  1522. }
  1523. // If box's coordinates are int and box size matches requested size
  1524. if (box[0] - (int) box[0] == 0 && box[2] - box[0] == xsize
  1525. && box[1] - (int) box[1] == 0 && box[3] - box[1] == ysize) {
  1526. imOut = ImagingCrop(imIn, box[0], box[1], box[2], box[3]);
  1527. }
  1528. else if (filter == IMAGING_TRANSFORM_NEAREST) {
  1529. double a[6];
  1530. memset(a, 0, sizeof a);
  1531. a[0] = (double) (box[2] - box[0]) / xsize;
  1532. a[4] = (double) (box[3] - box[1]) / ysize;
  1533. a[2] = box[0];
  1534. a[5] = box[1];
  1535. imOut = ImagingNewDirty(imIn->mode, xsize, ysize);
  1536. imOut = ImagingTransform(
  1537. imOut, imIn, IMAGING_TRANSFORM_AFFINE,
  1538. 0, 0, xsize, ysize,
  1539. a, filter, 1);
  1540. }
  1541. else {
  1542. imOut = ImagingResample(imIn, xsize, ysize, filter, box);
  1543. }
  1544. return PyImagingNew(imOut);
  1545. }
  1546. #define IS_RGB(mode)\
  1547. (!strcmp(mode, "RGB") || !strcmp(mode, "RGBA") || !strcmp(mode, "RGBX"))
  1548. static PyObject*
  1549. im_setmode(ImagingObject* self, PyObject* args)
  1550. {
  1551. /* attempt to modify the mode of an image in place */
  1552. Imaging im;
  1553. char* mode;
  1554. Py_ssize_t modelen;
  1555. if (!PyArg_ParseTuple(args, "s#:setmode", &mode, &modelen))
  1556. return NULL;
  1557. im = self->image;
  1558. /* move all logic in here to the libImaging primitive */
  1559. if (!strcmp(im->mode, mode)) {
  1560. ; /* same mode; always succeeds */
  1561. } else if (IS_RGB(im->mode) && IS_RGB(mode)) {
  1562. /* color to color */
  1563. strcpy(im->mode, mode);
  1564. im->bands = modelen;
  1565. if (!strcmp(mode, "RGBA"))
  1566. (void) ImagingFillBand(im, 3, 255);
  1567. } else {
  1568. /* trying doing an in-place conversion */
  1569. if (!ImagingConvertInPlace(im, mode))
  1570. return NULL;
  1571. }
  1572. if (self->access)
  1573. ImagingAccessDelete(im, self->access);
  1574. self->access = ImagingAccessNew(im);
  1575. Py_INCREF(Py_None);
  1576. return Py_None;
  1577. }
  1578. static PyObject*
  1579. _transform2(ImagingObject* self, PyObject* args)
  1580. {
  1581. static const char* wrong_number = "wrong number of matrix entries";
  1582. Imaging imOut;
  1583. Py_ssize_t n;
  1584. double *a;
  1585. ImagingObject* imagep;
  1586. int x0, y0, x1, y1;
  1587. int method;
  1588. PyObject* data;
  1589. int filter = IMAGING_TRANSFORM_NEAREST;
  1590. int fill = 1;
  1591. if (!PyArg_ParseTuple(args, "(iiii)O!iO|ii",
  1592. &x0, &y0, &x1, &y1,
  1593. &Imaging_Type, &imagep,
  1594. &method, &data,
  1595. &filter, &fill))
  1596. return NULL;
  1597. switch (method) {
  1598. case IMAGING_TRANSFORM_AFFINE:
  1599. n = 6;
  1600. break;
  1601. case IMAGING_TRANSFORM_PERSPECTIVE:
  1602. n = 8;
  1603. break;
  1604. case IMAGING_TRANSFORM_QUAD:
  1605. n = 8;
  1606. break;
  1607. default:
  1608. n = -1; /* force error */
  1609. }
  1610. a = getlist(data, &n, wrong_number, TYPE_DOUBLE);
  1611. if (!a)
  1612. return NULL;
  1613. imOut = ImagingTransform(
  1614. self->image, imagep->image, method,
  1615. x0, y0, x1, y1, a, filter, fill);
  1616. free(a);
  1617. if (!imOut)
  1618. return NULL;
  1619. Py_INCREF(Py_None);
  1620. return Py_None;
  1621. }
  1622. static PyObject*
  1623. _transpose(ImagingObject* self, PyObject* args)
  1624. {
  1625. Imaging imIn;
  1626. Imaging imOut;
  1627. int op;
  1628. if (!PyArg_ParseTuple(args, "i", &op))
  1629. return NULL;
  1630. imIn = self->image;
  1631. switch (op) {
  1632. case 0: /* flip left right */
  1633. case 1: /* flip top bottom */
  1634. case 3: /* rotate 180 */
  1635. imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
  1636. break;
  1637. case 2: /* rotate 90 */
  1638. case 4: /* rotate 270 */
  1639. case 5: /* transpose */
  1640. case 6: /* transverse */
  1641. imOut = ImagingNewDirty(imIn->mode, imIn->ysize, imIn->xsize);
  1642. break;
  1643. default:
  1644. PyErr_SetString(PyExc_ValueError, "No such transpose operation");
  1645. return NULL;
  1646. }
  1647. if (imOut)
  1648. switch (op) {
  1649. case 0:
  1650. (void) ImagingFlipLeftRight(imOut, imIn);
  1651. break;
  1652. case 1:
  1653. (void) ImagingFlipTopBottom(imOut, imIn);
  1654. break;
  1655. case 2:
  1656. (void) ImagingRotate90(imOut, imIn);
  1657. break;
  1658. case 3:
  1659. (void) ImagingRotate180(imOut, imIn);
  1660. break;
  1661. case 4:
  1662. (void) ImagingRotate270(imOut, imIn);
  1663. break;
  1664. case 5:
  1665. (void) ImagingTranspose(imOut, imIn);
  1666. break;
  1667. case 6:
  1668. (void) ImagingTransverse(imOut, imIn);
  1669. break;
  1670. }
  1671. return PyImagingNew(imOut);
  1672. }
  1673. #ifdef WITH_UNSHARPMASK
  1674. static PyObject*
  1675. _unsharp_mask(ImagingObject* self, PyObject* args)
  1676. {
  1677. Imaging imIn;
  1678. Imaging imOut;
  1679. float radius;
  1680. int percent, threshold;
  1681. if (!PyArg_ParseTuple(args, "fii", &radius, &percent, &threshold))
  1682. return NULL;
  1683. imIn = self->image;
  1684. imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
  1685. if (!imOut)
  1686. return NULL;
  1687. if (!ImagingUnsharpMask(imOut, imIn, radius, percent, threshold))
  1688. return NULL;
  1689. return PyImagingNew(imOut);
  1690. }
  1691. #endif
  1692. static PyObject*
  1693. _box_blur(ImagingObject* self, PyObject* args)
  1694. {
  1695. Imaging imIn;
  1696. Imaging imOut;
  1697. float radius;
  1698. int n = 1;
  1699. if (!PyArg_ParseTuple(args, "f|i", &radius, &n))
  1700. return NULL;
  1701. imIn = self->image;
  1702. imOut = ImagingNewDirty(imIn->mode, imIn->xsize, imIn->ysize);
  1703. if (!imOut)
  1704. return NULL;
  1705. if (!ImagingBoxBlur(imOut, imIn, radius, n)) {
  1706. ImagingDelete(imOut);
  1707. return NULL;
  1708. }
  1709. return PyImagingNew(imOut);
  1710. }
  1711. /* -------------------------------------------------------------------- */
  1712. static PyObject*
  1713. _isblock(ImagingObject* self, PyObject* args)
  1714. {
  1715. return PyBool_FromLong(self->image->block != NULL);
  1716. }
  1717. static PyObject*
  1718. _getbbox(ImagingObject* self, PyObject* args)
  1719. {
  1720. int bbox[4];
  1721. if (!ImagingGetBBox(self->image, bbox)) {
  1722. Py_INCREF(Py_None);
  1723. return Py_None;
  1724. }
  1725. return Py_BuildValue("iiii", bbox[0], bbox[1], bbox[2], bbox[3]);
  1726. }
  1727. static PyObject*
  1728. _getcolors(ImagingObject* self, PyObject* args)
  1729. {
  1730. ImagingColorItem* items;
  1731. int i, colors;
  1732. PyObject* out;
  1733. int maxcolors = 256;
  1734. if (!PyArg_ParseTuple(args, "i:getcolors", &maxcolors))
  1735. return NULL;
  1736. items = ImagingGetColors(self->image, maxcolors, &colors);
  1737. if (!items)
  1738. return NULL;
  1739. if (colors > maxcolors) {
  1740. out = Py_None;
  1741. Py_INCREF(out);
  1742. } else {
  1743. out = PyList_New(colors);
  1744. for (i = 0; i < colors; i++) {
  1745. ImagingColorItem* v = &items[i];
  1746. PyObject* item = Py_BuildValue(
  1747. "iN", v->count, getpixel(self->image, self->access, v->x, v->y)
  1748. );
  1749. PyList_SetItem(out, i, item);
  1750. }
  1751. }
  1752. free(items);
  1753. return out;
  1754. }
  1755. static PyObject*
  1756. _getextrema(ImagingObject* self, PyObject* args)
  1757. {
  1758. union {
  1759. UINT8 u[2];
  1760. INT32 i[2];
  1761. FLOAT32 f[2];
  1762. UINT16 s[2];
  1763. } extrema;
  1764. int status;
  1765. status = ImagingGetExtrema(self->image, &extrema);
  1766. if (status < 0)
  1767. return NULL;
  1768. if (status)
  1769. switch (self->image->type) {
  1770. case IMAGING_TYPE_UINT8:
  1771. return Py_BuildValue("BB", extrema.u[0], extrema.u[1]);
  1772. case IMAGING_TYPE_INT32:
  1773. return Py_BuildValue("ii", extrema.i[0], extrema.i[1]);
  1774. case IMAGING_TYPE_FLOAT32:
  1775. return Py_BuildValue("dd", extrema.f[0], extrema.f[1]);
  1776. case IMAGING_TYPE_SPECIAL:
  1777. if (strcmp(self->image->mode, "I;16") == 0) {
  1778. return Py_BuildValue("HH", extrema.s[0], extrema.s[1]);
  1779. }
  1780. }
  1781. Py_INCREF(Py_None);
  1782. return Py_None;
  1783. }
  1784. static PyObject*
  1785. _getprojection(ImagingObject* self, PyObject* args)
  1786. {
  1787. unsigned char* xprofile;
  1788. unsigned char* yprofile;
  1789. PyObject* result;
  1790. /* malloc check ok */
  1791. xprofile = malloc(self->image->xsize);
  1792. yprofile = malloc(self->image->ysize);
  1793. if (xprofile == NULL || yprofile == NULL) {
  1794. free(xprofile);
  1795. free(yprofile);
  1796. return PyErr_NoMemory();
  1797. }
  1798. ImagingGetProjection(self->image, (unsigned char *)xprofile, (unsigned char *)yprofile);
  1799. result = Py_BuildValue(PY_ARG_BYTES_LENGTH PY_ARG_BYTES_LENGTH,
  1800. xprofile, (Py_ssize_t)self->image->xsize,
  1801. yprofile, (Py_ssize_t)self->image->ysize);
  1802. free(xprofile);
  1803. free(yprofile);
  1804. return result;
  1805. }
  1806. /* -------------------------------------------------------------------- */
  1807. static PyObject*
  1808. _getband(ImagingObject* self, PyObject* args)
  1809. {
  1810. int band;
  1811. if (!PyArg_ParseTuple(args, "i", &band))
  1812. return NULL;
  1813. return PyImagingNew(ImagingGetBand(self->image, band));
  1814. }
  1815. static PyObject*
  1816. _fillband(ImagingObject* self, PyObject* args)
  1817. {
  1818. int band;
  1819. int color;
  1820. if (!PyArg_ParseTuple(args, "ii", &band, &color))
  1821. return NULL;
  1822. if (!ImagingFillBand(self->image, band, color))
  1823. return NULL;
  1824. Py_INCREF(Py_None);
  1825. return Py_None;
  1826. }
  1827. static PyObject*
  1828. _putband(ImagingObject* self, PyObject* args)
  1829. {
  1830. ImagingObject* imagep;
  1831. int band;
  1832. if (!PyArg_ParseTuple(args, "O!i",
  1833. &Imaging_Type, &imagep,
  1834. &band))
  1835. return NULL;
  1836. if (!ImagingPutBand(self->image, imagep->image, band))
  1837. return NULL;
  1838. Py_INCREF(Py_None);
  1839. return Py_None;
  1840. }
  1841. static PyObject*
  1842. _merge(PyObject* self, PyObject* args)
  1843. {
  1844. char* mode;
  1845. ImagingObject *band0 = NULL;
  1846. ImagingObject *band1 = NULL;
  1847. ImagingObject *band2 = NULL;
  1848. ImagingObject *band3 = NULL;
  1849. Imaging bands[4] = {NULL, NULL, NULL, NULL};
  1850. if (!PyArg_ParseTuple(args, "sO!|O!O!O!", &mode,
  1851. &Imaging_Type, &band0, &Imaging_Type, &band1,
  1852. &Imaging_Type, &band2, &Imaging_Type, &band3))
  1853. return NULL;
  1854. if (band0) bands[0] = band0->image;
  1855. if (band1) bands[1] = band1->image;
  1856. if (band2) bands[2] = band2->image;
  1857. if (band3) bands[3] = band3->image;
  1858. return PyImagingNew(ImagingMerge(mode, bands));
  1859. }
  1860. static PyObject*
  1861. _split(ImagingObject* self, PyObject* args)
  1862. {
  1863. int fails = 0;
  1864. Py_ssize_t i;
  1865. PyObject* list;
  1866. PyObject* imaging_object;
  1867. Imaging bands[4] = {NULL, NULL, NULL, NULL};
  1868. if ( ! ImagingSplit(self->image, bands))
  1869. return NULL;
  1870. list = PyTuple_New(self->image->bands);
  1871. for (i = 0; i < self->image->bands; i++) {
  1872. imaging_object = PyImagingNew(bands[i]);
  1873. if ( ! imaging_object)
  1874. fails += 1;
  1875. PyTuple_SET_ITEM(list, i, imaging_object);
  1876. }
  1877. if (fails) {
  1878. Py_DECREF(list);
  1879. list = NULL;
  1880. }
  1881. return list;
  1882. }
  1883. /* -------------------------------------------------------------------- */
  1884. #ifdef WITH_IMAGECHOPS
  1885. static PyObject*
  1886. _chop_invert(ImagingObject* self, PyObject* args)
  1887. {
  1888. return PyImagingNew(ImagingNegative(self->image));
  1889. }
  1890. static PyObject*
  1891. _chop_lighter(ImagingObject* self, PyObject* args)
  1892. {
  1893. ImagingObject* imagep;
  1894. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1895. return NULL;
  1896. return PyImagingNew(ImagingChopLighter(self->image, imagep->image));
  1897. }
  1898. static PyObject*
  1899. _chop_darker(ImagingObject* self, PyObject* args)
  1900. {
  1901. ImagingObject* imagep;
  1902. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1903. return NULL;
  1904. return PyImagingNew(ImagingChopDarker(self->image, imagep->image));
  1905. }
  1906. static PyObject*
  1907. _chop_difference(ImagingObject* self, PyObject* args)
  1908. {
  1909. ImagingObject* imagep;
  1910. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1911. return NULL;
  1912. return PyImagingNew(ImagingChopDifference(self->image, imagep->image));
  1913. }
  1914. static PyObject*
  1915. _chop_multiply(ImagingObject* self, PyObject* args)
  1916. {
  1917. ImagingObject* imagep;
  1918. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1919. return NULL;
  1920. return PyImagingNew(ImagingChopMultiply(self->image, imagep->image));
  1921. }
  1922. static PyObject*
  1923. _chop_screen(ImagingObject* self, PyObject* args)
  1924. {
  1925. ImagingObject* imagep;
  1926. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1927. return NULL;
  1928. return PyImagingNew(ImagingChopScreen(self->image, imagep->image));
  1929. }
  1930. static PyObject*
  1931. _chop_add(ImagingObject* self, PyObject* args)
  1932. {
  1933. ImagingObject* imagep;
  1934. float scale;
  1935. int offset;
  1936. scale = 1.0;
  1937. offset = 0;
  1938. if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep,
  1939. &scale, &offset))
  1940. return NULL;
  1941. return PyImagingNew(ImagingChopAdd(self->image, imagep->image,
  1942. scale, offset));
  1943. }
  1944. static PyObject*
  1945. _chop_subtract(ImagingObject* self, PyObject* args)
  1946. {
  1947. ImagingObject* imagep;
  1948. float scale;
  1949. int offset;
  1950. scale = 1.0;
  1951. offset = 0;
  1952. if (!PyArg_ParseTuple(args, "O!|fi", &Imaging_Type, &imagep,
  1953. &scale, &offset))
  1954. return NULL;
  1955. return PyImagingNew(ImagingChopSubtract(self->image, imagep->image,
  1956. scale, offset));
  1957. }
  1958. static PyObject*
  1959. _chop_and(ImagingObject* self, PyObject* args)
  1960. {
  1961. ImagingObject* imagep;
  1962. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1963. return NULL;
  1964. return PyImagingNew(ImagingChopAnd(self->image, imagep->image));
  1965. }
  1966. static PyObject*
  1967. _chop_or(ImagingObject* self, PyObject* args)
  1968. {
  1969. ImagingObject* imagep;
  1970. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1971. return NULL;
  1972. return PyImagingNew(ImagingChopOr(self->image, imagep->image));
  1973. }
  1974. static PyObject*
  1975. _chop_xor(ImagingObject* self, PyObject* args)
  1976. {
  1977. ImagingObject* imagep;
  1978. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1979. return NULL;
  1980. return PyImagingNew(ImagingChopXor(self->image, imagep->image));
  1981. }
  1982. static PyObject*
  1983. _chop_add_modulo(ImagingObject* self, PyObject* args)
  1984. {
  1985. ImagingObject* imagep;
  1986. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1987. return NULL;
  1988. return PyImagingNew(ImagingChopAddModulo(self->image, imagep->image));
  1989. }
  1990. static PyObject*
  1991. _chop_subtract_modulo(ImagingObject* self, PyObject* args)
  1992. {
  1993. ImagingObject* imagep;
  1994. if (!PyArg_ParseTuple(args, "O!", &Imaging_Type, &imagep))
  1995. return NULL;
  1996. return PyImagingNew(ImagingChopSubtractModulo(self->image, imagep->image));
  1997. }
  1998. #endif
  1999. /* -------------------------------------------------------------------- */
  2000. #ifdef WITH_IMAGEDRAW
  2001. static PyObject*
  2002. _font_new(PyObject* self_, PyObject* args)
  2003. {
  2004. ImagingFontObject *self;
  2005. int i, y0, y1;
  2006. static const char* wrong_length = "descriptor table has wrong size";
  2007. ImagingObject* imagep;
  2008. unsigned char* glyphdata;
  2009. Py_ssize_t glyphdata_length;
  2010. if (!PyArg_ParseTuple(args, "O!"PY_ARG_BYTES_LENGTH,
  2011. &Imaging_Type, &imagep,
  2012. &glyphdata, &glyphdata_length))
  2013. return NULL;
  2014. if (glyphdata_length != 256 * 20) {
  2015. PyErr_SetString(PyExc_ValueError, wrong_length);
  2016. return NULL;
  2017. }
  2018. self = PyObject_New(ImagingFontObject, &ImagingFont_Type);
  2019. if (self == NULL)
  2020. return NULL;
  2021. /* glyph bitmap */
  2022. self->bitmap = imagep->image;
  2023. y0 = y1 = 0;
  2024. /* glyph glyphs */
  2025. for (i = 0; i < 256; i++) {
  2026. self->glyphs[i].dx = S16(B16(glyphdata, 0));
  2027. self->glyphs[i].dy = S16(B16(glyphdata, 2));
  2028. self->glyphs[i].dx0 = S16(B16(glyphdata, 4));
  2029. self->glyphs[i].dy0 = S16(B16(glyphdata, 6));
  2030. self->glyphs[i].dx1 = S16(B16(glyphdata, 8));
  2031. self->glyphs[i].dy1 = S16(B16(glyphdata, 10));
  2032. self->glyphs[i].sx0 = S16(B16(glyphdata, 12));
  2033. self->glyphs[i].sy0 = S16(B16(glyphdata, 14));
  2034. self->glyphs[i].sx1 = S16(B16(glyphdata, 16));
  2035. self->glyphs[i].sy1 = S16(B16(glyphdata, 18));
  2036. if (self->glyphs[i].dy0 < y0)
  2037. y0 = self->glyphs[i].dy0;
  2038. if (self->glyphs[i].dy1 > y1)
  2039. y1 = self->glyphs[i].dy1;
  2040. glyphdata += 20;
  2041. }
  2042. self->baseline = -y0;
  2043. self->ysize = y1 - y0;
  2044. /* keep a reference to the bitmap object */
  2045. Py_INCREF(imagep);
  2046. self->ref = imagep;
  2047. return (PyObject*) self;
  2048. }
  2049. static void
  2050. _font_dealloc(ImagingFontObject* self)
  2051. {
  2052. Py_XDECREF(self->ref);
  2053. PyObject_Del(self);
  2054. }
  2055. static inline int
  2056. textwidth(ImagingFontObject* self, const unsigned char* text)
  2057. {
  2058. int xsize;
  2059. for (xsize = 0; *text; text++)
  2060. xsize += self->glyphs[*text].dx;
  2061. return xsize;
  2062. }
  2063. void _font_text_asBytes(PyObject* encoded_string, unsigned char** text){
  2064. /* Allocates *text, returns a 'new reference'. Caller is required to free */
  2065. PyObject* bytes = NULL;
  2066. Py_ssize_t len = 0;
  2067. char *buffer;
  2068. *text = NULL;
  2069. if (PyUnicode_CheckExact(encoded_string)){
  2070. bytes = PyUnicode_AsLatin1String(encoded_string);
  2071. if (!bytes) {
  2072. return;
  2073. }
  2074. PyBytes_AsStringAndSize(bytes, &buffer, &len);
  2075. } else if (PyBytes_Check(encoded_string)) {
  2076. PyBytes_AsStringAndSize(encoded_string, &buffer, &len);
  2077. }
  2078. *text = calloc(len+1,1);
  2079. if (*text) {
  2080. memcpy(*text, buffer, len);
  2081. } else {
  2082. ImagingError_MemoryError();
  2083. }
  2084. if (bytes) {
  2085. Py_DECREF(bytes);
  2086. }
  2087. return;
  2088. }
  2089. static PyObject*
  2090. _font_getmask(ImagingFontObject* self, PyObject* args)
  2091. {
  2092. Imaging im;
  2093. Imaging bitmap;
  2094. int x, b;
  2095. int i=0;
  2096. int status;
  2097. Glyph* glyph;
  2098. PyObject* encoded_string;
  2099. unsigned char* text;
  2100. char* mode = "";
  2101. if (!PyArg_ParseTuple(args, "O|s:getmask", &encoded_string, &mode)){
  2102. return NULL;
  2103. }
  2104. _font_text_asBytes(encoded_string, &text);
  2105. if (!text) {
  2106. return NULL;
  2107. }
  2108. im = ImagingNew(self->bitmap->mode, textwidth(self, text), self->ysize);
  2109. if (!im) {
  2110. free(text);
  2111. ImagingError_MemoryError();
  2112. return NULL;
  2113. }
  2114. b = 0;
  2115. (void) ImagingFill(im, &b);
  2116. b = self->baseline;
  2117. for (x = 0; text[i]; i++) {
  2118. glyph = &self->glyphs[text[i]];
  2119. bitmap = ImagingCrop(
  2120. self->bitmap,
  2121. glyph->sx0, glyph->sy0, glyph->sx1, glyph->sy1
  2122. );
  2123. if (!bitmap)
  2124. goto failed;
  2125. status = ImagingPaste(
  2126. im, bitmap, NULL,
  2127. glyph->dx0+x, glyph->dy0+b, glyph->dx1+x, glyph->dy1+b
  2128. );
  2129. ImagingDelete(bitmap);
  2130. if (status < 0)
  2131. goto failed;
  2132. x = x + glyph->dx;
  2133. b = b + glyph->dy;
  2134. }
  2135. free(text);
  2136. return PyImagingNew(im);
  2137. failed:
  2138. free(text);
  2139. ImagingDelete(im);
  2140. Py_RETURN_NONE;
  2141. }
  2142. static PyObject*
  2143. _font_getsize(ImagingFontObject* self, PyObject* args)
  2144. {
  2145. unsigned char* text;
  2146. PyObject* encoded_string;
  2147. PyObject* val;
  2148. if (!PyArg_ParseTuple(args, "O:getsize", &encoded_string))
  2149. return NULL;
  2150. _font_text_asBytes(encoded_string, &text);
  2151. if (!text) {
  2152. return NULL;
  2153. }
  2154. val = Py_BuildValue("ii", textwidth(self, text), self->ysize);
  2155. free(text);
  2156. return val;
  2157. }
  2158. static struct PyMethodDef _font_methods[] = {
  2159. {"getmask", (PyCFunction)_font_getmask, 1},
  2160. {"getsize", (PyCFunction)_font_getsize, 1},
  2161. {NULL, NULL} /* sentinel */
  2162. };
  2163. /* -------------------------------------------------------------------- */
  2164. static PyObject*
  2165. _draw_new(PyObject* self_, PyObject* args)
  2166. {
  2167. ImagingDrawObject *self;
  2168. ImagingObject* imagep;
  2169. int blend = 0;
  2170. if (!PyArg_ParseTuple(args, "O!|i", &Imaging_Type, &imagep, &blend))
  2171. return NULL;
  2172. self = PyObject_New(ImagingDrawObject, &ImagingDraw_Type);
  2173. if (self == NULL)
  2174. return NULL;
  2175. /* keep a reference to the image object */
  2176. Py_INCREF(imagep);
  2177. self->image = imagep;
  2178. self->ink[0] = self->ink[1] = self->ink[2] = self->ink[3] = 0;
  2179. self->blend = blend;
  2180. return (PyObject*) self;
  2181. }
  2182. static void
  2183. _draw_dealloc(ImagingDrawObject* self)
  2184. {
  2185. Py_XDECREF(self->image);
  2186. PyObject_Del(self);
  2187. }
  2188. extern Py_ssize_t PyPath_Flatten(PyObject* data, double **xy);
  2189. static PyObject*
  2190. _draw_ink(ImagingDrawObject* self, PyObject* args)
  2191. {
  2192. INT32 ink = 0;
  2193. PyObject* color;
  2194. if (!PyArg_ParseTuple(args, "O", &color))
  2195. return NULL;
  2196. if (!getink(color, self->image->image, (char*) &ink))
  2197. return NULL;
  2198. return PyInt_FromLong((int) ink);
  2199. }
  2200. static PyObject*
  2201. _draw_arc(ImagingDrawObject* self, PyObject* args)
  2202. {
  2203. double* xy;
  2204. Py_ssize_t n;
  2205. PyObject* data;
  2206. int ink;
  2207. int width = 0;
  2208. float start, end;
  2209. int op = 0;
  2210. if (!PyArg_ParseTuple(args, "Offi|ii", &data, &start, &end, &ink, &width))
  2211. return NULL;
  2212. n = PyPath_Flatten(data, &xy);
  2213. if (n < 0)
  2214. return NULL;
  2215. if (n != 2) {
  2216. PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
  2217. free(xy);
  2218. return NULL;
  2219. }
  2220. n = ImagingDrawArc(self->image->image,
  2221. (int) xy[0], (int) xy[1],
  2222. (int) xy[2], (int) xy[3],
  2223. start, end, &ink, width, op
  2224. );
  2225. free(xy);
  2226. if (n < 0)
  2227. return NULL;
  2228. Py_INCREF(Py_None);
  2229. return Py_None;
  2230. }
  2231. static PyObject*
  2232. _draw_bitmap(ImagingDrawObject* self, PyObject* args)
  2233. {
  2234. double *xy;
  2235. Py_ssize_t n;
  2236. PyObject *data;
  2237. ImagingObject* bitmap;
  2238. int ink;
  2239. if (!PyArg_ParseTuple(args, "OO!i", &data, &Imaging_Type, &bitmap, &ink))
  2240. return NULL;
  2241. n = PyPath_Flatten(data, &xy);
  2242. if (n < 0)
  2243. return NULL;
  2244. if (n != 1) {
  2245. PyErr_SetString(PyExc_TypeError,
  2246. "coordinate list must contain exactly 1 coordinate"
  2247. );
  2248. free(xy);
  2249. return NULL;
  2250. }
  2251. n = ImagingDrawBitmap(
  2252. self->image->image, (int) xy[0], (int) xy[1], bitmap->image,
  2253. &ink, self->blend
  2254. );
  2255. free(xy);
  2256. if (n < 0)
  2257. return NULL;
  2258. Py_INCREF(Py_None);
  2259. return Py_None;
  2260. }
  2261. static PyObject*
  2262. _draw_chord(ImagingDrawObject* self, PyObject* args)
  2263. {
  2264. double* xy;
  2265. Py_ssize_t n;
  2266. PyObject* data;
  2267. int ink, fill;
  2268. int width = 0;
  2269. float start, end;
  2270. if (!PyArg_ParseTuple(args, "Offii|i",
  2271. &data, &start, &end, &ink, &fill, &width))
  2272. return NULL;
  2273. n = PyPath_Flatten(data, &xy);
  2274. if (n < 0)
  2275. return NULL;
  2276. if (n != 2) {
  2277. PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
  2278. free(xy);
  2279. return NULL;
  2280. }
  2281. n = ImagingDrawChord(self->image->image,
  2282. (int) xy[0], (int) xy[1],
  2283. (int) xy[2], (int) xy[3],
  2284. start, end, &ink, fill, width, self->blend
  2285. );
  2286. free(xy);
  2287. if (n < 0)
  2288. return NULL;
  2289. Py_INCREF(Py_None);
  2290. return Py_None;
  2291. }
  2292. static PyObject*
  2293. _draw_ellipse(ImagingDrawObject* self, PyObject* args)
  2294. {
  2295. double* xy;
  2296. Py_ssize_t n;
  2297. PyObject* data;
  2298. int ink;
  2299. int fill = 0;
  2300. int width = 0;
  2301. if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width))
  2302. return NULL;
  2303. n = PyPath_Flatten(data, &xy);
  2304. if (n < 0)
  2305. return NULL;
  2306. if (n != 2) {
  2307. PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
  2308. free(xy);
  2309. return NULL;
  2310. }
  2311. n = ImagingDrawEllipse(self->image->image,
  2312. (int) xy[0], (int) xy[1],
  2313. (int) xy[2], (int) xy[3],
  2314. &ink, fill, width, self->blend
  2315. );
  2316. free(xy);
  2317. if (n < 0)
  2318. return NULL;
  2319. Py_INCREF(Py_None);
  2320. return Py_None;
  2321. }
  2322. static PyObject*
  2323. _draw_lines(ImagingDrawObject* self, PyObject* args)
  2324. {
  2325. double *xy;
  2326. Py_ssize_t i, n;
  2327. PyObject *data;
  2328. int ink;
  2329. int width = 0;
  2330. if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &width))
  2331. return NULL;
  2332. n = PyPath_Flatten(data, &xy);
  2333. if (n < 0)
  2334. return NULL;
  2335. if (width <= 1) {
  2336. double *p = NULL;
  2337. for (i = 0; i < n-1; i++) {
  2338. p = &xy[i+i];
  2339. if (ImagingDrawLine(
  2340. self->image->image,
  2341. (int) p[0], (int) p[1], (int) p[2], (int) p[3],
  2342. &ink, self->blend) < 0) {
  2343. free(xy);
  2344. return NULL;
  2345. }
  2346. }
  2347. if (p) /* draw last point */
  2348. ImagingDrawPoint(
  2349. self->image->image,
  2350. (int) p[2], (int) p[3],
  2351. &ink, self->blend
  2352. );
  2353. } else {
  2354. for (i = 0; i < n-1; i++) {
  2355. double *p = &xy[i+i];
  2356. if (ImagingDrawWideLine(
  2357. self->image->image,
  2358. (int) p[0], (int) p[1], (int) p[2], (int) p[3],
  2359. &ink, width, self->blend) < 0) {
  2360. free(xy);
  2361. return NULL;
  2362. }
  2363. }
  2364. }
  2365. free(xy);
  2366. Py_INCREF(Py_None);
  2367. return Py_None;
  2368. }
  2369. static PyObject*
  2370. _draw_points(ImagingDrawObject* self, PyObject* args)
  2371. {
  2372. double *xy;
  2373. Py_ssize_t i, n;
  2374. PyObject *data;
  2375. int ink;
  2376. if (!PyArg_ParseTuple(args, "Oi", &data, &ink))
  2377. return NULL;
  2378. n = PyPath_Flatten(data, &xy);
  2379. if (n < 0)
  2380. return NULL;
  2381. for (i = 0; i < n; i++) {
  2382. double *p = &xy[i+i];
  2383. if (ImagingDrawPoint(self->image->image, (int) p[0], (int) p[1],
  2384. &ink, self->blend) < 0) {
  2385. free(xy);
  2386. return NULL;
  2387. }
  2388. }
  2389. free(xy);
  2390. Py_INCREF(Py_None);
  2391. return Py_None;
  2392. }
  2393. #ifdef WITH_ARROW
  2394. /* from outline.c */
  2395. extern ImagingOutline PyOutline_AsOutline(PyObject* outline);
  2396. static PyObject*
  2397. _draw_outline(ImagingDrawObject* self, PyObject* args)
  2398. {
  2399. ImagingOutline outline;
  2400. PyObject* outline_;
  2401. int ink;
  2402. int fill = 0;
  2403. if (!PyArg_ParseTuple(args, "Oi|i", &outline_, &ink, &fill))
  2404. return NULL;
  2405. outline = PyOutline_AsOutline(outline_);
  2406. if (!outline) {
  2407. PyErr_SetString(PyExc_TypeError, "expected outline object");
  2408. return NULL;
  2409. }
  2410. if (ImagingDrawOutline(self->image->image, outline,
  2411. &ink, fill, self->blend) < 0)
  2412. return NULL;
  2413. Py_INCREF(Py_None);
  2414. return Py_None;
  2415. }
  2416. #endif
  2417. static PyObject*
  2418. _draw_pieslice(ImagingDrawObject* self, PyObject* args)
  2419. {
  2420. double* xy;
  2421. Py_ssize_t n;
  2422. PyObject* data;
  2423. int ink, fill;
  2424. int width = 0;
  2425. float start, end;
  2426. if (!PyArg_ParseTuple(args, "Offii|i", &data, &start, &end, &ink, &fill, &width))
  2427. return NULL;
  2428. n = PyPath_Flatten(data, &xy);
  2429. if (n < 0)
  2430. return NULL;
  2431. if (n != 2) {
  2432. PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
  2433. free(xy);
  2434. return NULL;
  2435. }
  2436. n = ImagingDrawPieslice(self->image->image,
  2437. (int) xy[0], (int) xy[1],
  2438. (int) xy[2], (int) xy[3],
  2439. start, end, &ink, fill, width, self->blend
  2440. );
  2441. free(xy);
  2442. if (n < 0)
  2443. return NULL;
  2444. Py_INCREF(Py_None);
  2445. return Py_None;
  2446. }
  2447. static PyObject*
  2448. _draw_polygon(ImagingDrawObject* self, PyObject* args)
  2449. {
  2450. double *xy;
  2451. int *ixy;
  2452. Py_ssize_t n, i;
  2453. PyObject* data;
  2454. int ink;
  2455. int fill = 0;
  2456. if (!PyArg_ParseTuple(args, "Oi|i", &data, &ink, &fill))
  2457. return NULL;
  2458. n = PyPath_Flatten(data, &xy);
  2459. if (n < 0)
  2460. return NULL;
  2461. if (n < 2) {
  2462. PyErr_SetString(PyExc_TypeError,
  2463. "coordinate list must contain at least 2 coordinates"
  2464. );
  2465. free(xy);
  2466. return NULL;
  2467. }
  2468. /* Copy list of vertices to array */
  2469. ixy = (int*) calloc(n, 2 * sizeof(int));
  2470. for (i = 0; i < n; i++) {
  2471. ixy[i+i] = (int) xy[i+i];
  2472. ixy[i+i+1] = (int) xy[i+i+1];
  2473. }
  2474. free(xy);
  2475. if (ImagingDrawPolygon(self->image->image, n, ixy,
  2476. &ink, fill, self->blend) < 0) {
  2477. free(ixy);
  2478. return NULL;
  2479. }
  2480. free(ixy);
  2481. Py_INCREF(Py_None);
  2482. return Py_None;
  2483. }
  2484. static PyObject*
  2485. _draw_rectangle(ImagingDrawObject* self, PyObject* args)
  2486. {
  2487. double* xy;
  2488. Py_ssize_t n;
  2489. PyObject* data;
  2490. int ink;
  2491. int fill = 0;
  2492. int width = 0;
  2493. if (!PyArg_ParseTuple(args, "Oi|ii", &data, &ink, &fill, &width))
  2494. return NULL;
  2495. n = PyPath_Flatten(data, &xy);
  2496. if (n < 0)
  2497. return NULL;
  2498. if (n != 2) {
  2499. PyErr_SetString(PyExc_TypeError, must_be_two_coordinates);
  2500. free(xy);
  2501. return NULL;
  2502. }
  2503. n = ImagingDrawRectangle(self->image->image,
  2504. (int) xy[0], (int) xy[1],
  2505. (int) xy[2], (int) xy[3],
  2506. &ink, fill, width, self->blend
  2507. );
  2508. free(xy);
  2509. if (n < 0)
  2510. return NULL;
  2511. Py_INCREF(Py_None);
  2512. return Py_None;
  2513. }
  2514. static struct PyMethodDef _draw_methods[] = {
  2515. #ifdef WITH_IMAGEDRAW
  2516. /* Graphics (ImageDraw) */
  2517. {"draw_lines", (PyCFunction)_draw_lines, 1},
  2518. #ifdef WITH_ARROW
  2519. {"draw_outline", (PyCFunction)_draw_outline, 1},
  2520. #endif
  2521. {"draw_polygon", (PyCFunction)_draw_polygon, 1},
  2522. {"draw_rectangle", (PyCFunction)_draw_rectangle, 1},
  2523. {"draw_points", (PyCFunction)_draw_points, 1},
  2524. {"draw_arc", (PyCFunction)_draw_arc, 1},
  2525. {"draw_bitmap", (PyCFunction)_draw_bitmap, 1},
  2526. {"draw_chord", (PyCFunction)_draw_chord, 1},
  2527. {"draw_ellipse", (PyCFunction)_draw_ellipse, 1},
  2528. {"draw_pieslice", (PyCFunction)_draw_pieslice, 1},
  2529. {"draw_ink", (PyCFunction)_draw_ink, 1},
  2530. #endif
  2531. {NULL, NULL} /* sentinel */
  2532. };
  2533. #endif
  2534. static PyObject*
  2535. pixel_access_new(ImagingObject* imagep, PyObject* args)
  2536. {
  2537. PixelAccessObject *self;
  2538. int readonly = 0;
  2539. if (!PyArg_ParseTuple(args, "|i", &readonly))
  2540. return NULL;
  2541. self = PyObject_New(PixelAccessObject, &PixelAccess_Type);
  2542. if (self == NULL)
  2543. return NULL;
  2544. /* keep a reference to the image object */
  2545. Py_INCREF(imagep);
  2546. self->image = imagep;
  2547. self->readonly = readonly;
  2548. return (PyObject*) self;
  2549. }
  2550. static void
  2551. pixel_access_dealloc(PixelAccessObject* self)
  2552. {
  2553. Py_XDECREF(self->image);
  2554. PyObject_Del(self);
  2555. }
  2556. static PyObject *
  2557. pixel_access_getitem(PixelAccessObject *self, PyObject *xy)
  2558. {
  2559. int x, y;
  2560. if (_getxy(xy, &x, &y))
  2561. return NULL;
  2562. return getpixel(self->image->image, self->image->access, x, y);
  2563. }
  2564. static int
  2565. pixel_access_setitem(PixelAccessObject *self, PyObject *xy, PyObject *color)
  2566. {
  2567. Imaging im = self->image->image;
  2568. char ink[4];
  2569. int x, y;
  2570. if (self->readonly) {
  2571. (void) ImagingError_ValueError(readonly);
  2572. return -1;
  2573. }
  2574. if (_getxy(xy, &x, &y))
  2575. return -1;
  2576. if (x < 0) {
  2577. x = im->xsize + x;
  2578. }
  2579. if (y < 0) {
  2580. y = im->ysize + y;
  2581. }
  2582. if (x < 0 || x >= im->xsize || y < 0 || y >= im->ysize) {
  2583. PyErr_SetString(PyExc_IndexError, outside_image);
  2584. return -1;
  2585. }
  2586. if (!color) /* FIXME: raise exception? */
  2587. return 0;
  2588. if (!getink(color, im, ink))
  2589. return -1;
  2590. self->image->access->put_pixel(im, x, y, ink);
  2591. return 0;
  2592. }
  2593. /* -------------------------------------------------------------------- */
  2594. /* EFFECTS (experimental) */
  2595. /* -------------------------------------------------------------------- */
  2596. #ifdef WITH_EFFECTS
  2597. static PyObject*
  2598. _effect_mandelbrot(ImagingObject* self, PyObject* args)
  2599. {
  2600. int xsize = 512;
  2601. int ysize = 512;
  2602. double extent[4];
  2603. int quality = 100;
  2604. extent[0] = -3; extent[1] = -2.5;
  2605. extent[2] = 2; extent[3] = 2.5;
  2606. if (!PyArg_ParseTuple(args, "|(ii)(dddd)i", &xsize, &ysize,
  2607. &extent[0], &extent[1], &extent[2], &extent[3],
  2608. &quality))
  2609. return NULL;
  2610. return PyImagingNew(ImagingEffectMandelbrot(xsize, ysize, extent, quality));
  2611. }
  2612. static PyObject*
  2613. _effect_noise(ImagingObject* self, PyObject* args)
  2614. {
  2615. int xsize, ysize;
  2616. float sigma = 128;
  2617. if (!PyArg_ParseTuple(args, "(ii)|f", &xsize, &ysize, &sigma))
  2618. return NULL;
  2619. return PyImagingNew(ImagingEffectNoise(xsize, ysize, sigma));
  2620. }
  2621. static PyObject*
  2622. _effect_spread(ImagingObject* self, PyObject* args)
  2623. {
  2624. int dist;
  2625. if (!PyArg_ParseTuple(args, "i", &dist))
  2626. return NULL;
  2627. return PyImagingNew(ImagingEffectSpread(self->image, dist));
  2628. }
  2629. #endif
  2630. /* -------------------------------------------------------------------- */
  2631. /* UTILITIES */
  2632. /* -------------------------------------------------------------------- */
  2633. static PyObject*
  2634. _getcodecstatus(PyObject* self, PyObject* args)
  2635. {
  2636. int status;
  2637. char* msg;
  2638. if (!PyArg_ParseTuple(args, "i", &status))
  2639. return NULL;
  2640. switch (status) {
  2641. case IMAGING_CODEC_OVERRUN:
  2642. msg = "buffer overrun"; break;
  2643. case IMAGING_CODEC_BROKEN:
  2644. msg = "broken data stream"; break;
  2645. case IMAGING_CODEC_UNKNOWN:
  2646. msg = "unrecognized data stream contents"; break;
  2647. case IMAGING_CODEC_CONFIG:
  2648. msg = "codec configuration error"; break;
  2649. case IMAGING_CODEC_MEMORY:
  2650. msg = "out of memory"; break;
  2651. default:
  2652. Py_RETURN_NONE;
  2653. }
  2654. return PyUnicode_FromString(msg);
  2655. }
  2656. /* -------------------------------------------------------------------- */
  2657. /* DEBUGGING HELPERS */
  2658. /* -------------------------------------------------------------------- */
  2659. static PyObject*
  2660. _save_ppm(ImagingObject* self, PyObject* args)
  2661. {
  2662. char* filename;
  2663. if (!PyArg_ParseTuple(args, "s", &filename))
  2664. return NULL;
  2665. if (!ImagingSavePPM(self->image, filename))
  2666. return NULL;
  2667. Py_INCREF(Py_None);
  2668. return Py_None;
  2669. }
  2670. /* -------------------------------------------------------------------- */
  2671. /* methods */
  2672. static struct PyMethodDef methods[] = {
  2673. /* Put commonly used methods first */
  2674. {"getpixel", (PyCFunction)_getpixel, 1},
  2675. {"putpixel", (PyCFunction)_putpixel, 1},
  2676. {"pixel_access", (PyCFunction)pixel_access_new, 1},
  2677. /* Standard processing methods (Image) */
  2678. {"color_lut_3d", (PyCFunction)_color_lut_3d, 1},
  2679. {"convert", (PyCFunction)_convert, 1},
  2680. {"convert2", (PyCFunction)_convert2, 1},
  2681. {"convert_matrix", (PyCFunction)_convert_matrix, 1},
  2682. {"convert_transparent", (PyCFunction)_convert_transparent, 1},
  2683. {"copy", (PyCFunction)_copy, 1},
  2684. {"crop", (PyCFunction)_crop, 1},
  2685. {"expand", (PyCFunction)_expand_image, 1},
  2686. {"filter", (PyCFunction)_filter, 1},
  2687. {"histogram", (PyCFunction)_histogram, 1},
  2688. {"entropy", (PyCFunction)_entropy, 1},
  2689. #ifdef WITH_MODEFILTER
  2690. {"modefilter", (PyCFunction)_modefilter, 1},
  2691. #endif
  2692. {"offset", (PyCFunction)_offset, 1},
  2693. {"paste", (PyCFunction)_paste, 1},
  2694. {"point", (PyCFunction)_point, 1},
  2695. {"point_transform", (PyCFunction)_point_transform, 1},
  2696. {"putdata", (PyCFunction)_putdata, 1},
  2697. #ifdef WITH_QUANTIZE
  2698. {"quantize", (PyCFunction)_quantize, 1},
  2699. #endif
  2700. #ifdef WITH_RANKFILTER
  2701. {"rankfilter", (PyCFunction)_rankfilter, 1},
  2702. #endif
  2703. {"resize", (PyCFunction)_resize, 1},
  2704. {"transpose", (PyCFunction)_transpose, 1},
  2705. {"transform2", (PyCFunction)_transform2, 1},
  2706. {"isblock", (PyCFunction)_isblock, 1},
  2707. {"getbbox", (PyCFunction)_getbbox, 1},
  2708. {"getcolors", (PyCFunction)_getcolors, 1},
  2709. {"getextrema", (PyCFunction)_getextrema, 1},
  2710. {"getprojection", (PyCFunction)_getprojection, 1},
  2711. {"getband", (PyCFunction)_getband, 1},
  2712. {"putband", (PyCFunction)_putband, 1},
  2713. {"split", (PyCFunction)_split, 1},
  2714. {"fillband", (PyCFunction)_fillband, 1},
  2715. {"setmode", (PyCFunction)im_setmode, 1},
  2716. {"getpalette", (PyCFunction)_getpalette, 1},
  2717. {"getpalettemode", (PyCFunction)_getpalettemode, 1},
  2718. {"putpalette", (PyCFunction)_putpalette, 1},
  2719. {"putpalettealpha", (PyCFunction)_putpalettealpha, 1},
  2720. {"putpalettealphas", (PyCFunction)_putpalettealphas, 1},
  2721. #ifdef WITH_IMAGECHOPS
  2722. /* Channel operations (ImageChops) */
  2723. {"chop_invert", (PyCFunction)_chop_invert, 1},
  2724. {"chop_lighter", (PyCFunction)_chop_lighter, 1},
  2725. {"chop_darker", (PyCFunction)_chop_darker, 1},
  2726. {"chop_difference", (PyCFunction)_chop_difference, 1},
  2727. {"chop_multiply", (PyCFunction)_chop_multiply, 1},
  2728. {"chop_screen", (PyCFunction)_chop_screen, 1},
  2729. {"chop_add", (PyCFunction)_chop_add, 1},
  2730. {"chop_subtract", (PyCFunction)_chop_subtract, 1},
  2731. {"chop_add_modulo", (PyCFunction)_chop_add_modulo, 1},
  2732. {"chop_subtract_modulo", (PyCFunction)_chop_subtract_modulo, 1},
  2733. {"chop_and", (PyCFunction)_chop_and, 1},
  2734. {"chop_or", (PyCFunction)_chop_or, 1},
  2735. {"chop_xor", (PyCFunction)_chop_xor, 1},
  2736. #endif
  2737. #ifdef WITH_UNSHARPMASK
  2738. /* Kevin Cazabon's unsharpmask extension */
  2739. {"gaussian_blur", (PyCFunction)_gaussian_blur, 1},
  2740. {"unsharp_mask", (PyCFunction)_unsharp_mask, 1},
  2741. #endif
  2742. {"box_blur", (PyCFunction)_box_blur, 1},
  2743. #ifdef WITH_EFFECTS
  2744. /* Special effects */
  2745. {"effect_spread", (PyCFunction)_effect_spread, 1},
  2746. #endif
  2747. /* Misc. */
  2748. {"new_block", (PyCFunction)_new_block, 1},
  2749. {"save_ppm", (PyCFunction)_save_ppm, 1},
  2750. {NULL, NULL} /* sentinel */
  2751. };
  2752. /* attributes */
  2753. static PyObject*
  2754. _getattr_mode(ImagingObject* self, void* closure)
  2755. {
  2756. return PyUnicode_FromString(self->image->mode);
  2757. }
  2758. static PyObject*
  2759. _getattr_size(ImagingObject* self, void* closure)
  2760. {
  2761. return Py_BuildValue("ii", self->image->xsize, self->image->ysize);
  2762. }
  2763. static PyObject*
  2764. _getattr_bands(ImagingObject* self, void* closure)
  2765. {
  2766. return PyInt_FromLong(self->image->bands);
  2767. }
  2768. static PyObject*
  2769. _getattr_id(ImagingObject* self, void* closure)
  2770. {
  2771. return PyInt_FromSsize_t((Py_ssize_t) self->image);
  2772. }
  2773. static PyObject*
  2774. _getattr_ptr(ImagingObject* self, void* closure)
  2775. {
  2776. return PyCapsule_New(self->image, IMAGING_MAGIC, NULL);
  2777. }
  2778. static PyObject*
  2779. _getattr_unsafe_ptrs(ImagingObject* self, void* closure)
  2780. {
  2781. return Py_BuildValue("(sn)(sn)(sn)",
  2782. "image8", self->image->image8,
  2783. "image32", self->image->image32,
  2784. "image", self->image->image
  2785. );
  2786. };
  2787. static struct PyGetSetDef getsetters[] = {
  2788. { "mode", (getter) _getattr_mode },
  2789. { "size", (getter) _getattr_size },
  2790. { "bands", (getter) _getattr_bands },
  2791. { "id", (getter) _getattr_id },
  2792. { "ptr", (getter) _getattr_ptr },
  2793. { "unsafe_ptrs", (getter) _getattr_unsafe_ptrs },
  2794. { NULL }
  2795. };
  2796. /* basic sequence semantics */
  2797. static Py_ssize_t
  2798. image_length(ImagingObject *self)
  2799. {
  2800. Imaging im = self->image;
  2801. return (Py_ssize_t) im->xsize * im->ysize;
  2802. }
  2803. static PyObject *
  2804. image_item(ImagingObject *self, Py_ssize_t i)
  2805. {
  2806. int x, y;
  2807. Imaging im = self->image;
  2808. if (im->xsize > 0) {
  2809. x = i % im->xsize;
  2810. y = i / im->xsize;
  2811. } else
  2812. x = y = 0; /* leave it to getpixel to raise an exception */
  2813. return getpixel(im, self->access, x, y);
  2814. }
  2815. static PySequenceMethods image_as_sequence = {
  2816. (lenfunc) image_length, /*sq_length*/
  2817. (binaryfunc) NULL, /*sq_concat*/
  2818. (ssizeargfunc) NULL, /*sq_repeat*/
  2819. (ssizeargfunc) image_item, /*sq_item*/
  2820. (ssizessizeargfunc) NULL, /*sq_slice*/
  2821. (ssizeobjargproc) NULL, /*sq_ass_item*/
  2822. (ssizessizeobjargproc) NULL, /*sq_ass_slice*/
  2823. };
  2824. /* type description */
  2825. static PyTypeObject Imaging_Type = {
  2826. PyVarObject_HEAD_INIT(NULL, 0)
  2827. "ImagingCore", /*tp_name*/
  2828. sizeof(ImagingObject), /*tp_size*/
  2829. 0, /*tp_itemsize*/
  2830. /* methods */
  2831. (destructor)_dealloc, /*tp_dealloc*/
  2832. 0, /*tp_print*/
  2833. 0, /*tp_getattr*/
  2834. 0, /*tp_setattr*/
  2835. 0, /*tp_compare*/
  2836. 0, /*tp_repr*/
  2837. 0, /*tp_as_number */
  2838. &image_as_sequence, /*tp_as_sequence */
  2839. 0, /*tp_as_mapping */
  2840. 0, /*tp_hash*/
  2841. 0, /*tp_call*/
  2842. 0, /*tp_str*/
  2843. 0, /*tp_getattro*/
  2844. 0, /*tp_setattro*/
  2845. 0, /*tp_as_buffer*/
  2846. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  2847. 0, /*tp_doc*/
  2848. 0, /*tp_traverse*/
  2849. 0, /*tp_clear*/
  2850. 0, /*tp_richcompare*/
  2851. 0, /*tp_weaklistoffset*/
  2852. 0, /*tp_iter*/
  2853. 0, /*tp_iternext*/
  2854. methods, /*tp_methods*/
  2855. 0, /*tp_members*/
  2856. getsetters, /*tp_getset*/
  2857. };
  2858. #ifdef WITH_IMAGEDRAW
  2859. static PyTypeObject ImagingFont_Type = {
  2860. PyVarObject_HEAD_INIT(NULL, 0)
  2861. "ImagingFont", /*tp_name*/
  2862. sizeof(ImagingFontObject), /*tp_size*/
  2863. 0, /*tp_itemsize*/
  2864. /* methods */
  2865. (destructor)_font_dealloc, /*tp_dealloc*/
  2866. 0, /*tp_print*/
  2867. 0, /*tp_getattr*/
  2868. 0, /*tp_setattr*/
  2869. 0, /*tp_compare*/
  2870. 0, /*tp_repr*/
  2871. 0, /*tp_as_number */
  2872. 0, /*tp_as_sequence */
  2873. 0, /*tp_as_mapping */
  2874. 0, /*tp_hash*/
  2875. 0, /*tp_call*/
  2876. 0, /*tp_str*/
  2877. 0, /*tp_getattro*/
  2878. 0, /*tp_setattro*/
  2879. 0, /*tp_as_buffer*/
  2880. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  2881. 0, /*tp_doc*/
  2882. 0, /*tp_traverse*/
  2883. 0, /*tp_clear*/
  2884. 0, /*tp_richcompare*/
  2885. 0, /*tp_weaklistoffset*/
  2886. 0, /*tp_iter*/
  2887. 0, /*tp_iternext*/
  2888. _font_methods, /*tp_methods*/
  2889. 0, /*tp_members*/
  2890. 0, /*tp_getset*/
  2891. };
  2892. static PyTypeObject ImagingDraw_Type = {
  2893. PyVarObject_HEAD_INIT(NULL, 0)
  2894. "ImagingDraw", /*tp_name*/
  2895. sizeof(ImagingDrawObject), /*tp_size*/
  2896. 0, /*tp_itemsize*/
  2897. /* methods */
  2898. (destructor)_draw_dealloc, /*tp_dealloc*/
  2899. 0, /*tp_print*/
  2900. 0, /*tp_getattr*/
  2901. 0, /*tp_setattr*/
  2902. 0, /*tp_compare*/
  2903. 0, /*tp_repr*/
  2904. 0, /*tp_as_number */
  2905. 0, /*tp_as_sequence */
  2906. 0, /*tp_as_mapping */
  2907. 0, /*tp_hash*/
  2908. 0, /*tp_call*/
  2909. 0, /*tp_str*/
  2910. 0, /*tp_getattro*/
  2911. 0, /*tp_setattro*/
  2912. 0, /*tp_as_buffer*/
  2913. Py_TPFLAGS_DEFAULT, /*tp_flags*/
  2914. 0, /*tp_doc*/
  2915. 0, /*tp_traverse*/
  2916. 0, /*tp_clear*/
  2917. 0, /*tp_richcompare*/
  2918. 0, /*tp_weaklistoffset*/
  2919. 0, /*tp_iter*/
  2920. 0, /*tp_iternext*/
  2921. _draw_methods, /*tp_methods*/
  2922. 0, /*tp_members*/
  2923. 0, /*tp_getset*/
  2924. };
  2925. #endif
  2926. static PyMappingMethods pixel_access_as_mapping = {
  2927. (lenfunc) NULL, /*mp_length*/
  2928. (binaryfunc) pixel_access_getitem, /*mp_subscript*/
  2929. (objobjargproc) pixel_access_setitem, /*mp_ass_subscript*/
  2930. };
  2931. /* type description */
  2932. static PyTypeObject PixelAccess_Type = {
  2933. PyVarObject_HEAD_INIT(NULL, 0)
  2934. "PixelAccess", sizeof(PixelAccessObject), 0,
  2935. /* methods */
  2936. (destructor)pixel_access_dealloc, /*tp_dealloc*/
  2937. 0, /*tp_print*/
  2938. 0, /*tp_getattr*/
  2939. 0, /*tp_setattr*/
  2940. 0, /*tp_compare*/
  2941. 0, /*tp_repr*/
  2942. 0, /*tp_as_number */
  2943. 0, /*tp_as_sequence */
  2944. &pixel_access_as_mapping, /*tp_as_mapping */
  2945. 0 /*tp_hash*/
  2946. };
  2947. /* -------------------------------------------------------------------- */
  2948. static PyObject*
  2949. _get_stats(PyObject* self, PyObject* args)
  2950. {
  2951. PyObject* d;
  2952. ImagingMemoryArena arena = &ImagingDefaultArena;
  2953. if (!PyArg_ParseTuple(args, ":get_stats"))
  2954. return NULL;
  2955. d = PyDict_New();
  2956. if ( ! d)
  2957. return NULL;
  2958. PyDict_SetItemString(d, "new_count",
  2959. PyInt_FromLong(arena->stats_new_count));
  2960. PyDict_SetItemString(d, "allocated_blocks",
  2961. PyInt_FromLong(arena->stats_allocated_blocks));
  2962. PyDict_SetItemString(d, "reused_blocks",
  2963. PyInt_FromLong(arena->stats_reused_blocks));
  2964. PyDict_SetItemString(d, "reallocated_blocks",
  2965. PyInt_FromLong(arena->stats_reallocated_blocks));
  2966. PyDict_SetItemString(d, "freed_blocks",
  2967. PyInt_FromLong(arena->stats_freed_blocks));
  2968. PyDict_SetItemString(d, "blocks_cached",
  2969. PyInt_FromLong(arena->blocks_cached));
  2970. return d;
  2971. }
  2972. static PyObject*
  2973. _reset_stats(PyObject* self, PyObject* args)
  2974. {
  2975. ImagingMemoryArena arena = &ImagingDefaultArena;
  2976. if (!PyArg_ParseTuple(args, ":reset_stats"))
  2977. return NULL;
  2978. arena->stats_new_count = 0;
  2979. arena->stats_allocated_blocks = 0;
  2980. arena->stats_reused_blocks = 0;
  2981. arena->stats_reallocated_blocks = 0;
  2982. arena->stats_freed_blocks = 0;
  2983. Py_INCREF(Py_None);
  2984. return Py_None;
  2985. }
  2986. static PyObject*
  2987. _get_alignment(PyObject* self, PyObject* args)
  2988. {
  2989. if (!PyArg_ParseTuple(args, ":get_alignment"))
  2990. return NULL;
  2991. return PyInt_FromLong(ImagingDefaultArena.alignment);
  2992. }
  2993. static PyObject*
  2994. _get_block_size(PyObject* self, PyObject* args)
  2995. {
  2996. if (!PyArg_ParseTuple(args, ":get_block_size"))
  2997. return NULL;
  2998. return PyInt_FromLong(ImagingDefaultArena.block_size);
  2999. }
  3000. static PyObject*
  3001. _get_blocks_max(PyObject* self, PyObject* args)
  3002. {
  3003. if (!PyArg_ParseTuple(args, ":get_blocks_max"))
  3004. return NULL;
  3005. return PyInt_FromLong(ImagingDefaultArena.blocks_max);
  3006. }
  3007. static PyObject*
  3008. _set_alignment(PyObject* self, PyObject* args)
  3009. {
  3010. int alignment;
  3011. if (!PyArg_ParseTuple(args, "i:set_alignment", &alignment))
  3012. return NULL;
  3013. if (alignment < 1 || alignment > 128) {
  3014. PyErr_SetString(PyExc_ValueError, "alignment should be from 1 to 128");
  3015. return NULL;
  3016. }
  3017. /* Is power of two */
  3018. if (alignment & (alignment - 1)) {
  3019. PyErr_SetString(PyExc_ValueError, "alignment should be power of two");
  3020. return NULL;
  3021. }
  3022. ImagingDefaultArena.alignment = alignment;
  3023. Py_INCREF(Py_None);
  3024. return Py_None;
  3025. }
  3026. static PyObject*
  3027. _set_block_size(PyObject* self, PyObject* args)
  3028. {
  3029. int block_size;
  3030. if (!PyArg_ParseTuple(args, "i:set_block_size", &block_size))
  3031. return NULL;
  3032. if (block_size <= 0) {
  3033. PyErr_SetString(PyExc_ValueError,
  3034. "block_size should be greater than 0");
  3035. return NULL;
  3036. }
  3037. if (block_size & 0xfff) {
  3038. PyErr_SetString(PyExc_ValueError,
  3039. "block_size should be multiple of 4096");
  3040. return NULL;
  3041. }
  3042. ImagingDefaultArena.block_size = block_size;
  3043. Py_INCREF(Py_None);
  3044. return Py_None;
  3045. }
  3046. static PyObject*
  3047. _set_blocks_max(PyObject* self, PyObject* args)
  3048. {
  3049. int blocks_max;
  3050. if (!PyArg_ParseTuple(args, "i:set_blocks_max", &blocks_max))
  3051. return NULL;
  3052. if (blocks_max < 0) {
  3053. PyErr_SetString(PyExc_ValueError,
  3054. "blocks_max should be greater than 0");
  3055. return NULL;
  3056. }
  3057. else if ( blocks_max > SIZE_MAX/sizeof(ImagingDefaultArena.blocks_pool[0])) {
  3058. PyErr_SetString(PyExc_ValueError,
  3059. "blocks_max is too large");
  3060. return NULL;
  3061. }
  3062. if ( ! ImagingMemorySetBlocksMax(&ImagingDefaultArena, blocks_max)) {
  3063. ImagingError_MemoryError();
  3064. return NULL;
  3065. }
  3066. Py_INCREF(Py_None);
  3067. return Py_None;
  3068. }
  3069. static PyObject*
  3070. _clear_cache(PyObject* self, PyObject* args)
  3071. {
  3072. int i = 0;
  3073. if (!PyArg_ParseTuple(args, "|i:clear_cache", &i))
  3074. return NULL;
  3075. ImagingMemoryClearCache(&ImagingDefaultArena, i);
  3076. Py_INCREF(Py_None);
  3077. return Py_None;
  3078. }
  3079. /* -------------------------------------------------------------------- */
  3080. /* FIXME: this is something of a mess. Should replace this with
  3081. pluggable codecs, but not before PIL 1.2 */
  3082. /* Decoders (in decode.c) */
  3083. extern PyObject* PyImaging_BcnDecoderNew(PyObject* self, PyObject* args);
  3084. extern PyObject* PyImaging_BitDecoderNew(PyObject* self, PyObject* args);
  3085. extern PyObject* PyImaging_FliDecoderNew(PyObject* self, PyObject* args);
  3086. extern PyObject* PyImaging_GifDecoderNew(PyObject* self, PyObject* args);
  3087. extern PyObject* PyImaging_HexDecoderNew(PyObject* self, PyObject* args);
  3088. extern PyObject* PyImaging_JpegDecoderNew(PyObject* self, PyObject* args);
  3089. extern PyObject* PyImaging_Jpeg2KDecoderNew(PyObject* self, PyObject* args);
  3090. extern PyObject* PyImaging_LibTiffDecoderNew(PyObject* self, PyObject* args);
  3091. extern PyObject* PyImaging_PackbitsDecoderNew(PyObject* self, PyObject* args);
  3092. extern PyObject* PyImaging_PcdDecoderNew(PyObject* self, PyObject* args);
  3093. extern PyObject* PyImaging_PcxDecoderNew(PyObject* self, PyObject* args);
  3094. extern PyObject* PyImaging_RawDecoderNew(PyObject* self, PyObject* args);
  3095. extern PyObject* PyImaging_SgiRleDecoderNew(PyObject* self, PyObject* args);
  3096. extern PyObject* PyImaging_SunRleDecoderNew(PyObject* self, PyObject* args);
  3097. extern PyObject* PyImaging_TgaRleDecoderNew(PyObject* self, PyObject* args);
  3098. extern PyObject* PyImaging_XbmDecoderNew(PyObject* self, PyObject* args);
  3099. extern PyObject* PyImaging_ZipDecoderNew(PyObject* self, PyObject* args);
  3100. /* Encoders (in encode.c) */
  3101. extern PyObject* PyImaging_EpsEncoderNew(PyObject* self, PyObject* args);
  3102. extern PyObject* PyImaging_GifEncoderNew(PyObject* self, PyObject* args);
  3103. extern PyObject* PyImaging_JpegEncoderNew(PyObject* self, PyObject* args);
  3104. extern PyObject* PyImaging_Jpeg2KEncoderNew(PyObject* self, PyObject* args);
  3105. extern PyObject* PyImaging_PcxEncoderNew(PyObject* self, PyObject* args);
  3106. extern PyObject* PyImaging_RawEncoderNew(PyObject* self, PyObject* args);
  3107. extern PyObject* PyImaging_TgaRleEncoderNew(PyObject* self, PyObject* args);
  3108. extern PyObject* PyImaging_XbmEncoderNew(PyObject* self, PyObject* args);
  3109. extern PyObject* PyImaging_ZipEncoderNew(PyObject* self, PyObject* args);
  3110. extern PyObject* PyImaging_LibTiffEncoderNew(PyObject* self, PyObject* args);
  3111. /* Display support etc (in display.c) */
  3112. #ifdef _WIN32
  3113. extern PyObject* PyImaging_CreateWindowWin32(PyObject* self, PyObject* args);
  3114. extern PyObject* PyImaging_DisplayWin32(PyObject* self, PyObject* args);
  3115. extern PyObject* PyImaging_DisplayModeWin32(PyObject* self, PyObject* args);
  3116. extern PyObject* PyImaging_GrabScreenWin32(PyObject* self, PyObject* args);
  3117. extern PyObject* PyImaging_GrabClipboardWin32(PyObject* self, PyObject* args);
  3118. extern PyObject* PyImaging_ListWindowsWin32(PyObject* self, PyObject* args);
  3119. extern PyObject* PyImaging_EventLoopWin32(PyObject* self, PyObject* args);
  3120. extern PyObject* PyImaging_DrawWmf(PyObject* self, PyObject* args);
  3121. #endif
  3122. /* Experimental path stuff (in path.c) */
  3123. extern PyObject* PyPath_Create(ImagingObject* self, PyObject* args);
  3124. /* Experimental outline stuff (in outline.c) */
  3125. extern PyObject* PyOutline_Create(ImagingObject* self, PyObject* args);
  3126. extern PyObject* PyImaging_Mapper(PyObject* self, PyObject* args);
  3127. extern PyObject* PyImaging_MapBuffer(PyObject* self, PyObject* args);
  3128. static PyMethodDef functions[] = {
  3129. /* Object factories */
  3130. {"alpha_composite", (PyCFunction)_alpha_composite, 1},
  3131. {"blend", (PyCFunction)_blend, 1},
  3132. {"fill", (PyCFunction)_fill, 1},
  3133. {"new", (PyCFunction)_new, 1},
  3134. {"merge", (PyCFunction)_merge, 1},
  3135. /* Functions */
  3136. {"convert", (PyCFunction)_convert2, 1},
  3137. /* Codecs */
  3138. {"bcn_decoder", (PyCFunction)PyImaging_BcnDecoderNew, 1},
  3139. {"bit_decoder", (PyCFunction)PyImaging_BitDecoderNew, 1},
  3140. {"eps_encoder", (PyCFunction)PyImaging_EpsEncoderNew, 1},
  3141. {"fli_decoder", (PyCFunction)PyImaging_FliDecoderNew, 1},
  3142. {"gif_decoder", (PyCFunction)PyImaging_GifDecoderNew, 1},
  3143. {"gif_encoder", (PyCFunction)PyImaging_GifEncoderNew, 1},
  3144. {"hex_decoder", (PyCFunction)PyImaging_HexDecoderNew, 1},
  3145. {"hex_encoder", (PyCFunction)PyImaging_EpsEncoderNew, 1}, /* EPS=HEX! */
  3146. #ifdef HAVE_LIBJPEG
  3147. {"jpeg_decoder", (PyCFunction)PyImaging_JpegDecoderNew, 1},
  3148. {"jpeg_encoder", (PyCFunction)PyImaging_JpegEncoderNew, 1},
  3149. #endif
  3150. #ifdef HAVE_OPENJPEG
  3151. {"jpeg2k_decoder", (PyCFunction)PyImaging_Jpeg2KDecoderNew, 1},
  3152. {"jpeg2k_encoder", (PyCFunction)PyImaging_Jpeg2KEncoderNew, 1},
  3153. #endif
  3154. #ifdef HAVE_LIBTIFF
  3155. {"libtiff_decoder", (PyCFunction)PyImaging_LibTiffDecoderNew, 1},
  3156. {"libtiff_encoder", (PyCFunction)PyImaging_LibTiffEncoderNew, 1},
  3157. #endif
  3158. {"packbits_decoder", (PyCFunction)PyImaging_PackbitsDecoderNew, 1},
  3159. {"pcd_decoder", (PyCFunction)PyImaging_PcdDecoderNew, 1},
  3160. {"pcx_decoder", (PyCFunction)PyImaging_PcxDecoderNew, 1},
  3161. {"pcx_encoder", (PyCFunction)PyImaging_PcxEncoderNew, 1},
  3162. {"raw_decoder", (PyCFunction)PyImaging_RawDecoderNew, 1},
  3163. {"raw_encoder", (PyCFunction)PyImaging_RawEncoderNew, 1},
  3164. {"sgi_rle_decoder", (PyCFunction)PyImaging_SgiRleDecoderNew, 1},
  3165. {"sun_rle_decoder", (PyCFunction)PyImaging_SunRleDecoderNew, 1},
  3166. {"tga_rle_decoder", (PyCFunction)PyImaging_TgaRleDecoderNew, 1},
  3167. {"tga_rle_encoder", (PyCFunction)PyImaging_TgaRleEncoderNew, 1},
  3168. {"xbm_decoder", (PyCFunction)PyImaging_XbmDecoderNew, 1},
  3169. {"xbm_encoder", (PyCFunction)PyImaging_XbmEncoderNew, 1},
  3170. #ifdef HAVE_LIBZ
  3171. {"zip_decoder", (PyCFunction)PyImaging_ZipDecoderNew, 1},
  3172. {"zip_encoder", (PyCFunction)PyImaging_ZipEncoderNew, 1},
  3173. #endif
  3174. /* Memory mapping */
  3175. #ifdef WITH_MAPPING
  3176. #ifdef _WIN32
  3177. {"map", (PyCFunction)PyImaging_Mapper, 1},
  3178. #endif
  3179. {"map_buffer", (PyCFunction)PyImaging_MapBuffer, 1},
  3180. #endif
  3181. /* Display support */
  3182. #ifdef _WIN32
  3183. {"display", (PyCFunction)PyImaging_DisplayWin32, 1},
  3184. {"display_mode", (PyCFunction)PyImaging_DisplayModeWin32, 1},
  3185. {"grabscreen", (PyCFunction)PyImaging_GrabScreenWin32, 1},
  3186. {"grabclipboard", (PyCFunction)PyImaging_GrabClipboardWin32, 1},
  3187. {"createwindow", (PyCFunction)PyImaging_CreateWindowWin32, 1},
  3188. {"eventloop", (PyCFunction)PyImaging_EventLoopWin32, 1},
  3189. {"listwindows", (PyCFunction)PyImaging_ListWindowsWin32, 1},
  3190. {"drawwmf", (PyCFunction)PyImaging_DrawWmf, 1},
  3191. #endif
  3192. /* Utilities */
  3193. {"getcodecstatus", (PyCFunction)_getcodecstatus, 1},
  3194. /* Special effects (experimental) */
  3195. #ifdef WITH_EFFECTS
  3196. {"effect_mandelbrot", (PyCFunction)_effect_mandelbrot, 1},
  3197. {"effect_noise", (PyCFunction)_effect_noise, 1},
  3198. {"linear_gradient", (PyCFunction)_linear_gradient, 1},
  3199. {"radial_gradient", (PyCFunction)_radial_gradient, 1},
  3200. {"wedge", (PyCFunction)_linear_gradient, 1}, /* Compatibility */
  3201. #endif
  3202. /* Drawing support stuff */
  3203. #ifdef WITH_IMAGEDRAW
  3204. {"font", (PyCFunction)_font_new, 1},
  3205. {"draw", (PyCFunction)_draw_new, 1},
  3206. #endif
  3207. /* Experimental path stuff */
  3208. #ifdef WITH_IMAGEPATH
  3209. {"path", (PyCFunction)PyPath_Create, 1},
  3210. #endif
  3211. /* Experimental arrow graphics stuff */
  3212. #ifdef WITH_ARROW
  3213. {"outline", (PyCFunction)PyOutline_Create, 1},
  3214. #endif
  3215. /* Resource management */
  3216. {"get_stats", (PyCFunction)_get_stats, 1},
  3217. {"reset_stats", (PyCFunction)_reset_stats, 1},
  3218. {"get_alignment", (PyCFunction)_get_alignment, 1},
  3219. {"get_block_size", (PyCFunction)_get_block_size, 1},
  3220. {"get_blocks_max", (PyCFunction)_get_blocks_max, 1},
  3221. {"set_alignment", (PyCFunction)_set_alignment, 1},
  3222. {"set_block_size", (PyCFunction)_set_block_size, 1},
  3223. {"set_blocks_max", (PyCFunction)_set_blocks_max, 1},
  3224. {"clear_cache", (PyCFunction)_clear_cache, 1},
  3225. {NULL, NULL} /* sentinel */
  3226. };
  3227. static int
  3228. setup_module(PyObject* m) {
  3229. PyObject* d = PyModule_GetDict(m);
  3230. const char* version = (char*)PILLOW_VERSION;
  3231. /* Ready object types */
  3232. if (PyType_Ready(&Imaging_Type) < 0)
  3233. return -1;
  3234. #ifdef WITH_IMAGEDRAW
  3235. if (PyType_Ready(&ImagingFont_Type) < 0)
  3236. return -1;
  3237. if (PyType_Ready(&ImagingDraw_Type) < 0)
  3238. return -1;
  3239. #endif
  3240. if (PyType_Ready(&PixelAccess_Type) < 0)
  3241. return -1;
  3242. ImagingAccessInit();
  3243. #ifdef HAVE_LIBJPEG
  3244. {
  3245. extern const char* ImagingJpegVersion(void);
  3246. PyDict_SetItemString(d, "jpeglib_version", PyUnicode_FromString(ImagingJpegVersion()));
  3247. }
  3248. #endif
  3249. #ifdef HAVE_OPENJPEG
  3250. {
  3251. extern const char *ImagingJpeg2KVersion(void);
  3252. PyDict_SetItemString(d, "jp2klib_version", PyUnicode_FromString(ImagingJpeg2KVersion()));
  3253. }
  3254. #endif
  3255. #ifdef LIBJPEG_TURBO_VERSION
  3256. PyModule_AddObject(m, "HAVE_LIBJPEGTURBO", Py_True);
  3257. #else
  3258. PyModule_AddObject(m, "HAVE_LIBJPEGTURBO", Py_False);
  3259. #endif
  3260. #ifdef HAVE_LIBZ
  3261. /* zip encoding strategies */
  3262. PyModule_AddIntConstant(m, "DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);
  3263. PyModule_AddIntConstant(m, "FILTERED", Z_FILTERED);
  3264. PyModule_AddIntConstant(m, "HUFFMAN_ONLY", Z_HUFFMAN_ONLY);
  3265. PyModule_AddIntConstant(m, "RLE", Z_RLE);
  3266. PyModule_AddIntConstant(m, "FIXED", Z_FIXED);
  3267. {
  3268. extern const char* ImagingZipVersion(void);
  3269. PyDict_SetItemString(d, "zlib_version", PyUnicode_FromString(ImagingZipVersion()));
  3270. }
  3271. #endif
  3272. #ifdef HAVE_LIBTIFF
  3273. {
  3274. extern const char * ImagingTiffVersion(void);
  3275. PyDict_SetItemString(d, "libtiff_version", PyUnicode_FromString(ImagingTiffVersion()));
  3276. }
  3277. #endif
  3278. PyDict_SetItemString(d, "PILLOW_VERSION", PyUnicode_FromString(version));
  3279. return 0;
  3280. }
  3281. #if PY_VERSION_HEX >= 0x03000000
  3282. PyMODINIT_FUNC
  3283. PyInit__imaging(void) {
  3284. PyObject* m;
  3285. static PyModuleDef module_def = {
  3286. PyModuleDef_HEAD_INIT,
  3287. "_imaging", /* m_name */
  3288. NULL, /* m_doc */
  3289. -1, /* m_size */
  3290. functions, /* m_methods */
  3291. };
  3292. m = PyModule_Create(&module_def);
  3293. if (setup_module(m) < 0)
  3294. return NULL;
  3295. return m;
  3296. }
  3297. #else
  3298. PyMODINIT_FUNC
  3299. init_imaging(void)
  3300. {
  3301. PyObject* m = Py_InitModule("_imaging", functions);
  3302. setup_module(m);
  3303. }
  3304. #endif