bayer.c 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187
  1. /*
  2. * 1394-Based Digital Camera Control Library
  3. *
  4. * Bayer pattern decoding functions
  5. *
  6. * Written by Damien Douxchamps and Frederic Devernay
  7. * The original VNG and AHD Bayer decoding are from Dave Coffin's DCRAW.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. #include <limits.h>
  24. #include <math.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. //#include "conversions.h"
  28. #include "bayer.h"
  29. #define CLIP(in, out)\
  30. in = in < 0 ? 0 : in;\
  31. in = in > 255 ? 255 : in;\
  32. out=in;
  33. #define CLIP16(in, out, bits)\
  34. in = in < 0 ? 0 : in;\
  35. in = in > ((1<<bits)-1) ? ((1<<bits)-1) : in;\
  36. out=in;
  37. void
  38. ClearBorders(uint8_t *rgb, int sx, int sy, int w)
  39. {
  40. int i, j;
  41. // black edges are added with a width w:
  42. i = 3 * sx * w - 1;
  43. j = 3 * sx * sy - 1;
  44. while (i >= 0) {
  45. rgb[i--] = 0;
  46. rgb[j--] = 0;
  47. }
  48. int low = sx * (w - 1) * 3 - 1 + w * 3;
  49. i = low + sx * (sy - w * 2 + 1) * 3;
  50. while (i > low) {
  51. j = 6 * w;
  52. while (j > 0) {
  53. rgb[i--] = 0;
  54. j--;
  55. }
  56. i -= (sx - 2 * w) * 3;
  57. }
  58. }
  59. void
  60. ClearBorders_uint16(uint16_t * rgb, int sx, int sy, int w)
  61. {
  62. int i, j;
  63. // black edges:
  64. i = 3 * sx * w - 1;
  65. j = 3 * sx * sy - 1;
  66. while (i >= 0) {
  67. rgb[i--] = 0;
  68. rgb[j--] = 0;
  69. }
  70. int low = sx * (w - 1) * 3 - 1 + w * 3;
  71. i = low + sx * (sy - w * 2 + 1) * 3;
  72. while (i > low) {
  73. j = 6 * w;
  74. while (j > 0) {
  75. rgb[i--] = 0;
  76. j--;
  77. }
  78. i -= (sx - 2 * w) * 3;
  79. }
  80. }
  81. /**************************************************************
  82. * Color conversion functions for cameras that can *
  83. * output raw-Bayer pattern images, such as some Basler and *
  84. * Point Grey camera. Most of the algos presented here come *
  85. * from http://www-ise.stanford.edu/~tingchen/ and have been *
  86. * converted from Matlab to C and extended to all elementary *
  87. * patterns. *
  88. **************************************************************/
  89. /* 8-bits versions */
  90. /* insprired by OpenCV's Bayer decoding */
  91. dc1394error_t
  92. dc1394_bayer_NearestNeighbor(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  93. {
  94. const int bayerStep = sx;
  95. const int rgbStep = 3 * sx;
  96. int width = sx;
  97. int height = sy;
  98. int blue = tile == DC1394_COLOR_FILTER_BGGR
  99. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  100. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  101. || tile == DC1394_COLOR_FILTER_GRBG;
  102. int i, imax, iinc;
  103. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  104. return DC1394_INVALID_COLOR_FILTER;
  105. /* add black border */
  106. imax = sx * sy * 3;
  107. for (i = sx * (sy - 1) * 3; i < imax; i++) {
  108. rgb[i] = 0;
  109. }
  110. iinc = (sx - 1) * 3;
  111. for (i = (sx - 1) * 3; i < imax; i += iinc) {
  112. rgb[i++] = 0;
  113. rgb[i++] = 0;
  114. rgb[i++] = 0;
  115. }
  116. rgb += 1;
  117. width -= 1;
  118. height -= 1;
  119. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  120. //int t0, t1;
  121. const uint8_t *bayerEnd = bayer + width;
  122. if (start_with_green) {
  123. rgb[-blue] = bayer[1];
  124. rgb[0] = bayer[bayerStep + 1];
  125. rgb[blue] = bayer[bayerStep];
  126. bayer++;
  127. rgb += 3;
  128. }
  129. if (blue > 0) {
  130. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  131. rgb[-1] = bayer[0];
  132. rgb[0] = bayer[1];
  133. rgb[1] = bayer[bayerStep + 1];
  134. rgb[2] = bayer[2];
  135. rgb[3] = bayer[bayerStep + 2];
  136. rgb[4] = bayer[bayerStep + 1];
  137. }
  138. } else {
  139. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  140. rgb[1] = bayer[0];
  141. rgb[0] = bayer[1];
  142. rgb[-1] = bayer[bayerStep + 1];
  143. rgb[4] = bayer[2];
  144. rgb[3] = bayer[bayerStep + 2];
  145. rgb[2] = bayer[bayerStep + 1];
  146. }
  147. }
  148. if (bayer < bayerEnd) {
  149. rgb[-blue] = bayer[0];
  150. rgb[0] = bayer[1];
  151. rgb[blue] = bayer[bayerStep + 1];
  152. bayer++;
  153. rgb += 3;
  154. }
  155. bayer -= width;
  156. rgb -= width * 3;
  157. blue = -blue;
  158. start_with_green = !start_with_green;
  159. }
  160. return DC1394_SUCCESS;
  161. }
  162. /* OpenCV's Bayer decoding */
  163. dc1394error_t
  164. dc1394_bayer_Bilinear(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  165. {
  166. const int bayerStep = sx;
  167. const int rgbStep = 3 * sx;
  168. int width = sx;
  169. int height = sy;
  170. /*
  171. the two letters of the OpenCV name are respectively
  172. the 4th and 3rd letters from the blinky name,
  173. and we also have to switch R and B (OpenCV is BGR)
  174. CV_BayerBG2BGR <-> DC1394_COLOR_FILTER_BGGR
  175. CV_BayerGB2BGR <-> DC1394_COLOR_FILTER_GBRG
  176. CV_BayerGR2BGR <-> DC1394_COLOR_FILTER_GRBG
  177. int blue = tile == CV_BayerBG2BGR || tile == CV_BayerGB2BGR ? -1 : 1;
  178. int start_with_green = tile == CV_BayerGB2BGR || tile == CV_BayerGR2BGR;
  179. */
  180. int blue = tile == DC1394_COLOR_FILTER_BGGR
  181. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  182. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  183. || tile == DC1394_COLOR_FILTER_GRBG;
  184. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  185. return DC1394_INVALID_COLOR_FILTER;
  186. ClearBorders(rgb, sx, sy, 1);
  187. rgb += rgbStep + 3 + 1;
  188. height -= 2;
  189. width -= 2;
  190. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  191. int t0, t1;
  192. const uint8_t *bayerEnd = bayer + width;
  193. if (start_with_green) {
  194. /* OpenCV has a bug in the next line, which was
  195. t0 = (bayer[0] + bayer[bayerStep * 2] + 1) >> 1; */
  196. t0 = (bayer[1] + bayer[bayerStep * 2 + 1] + 1) >> 1;
  197. t1 = (bayer[bayerStep] + bayer[bayerStep + 2] + 1) >> 1;
  198. rgb[-blue] = (uint8_t) t0;
  199. rgb[0] = bayer[bayerStep + 1];
  200. rgb[blue] = (uint8_t) t1;
  201. bayer++;
  202. rgb += 3;
  203. }
  204. if (blue > 0) {
  205. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  206. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  207. bayer[bayerStep * 2 + 2] + 2) >> 2;
  208. t1 = (bayer[1] + bayer[bayerStep] +
  209. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  210. 2) >> 2;
  211. rgb[-1] = (uint8_t) t0;
  212. rgb[0] = (uint8_t) t1;
  213. rgb[1] = bayer[bayerStep + 1];
  214. t0 = (bayer[2] + bayer[bayerStep * 2 + 2] + 1) >> 1;
  215. t1 = (bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  216. 1) >> 1;
  217. rgb[2] = (uint8_t) t0;
  218. rgb[3] = bayer[bayerStep + 2];
  219. rgb[4] = (uint8_t) t1;
  220. }
  221. } else {
  222. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  223. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  224. bayer[bayerStep * 2 + 2] + 2) >> 2;
  225. t1 = (bayer[1] + bayer[bayerStep] +
  226. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  227. 2) >> 2;
  228. rgb[1] = (uint8_t) t0;
  229. rgb[0] = (uint8_t) t1;
  230. rgb[-1] = bayer[bayerStep + 1];
  231. t0 = (bayer[2] + bayer[bayerStep * 2 + 2] + 1) >> 1;
  232. t1 = (bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  233. 1) >> 1;
  234. rgb[4] = (uint8_t) t0;
  235. rgb[3] = bayer[bayerStep + 2];
  236. rgb[2] = (uint8_t) t1;
  237. }
  238. }
  239. if (bayer < bayerEnd) {
  240. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  241. bayer[bayerStep * 2 + 2] + 2) >> 2;
  242. t1 = (bayer[1] + bayer[bayerStep] +
  243. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  244. 2) >> 2;
  245. rgb[-blue] = (uint8_t) t0;
  246. rgb[0] = (uint8_t) t1;
  247. rgb[blue] = bayer[bayerStep + 1];
  248. bayer++;
  249. rgb += 3;
  250. }
  251. bayer -= width;
  252. rgb -= width * 3;
  253. blue = -blue;
  254. start_with_green = !start_with_green;
  255. }
  256. return DC1394_SUCCESS;
  257. }
  258. /* High-Quality Linear Interpolation For Demosaicing Of
  259. Bayer-Patterned Color Images, by Henrique S. Malvar, Li-wei He, and
  260. Ross Cutler, in ICASSP'04 */
  261. dc1394error_t
  262. dc1394_bayer_HQLinear(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  263. {
  264. const int bayerStep = sx;
  265. const int rgbStep = 3 * sx;
  266. int width = sx;
  267. int height = sy;
  268. int blue = tile == DC1394_COLOR_FILTER_BGGR
  269. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  270. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  271. || tile == DC1394_COLOR_FILTER_GRBG;
  272. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  273. return DC1394_INVALID_COLOR_FILTER;
  274. ClearBorders(rgb, sx, sy, 2);
  275. rgb += 2 * rgbStep + 6 + 1;
  276. height -= 4;
  277. width -= 4;
  278. /* We begin with a (+1 line,+1 column) offset with respect to bilinear decoding, so start_with_green is the same, but blue is opposite */
  279. blue = -blue;
  280. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  281. int t0, t1;
  282. const uint8_t *bayerEnd = bayer + width;
  283. const int bayerStep2 = bayerStep * 2;
  284. const int bayerStep3 = bayerStep * 3;
  285. const int bayerStep4 = bayerStep * 4;
  286. if (start_with_green) {
  287. /* at green pixel */
  288. rgb[0] = bayer[bayerStep2 + 2];
  289. t0 = rgb[0] * 5
  290. + ((bayer[bayerStep + 2] + bayer[bayerStep3 + 2]) << 2)
  291. - bayer[2]
  292. - bayer[bayerStep + 1]
  293. - bayer[bayerStep + 3]
  294. - bayer[bayerStep3 + 1]
  295. - bayer[bayerStep3 + 3]
  296. - bayer[bayerStep4 + 2]
  297. + ((bayer[bayerStep2] + bayer[bayerStep2 + 4] + 1) >> 1);
  298. t1 = rgb[0] * 5 +
  299. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 3]) << 2)
  300. - bayer[bayerStep2]
  301. - bayer[bayerStep + 1]
  302. - bayer[bayerStep + 3]
  303. - bayer[bayerStep3 + 1]
  304. - bayer[bayerStep3 + 3]
  305. - bayer[bayerStep2 + 4]
  306. + ((bayer[2] + bayer[bayerStep4 + 2] + 1) >> 1);
  307. t0 = (t0 + 4) >> 3;
  308. CLIP(t0, rgb[-blue]);
  309. t1 = (t1 + 4) >> 3;
  310. CLIP(t1, rgb[blue]);
  311. bayer++;
  312. rgb += 3;
  313. }
  314. if (blue > 0) {
  315. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  316. /* B at B */
  317. rgb[1] = bayer[bayerStep2 + 2];
  318. /* R at B */
  319. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  320. bayer[bayerStep3 + 1] + bayer[bayerStep3 + 3]) << 1)
  321. -
  322. (((bayer[2] + bayer[bayerStep2] +
  323. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  324. 2]) * 3 + 1) >> 1)
  325. + rgb[1] * 6;
  326. /* G at B */
  327. t1 = ((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  328. bayer[bayerStep2 + 3] + bayer[bayerStep3 + 2]) << 1)
  329. - (bayer[2] + bayer[bayerStep2] +
  330. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  331. + (rgb[1] << 2);
  332. t0 = (t0 + 4) >> 3;
  333. CLIP(t0, rgb[-1]);
  334. t1 = (t1 + 4) >> 3;
  335. CLIP(t1, rgb[0]);
  336. /* at green pixel */
  337. rgb[3] = bayer[bayerStep2 + 3];
  338. t0 = rgb[3] * 5
  339. + ((bayer[bayerStep + 3] + bayer[bayerStep3 + 3]) << 2)
  340. - bayer[3]
  341. - bayer[bayerStep + 2]
  342. - bayer[bayerStep + 4]
  343. - bayer[bayerStep3 + 2]
  344. - bayer[bayerStep3 + 4]
  345. - bayer[bayerStep4 + 3]
  346. +
  347. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 5] +
  348. 1) >> 1);
  349. t1 = rgb[3] * 5 +
  350. ((bayer[bayerStep2 + 2] + bayer[bayerStep2 + 4]) << 2)
  351. - bayer[bayerStep2 + 1]
  352. - bayer[bayerStep + 2]
  353. - bayer[bayerStep + 4]
  354. - bayer[bayerStep3 + 2]
  355. - bayer[bayerStep3 + 4]
  356. - bayer[bayerStep2 + 5]
  357. + ((bayer[3] + bayer[bayerStep4 + 3] + 1) >> 1);
  358. t0 = (t0 + 4) >> 3;
  359. CLIP(t0, rgb[2]);
  360. t1 = (t1 + 4) >> 3;
  361. CLIP(t1, rgb[4]);
  362. }
  363. } else {
  364. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  365. /* R at R */
  366. rgb[-1] = bayer[bayerStep2 + 2];
  367. /* B at R */
  368. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  369. bayer[bayerStep3 + 1] + bayer[bayerStep3 + 3]) << 1)
  370. -
  371. (((bayer[2] + bayer[bayerStep2] +
  372. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  373. 2]) * 3 + 1) >> 1)
  374. + rgb[-1] * 6;
  375. /* G at R */
  376. t1 = ((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  377. bayer[bayerStep2 + 3] + bayer[bayerStep * 3 +
  378. 2]) << 1)
  379. - (bayer[2] + bayer[bayerStep2] +
  380. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  381. + (rgb[-1] << 2);
  382. t0 = (t0 + 4) >> 3;
  383. CLIP(t0, rgb[1]);
  384. t1 = (t1 + 4) >> 3;
  385. CLIP(t1, rgb[0]);
  386. /* at green pixel */
  387. rgb[3] = bayer[bayerStep2 + 3];
  388. t0 = rgb[3] * 5
  389. + ((bayer[bayerStep + 3] + bayer[bayerStep3 + 3]) << 2)
  390. - bayer[3]
  391. - bayer[bayerStep + 2]
  392. - bayer[bayerStep + 4]
  393. - bayer[bayerStep3 + 2]
  394. - bayer[bayerStep3 + 4]
  395. - bayer[bayerStep4 + 3]
  396. +
  397. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 5] +
  398. 1) >> 1);
  399. t1 = rgb[3] * 5 +
  400. ((bayer[bayerStep2 + 2] + bayer[bayerStep2 + 4]) << 2)
  401. - bayer[bayerStep2 + 1]
  402. - bayer[bayerStep + 2]
  403. - bayer[bayerStep + 4]
  404. - bayer[bayerStep3 + 2]
  405. - bayer[bayerStep3 + 4]
  406. - bayer[bayerStep2 + 5]
  407. + ((bayer[3] + bayer[bayerStep4 + 3] + 1) >> 1);
  408. t0 = (t0 + 4) >> 3;
  409. CLIP(t0, rgb[4]);
  410. t1 = (t1 + 4) >> 3;
  411. CLIP(t1, rgb[2]);
  412. }
  413. }
  414. if (bayer < bayerEnd) {
  415. /* B at B */
  416. rgb[blue] = bayer[bayerStep2 + 2];
  417. /* R at B */
  418. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  419. bayer[bayerStep3 + 1] + bayer[bayerStep3 + 3]) << 1)
  420. -
  421. (((bayer[2] + bayer[bayerStep2] +
  422. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  423. 2]) * 3 + 1) >> 1)
  424. + rgb[blue] * 6;
  425. /* G at B */
  426. t1 = (((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  427. bayer[bayerStep2 + 3] + bayer[bayerStep3 + 2])) << 1)
  428. - (bayer[2] + bayer[bayerStep2] +
  429. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  430. + (rgb[blue] << 2);
  431. t0 = (t0 + 4) >> 3;
  432. CLIP(t0, rgb[-blue]);
  433. t1 = (t1 + 4) >> 3;
  434. CLIP(t1, rgb[0]);
  435. bayer++;
  436. rgb += 3;
  437. }
  438. bayer -= width;
  439. rgb -= width * 3;
  440. blue = -blue;
  441. start_with_green = !start_with_green;
  442. }
  443. return DC1394_SUCCESS;
  444. }
  445. /* coriander's Bayer decoding */
  446. /* Edge Sensing Interpolation II from http://www-ise.stanford.edu/~tingchen/ */
  447. /* (Laroche,Claude A. "Apparatus and method for adaptively
  448. interpolating a full color image utilizing chrominance gradients"
  449. U.S. Patent 5,373,322) */
  450. dc1394error_t
  451. dc1394_bayer_EdgeSense(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  452. {
  453. /* Removed due to patent concerns */
  454. return DC1394_FUNCTION_NOT_SUPPORTED;
  455. }
  456. /* coriander's Bayer decoding */
  457. dc1394error_t
  458. dc1394_bayer_Downsample(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  459. {
  460. uint8_t *outR, *outG, *outB;
  461. register int i, j;
  462. int tmp;
  463. switch (tile) {
  464. case DC1394_COLOR_FILTER_GRBG:
  465. case DC1394_COLOR_FILTER_BGGR:
  466. outR = &rgb[0];
  467. outG = &rgb[1];
  468. outB = &rgb[2];
  469. break;
  470. case DC1394_COLOR_FILTER_GBRG:
  471. case DC1394_COLOR_FILTER_RGGB:
  472. outR = &rgb[2];
  473. outG = &rgb[1];
  474. outB = &rgb[0];
  475. break;
  476. default:
  477. return DC1394_INVALID_COLOR_FILTER;
  478. }
  479. switch (tile) {
  480. case DC1394_COLOR_FILTER_GRBG: //---------------------------------------------------------
  481. case DC1394_COLOR_FILTER_GBRG:
  482. for (i = 0; i < sy*sx; i += (sx<<1)) {
  483. for (j = 0; j < sx; j += 2) {
  484. tmp = ((bayer[i + j] + bayer[i + sx + j + 1]) >> 1);
  485. CLIP(tmp, outG[((i >> 2) + (j >> 1)) * 3]);
  486. tmp = bayer[i + sx + j + 1];
  487. CLIP(tmp, outR[((i >> 2) + (j >> 1)) * 3]);
  488. tmp = bayer[i + sx + j];
  489. CLIP(tmp, outB[((i >> 2) + (j >> 1)) * 3]);
  490. }
  491. }
  492. break;
  493. case DC1394_COLOR_FILTER_BGGR: //---------------------------------------------------------
  494. case DC1394_COLOR_FILTER_RGGB:
  495. for (i = 0; i < sy*sx; i += (sx<<1)) {
  496. for (j = 0; j < sx; j += 2) {
  497. tmp = ((bayer[i + sx + j] + bayer[i + j + 1]) >> 1);
  498. CLIP(tmp, outG[((i >> 2) + (j >> 1)) * 3]);
  499. tmp = bayer[i + sx + j + 1];
  500. CLIP(tmp, outR[((i >> 2) + (j >> 1)) * 3]);
  501. tmp = bayer[i + j];
  502. CLIP(tmp, outB[((i >> 2) + (j >> 1)) * 3]);
  503. }
  504. }
  505. break;
  506. }
  507. return DC1394_SUCCESS;
  508. }
  509. /* this is the method used inside AVT cameras. See AVT docs. */
  510. dc1394error_t
  511. dc1394_bayer_Simple(const uint8_t *restrict bayer, uint8_t *restrict rgb, int sx, int sy, int tile)
  512. {
  513. const int bayerStep = sx;
  514. const int rgbStep = 3 * sx;
  515. int width = sx;
  516. int height = sy;
  517. int blue = tile == DC1394_COLOR_FILTER_BGGR
  518. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  519. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  520. || tile == DC1394_COLOR_FILTER_GRBG;
  521. int i, imax, iinc;
  522. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  523. return DC1394_INVALID_COLOR_FILTER;
  524. /* add black border */
  525. imax = sx * sy * 3;
  526. for (i = sx * (sy - 1) * 3; i < imax; i++) {
  527. rgb[i] = 0;
  528. }
  529. iinc = (sx - 1) * 3;
  530. for (i = (sx - 1) * 3; i < imax; i += iinc) {
  531. rgb[i++] = 0;
  532. rgb[i++] = 0;
  533. rgb[i++] = 0;
  534. }
  535. rgb += 1;
  536. width -= 1;
  537. height -= 1;
  538. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  539. const uint8_t *bayerEnd = bayer + width;
  540. if (start_with_green) {
  541. rgb[-blue] = bayer[1];
  542. rgb[0] = (bayer[0] + bayer[bayerStep + 1] + 1) >> 1;
  543. rgb[blue] = bayer[bayerStep];
  544. bayer++;
  545. rgb += 3;
  546. }
  547. if (blue > 0) {
  548. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  549. rgb[-1] = bayer[0];
  550. rgb[0] = (bayer[1] + bayer[bayerStep] + 1) >> 1;
  551. rgb[1] = bayer[bayerStep + 1];
  552. rgb[2] = bayer[2];
  553. rgb[3] = (bayer[1] + bayer[bayerStep + 2] + 1) >> 1;
  554. rgb[4] = bayer[bayerStep + 1];
  555. }
  556. } else {
  557. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  558. rgb[1] = bayer[0];
  559. rgb[0] = (bayer[1] + bayer[bayerStep] + 1) >> 1;
  560. rgb[-1] = bayer[bayerStep + 1];
  561. rgb[4] = bayer[2];
  562. rgb[3] = (bayer[1] + bayer[bayerStep + 2] + 1) >> 1;
  563. rgb[2] = bayer[bayerStep + 1];
  564. }
  565. }
  566. if (bayer < bayerEnd) {
  567. rgb[-blue] = bayer[0];
  568. rgb[0] = (bayer[1] + bayer[bayerStep] + 1) >> 1;
  569. rgb[blue] = bayer[bayerStep + 1];
  570. bayer++;
  571. rgb += 3;
  572. }
  573. bayer -= width;
  574. rgb -= width * 3;
  575. blue = -blue;
  576. start_with_green = !start_with_green;
  577. }
  578. return DC1394_SUCCESS;
  579. }
  580. /* 16-bits versions */
  581. /* insprired by OpenCV's Bayer decoding */
  582. dc1394error_t
  583. dc1394_bayer_NearestNeighbor_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  584. {
  585. const int bayerStep = sx;
  586. const int rgbStep = 3 * sx;
  587. int width = sx;
  588. int height = sy;
  589. int blue = tile == DC1394_COLOR_FILTER_BGGR
  590. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  591. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  592. || tile == DC1394_COLOR_FILTER_GRBG;
  593. int i, iinc, imax;
  594. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  595. return DC1394_INVALID_COLOR_FILTER;
  596. /* add black border */
  597. imax = sx * sy * 3;
  598. for (i = sx * (sy - 1) * 3; i < imax; i++) {
  599. rgb[i] = 0;
  600. }
  601. iinc = (sx - 1) * 3;
  602. for (i = (sx - 1) * 3; i < imax; i += iinc) {
  603. rgb[i++] = 0;
  604. rgb[i++] = 0;
  605. rgb[i++] = 0;
  606. }
  607. rgb += 1;
  608. height -= 1;
  609. width -= 1;
  610. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  611. //int t0, t1;
  612. const uint16_t *bayerEnd = bayer + width;
  613. if (start_with_green) {
  614. rgb[-blue] = bayer[1];
  615. rgb[0] = bayer[bayerStep + 1];
  616. rgb[blue] = bayer[bayerStep];
  617. bayer++;
  618. rgb += 3;
  619. }
  620. if (blue > 0) {
  621. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  622. rgb[-1] = bayer[0];
  623. rgb[0] = bayer[1];
  624. rgb[1] = bayer[bayerStep + 1];
  625. rgb[2] = bayer[2];
  626. rgb[3] = bayer[bayerStep + 2];
  627. rgb[4] = bayer[bayerStep + 1];
  628. }
  629. } else {
  630. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  631. rgb[1] = bayer[0];
  632. rgb[0] = bayer[1];
  633. rgb[-1] = bayer[bayerStep + 1];
  634. rgb[4] = bayer[2];
  635. rgb[3] = bayer[bayerStep + 2];
  636. rgb[2] = bayer[bayerStep + 1];
  637. }
  638. }
  639. if (bayer < bayerEnd) {
  640. rgb[-blue] = bayer[0];
  641. rgb[0] = bayer[1];
  642. rgb[blue] = bayer[bayerStep + 1];
  643. bayer++;
  644. rgb += 3;
  645. }
  646. bayer -= width;
  647. rgb -= width * 3;
  648. blue = -blue;
  649. start_with_green = !start_with_green;
  650. }
  651. return DC1394_SUCCESS;
  652. }
  653. /* OpenCV's Bayer decoding */
  654. dc1394error_t
  655. dc1394_bayer_Bilinear_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  656. {
  657. const int bayerStep = sx;
  658. const int rgbStep = 3 * sx;
  659. int width = sx;
  660. int height = sy;
  661. int blue = tile == DC1394_COLOR_FILTER_BGGR
  662. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  663. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  664. || tile == DC1394_COLOR_FILTER_GRBG;
  665. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  666. return DC1394_INVALID_COLOR_FILTER;
  667. rgb += rgbStep + 3 + 1;
  668. height -= 2;
  669. width -= 2;
  670. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  671. int t0, t1;
  672. const uint16_t *bayerEnd = bayer + width;
  673. if (start_with_green) {
  674. /* OpenCV has a bug in the next line, which was
  675. t0 = (bayer[0] + bayer[bayerStep * 2] + 1) >> 1; */
  676. t0 = (bayer[1] + bayer[bayerStep * 2 + 1] + 1) >> 1;
  677. t1 = (bayer[bayerStep] + bayer[bayerStep + 2] + 1) >> 1;
  678. rgb[-blue] = (uint16_t) t0;
  679. rgb[0] = bayer[bayerStep + 1];
  680. rgb[blue] = (uint16_t) t1;
  681. bayer++;
  682. rgb += 3;
  683. }
  684. if (blue > 0) {
  685. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  686. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  687. bayer[bayerStep * 2 + 2] + 2) >> 2;
  688. t1 = (bayer[1] + bayer[bayerStep] +
  689. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  690. 2) >> 2;
  691. rgb[-1] = (uint16_t) t0;
  692. rgb[0] = (uint16_t) t1;
  693. rgb[1] = bayer[bayerStep + 1];
  694. t0 = (bayer[2] + bayer[bayerStep * 2 + 2] + 1) >> 1;
  695. t1 = (bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  696. 1) >> 1;
  697. rgb[2] = (uint16_t) t0;
  698. rgb[3] = bayer[bayerStep + 2];
  699. rgb[4] = (uint16_t) t1;
  700. }
  701. } else {
  702. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  703. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  704. bayer[bayerStep * 2 + 2] + 2) >> 2;
  705. t1 = (bayer[1] + bayer[bayerStep] +
  706. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  707. 2) >> 2;
  708. rgb[1] = (uint16_t) t0;
  709. rgb[0] = (uint16_t) t1;
  710. rgb[-1] = bayer[bayerStep + 1];
  711. t0 = (bayer[2] + bayer[bayerStep * 2 + 2] + 1) >> 1;
  712. t1 = (bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  713. 1) >> 1;
  714. rgb[4] = (uint16_t) t0;
  715. rgb[3] = bayer[bayerStep + 2];
  716. rgb[2] = (uint16_t) t1;
  717. }
  718. }
  719. if (bayer < bayerEnd) {
  720. t0 = (bayer[0] + bayer[2] + bayer[bayerStep * 2] +
  721. bayer[bayerStep * 2 + 2] + 2) >> 2;
  722. t1 = (bayer[1] + bayer[bayerStep] +
  723. bayer[bayerStep + 2] + bayer[bayerStep * 2 + 1] +
  724. 2) >> 2;
  725. rgb[-blue] = (uint16_t) t0;
  726. rgb[0] = (uint16_t) t1;
  727. rgb[blue] = bayer[bayerStep + 1];
  728. bayer++;
  729. rgb += 3;
  730. }
  731. bayer -= width;
  732. rgb -= width * 3;
  733. blue = -blue;
  734. start_with_green = !start_with_green;
  735. }
  736. return DC1394_SUCCESS;
  737. }
  738. /* High-Quality Linear Interpolation For Demosaicing Of
  739. Bayer-Patterned Color Images, by Henrique S. Malvar, Li-wei He, and
  740. Ross Cutler, in ICASSP'04 */
  741. dc1394error_t
  742. dc1394_bayer_HQLinear_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  743. {
  744. const int bayerStep = sx;
  745. const int rgbStep = 3 * sx;
  746. int width = sx;
  747. int height = sy;
  748. /*
  749. the two letters of the OpenCV name are respectively
  750. the 4th and 3rd letters from the blinky name,
  751. and we also have to switch R and B (OpenCV is BGR)
  752. CV_BayerBG2BGR <-> DC1394_COLOR_FILTER_BGGR
  753. CV_BayerGB2BGR <-> DC1394_COLOR_FILTER_GBRG
  754. CV_BayerGR2BGR <-> DC1394_COLOR_FILTER_GRBG
  755. int blue = tile == CV_BayerBG2BGR || tile == CV_BayerGB2BGR ? -1 : 1;
  756. int start_with_green = tile == CV_BayerGB2BGR || tile == CV_BayerGR2BGR;
  757. */
  758. int blue = tile == DC1394_COLOR_FILTER_BGGR
  759. || tile == DC1394_COLOR_FILTER_GBRG ? -1 : 1;
  760. int start_with_green = tile == DC1394_COLOR_FILTER_GBRG
  761. || tile == DC1394_COLOR_FILTER_GRBG;
  762. if ((tile>DC1394_COLOR_FILTER_MAX)||(tile<DC1394_COLOR_FILTER_MIN))
  763. return DC1394_INVALID_COLOR_FILTER;
  764. ClearBorders_uint16(rgb, sx, sy, 2);
  765. rgb += 2 * rgbStep + 6 + 1;
  766. height -= 4;
  767. width -= 4;
  768. /* We begin with a (+1 line,+1 column) offset with respect to bilinear decoding, so start_with_green is the same, but blue is opposite */
  769. blue = -blue;
  770. for (; height--; bayer += bayerStep, rgb += rgbStep) {
  771. int t0, t1;
  772. const uint16_t *bayerEnd = bayer + width;
  773. const int bayerStep2 = bayerStep * 2;
  774. const int bayerStep3 = bayerStep * 3;
  775. const int bayerStep4 = bayerStep * 4;
  776. if (start_with_green) {
  777. /* at green pixel */
  778. rgb[0] = bayer[bayerStep2 + 2];
  779. t0 = rgb[0] * 5
  780. + ((bayer[bayerStep + 2] + bayer[bayerStep3 + 2]) << 2)
  781. - bayer[2]
  782. - bayer[bayerStep + 1]
  783. - bayer[bayerStep + 3]
  784. - bayer[bayerStep3 + 1]
  785. - bayer[bayerStep3 + 3]
  786. - bayer[bayerStep4 + 2]
  787. + ((bayer[bayerStep2] + bayer[bayerStep2 + 4] + 1) >> 1);
  788. t1 = rgb[0] * 5 +
  789. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 3]) << 2)
  790. - bayer[bayerStep2]
  791. - bayer[bayerStep + 1]
  792. - bayer[bayerStep + 3]
  793. - bayer[bayerStep3 + 1]
  794. - bayer[bayerStep3 + 3]
  795. - bayer[bayerStep2 + 4]
  796. + ((bayer[2] + bayer[bayerStep4 + 2] + 1) >> 1);
  797. t0 = (t0 + 4) >> 3;
  798. CLIP16(t0, rgb[-blue], bits);
  799. t1 = (t1 + 4) >> 3;
  800. CLIP16(t1, rgb[blue], bits);
  801. bayer++;
  802. rgb += 3;
  803. }
  804. if (blue > 0) {
  805. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  806. /* B at B */
  807. rgb[1] = bayer[bayerStep2 + 2];
  808. /* R at B */
  809. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  810. bayer[bayerStep3 + 1] + bayer[bayerStep3 + 3]) << 1)
  811. -
  812. (((bayer[2] + bayer[bayerStep2] +
  813. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  814. 2]) * 3 + 1) >> 1)
  815. + rgb[1] * 6;
  816. /* G at B */
  817. t1 = ((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  818. bayer[bayerStep2 + 3] + bayer[bayerStep * 3 +
  819. 2]) << 1)
  820. - (bayer[2] + bayer[bayerStep2] +
  821. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  822. + (rgb[1] << 2);
  823. t0 = (t0 + 4) >> 3;
  824. CLIP16(t0, rgb[-1], bits);
  825. t1 = (t1 + 4) >> 3;
  826. CLIP16(t1, rgb[0], bits);
  827. /* at green pixel */
  828. rgb[3] = bayer[bayerStep2 + 3];
  829. t0 = rgb[3] * 5
  830. + ((bayer[bayerStep + 3] + bayer[bayerStep3 + 3]) << 2)
  831. - bayer[3]
  832. - bayer[bayerStep + 2]
  833. - bayer[bayerStep + 4]
  834. - bayer[bayerStep3 + 2]
  835. - bayer[bayerStep3 + 4]
  836. - bayer[bayerStep4 + 3]
  837. +
  838. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 5] +
  839. 1) >> 1);
  840. t1 = rgb[3] * 5 +
  841. ((bayer[bayerStep2 + 2] + bayer[bayerStep2 + 4]) << 2)
  842. - bayer[bayerStep2 + 1]
  843. - bayer[bayerStep + 2]
  844. - bayer[bayerStep + 4]
  845. - bayer[bayerStep3 + 2]
  846. - bayer[bayerStep3 + 4]
  847. - bayer[bayerStep2 + 5]
  848. + ((bayer[3] + bayer[bayerStep4 + 3] + 1) >> 1);
  849. t0 = (t0 + 4) >> 3;
  850. CLIP16(t0, rgb[2], bits);
  851. t1 = (t1 + 4) >> 3;
  852. CLIP16(t1, rgb[4], bits);
  853. }
  854. } else {
  855. for (; bayer <= bayerEnd - 2; bayer += 2, rgb += 6) {
  856. /* R at R */
  857. rgb[-1] = bayer[bayerStep2 + 2];
  858. /* B at R */
  859. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  860. bayer[bayerStep * 3 + 1] + bayer[bayerStep3 +
  861. 3]) << 1)
  862. -
  863. (((bayer[2] + bayer[bayerStep2] +
  864. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  865. 2]) * 3 + 1) >> 1)
  866. + rgb[-1] * 6;
  867. /* G at R */
  868. t1 = ((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  869. bayer[bayerStep2 + 3] + bayer[bayerStep3 + 2]) << 1)
  870. - (bayer[2] + bayer[bayerStep2] +
  871. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  872. + (rgb[-1] << 2);
  873. t0 = (t0 + 4) >> 3;
  874. CLIP16(t0, rgb[1], bits);
  875. t1 = (t1 + 4) >> 3;
  876. CLIP16(t1, rgb[0], bits);
  877. /* at green pixel */
  878. rgb[3] = bayer[bayerStep2 + 3];
  879. t0 = rgb[3] * 5
  880. + ((bayer[bayerStep + 3] + bayer[bayerStep3 + 3]) << 2)
  881. - bayer[3]
  882. - bayer[bayerStep + 2]
  883. - bayer[bayerStep + 4]
  884. - bayer[bayerStep3 + 2]
  885. - bayer[bayerStep3 + 4]
  886. - bayer[bayerStep4 + 3]
  887. +
  888. ((bayer[bayerStep2 + 1] + bayer[bayerStep2 + 5] +
  889. 1) >> 1);
  890. t1 = rgb[3] * 5 +
  891. ((bayer[bayerStep2 + 2] + bayer[bayerStep2 + 4]) << 2)
  892. - bayer[bayerStep2 + 1]
  893. - bayer[bayerStep + 2]
  894. - bayer[bayerStep + 4]
  895. - bayer[bayerStep3 + 2]
  896. - bayer[bayerStep3 + 4]
  897. - bayer[bayerStep2 + 5]
  898. + ((bayer[3] + bayer[bayerStep4 + 3] + 1) >> 1);
  899. t0 = (t0 + 4) >> 3;
  900. CLIP16(t0, rgb[4], bits);
  901. t1 = (t1 + 4) >> 3;
  902. CLIP16(t1, rgb[2], bits);
  903. }
  904. }
  905. if (bayer < bayerEnd) {
  906. /* B at B */
  907. rgb[blue] = bayer[bayerStep2 + 2];
  908. /* R at B */
  909. t0 = ((bayer[bayerStep + 1] + bayer[bayerStep + 3] +
  910. bayer[bayerStep3 + 1] + bayer[bayerStep3 + 3]) << 1)
  911. -
  912. (((bayer[2] + bayer[bayerStep2] +
  913. bayer[bayerStep2 + 4] + bayer[bayerStep4 +
  914. 2]) * 3 + 1) >> 1)
  915. + rgb[blue] * 6;
  916. /* G at B */
  917. t1 = (((bayer[bayerStep + 2] + bayer[bayerStep2 + 1] +
  918. bayer[bayerStep2 + 3] + bayer[bayerStep3 + 2])) << 1)
  919. - (bayer[2] + bayer[bayerStep2] +
  920. bayer[bayerStep2 + 4] + bayer[bayerStep4 + 2])
  921. + (rgb[blue] << 2);
  922. t0 = (t0 + 4) >> 3;
  923. CLIP16(t0, rgb[-blue], bits);
  924. t1 = (t1 + 4) >> 3;
  925. CLIP16(t1, rgb[0], bits);
  926. bayer++;
  927. rgb += 3;
  928. }
  929. bayer -= width;
  930. rgb -= width * 3;
  931. blue = -blue;
  932. start_with_green = !start_with_green;
  933. }
  934. return DC1394_SUCCESS;
  935. }
  936. /* coriander's Bayer decoding */
  937. dc1394error_t
  938. dc1394_bayer_EdgeSense_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  939. {
  940. /* Removed due to patent concerns */
  941. return DC1394_FUNCTION_NOT_SUPPORTED;
  942. }
  943. /* coriander's Bayer decoding */
  944. dc1394error_t
  945. dc1394_bayer_Downsample_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  946. {
  947. uint16_t *outR, *outG, *outB;
  948. register int i, j;
  949. int tmp;
  950. switch (tile) {
  951. case DC1394_COLOR_FILTER_GRBG:
  952. case DC1394_COLOR_FILTER_BGGR:
  953. outR = &rgb[0];
  954. outG = &rgb[1];
  955. outB = &rgb[2];
  956. break;
  957. case DC1394_COLOR_FILTER_GBRG:
  958. case DC1394_COLOR_FILTER_RGGB:
  959. outR = &rgb[2];
  960. outG = &rgb[1];
  961. outB = &rgb[0];
  962. break;
  963. default:
  964. return DC1394_INVALID_COLOR_FILTER;
  965. }
  966. switch (tile) {
  967. case DC1394_COLOR_FILTER_GRBG: //---------------------------------------------------------
  968. case DC1394_COLOR_FILTER_GBRG:
  969. for (i = 0; i < sy*sx; i += (sx<<1)) {
  970. for (j = 0; j < sx; j += 2) {
  971. tmp =
  972. ((bayer[i + j] + bayer[i + sx + j + 1]) >> 1);
  973. CLIP16(tmp, outG[((i >> 2) + (j >> 1)) * 3], bits);
  974. tmp = bayer[i + sx + j + 1];
  975. CLIP16(tmp, outR[((i >> 2) + (j >> 1)) * 3], bits);
  976. tmp = bayer[i + sx + j];
  977. CLIP16(tmp, outB[((i >> 2) + (j >> 1)) * 3], bits);
  978. }
  979. }
  980. break;
  981. case DC1394_COLOR_FILTER_BGGR: //---------------------------------------------------------
  982. case DC1394_COLOR_FILTER_RGGB:
  983. for (i = 0; i < sy*sx; i += (sx<<1)) {
  984. for (j = 0; j < sx; j += 2) {
  985. tmp =
  986. ((bayer[i + sx + j] + bayer[i + j + 1]) >> 1);
  987. CLIP16(tmp, outG[((i >> 2) + (j >> 1)) * 3], bits);
  988. tmp = bayer[i + sx + j + 1];
  989. CLIP16(tmp, outR[((i >> 2) + (j >> 1)) * 3], bits);
  990. tmp = bayer[i + j];
  991. CLIP16(tmp, outB[((i >> 2) + (j >> 1)) * 3], bits);
  992. }
  993. }
  994. break;
  995. }
  996. return DC1394_SUCCESS;
  997. }
  998. /* coriander's Bayer decoding */
  999. dc1394error_t
  1000. dc1394_bayer_Simple_uint16(const uint16_t *restrict bayer, uint16_t *restrict rgb, int sx, int sy, int tile, int bits)
  1001. {
  1002. uint16_t *outR, *outG, *outB;
  1003. register int i, j;
  1004. int tmp, base;
  1005. // sx and sy should be even
  1006. switch (tile) {
  1007. case DC1394_COLOR_FILTER_GRBG:
  1008. case DC1394_COLOR_FILTER_BGGR:
  1009. outR = &rgb[0];
  1010. outG = &rgb[1];
  1011. outB = &rgb[2];
  1012. break;
  1013. case DC1394_COLOR_FILTER_GBRG:
  1014. case DC1394_COLOR_FILTER_RGGB:
  1015. outR = &rgb[2];
  1016. outG = &rgb[1];
  1017. outB = &rgb[0];
  1018. break;
  1019. default:
  1020. return DC1394_INVALID_COLOR_FILTER;
  1021. }
  1022. switch (tile) {
  1023. case DC1394_COLOR_FILTER_GRBG:
  1024. case DC1394_COLOR_FILTER_BGGR:
  1025. outR = &rgb[0];
  1026. outG = &rgb[1];
  1027. outB = &rgb[2];
  1028. break;
  1029. case DC1394_COLOR_FILTER_GBRG:
  1030. case DC1394_COLOR_FILTER_RGGB:
  1031. outR = &rgb[2];
  1032. outG = &rgb[1];
  1033. outB = &rgb[0];
  1034. break;
  1035. default:
  1036. outR = NULL;
  1037. outG = NULL;
  1038. outB = NULL;
  1039. break;
  1040. }
  1041. switch (tile) {
  1042. case DC1394_COLOR_FILTER_GRBG: //---------------------------------------------------------
  1043. case DC1394_COLOR_FILTER_GBRG:
  1044. for (i = 0; i < sy - 1; i += 2) {
  1045. for (j = 0; j < sx - 1; j += 2) {
  1046. base = i * sx + j;
  1047. tmp = ((bayer[base] + bayer[base + sx + 1]) >> 1);
  1048. CLIP16(tmp, outG[base * 3], bits);
  1049. tmp = bayer[base + 1];
  1050. CLIP16(tmp, outR[base * 3], bits);
  1051. tmp = bayer[base + sx];
  1052. CLIP16(tmp, outB[base * 3], bits);
  1053. }
  1054. }
  1055. for (i = 0; i < sy - 1; i += 2) {
  1056. for (j = 1; j < sx - 1; j += 2) {
  1057. base = i * sx + j;
  1058. tmp = ((bayer[base + 1] + bayer[base + sx]) >> 1);
  1059. CLIP16(tmp, outG[(base) * 3], bits);
  1060. tmp = bayer[base];
  1061. CLIP16(tmp, outR[(base) * 3], bits);
  1062. tmp = bayer[base + 1 + sx];
  1063. CLIP16(tmp, outB[(base) * 3], bits);
  1064. }
  1065. }
  1066. for (i = 1; i < sy - 1; i += 2) {
  1067. for (j = 0; j < sx - 1; j += 2) {
  1068. base = i * sx + j;
  1069. tmp = ((bayer[base + sx] + bayer[base + 1]) >> 1);
  1070. CLIP16(tmp, outG[base * 3], bits);
  1071. tmp = bayer[base + sx + 1];
  1072. CLIP16(tmp, outR[base * 3], bits);
  1073. tmp = bayer[base];
  1074. CLIP16(tmp, outB[base * 3], bits);
  1075. }
  1076. }
  1077. for (i = 1; i < sy - 1; i += 2) {
  1078. for (j = 1; j < sx - 1; j += 2) {
  1079. base = i * sx + j;
  1080. tmp = ((bayer[base] + bayer[base + 1 + sx]) >> 1);
  1081. CLIP16(tmp, outG[(base) * 3], bits);
  1082. tmp = bayer[base + sx];
  1083. CLIP16(tmp, outR[(base) * 3], bits);
  1084. tmp = bayer[base + 1];
  1085. CLIP16(tmp, outB[(base) * 3], bits);
  1086. }
  1087. }
  1088. break;
  1089. case DC1394_COLOR_FILTER_BGGR: //---------------------------------------------------------
  1090. case DC1394_COLOR_FILTER_RGGB:
  1091. for (i = 0; i < sy - 1; i += 2) {
  1092. for (j = 0; j < sx - 1; j += 2) {
  1093. base = i * sx + j;
  1094. tmp = ((bayer[base + sx] + bayer[base + 1]) >> 1);
  1095. CLIP16(tmp, outG[base * 3], bits);
  1096. tmp = bayer[base + sx + 1];
  1097. CLIP16(tmp, outR[base * 3], bits);
  1098. tmp = bayer[base];
  1099. CLIP16(tmp, outB[base * 3], bits);
  1100. }
  1101. }
  1102. for (i = 1; i < sy - 1; i += 2) {
  1103. for (j = 0; j < sx - 1; j += 2) {
  1104. base = i * sx + j;
  1105. tmp = ((bayer[base] + bayer[base + 1 + sx]) >> 1);
  1106. CLIP16(tmp, outG[(base) * 3], bits);
  1107. tmp = bayer[base + 1];
  1108. CLIP16(tmp, outR[(base) * 3], bits);
  1109. tmp = bayer[base + sx];
  1110. CLIP16(tmp, outB[(base) * 3], bits);
  1111. }
  1112. }
  1113. for (i = 0; i < sy - 1; i += 2) {
  1114. for (j = 1; j < sx - 1; j += 2) {
  1115. base = i * sx + j;
  1116. tmp = ((bayer[base] + bayer[base + sx + 1]) >> 1);
  1117. CLIP16(tmp, outG[base * 3], bits);
  1118. tmp = bayer[base + sx];
  1119. CLIP16(tmp, outR[base * 3], bits);
  1120. tmp = bayer[base + 1];
  1121. CLIP16(tmp, outB[base * 3], bits);
  1122. }
  1123. }
  1124. for (i = 1; i < sy - 1; i += 2) {
  1125. for (j = 1; j < sx - 1; j += 2) {
  1126. base = i * sx + j;
  1127. tmp = ((bayer[base + 1] + bayer[base + sx]) >> 1);
  1128. CLIP16(tmp, outG[(base) * 3], bits);
  1129. tmp = bayer[base];
  1130. CLIP16(tmp, outR[(base) * 3], bits);
  1131. tmp = bayer[base + 1 + sx];
  1132. CLIP16(tmp, outB[(base) * 3], bits);
  1133. }
  1134. }
  1135. break;
  1136. }
  1137. /* add black border */
  1138. for (i = sx * (sy - 1) * 3; i < sx * sy * 3; i++) {
  1139. rgb[i] = 0;
  1140. }
  1141. for (i = (sx - 1) * 3; i < sx * sy * 3; i += (sx - 1) * 3) {
  1142. rgb[i++] = 0;
  1143. rgb[i++] = 0;
  1144. rgb[i++] = 0;
  1145. }
  1146. return DC1394_SUCCESS;
  1147. }
  1148. /* Variable Number of Gradients, from dcraw <http://www.cybercom.net/~dcoffin/dcraw/> */
  1149. /* Ported to libdc1394 by Frederic Devernay */
  1150. #define FORC3 for (c=0; c < 3; c++)
  1151. #define SQR(x) ((x)*(x))
  1152. #define ABS(x) (((int)(x) ^ ((int)(x) >> 31)) - ((int)(x) >> 31))
  1153. #ifndef MIN
  1154. #define MIN(a,b) ((a) < (b) ? (a) : (b))
  1155. #endif
  1156. #ifndef MAX
  1157. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  1158. #endif
  1159. #define LIM(x,min,max) MAX(min,MIN(x,max))
  1160. #define ULIM(x,y,z) ((y) < (z) ? LIM(x,y,z) : LIM(x,z,y))
  1161. /*
  1162. In order to inline this calculation, I make the risky
  1163. assumption that all filter patterns can be described
  1164. by a repeating pattern of eight rows and two columns
  1165. Return values are either 0/1/2/3 = G/M/C/Y or 0/1/2/3 = R/G1/B/G2
  1166. */
  1167. #define FC(row,col) \
  1168. (filters >> ((((row) << 1 & 14) + ((col) & 1)) << 1) & 3)
  1169. /*
  1170. This algorithm is officially called:
  1171. "Interpolation using a Threshold-based variable number of gradients"
  1172. described in http://www-ise.stanford.edu/~tingchen/algodep/vargra.html
  1173. I've extended the basic idea to work with non-Bayer filter arrays.
  1174. Gradients are numbered clockwise from NW=0 to W=7.
  1175. */
  1176. static const signed char bayervng_terms[] = {
  1177. -2,-2,+0,-1,0,0x01, -2,-2,+0,+0,1,0x01, -2,-1,-1,+0,0,0x01,
  1178. -2,-1,+0,-1,0,0x02, -2,-1,+0,+0,0,0x03, -2,-1,+0,+1,1,0x01,
  1179. -2,+0,+0,-1,0,0x06, -2,+0,+0,+0,1,0x02, -2,+0,+0,+1,0,0x03,
  1180. -2,+1,-1,+0,0,0x04, -2,+1,+0,-1,1,0x04, -2,+1,+0,+0,0,0x06,
  1181. -2,+1,+0,+1,0,0x02, -2,+2,+0,+0,1,0x04, -2,+2,+0,+1,0,0x04,
  1182. -1,-2,-1,+0,0,0x80, -1,-2,+0,-1,0,0x01, -1,-2,+1,-1,0,0x01,
  1183. -1,-2,+1,+0,1,0x01, -1,-1,-1,+1,0,0x88, -1,-1,+1,-2,0,0x40,
  1184. -1,-1,+1,-1,0,0x22, -1,-1,+1,+0,0,0x33, -1,-1,+1,+1,1,0x11,
  1185. -1,+0,-1,+2,0,0x08, -1,+0,+0,-1,0,0x44, -1,+0,+0,+1,0,0x11,
  1186. -1,+0,+1,-2,1,0x40, -1,+0,+1,-1,0,0x66, -1,+0,+1,+0,1,0x22,
  1187. -1,+0,+1,+1,0,0x33, -1,+0,+1,+2,1,0x10, -1,+1,+1,-1,1,0x44,
  1188. -1,+1,+1,+0,0,0x66, -1,+1,+1,+1,0,0x22, -1,+1,+1,+2,0,0x10,
  1189. -1,+2,+0,+1,0,0x04, -1,+2,+1,+0,1,0x04, -1,+2,+1,+1,0,0x04,
  1190. +0,-2,+0,+0,1,0x80, +0,-1,+0,+1,1,0x88, +0,-1,+1,-2,0,0x40,
  1191. +0,-1,+1,+0,0,0x11, +0,-1,+2,-2,0,0x40, +0,-1,+2,-1,0,0x20,
  1192. +0,-1,+2,+0,0,0x30, +0,-1,+2,+1,1,0x10, +0,+0,+0,+2,1,0x08,
  1193. +0,+0,+2,-2,1,0x40, +0,+0,+2,-1,0,0x60, +0,+0,+2,+0,1,0x20,
  1194. +0,+0,+2,+1,0,0x30, +0,+0,+2,+2,1,0x10, +0,+1,+1,+0,0,0x44,
  1195. +0,+1,+1,+2,0,0x10, +0,+1,+2,-1,1,0x40, +0,+1,+2,+0,0,0x60,
  1196. +0,+1,+2,+1,0,0x20, +0,+1,+2,+2,0,0x10, +1,-2,+1,+0,0,0x80,
  1197. +1,-1,+1,+1,0,0x88, +1,+0,+1,+2,0,0x08, +1,+0,+2,-1,0,0x40,
  1198. +1,+0,+2,+1,0,0x10
  1199. }, bayervng_chood[] = { -1,-1, -1,0, -1,+1, 0,+1, +1,+1, +1,0, +1,-1, 0,-1 };
  1200. dc1394error_t
  1201. dc1394_bayer_VNG(const uint8_t *restrict bayer,
  1202. uint8_t *restrict dst, int sx, int sy,
  1203. dc1394color_filter_t pattern)
  1204. {
  1205. const int height = sy, width = sx;
  1206. static const signed char *cp;
  1207. /* the following has the same type as the image */
  1208. uint8_t (*brow[5])[3], *pix; /* [FD] */
  1209. int code[8][2][320], *ip, gval[8], gmin, gmax, sum[4];
  1210. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  1211. int g, diff, thold, num, c;
  1212. uint32_t filters; /* [FD] */
  1213. /* first, use bilinear bayer decoding */
  1214. dc1394_bayer_Bilinear(bayer, dst, sx, sy, pattern);
  1215. switch(pattern) {
  1216. case DC1394_COLOR_FILTER_BGGR:
  1217. filters = 0x16161616;
  1218. break;
  1219. case DC1394_COLOR_FILTER_GRBG:
  1220. filters = 0x61616161;
  1221. break;
  1222. case DC1394_COLOR_FILTER_RGGB:
  1223. filters = 0x94949494;
  1224. break;
  1225. case DC1394_COLOR_FILTER_GBRG:
  1226. filters = 0x49494949;
  1227. break;
  1228. default:
  1229. return DC1394_INVALID_COLOR_FILTER;
  1230. }
  1231. for (row=0; row < 8; row++) { /* Precalculate for VNG */
  1232. for (col=0; col < 2; col++) {
  1233. ip = code[row][col];
  1234. for (cp=bayervng_terms, t=0; t < 64; t++) {
  1235. y1 = *cp++; x1 = *cp++;
  1236. y2 = *cp++; x2 = *cp++;
  1237. weight = *cp++;
  1238. grads = *cp++;
  1239. color = FC(row+y1,col+x1);
  1240. if (FC(row+y2,col+x2) != color) continue;
  1241. diag = (FC(row,col+1) == color && FC(row+1,col) == color) ? 2:1;
  1242. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  1243. *ip++ = (y1*width + x1)*3 + color; /* [FD] */
  1244. *ip++ = (y2*width + x2)*3 + color; /* [FD] */
  1245. *ip++ = weight;
  1246. for (g=0; g < 8; g++)
  1247. if (grads & 1<<g) *ip++ = g;
  1248. *ip++ = -1;
  1249. }
  1250. *ip++ = INT_MAX;
  1251. for (cp=bayervng_chood, g=0; g < 8; g++) {
  1252. y = *cp++; x = *cp++;
  1253. *ip++ = (y*width + x) * 3; /* [FD] */
  1254. color = FC(row,col);
  1255. if (FC(row+y,col+x) != color && FC(row+y*2,col+x*2) == color)
  1256. *ip++ = (y*width + x) * 6 + color; /* [FD] */
  1257. else
  1258. *ip++ = 0;
  1259. }
  1260. }
  1261. }
  1262. brow[4] = calloc (width*3, sizeof **brow);
  1263. //merror (brow[4], "vng_interpolate()");
  1264. for (row=0; row < 3; row++)
  1265. brow[row] = brow[4] + row*width;
  1266. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  1267. for (col=2; col < width-2; col++) {
  1268. pix = dst + (row*width+col)*3; /* [FD] */
  1269. ip = code[row & 7][col & 1];
  1270. memset (gval, 0, sizeof gval);
  1271. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  1272. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  1273. gval[ip[3]] += diff;
  1274. ip += 5;
  1275. if ((g = ip[-1]) == -1) continue;
  1276. gval[g] += diff;
  1277. while ((g = *ip++) != -1)
  1278. gval[g] += diff;
  1279. }
  1280. ip++;
  1281. gmin = gmax = gval[0]; /* Choose a threshold */
  1282. for (g=1; g < 8; g++) {
  1283. if (gmin > gval[g]) gmin = gval[g];
  1284. if (gmax < gval[g]) gmax = gval[g];
  1285. }
  1286. if (gmax == 0) {
  1287. memcpy (brow[2][col], pix, 3 * sizeof *dst); /* [FD] */
  1288. continue;
  1289. }
  1290. thold = gmin + (gmax >> 1);
  1291. memset (sum, 0, sizeof sum);
  1292. color = FC(row,col);
  1293. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  1294. if (gval[g] <= thold) {
  1295. for (c=0; c < 3; c++) /* [FD] */
  1296. if (c == color && ip[1])
  1297. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  1298. else
  1299. sum[c] += pix[ip[0] + c];
  1300. num++;
  1301. }
  1302. }
  1303. for (c=0; c < 3; c++) { /* [FD] Save to buffer */
  1304. t = pix[color];
  1305. if (c != color)
  1306. t += (sum[c] - sum[color]) / num;
  1307. CLIP(t,brow[2][col][c]); /* [FD] */
  1308. }
  1309. }
  1310. if (row > 3) /* Write buffer to image */
  1311. memcpy (dst + 3*((row-2)*width+2), brow[0]+2, (width-4)*3*sizeof *dst); /* [FD] */
  1312. for (g=0; g < 4; g++)
  1313. brow[(g-1) & 3] = brow[g];
  1314. }
  1315. memcpy (dst + 3*((row-2)*width+2), brow[0]+2, (width-4)*3*sizeof *dst);
  1316. memcpy (dst + 3*((row-1)*width+2), brow[1]+2, (width-4)*3*sizeof *dst);
  1317. free (brow[4]);
  1318. return DC1394_SUCCESS;
  1319. }
  1320. dc1394error_t
  1321. dc1394_bayer_VNG_uint16(const uint16_t *restrict bayer,
  1322. uint16_t *restrict dst, int sx, int sy,
  1323. dc1394color_filter_t pattern, int bits)
  1324. {
  1325. const int height = sy, width = sx;
  1326. static const signed char *cp;
  1327. /* the following has the same type as the image */
  1328. uint16_t (*brow[5])[3], *pix; /* [FD] */
  1329. int code[8][2][320], *ip, gval[8], gmin, gmax, sum[4];
  1330. int row, col, x, y, x1, x2, y1, y2, t, weight, grads, color, diag;
  1331. int g, diff, thold, num, c;
  1332. uint32_t filters; /* [FD] */
  1333. /* first, use bilinear bayer decoding */
  1334. dc1394_bayer_Bilinear_uint16(bayer, dst, sx, sy, pattern, bits);
  1335. switch(pattern) {
  1336. case DC1394_COLOR_FILTER_BGGR:
  1337. filters = 0x16161616;
  1338. break;
  1339. case DC1394_COLOR_FILTER_GRBG:
  1340. filters = 0x61616161;
  1341. break;
  1342. case DC1394_COLOR_FILTER_RGGB:
  1343. filters = 0x94949494;
  1344. break;
  1345. case DC1394_COLOR_FILTER_GBRG:
  1346. filters = 0x49494949;
  1347. break;
  1348. default:
  1349. return DC1394_INVALID_COLOR_FILTER;
  1350. }
  1351. for (row=0; row < 8; row++) { /* Precalculate for VNG */
  1352. for (col=0; col < 2; col++) {
  1353. ip = code[row][col];
  1354. for (cp=bayervng_terms, t=0; t < 64; t++) {
  1355. y1 = *cp++; x1 = *cp++;
  1356. y2 = *cp++; x2 = *cp++;
  1357. weight = *cp++;
  1358. grads = *cp++;
  1359. color = FC(row+y1,col+x1);
  1360. if (FC(row+y2,col+x2) != color) continue;
  1361. diag = (FC(row,col+1) == color && FC(row+1,col) == color) ? 2:1;
  1362. if (abs(y1-y2) == diag && abs(x1-x2) == diag) continue;
  1363. *ip++ = (y1*width + x1)*3 + color; /* [FD] */
  1364. *ip++ = (y2*width + x2)*3 + color; /* [FD] */
  1365. *ip++ = weight;
  1366. for (g=0; g < 8; g++)
  1367. if (grads & 1<<g) *ip++ = g;
  1368. *ip++ = -1;
  1369. }
  1370. *ip++ = INT_MAX;
  1371. for (cp=bayervng_chood, g=0; g < 8; g++) {
  1372. y = *cp++; x = *cp++;
  1373. *ip++ = (y*width + x) * 3; /* [FD] */
  1374. color = FC(row,col);
  1375. if (FC(row+y,col+x) != color && FC(row+y*2,col+x*2) == color)
  1376. *ip++ = (y*width + x) * 6 + color; /* [FD] */
  1377. else
  1378. *ip++ = 0;
  1379. }
  1380. }
  1381. }
  1382. brow[4] = calloc (width*3, sizeof **brow);
  1383. //merror (brow[4], "vng_interpolate()");
  1384. for (row=0; row < 3; row++)
  1385. brow[row] = brow[4] + row*width;
  1386. for (row=2; row < height-2; row++) { /* Do VNG interpolation */
  1387. for (col=2; col < width-2; col++) {
  1388. pix = dst + (row*width+col)*3; /* [FD] */
  1389. ip = code[row & 7][col & 1];
  1390. memset (gval, 0, sizeof gval);
  1391. while ((g = ip[0]) != INT_MAX) { /* Calculate gradients */
  1392. diff = ABS(pix[g] - pix[ip[1]]) << ip[2];
  1393. gval[ip[3]] += diff;
  1394. ip += 5;
  1395. if ((g = ip[-1]) == -1) continue;
  1396. gval[g] += diff;
  1397. while ((g = *ip++) != -1)
  1398. gval[g] += diff;
  1399. }
  1400. ip++;
  1401. gmin = gmax = gval[0]; /* Choose a threshold */
  1402. for (g=1; g < 8; g++) {
  1403. if (gmin > gval[g]) gmin = gval[g];
  1404. if (gmax < gval[g]) gmax = gval[g];
  1405. }
  1406. if (gmax == 0) {
  1407. memcpy (brow[2][col], pix, 3 * sizeof *dst); /* [FD] */
  1408. continue;
  1409. }
  1410. thold = gmin + (gmax >> 1);
  1411. memset (sum, 0, sizeof sum);
  1412. color = FC(row,col);
  1413. for (num=g=0; g < 8; g++,ip+=2) { /* Average the neighbors */
  1414. if (gval[g] <= thold) {
  1415. for (c=0; c < 3; c++) /* [FD] */
  1416. if (c == color && ip[1])
  1417. sum[c] += (pix[c] + pix[ip[1]]) >> 1;
  1418. else
  1419. sum[c] += pix[ip[0] + c];
  1420. num++;
  1421. }
  1422. }
  1423. for (c=0; c < 3; c++) { /* [FD] Save to buffer */
  1424. t = pix[color];
  1425. if (c != color)
  1426. t += (sum[c] - sum[color]) / num;
  1427. CLIP16(t,brow[2][col][c],bits); /* [FD] */
  1428. }
  1429. }
  1430. if (row > 3) /* Write buffer to image */
  1431. memcpy (dst + 3*((row-2)*width+2), brow[0]+2, (width-4)*3*sizeof *dst); /* [FD] */
  1432. for (g=0; g < 4; g++)
  1433. brow[(g-1) & 3] = brow[g];
  1434. }
  1435. memcpy (dst + 3*((row-2)*width+2), brow[0]+2, (width-4)*3*sizeof *dst);
  1436. memcpy (dst + 3*((row-1)*width+2), brow[1]+2, (width-4)*3*sizeof *dst);
  1437. free (brow[4]);
  1438. return DC1394_SUCCESS;
  1439. }
  1440. /* AHD interpolation ported from dcraw to libdc1394 by Samuel Audet */
  1441. static dc1394bool_t ahd_inited = DC1394_FALSE; /* WARNING: not multi-processor safe */
  1442. #define CLIPOUT(x) LIM(x,0,255)
  1443. #define CLIPOUT16(x,bits) LIM(x,0,((1<<bits)-1))
  1444. static const double xyz_rgb[3][3] = { /* XYZ from RGB */
  1445. { 0.412453, 0.357580, 0.180423 },
  1446. { 0.212671, 0.715160, 0.072169 },
  1447. { 0.019334, 0.119193, 0.950227 } };
  1448. static const float d65_white[3] = { 0.950456, 1, 1.088754 };
  1449. static void cam_to_cielab (uint16_t cam[3], float lab[3]) /* [SA] */
  1450. {
  1451. int c, i, j;
  1452. float r, xyz[3];
  1453. static float cbrt[0x10000], xyz_cam[3][4];
  1454. if (cam == NULL) {
  1455. for (i=0; i < 0x10000; i++) {
  1456. r = i / 65535.0;
  1457. cbrt[i] = r > 0.008856 ? pow(r,1/3.0) : 7.787*r + 16/116.0;
  1458. }
  1459. for (i=0; i < 3; i++)
  1460. for (j=0; j < 3; j++) /* [SA] */
  1461. xyz_cam[i][j] = xyz_rgb[i][j] / d65_white[i]; /* [SA] */
  1462. } else {
  1463. xyz[0] = xyz[1] = xyz[2] = 0.5;
  1464. FORC3 { /* [SA] */
  1465. xyz[0] += xyz_cam[0][c] * cam[c];
  1466. xyz[1] += xyz_cam[1][c] * cam[c];
  1467. xyz[2] += xyz_cam[2][c] * cam[c];
  1468. }
  1469. xyz[0] = cbrt[CLIPOUT16((int) xyz[0],16)]; /* [SA] */
  1470. xyz[1] = cbrt[CLIPOUT16((int) xyz[1],16)]; /* [SA] */
  1471. xyz[2] = cbrt[CLIPOUT16((int) xyz[2],16)]; /* [SA] */
  1472. lab[0] = 116 * xyz[1] - 16;
  1473. lab[1] = 500 * (xyz[0] - xyz[1]);
  1474. lab[2] = 200 * (xyz[1] - xyz[2]);
  1475. }
  1476. }
  1477. /*
  1478. Adaptive Homogeneity-Directed interpolation is based on
  1479. the work of Keigo Hirakawa, Thomas Parks, and Paul Lee.
  1480. */
  1481. #define TS 256 /* Tile Size */
  1482. dc1394error_t
  1483. dc1394_bayer_AHD(const uint8_t *restrict bayer,
  1484. uint8_t *restrict dst, int sx, int sy,
  1485. dc1394color_filter_t pattern)
  1486. {
  1487. int i, j, top, left, row, col, tr, tc, fc, c, d, val, hm[2];
  1488. /* the following has the same type as the image */
  1489. uint8_t (*pix)[3], (*rix)[3]; /* [SA] */
  1490. uint16_t rix16[3]; /* [SA] */
  1491. static const int dir[4] = { -1, 1, -TS, TS };
  1492. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  1493. float flab[3]; /* [SA] */
  1494. uint8_t (*rgb)[TS][TS][3];
  1495. short (*lab)[TS][TS][3];
  1496. char (*homo)[TS][TS], *buffer;
  1497. /* start - new code for libdc1394 */
  1498. uint32_t filters;
  1499. const int height = sy, width = sx;
  1500. int x, y;
  1501. if (ahd_inited==DC1394_FALSE) {
  1502. /* WARNING: this might not be multi-processor safe */
  1503. cam_to_cielab (NULL,NULL);
  1504. ahd_inited = DC1394_TRUE;
  1505. }
  1506. switch(pattern) {
  1507. case DC1394_COLOR_FILTER_BGGR:
  1508. filters = 0x16161616;
  1509. break;
  1510. case DC1394_COLOR_FILTER_GRBG:
  1511. filters = 0x61616161;
  1512. break;
  1513. case DC1394_COLOR_FILTER_RGGB:
  1514. filters = 0x94949494;
  1515. break;
  1516. case DC1394_COLOR_FILTER_GBRG:
  1517. filters = 0x49494949;
  1518. break;
  1519. default:
  1520. return DC1394_INVALID_COLOR_FILTER;
  1521. }
  1522. /* fill-in destination with known exact values */
  1523. for (y = 0; y < height; y++) {
  1524. for (x = 0; x < width; x++) {
  1525. int channel = FC(y,x);
  1526. dst[(y*width+x)*3 + channel] = bayer[y*width+x];
  1527. }
  1528. }
  1529. /* end - new code for libdc1394 */
  1530. /* start - code from border_interpolate (int border) */
  1531. {
  1532. int border = 3;
  1533. unsigned row, col, y, x, f, c, sum[8];
  1534. for (row=0; row < height; row++)
  1535. for (col=0; col < width; col++) {
  1536. if (col==border && row >= border && row < height-border)
  1537. col = width-border;
  1538. memset (sum, 0, sizeof sum);
  1539. for (y=row-1; y != row+2; y++)
  1540. for (x=col-1; x != col+2; x++)
  1541. if (y < height && x < width) {
  1542. f = FC(y,x);
  1543. sum[f] += dst[(y*width+x)*3 + f]; /* [SA] */
  1544. sum[f+4]++;
  1545. }
  1546. f = FC(row,col);
  1547. FORC3 if (c != f && sum[c+4]) /* [SA] */
  1548. dst[(row*width+col)*3 + c] = sum[c] / sum[c+4]; /* [SA] */
  1549. }
  1550. }
  1551. /* end - code from border_interpolate (int border) */
  1552. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  1553. /* merror (buffer, "ahd_interpolate()"); */
  1554. rgb = (uint8_t(*)[TS][TS][3]) buffer; /* [SA] */
  1555. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  1556. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  1557. for (top=0; top < height; top += TS-6)
  1558. for (left=0; left < width; left += TS-6) {
  1559. memset (rgb, 0, 12*TS*TS);
  1560. /* Interpolate green horizontally and vertically: */
  1561. for (row = top < 2 ? 2:top; row < top+TS && row < height-2; row++) {
  1562. col = left + (FC(row,left) == 1);
  1563. if (col < 2) col += 2;
  1564. for (fc = FC(row,col); col < left+TS && col < width-2; col+=2) {
  1565. pix = (uint8_t (*)[3])dst + (row*width+col); /* [SA] */
  1566. val = ((pix[-1][1] + pix[0][fc] + pix[1][1]) * 2
  1567. - pix[-2][fc] - pix[2][fc]) >> 2;
  1568. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  1569. val = ((pix[-width][1] + pix[0][fc] + pix[width][1]) * 2
  1570. - pix[-2*width][fc] - pix[2*width][fc]) >> 2;
  1571. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  1572. }
  1573. }
  1574. /* Interpolate red and blue, and convert to CIELab: */
  1575. for (d=0; d < 2; d++)
  1576. for (row=top+1; row < top+TS-1 && row < height-1; row++)
  1577. for (col=left+1; col < left+TS-1 && col < width-1; col++) {
  1578. pix = (uint8_t (*)[3])dst + (row*width+col); /* [SA] */
  1579. rix = &rgb[d][row-top][col-left];
  1580. if ((c = 2 - FC(row,col)) == 1) {
  1581. c = FC(row+1,col);
  1582. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  1583. - rix[-1][1] - rix[1][1] ) >> 1);
  1584. rix[0][2-c] = CLIPOUT(val); /* [SA] */
  1585. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  1586. - rix[-TS][1] - rix[TS][1] ) >> 1);
  1587. } else
  1588. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  1589. + pix[+width-1][c] + pix[+width+1][c]
  1590. - rix[-TS-1][1] - rix[-TS+1][1]
  1591. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  1592. rix[0][c] = CLIPOUT(val); /* [SA] */
  1593. c = FC(row,col);
  1594. rix[0][c] = pix[0][c];
  1595. rix16[0] = rix[0][0]; /* [SA] */
  1596. rix16[1] = rix[0][1]; /* [SA] */
  1597. rix16[2] = rix[0][2]; /* [SA] */
  1598. cam_to_cielab (rix16, flab); /* [SA] */
  1599. FORC3 lab[d][row-top][col-left][c] = 64*flab[c];
  1600. }
  1601. /* Build homogeneity maps from the CIELab images: */
  1602. memset (homo, 0, 2*TS*TS);
  1603. for (row=top+2; row < top+TS-2 && row < height; row++) {
  1604. tr = row-top;
  1605. for (col=left+2; col < left+TS-2 && col < width; col++) {
  1606. tc = col-left;
  1607. for (d=0; d < 2; d++)
  1608. for (i=0; i < 4; i++)
  1609. ldiff[d][i] = ABS(lab[d][tr][tc][0]-lab[d][tr][tc+dir[i]][0]);
  1610. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  1611. MAX(ldiff[1][2],ldiff[1][3]));
  1612. for (d=0; d < 2; d++)
  1613. for (i=0; i < 4; i++)
  1614. if (i >> 1 == d || ldiff[d][i] <= leps)
  1615. abdiff[d][i] = SQR(lab[d][tr][tc][1]-lab[d][tr][tc+dir[i]][1])
  1616. + SQR(lab[d][tr][tc][2]-lab[d][tr][tc+dir[i]][2]);
  1617. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  1618. MAX(abdiff[1][2],abdiff[1][3]));
  1619. for (d=0; d < 2; d++)
  1620. for (i=0; i < 4; i++)
  1621. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  1622. homo[d][tr][tc]++;
  1623. }
  1624. }
  1625. /* Combine the most homogenous pixels for the final result: */
  1626. for (row=top+3; row < top+TS-3 && row < height-3; row++) {
  1627. tr = row-top;
  1628. for (col=left+3; col < left+TS-3 && col < width-3; col++) {
  1629. tc = col-left;
  1630. for (d=0; d < 2; d++)
  1631. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  1632. for (j=tc-1; j <= tc+1; j++)
  1633. hm[d] += homo[d][i][j];
  1634. if (hm[0] != hm[1])
  1635. FORC3 dst[(row*width+col)*3 + c] = CLIPOUT(rgb[hm[1] > hm[0]][tr][tc][c]); /* [SA] */
  1636. else
  1637. FORC3 dst[(row*width+col)*3 + c] =
  1638. CLIPOUT((rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1); /* [SA] */
  1639. }
  1640. }
  1641. }
  1642. free (buffer);
  1643. return DC1394_SUCCESS;
  1644. }
  1645. dc1394error_t
  1646. dc1394_bayer_AHD_uint16(const uint16_t *restrict bayer,
  1647. uint16_t *restrict dst, int sx, int sy,
  1648. dc1394color_filter_t pattern, int bits)
  1649. {
  1650. int i, j, top, left, row, col, tr, tc, fc, c, d, val, hm[2];
  1651. /* the following has the same type as the image */
  1652. uint16_t (*pix)[3], (*rix)[3]; /* [SA] */
  1653. static const int dir[4] = { -1, 1, -TS, TS };
  1654. unsigned ldiff[2][4], abdiff[2][4], leps, abeps;
  1655. float flab[3];
  1656. uint16_t (*rgb)[TS][TS][3]; /* [SA] */
  1657. short (*lab)[TS][TS][3];
  1658. char (*homo)[TS][TS], *buffer;
  1659. /* start - new code for libdc1394 */
  1660. uint32_t filters;
  1661. const int height = sy, width = sx;
  1662. int x, y;
  1663. if (ahd_inited==DC1394_FALSE) {
  1664. /* WARNING: this might not be multi-processor safe */
  1665. cam_to_cielab (NULL,NULL);
  1666. ahd_inited = DC1394_TRUE;
  1667. }
  1668. switch(pattern) {
  1669. case DC1394_COLOR_FILTER_BGGR:
  1670. filters = 0x16161616;
  1671. break;
  1672. case DC1394_COLOR_FILTER_GRBG:
  1673. filters = 0x61616161;
  1674. break;
  1675. case DC1394_COLOR_FILTER_RGGB:
  1676. filters = 0x94949494;
  1677. break;
  1678. case DC1394_COLOR_FILTER_GBRG:
  1679. filters = 0x49494949;
  1680. break;
  1681. default:
  1682. return DC1394_INVALID_COLOR_FILTER;
  1683. }
  1684. /* fill-in destination with known exact values */
  1685. for (y = 0; y < height; y++) {
  1686. for (x = 0; x < width; x++) {
  1687. int channel = FC(y,x);
  1688. dst[(y*width+x)*3 + channel] = bayer[y*width+x];
  1689. }
  1690. }
  1691. /* end - new code for libdc1394 */
  1692. /* start - code from border_interpolate(int border) */
  1693. {
  1694. int border = 3;
  1695. unsigned row, col, y, x, f, c, sum[8];
  1696. for (row=0; row < height; row++)
  1697. for (col=0; col < width; col++) {
  1698. if (col==border && row >= border && row < height-border)
  1699. col = width-border;
  1700. memset (sum, 0, sizeof sum);
  1701. for (y=row-1; y != row+2; y++)
  1702. for (x=col-1; x != col+2; x++)
  1703. if (y < height && x < width) {
  1704. f = FC(y,x);
  1705. sum[f] += dst[(y*width+x)*3 + f]; /* [SA] */
  1706. sum[f+4]++;
  1707. }
  1708. f = FC(row,col);
  1709. FORC3 if (c != f && sum[c+4]) /* [SA] */
  1710. dst[(row*width+col)*3 + c] = sum[c] / sum[c+4]; /* [SA] */
  1711. }
  1712. }
  1713. /* end - code from border_interpolate(int border) */
  1714. buffer = (char *) malloc (26*TS*TS); /* 1664 kB */
  1715. /* merror (buffer, "ahd_interpolate()"); */
  1716. rgb = (uint16_t(*)[TS][TS][3]) buffer; /* [SA] */
  1717. lab = (short (*)[TS][TS][3])(buffer + 12*TS*TS);
  1718. homo = (char (*)[TS][TS]) (buffer + 24*TS*TS);
  1719. for (top=0; top < height; top += TS-6)
  1720. for (left=0; left < width; left += TS-6) {
  1721. memset (rgb, 0, 12*TS*TS);
  1722. /* Interpolate green horizontally and vertically: */
  1723. for (row = top < 2 ? 2:top; row < top+TS && row < height-2; row++) {
  1724. col = left + (FC(row,left) == 1);
  1725. if (col < 2) col += 2;
  1726. for (fc = FC(row,col); col < left+TS && col < width-2; col+=2) {
  1727. pix = (uint16_t (*)[3])dst + (row*width+col); /* [SA] */
  1728. val = ((pix[-1][1] + pix[0][fc] + pix[1][1]) * 2
  1729. - pix[-2][fc] - pix[2][fc]) >> 2;
  1730. rgb[0][row-top][col-left][1] = ULIM(val,pix[-1][1],pix[1][1]);
  1731. val = ((pix[-width][1] + pix[0][fc] + pix[width][1]) * 2
  1732. - pix[-2*width][fc] - pix[2*width][fc]) >> 2;
  1733. rgb[1][row-top][col-left][1] = ULIM(val,pix[-width][1],pix[width][1]);
  1734. }
  1735. }
  1736. /* Interpolate red and blue, and convert to CIELab: */
  1737. for (d=0; d < 2; d++)
  1738. for (row=top+1; row < top+TS-1 && row < height-1; row++)
  1739. for (col=left+1; col < left+TS-1 && col < width-1; col++) {
  1740. pix = (uint16_t (*)[3])dst + (row*width+col); /* [SA] */
  1741. rix = &rgb[d][row-top][col-left];
  1742. if ((c = 2 - FC(row,col)) == 1) {
  1743. c = FC(row+1,col);
  1744. val = pix[0][1] + (( pix[-1][2-c] + pix[1][2-c]
  1745. - rix[-1][1] - rix[1][1] ) >> 1);
  1746. rix[0][2-c] = CLIPOUT16(val, bits); /* [SA] */
  1747. val = pix[0][1] + (( pix[-width][c] + pix[width][c]
  1748. - rix[-TS][1] - rix[TS][1] ) >> 1);
  1749. } else
  1750. val = rix[0][1] + (( pix[-width-1][c] + pix[-width+1][c]
  1751. + pix[+width-1][c] + pix[+width+1][c]
  1752. - rix[-TS-1][1] - rix[-TS+1][1]
  1753. - rix[+TS-1][1] - rix[+TS+1][1] + 1) >> 2);
  1754. rix[0][c] = CLIPOUT16(val, bits); /* [SA] */
  1755. c = FC(row,col);
  1756. rix[0][c] = pix[0][c];
  1757. cam_to_cielab (rix[0], flab);
  1758. FORC3 lab[d][row-top][col-left][c] = 64*flab[c];
  1759. }
  1760. /* Build homogeneity maps from the CIELab images: */
  1761. memset (homo, 0, 2*TS*TS);
  1762. for (row=top+2; row < top+TS-2 && row < height; row++) {
  1763. tr = row-top;
  1764. for (col=left+2; col < left+TS-2 && col < width; col++) {
  1765. tc = col-left;
  1766. for (d=0; d < 2; d++)
  1767. for (i=0; i < 4; i++)
  1768. ldiff[d][i] = ABS(lab[d][tr][tc][0]-lab[d][tr][tc+dir[i]][0]);
  1769. leps = MIN(MAX(ldiff[0][0],ldiff[0][1]),
  1770. MAX(ldiff[1][2],ldiff[1][3]));
  1771. for (d=0; d < 2; d++)
  1772. for (i=0; i < 4; i++)
  1773. if (i >> 1 == d || ldiff[d][i] <= leps)
  1774. abdiff[d][i] = SQR(lab[d][tr][tc][1]-lab[d][tr][tc+dir[i]][1])
  1775. + SQR(lab[d][tr][tc][2]-lab[d][tr][tc+dir[i]][2]);
  1776. abeps = MIN(MAX(abdiff[0][0],abdiff[0][1]),
  1777. MAX(abdiff[1][2],abdiff[1][3]));
  1778. for (d=0; d < 2; d++)
  1779. for (i=0; i < 4; i++)
  1780. if (ldiff[d][i] <= leps && abdiff[d][i] <= abeps)
  1781. homo[d][tr][tc]++;
  1782. }
  1783. }
  1784. /* Combine the most homogenous pixels for the final result: */
  1785. for (row=top+3; row < top+TS-3 && row < height-3; row++) {
  1786. tr = row-top;
  1787. for (col=left+3; col < left+TS-3 && col < width-3; col++) {
  1788. tc = col-left;
  1789. for (d=0; d < 2; d++)
  1790. for (hm[d]=0, i=tr-1; i <= tr+1; i++)
  1791. for (j=tc-1; j <= tc+1; j++)
  1792. hm[d] += homo[d][i][j];
  1793. if (hm[0] != hm[1])
  1794. FORC3 dst[(row*width+col)*3 + c] = CLIPOUT16(rgb[hm[1] > hm[0]][tr][tc][c], bits); /* [SA] */
  1795. else
  1796. FORC3 dst[(row*width+col)*3 + c] =
  1797. CLIPOUT16((rgb[0][tr][tc][c] + rgb[1][tr][tc][c]) >> 1, bits); /* [SA] */
  1798. }
  1799. }
  1800. }
  1801. free (buffer);
  1802. return DC1394_SUCCESS;
  1803. }
  1804. dc1394error_t
  1805. dc1394_bayer_decoding_8bit(const uint8_t *restrict bayer, uint8_t *restrict rgb, uint32_t sx, uint32_t sy, dc1394color_filter_t tile, dc1394bayer_method_t method)
  1806. {
  1807. switch (method) {
  1808. case DC1394_BAYER_METHOD_NEAREST:
  1809. return dc1394_bayer_NearestNeighbor(bayer, rgb, sx, sy, tile);
  1810. case DC1394_BAYER_METHOD_SIMPLE:
  1811. return dc1394_bayer_Simple(bayer, rgb, sx, sy, tile);
  1812. case DC1394_BAYER_METHOD_BILINEAR:
  1813. return dc1394_bayer_Bilinear(bayer, rgb, sx, sy, tile);
  1814. case DC1394_BAYER_METHOD_HQLINEAR:
  1815. return dc1394_bayer_HQLinear(bayer, rgb, sx, sy, tile);
  1816. case DC1394_BAYER_METHOD_DOWNSAMPLE:
  1817. return dc1394_bayer_Downsample(bayer, rgb, sx, sy, tile);
  1818. case DC1394_BAYER_METHOD_EDGESENSE:
  1819. return dc1394_bayer_EdgeSense(bayer, rgb, sx, sy, tile);
  1820. case DC1394_BAYER_METHOD_VNG:
  1821. return dc1394_bayer_VNG(bayer, rgb, sx, sy, tile);
  1822. case DC1394_BAYER_METHOD_AHD:
  1823. return dc1394_bayer_AHD(bayer, rgb, sx, sy, tile);
  1824. default:
  1825. return DC1394_INVALID_BAYER_METHOD;
  1826. }
  1827. }
  1828. dc1394error_t
  1829. dc1394_bayer_decoding_16bit(const uint16_t *restrict bayer, uint16_t *restrict rgb, uint32_t sx, uint32_t sy, dc1394color_filter_t tile, dc1394bayer_method_t method, uint32_t bits)
  1830. {
  1831. switch (method) {
  1832. case DC1394_BAYER_METHOD_NEAREST:
  1833. return dc1394_bayer_NearestNeighbor_uint16(bayer, rgb, sx, sy, tile, bits);
  1834. case DC1394_BAYER_METHOD_SIMPLE:
  1835. return dc1394_bayer_Simple_uint16(bayer, rgb, sx, sy, tile, bits);
  1836. case DC1394_BAYER_METHOD_BILINEAR:
  1837. return dc1394_bayer_Bilinear_uint16(bayer, rgb, sx, sy, tile, bits);
  1838. case DC1394_BAYER_METHOD_HQLINEAR:
  1839. return dc1394_bayer_HQLinear_uint16(bayer, rgb, sx, sy, tile, bits);
  1840. case DC1394_BAYER_METHOD_DOWNSAMPLE:
  1841. return dc1394_bayer_Downsample_uint16(bayer, rgb, sx, sy, tile, bits);
  1842. case DC1394_BAYER_METHOD_EDGESENSE:
  1843. return dc1394_bayer_EdgeSense_uint16(bayer, rgb, sx, sy, tile, bits);
  1844. case DC1394_BAYER_METHOD_VNG:
  1845. return dc1394_bayer_VNG_uint16(bayer, rgb, sx, sy, tile, bits);
  1846. case DC1394_BAYER_METHOD_AHD:
  1847. return dc1394_bayer_AHD_uint16(bayer, rgb, sx, sy, tile, bits);
  1848. default:
  1849. return DC1394_INVALID_BAYER_METHOD;
  1850. }
  1851. }
  1852. #if 0
  1853. dc1394error_t
  1854. Adapt_buffer_bayer(dc1394video_frame_t *in, dc1394video_frame_t *out, dc1394bayer_method_t method)
  1855. {
  1856. uint32_t bpp;
  1857. // conversions will halve the buffer size if the method is DOWNSAMPLE:
  1858. out->size[0]=in->size[0];
  1859. out->size[1]=in->size[1];
  1860. if (method == DC1394_BAYER_METHOD_DOWNSAMPLE) {
  1861. out->size[0]/=2; // ODD SIZE CASES NOT TAKEN INTO ACCOUNT
  1862. out->size[1]/=2;
  1863. }
  1864. // as a convention we divide the image position by two in the case of a DOWNSAMPLE:
  1865. out->position[0]=in->position[0];
  1866. out->position[1]=in->position[1];
  1867. if (method == DC1394_BAYER_METHOD_DOWNSAMPLE) {
  1868. out->position[0]/=2;
  1869. out->position[1]/=2;
  1870. }
  1871. // the destination color coding is ALWAYS RGB. Set this.
  1872. if ( (in->color_coding==DC1394_COLOR_CODING_RAW16) ||
  1873. (in->color_coding==DC1394_COLOR_CODING_MONO16) )
  1874. out->color_coding=DC1394_COLOR_CODING_RGB16;
  1875. else
  1876. out->color_coding=DC1394_COLOR_CODING_RGB8;
  1877. // keep the color filter value in all cases. If the format is not raw it will not be further used anyway
  1878. out->color_filter=in->color_filter;
  1879. // The output is never YUV, hence nothing to do about YUV byte order
  1880. // bit depth is conserved for 16 bit and set to 8bit for 8bit:
  1881. if ( (in->color_coding==DC1394_COLOR_CODING_RAW16) ||
  1882. (in->color_coding==DC1394_COLOR_CODING_MONO16) )
  1883. out->data_depth=in->data_depth;
  1884. else
  1885. out->data_depth=8;
  1886. // don't know what to do with stride... >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
  1887. // out->stride=??
  1888. // the video mode should not change. Color coding and other stuff can be accessed in specific fields of this struct
  1889. out->video_mode = in->video_mode;
  1890. // padding is kept:
  1891. out->padding_bytes = in->padding_bytes;
  1892. // image bytes changes: >>>> TODO: STRIDE SHOULD BE TAKEN INTO ACCOUNT... <<<<
  1893. dc1394_get_color_coding_bit_size(out->color_coding, &bpp);
  1894. out->image_bytes=(out->size[0]*out->size[1]*bpp)/8;
  1895. // total is image_bytes + padding_bytes
  1896. out->total_bytes = out->image_bytes + out->padding_bytes;
  1897. // bytes-per-packet and packets_per_frame are internal data that can be kept as is.
  1898. out->packet_size = in->packet_size;
  1899. out->packets_per_frame = in->packets_per_frame;
  1900. // timestamp, frame_behind, id and camera are copied too:
  1901. out->timestamp = in->timestamp;
  1902. out->frames_behind = in->frames_behind;
  1903. out->camera = in->camera;
  1904. out->id = in->id;
  1905. // verify memory allocation:
  1906. if (out->total_bytes>out->allocated_image_bytes) {
  1907. free(out->image);
  1908. out->image=(uint8_t*)malloc(out->total_bytes*sizeof(uint8_t));
  1909. if (out->image)
  1910. out->allocated_image_bytes = out->total_bytes*sizeof(uint8_t);
  1911. else
  1912. out->allocated_image_bytes = 0;
  1913. }
  1914. // Copy padding bytes:
  1915. if(out->image)
  1916. memcpy(&(out->image[out->image_bytes]),&(in->image[in->image_bytes]),out->padding_bytes);
  1917. out->little_endian=0; // not used before 1.32 is out.
  1918. out->data_in_padding=0; // not used before 1.32 is out.
  1919. if(out->image)
  1920. return DC1394_SUCCESS;
  1921. return DC1394_MEMORY_ALLOCATION_FAILURE;
  1922. }
  1923. dc1394error_t
  1924. dc1394_debayer_frames(dc1394video_frame_t *in, dc1394video_frame_t *out, dc1394bayer_method_t method)
  1925. {
  1926. if ((method<DC1394_BAYER_METHOD_MIN)||(method>DC1394_BAYER_METHOD_MAX))
  1927. return DC1394_INVALID_BAYER_METHOD;
  1928. switch (in->color_coding) {
  1929. case DC1394_COLOR_CODING_RAW8:
  1930. case DC1394_COLOR_CODING_MONO8:
  1931. if(DC1394_SUCCESS != Adapt_buffer_bayer(in,out,method))
  1932. return DC1394_MEMORY_ALLOCATION_FAILURE;
  1933. switch (method) {
  1934. case DC1394_BAYER_METHOD_NEAREST:
  1935. return dc1394_bayer_NearestNeighbor(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1936. case DC1394_BAYER_METHOD_SIMPLE:
  1937. return dc1394_bayer_Simple(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1938. case DC1394_BAYER_METHOD_BILINEAR:
  1939. return dc1394_bayer_Bilinear(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1940. case DC1394_BAYER_METHOD_HQLINEAR:
  1941. return dc1394_bayer_HQLinear(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1942. case DC1394_BAYER_METHOD_DOWNSAMPLE:
  1943. return dc1394_bayer_Downsample(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1944. case DC1394_BAYER_METHOD_EDGESENSE:
  1945. return dc1394_bayer_EdgeSense(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1946. case DC1394_BAYER_METHOD_VNG:
  1947. return dc1394_bayer_VNG(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1948. case DC1394_BAYER_METHOD_AHD:
  1949. return dc1394_bayer_AHD(in->image, out->image, in->size[0], in->size[1], in->color_filter);
  1950. }
  1951. break;
  1952. case DC1394_COLOR_CODING_MONO16:
  1953. case DC1394_COLOR_CODING_RAW16:
  1954. if(DC1394_SUCCESS != Adapt_buffer_bayer(in,out,method))
  1955. return DC1394_MEMORY_ALLOCATION_FAILURE;
  1956. switch (method) {
  1957. case DC1394_BAYER_METHOD_NEAREST:
  1958. return dc1394_bayer_NearestNeighbor_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1959. case DC1394_BAYER_METHOD_SIMPLE:
  1960. return dc1394_bayer_Simple_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1961. case DC1394_BAYER_METHOD_BILINEAR:
  1962. return dc1394_bayer_Bilinear_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1963. case DC1394_BAYER_METHOD_HQLINEAR:
  1964. return dc1394_bayer_HQLinear_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1965. case DC1394_BAYER_METHOD_DOWNSAMPLE:
  1966. return dc1394_bayer_Downsample_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1967. case DC1394_BAYER_METHOD_EDGESENSE:
  1968. return dc1394_bayer_EdgeSense_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1969. case DC1394_BAYER_METHOD_VNG:
  1970. return dc1394_bayer_VNG_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1971. case DC1394_BAYER_METHOD_AHD:
  1972. return dc1394_bayer_AHD_uint16((uint16_t*)in->image, (uint16_t*)out->image, in->size[0], in->size[1], in->color_filter, in->data_depth);
  1973. }
  1974. break;
  1975. default:
  1976. return DC1394_FUNCTION_NOT_SUPPORTED;
  1977. }
  1978. return DC1394_SUCCESS;
  1979. }
  1980. #endif