PixMatrix.cpp 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085
  1. // PixMatrix.cpp: implementation of the CPixMatrix class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include <memory.h>
  6. #include <math.h>
  7. #include <stdio.h>
  8. #include "PixMatrix.h"
  9. #ifdef _DEBUG
  10. #undef THIS_FILE
  11. static char THIS_FILE[]=__FILE__;
  12. //#define new DEBUG_NEW
  13. #endif
  14. #define FULL_IMG_WIDTH 4000L
  15. #define FULL_IMG_HEIGHT 4000L
  16. #define PIXEL_MAX_VALUE 65536
  17. CPixMatrix* CPixMatrix::m_instance = NULL;
  18. CPixMatrix* CPixMatrix::Instance()
  19. {
  20. if(m_instance == NULL) {
  21. m_instance = new CPixMatrix(FULL_IMG_WIDTH, FULL_IMG_HEIGHT, 15, 45);
  22. }
  23. return m_instance;
  24. }
  25. CPixMatrix::CPixMatrix(int width, int height, int woffset, int hoffset, int nPixelMax)
  26. :m_nWidth(width), m_nHeight(height), m_nWOffset(woffset), m_nHOffset(hoffset), m_nPixelMax(nPixelMax)
  27. {
  28. m_BadPixelMap = NULL;
  29. m_BadNewPixelMap = NULL;
  30. m_BadPixArray = NULL;
  31. m_xLineMap = NULL;
  32. m_yLineMap = NULL;
  33. m_doubleLineMap = NULL;
  34. m_NewMap = new PIX_MAP[m_nHeight];
  35. //m_curImg = NULL;////////////////////////chenGN 2013.01.31
  36. //m_lastImg = NULL;////////////////////////chenGN 2013.01.31
  37. m_mapImg = NULL;
  38. m_curAvg = -1;
  39. m_lastAvg = -1;
  40. ZeroMemory(m_charFilename, 256);
  41. m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
  42. m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
  43. //nWidth = 0;
  44. //nHeight = 0;
  45. m_pBadPixNum = NULL;
  46. //m_datalen = new int [ 400 ];
  47. m_nLinePoint = 50;////×?D??μ??μ?êy, zhaoyiru, 2017.05.09
  48. m_nLineData = int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint);//×?′ó?μ????êy, zhaoyiru, 2017.05.09
  49. m_datalen = new int[ m_nLineData ] ;
  50. }
  51. CPixMatrix::~CPixMatrix()
  52. {
  53. if(m_NewMap) {
  54. delete [] m_NewMap;
  55. m_NewMap = NULL;
  56. }
  57. if(m_BadPixArray) {
  58. delete [] m_BadPixArray;
  59. m_BadPixArray = NULL;
  60. }
  61. if( m_TempImage != NULL )
  62. {
  63. delete [] m_TempImage;
  64. m_TempImage = NULL;
  65. }
  66. if( m_TempImage1 != NULL )
  67. {
  68. delete [] m_TempImage1;
  69. m_TempImage1 = NULL;
  70. }
  71. if (m_mapImg)//add by ys20180109不在析构函数中释放的话会造成内存泄漏
  72. {
  73. delete[] m_mapImg;
  74. m_mapImg = NULL;
  75. }//add end
  76. FreeBadPixelMap();
  77. //FreeBadNewPixelMap();
  78. FreeDBadLineMap();
  79. if( m_pBadPixNum != NULL )
  80. {
  81. //delete m_pBadPixNum;
  82. m_pBadPixNum = NULL;
  83. }
  84. if( m_datalen != NULL )
  85. {
  86. delete [] m_datalen;
  87. m_datalen = NULL;
  88. }
  89. }
  90. void CPixMatrix::CorrectBadPixels(WORD* image)
  91. {
  92. long line;
  93. long pix_num;
  94. long bad_pix_num;
  95. long bad_num;
  96. long *p_bad_pix_num;
  97. float new_value;
  98. long w_new_value;
  99. float divisor;
  100. WORD *p_line, *p_prev_line, *p_next_line;
  101. long unProcLineNum;
  102. long unProcTotalNum = 0;
  103. if(m_BadPixelMap == NULL){ // can't do anything without a map
  104. return;
  105. }
  106. if(m_BadPixArray == NULL) {
  107. m_BadPixArray = new long [MAXI_BADPIX_COUNT*sizeof(long)];
  108. if(m_BadPixArray == NULL) {
  109. return;
  110. }
  111. }
  112. MarkBadAdjacentPixels();
  113. for(line = 0 ; line < m_nHeight ; ++line) {
  114. m_NewMap[line].len = 0;
  115. }
  116. int nXOffset = m_nWOffset;
  117. int nYOffset = m_nHOffset;
  118. int nLeft = nXOffset;
  119. int nRight = m_nWidth - nXOffset - 1;
  120. int nTop = nYOffset;
  121. int nBottom = m_nHeight - nYOffset - 1;
  122. int nXStartPos = nLeft;
  123. int nXEndPos = nRight;
  124. int nYStartPos = nTop;
  125. int nYEndPos = nBottom;
  126. int nBadPxlInfo = 0;
  127. int nPixelPos = 0;
  128. int nIdxI = 0;
  129. float fPVSum = 0.0f;
  130. float fDivisor = 0.0f;
  131. int nAvgPxlVal = (int)(CalcGlbAvgPxlValueBySamp(image, m_nWidth, m_nHeight, nXOffset, nYOffset));
  132. //校正最上面一行的坏点的像素值
  133. if (0 < m_BadPixelMap[nTop].num_entries)
  134. {
  135. for (nIdxI = 0; nIdxI < m_BadPixelMap[nTop].num_entries; nIdxI++)
  136. {
  137. nBadPxlInfo = m_BadPixelMap[nTop].bad_pixel_num[nIdxI];
  138. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  139. fPVSum = 0.0f;
  140. fDivisor = 0.0f;
  141. //左边像素[nTop, nPixelPos - 1]
  142. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  143. {
  144. fPVSum += image[nTop * m_nWidth + nPixelPos - 1];
  145. fDivisor += 1.0f;
  146. }
  147. //左下角像素[nTop + 1, nPixelPos - 1]
  148. if ((nLeft <= (nPixelPos - 1)) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
  149. {
  150. fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos - 1];
  151. fDivisor += 0.707f;
  152. }
  153. //右边像素[nTop, nPixelPos + 1]
  154. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  155. {
  156. fPVSum += image[nTop * m_nWidth + nPixelPos + 1];
  157. fDivisor += 1.0f;
  158. }
  159. //右下角像素[nTop + 1, nPixelPos + 1]
  160. if (((nPixelPos + 1) <= nRight) && ((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
  161. {
  162. fPVSum += 0.707f * image[(nTop + 1) * m_nWidth + nPixelPos + 1];
  163. fDivisor += 0.707f;
  164. }
  165. //下边像素[nTop + 1, nPixelPos]
  166. if (((nTop + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  167. {
  168. fPVSum += image[(nTop + 1) * m_nWidth + nPixelPos];
  169. fDivisor += 1.0f;
  170. }
  171. if (0.9f < fDivisor)
  172. {
  173. image[nTop * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  174. }
  175. else
  176. {
  177. image[nTop * m_nWidth + nPixelPos] = nAvgPxlVal;
  178. }
  179. }
  180. }
  181. //校正最下面一行坏点的像素值
  182. if (0 < m_BadPixelMap[nBottom].num_entries)
  183. {
  184. for (nIdxI = 0; nIdxI < m_BadPixelMap[nBottom].num_entries; nIdxI++)
  185. {
  186. nBadPxlInfo = m_BadPixelMap[nBottom].bad_pixel_num[nIdxI];
  187. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  188. fPVSum = 0.0f;
  189. fDivisor = 0.0f;
  190. //左边像素[nBottom, nPixelPos - 1]
  191. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  192. {
  193. fPVSum += image[nBottom * m_nWidth + nPixelPos - 1];
  194. fDivisor += 1.0f;
  195. }
  196. //左上角像素[nBottom - 1, nPixelPos - 1]
  197. if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
  198. {
  199. fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos - 1];
  200. fDivisor += 0.707f;
  201. }
  202. //右边像素[nBottom, nPixelPos + 1]
  203. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  204. {
  205. fPVSum += image[nBottom * m_nWidth + nPixelPos + 1];
  206. fDivisor += 1.0f;
  207. }
  208. //右上角像素[nBottom - 1, nPixelPos + 1]
  209. if (((nPixelPos + 1) <= nRight) && (nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
  210. {
  211. fPVSum += 0.707f * image[(nBottom - 1) * m_nWidth + nPixelPos + 1];
  212. fDivisor += 0.707f;
  213. }
  214. //上边像素[nBottom - 1, nPixelPos]
  215. if ((nTop <= (nBottom - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  216. {
  217. fPVSum += image[(nBottom - 1) * m_nWidth + nPixelPos];
  218. fDivisor += 1.0f;
  219. }
  220. if (0.9f < fDivisor)
  221. {
  222. image[nBottom * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  223. }
  224. else
  225. {
  226. image[nBottom * m_nWidth + nPixelPos] = nAvgPxlVal;
  227. }
  228. }
  229. }
  230. //校正最左边列和最右边列的坏点的像素值
  231. int nRow = 0;
  232. int nLeftBadPxlFlag = -1;
  233. int nRightBadPxlFlag = -1;
  234. for (nRow = nTop + 1; nRow < nBottom; nRow++)
  235. {
  236. nLeftBadPxlFlag = -1;
  237. nRightBadPxlFlag = -1;
  238. for (nIdxI = 0; nIdxI < m_BadPixelMap[nRow].num_entries; nIdxI++)
  239. {
  240. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nIdxI];
  241. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  242. if (nPixelPos == nLeft)
  243. {
  244. nLeftBadPxlFlag = nIdxI;
  245. }
  246. else if (nPixelPos == nRight)
  247. {
  248. nRightBadPxlFlag = nIdxI;
  249. }
  250. }
  251. //校正最左列的坏点的像素值
  252. if (-1 < nLeftBadPxlFlag)
  253. {
  254. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nLeftBadPxlFlag];
  255. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  256. fPVSum = 0.0f;
  257. fDivisor = 0.0f;
  258. //上边像素[nRow - 1, nPixelPos]
  259. if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  260. {
  261. fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
  262. fDivisor += 1.0f;
  263. }
  264. //右上角像素[nRow - 1, nPixelPos + 1]
  265. if (((nPixelPos + 1) <= nRight) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_2) == 0))
  266. {
  267. fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos + 1];
  268. fDivisor += 0.707f;
  269. }
  270. //右边像素[nRow, nPixelPos + 1]
  271. if (((nPixelPos + 1) <= nRight) && ((nBadPxlInfo & NEIGHBOR_4) == 0))
  272. {
  273. fPVSum += image[nRow * m_nWidth + nPixelPos + 1];
  274. fDivisor += 1.0f;
  275. }
  276. //右下角像素[nRow + 1, nPixelPos + 1]
  277. if (((nPixelPos + 1) <= nRight) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_7) == 0))
  278. {
  279. fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos + 1];
  280. fDivisor += 0.707f;
  281. }
  282. //下边像素[nRow + 1, nPixelPos]
  283. if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  284. {
  285. fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
  286. fDivisor += 1.0f;
  287. }
  288. if (0.9f < fDivisor)
  289. {
  290. image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  291. }
  292. else
  293. {
  294. image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
  295. }
  296. }
  297. //校正最右列的坏点的像素值
  298. if (-1 < nRightBadPxlFlag)
  299. {
  300. nBadPxlInfo = m_BadPixelMap[nRow].bad_pixel_num[nRightBadPxlFlag];
  301. nPixelPos = nBadPxlInfo & OFFSET_MASK;
  302. fPVSum = 0.0f;
  303. fDivisor = 0.0f;
  304. //上边像素[nRow - 1, nPixelPos]
  305. if ((nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_1) == 0))
  306. {
  307. fPVSum += image[(nRow - 1) * m_nWidth + nPixelPos];
  308. fDivisor += 1.0f;
  309. }
  310. //左上角像素[nRow - 1, nPixelPos - 1]
  311. if ((nLeft <= (nPixelPos - 1)) && (nTop <= (nRow - 1)) && ((nBadPxlInfo & NEIGHBOR_0) == 0))
  312. {
  313. fPVSum += 0.707f * image[(nRow - 1) * m_nWidth + nPixelPos - 1];
  314. fDivisor += 0.707f;
  315. }
  316. //左边像素[nRow, nPixelPos - 1]
  317. if ((nLeft <= (nPixelPos - 1)) && ((nBadPxlInfo & NEIGHBOR_3) == 0))
  318. {
  319. fPVSum += image[nRow * m_nWidth + nPixelPos - 1];
  320. fDivisor += 1.0f;
  321. }
  322. //左下角像素[nRow + 1, nPixelPos - 1]
  323. if ((nLeft <= (nPixelPos - 1)) && ((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_5) == 0))
  324. {
  325. fPVSum += 0.707f * image[(nRow + 1) * m_nWidth + nPixelPos - 1];
  326. fDivisor += 0.707f;
  327. }
  328. //下边像素[nRow + 1, nPixelPos]
  329. if (((nRow + 1) <= nBottom) && ((nBadPxlInfo & NEIGHBOR_6) == 0))
  330. {
  331. fPVSum += image[(nRow + 1) * m_nWidth + nPixelPos];
  332. fDivisor += 1.0f;
  333. }
  334. if (0.9f < fDivisor)
  335. {
  336. image[nRow * m_nWidth + nPixelPos] = (int)(fPVSum / fDivisor);
  337. }
  338. else
  339. {
  340. image[nRow * m_nWidth + nPixelPos] = nAvgPxlVal;
  341. }
  342. }
  343. }
  344. //校正其他坏点的像素值
  345. for(line = nTop + 1; line < nBottom ; ++line)
  346. {
  347. /* Precalculate the column pointers */
  348. p_line = image + (line * m_nWidth);
  349. p_next_line = p_line + m_nWidth;
  350. p_prev_line = p_line - m_nWidth;
  351. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
  352. unProcLineNum = 0;
  353. m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
  354. for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num)
  355. {
  356. bad_pix_num = *p_bad_pix_num++;
  357. bad_num = bad_pix_num & OFFSET_MASK;
  358. if (bad_num == nLeft || bad_num == nRight)
  359. {
  360. continue;
  361. }
  362. new_value = 0;
  363. divisor = 0;
  364. nPixelPos = bad_pix_num & NEIGHBOR_1;
  365. if(nPixelPos == 0)
  366. {
  367. new_value += *(p_prev_line + bad_num);
  368. divisor += 1.0f;
  369. }
  370. nPixelPos = bad_pix_num & NEIGHBOR_3;
  371. if(nPixelPos == 0 && 0 < bad_num)
  372. {
  373. new_value += *(p_line + bad_num - 1);
  374. divisor += 1.0f;
  375. }
  376. nPixelPos = bad_pix_num & NEIGHBOR_4;
  377. if(nPixelPos == 0)
  378. {
  379. new_value += *(p_line + bad_num + 1);
  380. divisor += 1.0f;
  381. }
  382. nPixelPos = bad_pix_num & NEIGHBOR_6;
  383. if(nPixelPos == 0)
  384. {
  385. new_value += *(p_next_line + bad_num);
  386. divisor += 1.0f;
  387. }
  388. if(divisor < 1.1f && divisor > 0.9f)
  389. {
  390. nPixelPos = bad_pix_num & NEIGHBOR_0;
  391. if(nPixelPos == 0 && bad_num > 0)
  392. {
  393. new_value += 0.707f * *(p_prev_line + bad_num - 1);
  394. divisor += 0.707f;
  395. }
  396. nPixelPos = bad_pix_num & NEIGHBOR_2;
  397. if(nPixelPos == 0)
  398. {
  399. new_value += 0.707f * *(p_prev_line + bad_num + 1);
  400. divisor += 0.707f;
  401. }
  402. nPixelPos = bad_pix_num & NEIGHBOR_7;
  403. if(nPixelPos == 0)
  404. {
  405. new_value += 0.707f * *(p_next_line + bad_num + 1);
  406. divisor += 0.707f;
  407. }
  408. nPixelPos = bad_pix_num & NEIGHBOR_5;
  409. if(nPixelPos == 0)
  410. {
  411. new_value += 0.707f * *(p_next_line + bad_num - 1);
  412. divisor += 0.707f;
  413. }
  414. }
  415. // At least one neighbor is good
  416. if(divisor > 1.9f)
  417. {
  418. w_new_value = (long)(new_value / divisor);
  419. //w_new_value = floor((new_value / divisor)+0.5);
  420. *(p_line + bad_num) = (WORD)w_new_value;
  421. }
  422. else
  423. { /* Added the pixel to the new bad pixel map */
  424. unProcLineNum++;
  425. m_BadPixArray[unProcTotalNum] = bad_pix_num & OFFSET_MASK;
  426. unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
  427. }
  428. }
  429. m_NewMap[line].len = unProcLineNum;
  430. }
  431. //
  432. // Fix the remaining bad pixels until no more bad pixels are left
  433. //
  434. while(unProcTotalNum > 0)
  435. {
  436. MarkBadPixels(m_nHeight, m_NewMap);
  437. unProcTotalNum = 0;
  438. for(line = 1; line < m_nHeight - 1; ++line)
  439. {
  440. /* Precalculate the column pointers */
  441. p_line = image + line * m_nWidth;
  442. p_next_line = p_line + m_nWidth;
  443. p_prev_line = p_line - m_nWidth;
  444. p_bad_pix_num = m_NewMap[line].p_Pix;
  445. unProcLineNum = 0;
  446. m_NewMap[line].p_Pix = &m_BadPixArray[unProcTotalNum];
  447. for(pix_num = 0; pix_num < (int)m_NewMap[line].len; ++pix_num)
  448. {
  449. bad_pix_num = *p_bad_pix_num++;
  450. bad_num = bad_pix_num & OFFSET_MASK;
  451. new_value = 0;
  452. divisor = 0;
  453. if (nLeft == bad_num || nRight == bad_num)
  454. {
  455. continue;
  456. }
  457. nPixelPos = bad_pix_num & NEIGHBOR_1;
  458. if(nPixelPos == 0)
  459. {
  460. new_value += *(p_prev_line + bad_num);
  461. divisor += 1.0f;
  462. }
  463. nPixelPos = bad_pix_num & NEIGHBOR_3;
  464. if(nPixelPos == 0 && 0 < bad_num)
  465. {
  466. new_value += *(p_line + bad_num - 1);
  467. divisor += 1.0f;
  468. }
  469. nPixelPos = bad_pix_num & NEIGHBOR_4;
  470. if(nPixelPos == 0)
  471. {
  472. new_value += *(p_line + bad_num + 1);
  473. divisor += 1.0f;
  474. }
  475. nPixelPos = bad_pix_num & NEIGHBOR_6;
  476. if(nPixelPos == 0)
  477. {
  478. new_value += *(p_next_line + bad_num);
  479. divisor += 1.0f;
  480. }
  481. if(divisor < 1.1f && divisor > 0.9f)
  482. {
  483. nPixelPos = bad_pix_num & NEIGHBOR_0;
  484. if(nPixelPos == 0 && 0 < bad_num)
  485. {
  486. new_value += 0.707f * *(p_prev_line + bad_num - 1);
  487. divisor += 0.707f;
  488. }
  489. nPixelPos = bad_pix_num & NEIGHBOR_2;
  490. if(nPixelPos == 0)
  491. {
  492. new_value += 0.707f * *(p_prev_line + bad_num + 1);
  493. divisor += 0.707f;
  494. }
  495. nPixelPos = bad_pix_num & NEIGHBOR_7;
  496. if(nPixelPos == 0)
  497. {
  498. new_value += 0.707f * *(p_next_line + bad_num + 1);
  499. divisor += 0.707f;
  500. }
  501. nPixelPos = bad_pix_num & NEIGHBOR_5;
  502. if(nPixelPos == 0 && 0 < bad_num)
  503. {
  504. new_value += 0.707f * *(p_next_line + bad_num - 1);
  505. divisor += 0.707f;
  506. }
  507. }
  508. if(divisor > 1.9f)
  509. {
  510. //w_new_value = floor((new_value / divisor)+0.5);
  511. w_new_value = (long)((new_value / divisor)+0.5);
  512. *(p_line + bad_num) = (WORD)w_new_value;
  513. }
  514. else
  515. { /* Goes to the new map */
  516. unProcLineNum++;
  517. m_BadPixArray[unProcTotalNum] = bad_num;
  518. unProcTotalNum = (unProcTotalNum + 1) % MAXI_BADPIX_COUNT;
  519. }
  520. }
  521. m_NewMap[line].len = unProcLineNum;
  522. }
  523. }
  524. return;
  525. }
  526. #define CHECK_NEIGHBOR_NEW_U(a, b) \
  527. while(ju < New_Map[a].len) \
  528. { \
  529. jj = New_Map[a].p_Pix[ju] & OFFSET_MASK; \
  530. if(jj == (b))\
  531. bad_neighbor |= NEIGHBOR_1;\
  532. else if(jj == (b - 1)) \
  533. bad_neighbor |= NEIGHBOR_0; \
  534. else if(jj == (b + 1)) \
  535. bad_neighbor |= NEIGHBOR_2; \
  536. if(jj >= (b + 1)){ \
  537. ju = ju ? ju - 1 : ju;\
  538. break; }\
  539. if((ju + 1) >= New_Map[a].len) break; \
  540. ju++;\
  541. }
  542. #define CHECK_NEIGHBOR_U(a, b) \
  543. while(ju < m_BadPixelMap[a].num_entries) \
  544. { \
  545. jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK; \
  546. if(jj == (b))\
  547. bad_neighbor |= NEIGHBOR_1;\
  548. else if(jj == (b - 1)) \
  549. bad_neighbor |= NEIGHBOR_0; \
  550. else if(jj == (b + 1)) \
  551. bad_neighbor |= NEIGHBOR_2; \
  552. if(jj >= (b + 1)){ \
  553. ju = ju ? ju - 1 : ju;\
  554. break; } \
  555. if((ju + 1) >= m_BadPixelMap[a].num_entries) break; \
  556. ju++;\
  557. }
  558. #define CHECK_NEIGHBOR_NEW_D(a, b) \
  559. while(jd < New_Map[a].len) \
  560. { \
  561. jj = New_Map[a].p_Pix[jd] & OFFSET_MASK; \
  562. if(jj == (long)(b))\
  563. bad_neighbor |= NEIGHBOR_6;\
  564. else if(jj == (long)(b - 1)) \
  565. bad_neighbor |= NEIGHBOR_5; \
  566. else if(jj == (long)(b + 1)) \
  567. bad_neighbor |= NEIGHBOR_7; \
  568. if(jj >= (long)(b + 1)){ \
  569. jd = jd ? jd - 1 : jd;\
  570. break;} \
  571. if( ( jd + 1 ) >= New_Map[ a ].len ) break; \
  572. jd++;\
  573. }
  574. #define CHECK_NEIGHBOR_D(a, b) \
  575. while(jd < m_BadPixelMap[a].num_entries )\
  576. { \
  577. jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK; \
  578. if(jj == (b))\
  579. bad_neighbor |= NEIGHBOR_6;\
  580. else if(jj == (b - 1)) \
  581. bad_neighbor |= NEIGHBOR_5; \
  582. else if(jj == (b + 1)) \
  583. bad_neighbor |= NEIGHBOR_7; \
  584. if(jj >= (b + 1)){ \
  585. jd = jd ? jd - 1 : jd;\
  586. break;}\
  587. if((jd + 1) >= m_BadPixelMap[a].num_entries ) break; \
  588. jd++;\
  589. }
  590. void CPixMatrix::MarkBadPixels(int NumLines, PIX_MAP *New_Map)
  591. {
  592. long i, ju, jd;
  593. long jj;
  594. long pix_num;
  595. long *p_bad_pix;
  596. long bad_pix_num;
  597. long bad_neighbor;
  598. for(i = 1; i < NumLines - 1; ++i) {
  599. p_bad_pix = New_Map[i].p_Pix;
  600. for(pix_num = 0, ju = 0, jd = 0; pix_num < (int)New_Map[i].len; ++pix_num) {
  601. bad_pix_num = *p_bad_pix & OFFSET_MASK;
  602. bad_neighbor = 0;
  603. CHECK_NEIGHBOR_NEW_U((long)(i - 1), bad_pix_num);
  604. if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
  605. bad_neighbor |= NEIGHBOR_3;
  606. if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
  607. bad_neighbor |= NEIGHBOR_4;
  608. CHECK_NEIGHBOR_NEW_D(i + 1, bad_pix_num);
  609. *p_bad_pix++ = bad_pix_num | bad_neighbor;
  610. }
  611. }
  612. }
  613. // /****************************************************************************/
  614. // /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
  615. // that have adjacent bad pixels */
  616. //
  617. // void CPixMatrix::MarkBadAdjacentPixels()
  618. // {
  619. // long i, ju, jd;
  620. // long jj;
  621. // long pix_num;
  622. // long *p_bad_pix;
  623. // long bad_pix_num;
  624. // long bad_neighbor;
  625. //
  626. // /*
  627. // * Mark the bad pixel map for high-resolution image
  628. // */
  629. // if(m_BadPixelMap == NULL) return;
  630. // int
  631. // for(i = 1; i < (m_nHeight - 1); ++i)
  632. // {
  633. // p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
  634. // for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
  635. // {
  636. // bad_pix_num = *p_bad_pix & OFFSET_MASK;
  637. // bad_neighbor = 0;
  638. // CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
  639. // if((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1)
  640. // {
  641. // bad_neighbor |= NEIGHBOR_3;
  642. // }
  643. // if((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1)
  644. // {
  645. // bad_neighbor |= NEIGHBOR_4;
  646. // }
  647. // CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
  648. // *p_bad_pix++ = bad_pix_num | bad_neighbor;
  649. // }
  650. // }
  651. // }
  652. /****************************************************************************/
  653. /* Use the upper byte of the bad pixel map offset to pre-mark bad pixels
  654. that have adjacent bad pixels */
  655. //Modified by Alex Stocks on 2011/01/30
  656. void CPixMatrix::MarkBadAdjacentPixels()
  657. {
  658. if(m_BadPixelMap == NULL)
  659. {
  660. return;
  661. }
  662. long i, ju, jd;
  663. long jj;
  664. long pix_num;
  665. long *p_bad_pix;
  666. long bad_pix_num;
  667. long bad_neighbor;
  668. int a = 0;
  669. int b = 0;
  670. /*
  671. * Mark the bad pixel map for high-resolution image
  672. */
  673. int nXOffset = 0 < m_nWOffset ? m_nWOffset : 0;
  674. int nXStartPos = nXOffset;
  675. int nXEndPos = m_nWidth - nXOffset;
  676. int nYOffset = 0 < m_nHOffset ? m_nHOffset : 0;
  677. int nYStartPos = nYOffset;
  678. int nYEndPos = m_nHeight - nYOffset;
  679. int nLeft = nXStartPos;
  680. int nRight = nXEndPos - 1;
  681. int nTop = nYStartPos;
  682. int nBottom = nYEndPos - 1;
  683. //for(i = 1; i < (m_nHeight - 1); ++i)
  684. for (i = nYStartPos; i < nYEndPos; i++)
  685. {
  686. p_bad_pix = m_BadPixelMap[i].bad_pixel_num;
  687. for(pix_num = 0, ju = 0, jd = 0 ; pix_num < m_BadPixelMap[i].num_entries ; pix_num++)
  688. {
  689. bad_pix_num = *p_bad_pix & OFFSET_MASK;
  690. bad_neighbor = 0;
  691. //CHECK_NEIGHBOR_U((long)(i - 1), bad_pix_num);
  692. a = i - 1; //上一行
  693. b = bad_pix_num; //列
  694. //对每个坏点像素的上一行的三个位置进行检查
  695. while((nTop <= a) && (ju < m_BadPixelMap[a].num_entries))
  696. {
  697. jj = m_BadPixelMap[a].bad_pixel_num[ju] & OFFSET_MASK;
  698. if(jj == (b))
  699. {//上面
  700. bad_neighbor |= NEIGHBOR_1;
  701. }
  702. else if(nLeft <= (b - 1) && ((b - 1) == jj))
  703. {//左上
  704. bad_neighbor |= NEIGHBOR_0;
  705. }
  706. else if((b + 1) <= nRight && ((b + 1) == jj))
  707. {//右上
  708. bad_neighbor |= NEIGHBOR_2;
  709. }
  710. if((b + 1) <= jj)
  711. {
  712. ju = ju ? ju - 1 : ju;
  713. break;
  714. }
  715. if(m_BadPixelMap[a].num_entries <= (ju + 1))
  716. {
  717. break;
  718. }
  719. ju++;
  720. }
  721. //对坏点像素左边和右边进行检查
  722. if((nLeft <= (b - 1)) && ((*(p_bad_pix - 1) & OFFSET_MASK) == bad_pix_num - 1))
  723. {//左边
  724. bad_neighbor |= NEIGHBOR_3;
  725. }
  726. if(((b + 1) <= nRight) && ((*(p_bad_pix + 1) & OFFSET_MASK) == bad_pix_num + 1))
  727. {//右边
  728. bad_neighbor |= NEIGHBOR_4;
  729. }
  730. //CHECK_NEIGHBOR_D((long)(i + 1), bad_pix_num);
  731. a = i + 1; //下一行
  732. b = bad_pix_num; //列
  733. //对坏点像素的下一行进行检查
  734. while((a <= nBottom) && (jd < m_BadPixelMap[a].num_entries) )
  735. {
  736. jj = m_BadPixelMap[a].bad_pixel_num[jd] & OFFSET_MASK;
  737. if(jj == (b))
  738. {//下面
  739. bad_neighbor |= NEIGHBOR_6;
  740. }
  741. else if(nLeft <= (b - 1) && (jj == (b - 1)))
  742. {//左下
  743. bad_neighbor |= NEIGHBOR_5;
  744. }
  745. else if((b + 1) <= nRight && (jj == (b + 1)))
  746. {//右下
  747. bad_neighbor |= NEIGHBOR_7;
  748. }
  749. if(jj >= (b + 1))
  750. {
  751. jd = jd ? jd - 1 : jd;
  752. break;
  753. }
  754. if((jd + 1) >= m_BadPixelMap[a].num_entries )
  755. {
  756. break;
  757. }
  758. jd++;
  759. }
  760. //bad_neighbor记录了坏点位置8邻域内所有的坏点的位置
  761. *p_bad_pix++ = bad_pix_num | bad_neighbor;
  762. }
  763. }
  764. }
  765. bool CPixMatrix::LoadBadPixelMap(const char *fileName)
  766. {
  767. //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
  768. strcpy(m_charFilename, fileName);
  769. FILE *fp;
  770. int num_entries;
  771. int entry;
  772. int line_num;
  773. int file_line_num;
  774. long *p_bad_pixels;
  775. bool status = TRUE;
  776. long bad_pixel_num = 0;
  777. char *data_in = NULL;
  778. char *p_token;
  779. long num_bad_pixels = 0;
  780. /* Hopefully, the largest line in a bad pix map */
  781. // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
  782. data_in = new char [MAX_PIX_MAP_LINE];
  783. if(data_in == NULL) {
  784. status = FALSE;
  785. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
  786. }
  787. fp = fopen(fileName, "rb");
  788. if(fp == NULL) {
  789. status = FALSE;
  790. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
  791. }
  792. else {
  793. if(m_BadPixelMap != NULL) {
  794. FreeBadPixelMap();
  795. }
  796. //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
  797. m_BadPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
  798. if(m_BadPixelMap == NULL) {
  799. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
  800. status = FALSE;
  801. }
  802. else
  803. {
  804. for (int i=0;i<m_nHeight;i++)
  805. {
  806. m_BadPixelMap[i].bad_pixel_num = NULL;
  807. }
  808. }
  809. }
  810. /* Data structures created OK and file is open, let's roll */
  811. if(status == TRUE) {
  812. line_num = 0;
  813. while((line_num < m_nHeight) && (status == TRUE)) {
  814. if(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  815. // Each line in bad_pixel.map begins with "linenum,num_entries: "
  816. if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
  817. // Verify the line number read matches the line num we are on
  818. if(line_num != file_line_num) {
  819. status = FALSE;
  820. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
  821. }
  822. // Verify that num_entries is not larger than pixels-per-line
  823. if(num_entries > m_nWidth) {
  824. status = FALSE;
  825. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
  826. // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
  827. }
  828. /* Tokenize the header */
  829. p_token = strtok(data_in, ":");
  830. if(status == TRUE) {
  831. /* Create the correct size bad pixel map entry */
  832. //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
  833. m_BadPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
  834. if( (m_BadPixelMap)[line_num].bad_pixel_num == NULL) {
  835. status = FALSE;
  836. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
  837. }
  838. else {
  839. memset( (m_BadPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
  840. p_bad_pixels = (m_BadPixelMap)[line_num].bad_pixel_num;
  841. (m_BadPixelMap)[line_num].num_entries = num_entries;
  842. /* Fill in the array */
  843. for(entry = 0 ; entry < num_entries ; ++entry) {
  844. /*
  845. * The remainder of each line of the Bad Pixel File consists
  846. * of numbers indicating the x-coordinate value of the bad
  847. * pixels. They should be in ascending order. None of them
  848. * should be greater than capture_config.line_size.
  849. */
  850. p_token = strtok(NULL, ",");
  851. if((p_token == NULL) ||
  852. (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
  853. status = FALSE;
  854. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
  855. }
  856. else {
  857. if(bad_pixel_num > m_nWidth) {
  858. status = FALSE;
  859. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
  860. // "exceeds line_size (%d)",
  861. // bad_pixel_num, entry, m_nWidth);
  862. }
  863. else {
  864. p_bad_pixels[entry] = bad_pixel_num;
  865. num_bad_pixels++;
  866. }
  867. }
  868. }
  869. }
  870. }
  871. line_num++;
  872. }
  873. /* Not a bad pixel entry; This is a bad line in the file */
  874. else {
  875. if(strstr(data_in, "synthseam"))
  876. continue;
  877. else
  878. status = FALSE;
  879. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
  880. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
  881. }
  882. }
  883. else {
  884. // file read error here or end of file.
  885. break;
  886. }
  887. }
  888. }
  889. if(status == TRUE) {
  890. if(num_bad_pixels > MAX_BAD_PIXELS) {
  891. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
  892. // MAX_BAD_PIXELS, num_bad_pixels);
  893. }
  894. }
  895. if(fp != NULL) {
  896. fclose(fp);
  897. }
  898. if(data_in != NULL) {
  899. delete [] data_in;
  900. }
  901. if(status != TRUE)
  902. {
  903. FreeBadPixelMap();
  904. }
  905. else
  906. {
  907. /////////////////////////新方法-基于二值图坏线识别
  908. BadLineRecognize();
  909. }
  910. return status;
  911. }
  912. bool CPixMatrix::LoadBadPixelMapChar(char* data_char, bool bNew)
  913. {
  914. //BAD_PIXEL_ENTRY **theMap = &m_BadPixelMap;
  915. // FILE *fp;
  916. int num_entries;
  917. int entry;
  918. int line_num;
  919. int file_line_num;
  920. long *p_bad_pixels;
  921. bool status = TRUE;
  922. long bad_pixel_num = 0;
  923. char *data_in = NULL;
  924. char *p_token;
  925. long num_bad_pixels = 0;
  926. // char *data_temp;
  927. long char_readcount = 0;
  928. /* Hopefully, the largest line in a bad pix map */
  929. // data_in = (char *)malloc(MAX_PIX_MAP_LINE);
  930. // data_in = new char [MAX_PIX_MAP_LINE];
  931. // if(data_in == NULL) {
  932. // status = FALSE;
  933. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: malloc of data_in failed.");
  934. // }
  935. // fp = fopen(fileName, "rb");
  936. if(data_char == NULL) {
  937. status = FALSE;
  938. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Open of file : %s failed.", fileName);
  939. }
  940. else {
  941. if(m_BadNewPixelMap != NULL) {
  942. FreeBadPixelMap();
  943. }
  944. //m_BadPixelMap = (BAD_PIXEL_ENTRY *)calloc(m_nHeight, sizeof(BAD_PIXEL_ENTRY));
  945. m_BadNewPixelMap = new BAD_PIXEL_ENTRY [m_nHeight*sizeof(BAD_PIXEL_ENTRY)];
  946. if(m_BadNewPixelMap == NULL) {
  947. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of m_BadPixelMap failed.");
  948. status = FALSE;
  949. }
  950. }
  951. /* Data structures created OK and file is open, let's roll */
  952. if(status == TRUE) {
  953. line_num = 0;
  954. while((line_num < m_nHeight) && (status == TRUE)) {
  955. data_in = strtok(data_char+char_readcount, "\n");
  956. char_readcount += long(strlen( data_in )+1);
  957. if(data_in!= NULL) {
  958. // Each line in bad_pixel.map begins with "linenum,num_entries: "
  959. if(sscanf(data_in, "%d,%d: ", &file_line_num, &num_entries) == 2) {
  960. // Verify the line number read matches the line num we are on
  961. if(line_num != file_line_num) {
  962. status = FALSE;
  963. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d.", line_num);
  964. }
  965. // Verify that num_entries is not larger than pixels-per-line
  966. if(num_entries > m_nWidth) {
  967. status = FALSE;
  968. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Pixel map read error for col %d; "
  969. // "num_entries (%d) > line_size (%d)", line_num, num_entries, m_nWidth);
  970. }
  971. /* Tokenize the header */
  972. p_token = strtok(data_in, ":");
  973. if(status == TRUE) {
  974. /* Create the correct size bad pixel map entry */
  975. //(m_BadPixelMap)[line_num].bad_pixel_num = (long *)malloc((num_entries + 1) * sizeof(long));
  976. m_BadNewPixelMap[line_num].bad_pixel_num = new long [(num_entries+1)*sizeof(long)];
  977. if( (m_BadNewPixelMap)[line_num].bad_pixel_num == NULL) {
  978. status = FALSE;
  979. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Malloc of p_bad_pixels for line %d failed.", line_num);
  980. }
  981. else {
  982. memset( (m_BadNewPixelMap)[line_num].bad_pixel_num, 0, (num_entries + 1) * sizeof(long));
  983. p_bad_pixels = (m_BadNewPixelMap)[line_num].bad_pixel_num;
  984. (m_BadNewPixelMap)[line_num].num_entries = num_entries;
  985. /* Fill in the array */
  986. for(entry = 0 ; entry < num_entries ; ++entry) {
  987. /*
  988. * The remainder of each line of the Bad Pixel File consists
  989. * of numbers indicating the x-coordinate value of the bad
  990. * pixels. They should be in ascending order. None of them
  991. * should be greater than capture_config.line_size.
  992. */
  993. p_token = strtok(NULL, ",");
  994. if((p_token == NULL) ||
  995. (sscanf(p_token, "%ld,", &bad_pixel_num) != 1)) {
  996. status = FALSE;
  997. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read failure on pixel entry %d.", entry);
  998. }
  999. else {
  1000. if(bad_pixel_num > m_nWidth) {
  1001. status = FALSE;
  1002. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad value (%ld) on pixel entry %d "
  1003. // "exceeds line_size (%d)",
  1004. // bad_pixel_num, entry, m_nWidth);
  1005. }
  1006. else {
  1007. p_bad_pixels[entry] = bad_pixel_num;
  1008. num_bad_pixels++;
  1009. }
  1010. }
  1011. }
  1012. }
  1013. }
  1014. line_num++;
  1015. }
  1016. /* Not a bad pixel entry; This is a bad line in the file */
  1017. else {
  1018. if(strstr(data_in, "synthseam"))
  1019. continue;
  1020. else
  1021. status = FALSE;
  1022. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Read of entry for line_num %d failed", line_num );
  1023. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, " Line_num %d is: <%s>", line_num, data_in );
  1024. }
  1025. }
  1026. else {
  1027. // file read error here or end of file.
  1028. break;
  1029. }
  1030. }
  1031. }
  1032. if(status == TRUE) {
  1033. if(num_bad_pixels > MAX_BAD_PIXELS) {
  1034. // DRUTIL_LogMessage( LOG_LEVEL_ERROR, "ERROR: Bad pixel map exceeds limit (%ld) : %ld pixels",
  1035. // MAX_BAD_PIXELS, num_bad_pixels);
  1036. }
  1037. }
  1038. if(status != TRUE) {
  1039. FreeBadNewPixelMap();
  1040. }
  1041. if(!bNew)
  1042. {
  1043. if(m_BadPixelMap != NULL) {
  1044. FreeBadPixelMap();
  1045. }
  1046. m_BadPixelMap = m_BadNewPixelMap;
  1047. m_BadNewPixelMap = NULL;
  1048. }
  1049. return status;
  1050. }
  1051. bool CPixMatrix::SaveBadPixelMap(const char *fileName)
  1052. {
  1053. FILE *fp;
  1054. char *data_out = NULL;
  1055. long char_count = 0;
  1056. bool status = TRUE;
  1057. long line;
  1058. long bad_pix_num;
  1059. long bad_num;
  1060. long pix_num;
  1061. long *p_bad_pix_num;
  1062. long char_length;
  1063. data_out = new char [MAXI_BADPIX_COUNT*2];
  1064. for(line = 0 ; line < m_nHeight; ++line) {
  1065. char_length = sprintf(data_out+char_count,"%d,%d:",line,m_BadPixelMap[line].num_entries);
  1066. char_count += char_length;
  1067. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;//
  1068. for(pix_num = 0; pix_num < m_BadPixelMap[line].num_entries; ++pix_num) {
  1069. bad_pix_num = *p_bad_pix_num++;
  1070. bad_num = bad_pix_num & OFFSET_MASK;
  1071. char_length = sprintf(data_out+char_count,"%d,",bad_num);
  1072. char_count += char_length;
  1073. }
  1074. char_length = sprintf(data_out+char_count,"\n");
  1075. char_count += char_length;
  1076. }
  1077. fp = fopen(fileName, "wb");
  1078. if (fwrite(data_out,1,char_count,fp)!=char_count){
  1079. status = FALSE;
  1080. }
  1081. fclose(fp);
  1082. if(m_BadPixelMap == NULL)
  1083. {
  1084. status = FALSE;
  1085. }
  1086. else
  1087. {
  1088. //////////////////////////////////////////坏线自动识别
  1089. BadLineRecognize();
  1090. }
  1091. if(data_out != NULL) {
  1092. delete [] data_out;
  1093. }
  1094. return status;
  1095. }
  1096. int CPixMatrix::BadGridLineCorrect1( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1097. {
  1098. if( !pImage || nEntries < m_nHOffset || nEntries > nHeight - m_nHOffset )
  1099. return -1;
  1100. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1101. if( nEntries -2 < m_nHOffset)
  1102. {
  1103. for( int i = nStartPoint; i < nEndPoint; i++ )
  1104. {
  1105. pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries +2 ) * nWidth + i] ;
  1106. }
  1107. }
  1108. else if( nEntries + 2 > nHeight - m_nHOffset )
  1109. {
  1110. for( int i = nStartPoint; i < nEndPoint; i++ )
  1111. {
  1112. pImage[ (nEntries) * nWidth + i] = m_TempImage[( nEntries -2 ) * nWidth + i] ;
  1113. }
  1114. }
  1115. else
  1116. {
  1117. for( int i = nStartPoint; i < nEndPoint; i++ )
  1118. {
  1119. pImage[ (nEntries) * nWidth + i] = (unsigned short) (0.5*( m_TempImage[( nEntries +2 ) * nWidth + i] + m_TempImage[( nEntries -2 ) * nWidth + i] ));
  1120. }
  1121. }
  1122. return 0;
  1123. }
  1124. //¨¨£¤??è¨???????ê??|ì??3????D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
  1125. int CPixMatrix::BadGridLineCorrect2( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)//¨o¨2??à
  1126. {
  1127. if( !pImage || nEntries < m_nWOffset || nEntries > nWidth - m_nWOffset )
  1128. return -1;
  1129. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1130. if( nEntries -2 <= m_nWOffset)
  1131. {
  1132. for( int i = nStartPoint; i < nEndPoint; i++ )
  1133. {
  1134. pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries + 2];
  1135. }
  1136. }
  1137. else if( nEntries + 2 > nHeight - m_nWOffset )
  1138. {
  1139. for( int i = nStartPoint; i < nEndPoint; i++ )
  1140. {
  1141. pImage[i * nWidth + nEntries] =m_TempImage[( i ) * nWidth + nEntries - 2];
  1142. }
  1143. }
  1144. else
  1145. {
  1146. for( int i = nStartPoint; i < nEndPoint; i++ )
  1147. {
  1148. pImage[i * nWidth + nEntries] = (unsigned short)(0.5*(m_TempImage[(i)* nWidth + nEntries - 2] + m_TempImage[(i)* nWidth + nEntries + 2]));
  1149. }
  1150. }
  1151. return 0;
  1152. }
  1153. //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê??????|ì???ê? zhaoyiru,2017.05.09
  1154. int CPixMatrix::BadGridLineCorrect3( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1155. {
  1156. if( !pImage || nEntries < m_nHOffset+2 || nEntries > nHeight -m_nHOffset-3 )
  1157. return -1;
  1158. if ( nStartPoint < m_nWOffset + 2 )
  1159. nStartPoint = m_nWOffset + 2;
  1160. if ( nEndPoint > m_nWOffset -3)
  1161. nEndPoint = nWidth- m_nWOffset -3;
  1162. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1163. const float coef1=0.25f;
  1164. const float coef2=0.5f;//0.67f;
  1165. const float coef3=0.5f;//0.33f;
  1166. const float coef4=1.0f;
  1167. const float coef5=2.0f;
  1168. const float coef6=1.0f;
  1169. for( int i = nStartPoint; i < nEndPoint; i++ )
  1170. {
  1171. int nA,nB,nC,nA1, nB1,nC1;
  1172. nA1 = (int)(coef1*(m_TempImage[(nEntries - 1)*m_nWidth + i - 2] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i - 1] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i] * coef6 -
  1173. m_TempImage[(nEntries+1)*m_nWidth + i]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i +2]*coef6));
  1174. nB1 = (int)(coef1*(m_TempImage[(nEntries - 1)*m_nWidth + i - 1] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i + 1] * coef6 -
  1175. m_TempImage[(nEntries+1)*m_nWidth + i-1 ]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef6));
  1176. nC1 = (int)(coef1*(m_TempImage[(nEntries - 1)*m_nWidth + i] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i + 1] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i + 2] * coef6 -
  1177. m_TempImage[(nEntries+1)*m_nWidth + i-2]*coef4 - m_TempImage[(nEntries+1)*m_nWidth + i-1]*coef5 - m_TempImage[(nEntries+1)*m_nWidth + i]*coef6));
  1178. nA = nA1*nA1;
  1179. nB = nB1*nB1;
  1180. nC = nC1*nC1;
  1181. if( nA <= nB && nA<= nC )
  1182. {
  1183. pImage[(nEntries)*m_nWidth + i] = (unsigned short)(coef1*coef2*(m_TempImage[(nEntries - 1)*m_nWidth + i - 2] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i - 1] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i] * coef6) +
  1184. coef1*coef3*(m_TempImage[(nEntries+1)*m_nWidth + i]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef5 +m_TempImage[(nEntries+1)*m_nWidth + i +2]*coef6));
  1185. }
  1186. else if( nB<= nC )
  1187. {
  1188. pImage[(nEntries)*m_nWidth + i] = (unsigned short)(coef1*coef2*(m_TempImage[(nEntries - 1)*m_nWidth + i - 1] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i + 1] * coef6) +
  1189. coef1*coef3*( m_TempImage[(nEntries+1)*m_nWidth + i-1 ]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i]*coef5 + m_TempImage[(nEntries+1)*m_nWidth + i+1]*coef6));
  1190. }
  1191. else
  1192. {
  1193. pImage[(nEntries)*m_nWidth + i] = (unsigned short)(coef1*coef2*(m_TempImage[(nEntries - 1)*m_nWidth + i] * coef4 + m_TempImage[(nEntries - 1)*m_nWidth + i + 1] * coef5 + m_TempImage[(nEntries - 1)*m_nWidth + i + 2] * coef6) +
  1194. coef1*coef3*(m_TempImage[(nEntries+1)*m_nWidth + i-2]*coef4 + m_TempImage[(nEntries+1)*m_nWidth + i-1]*coef5 + m_TempImage[(nEntries+1)*m_nWidth + i]*coef6));
  1195. }
  1196. }
  1197. return 0;
  1198. }
  1199. //¨¨£¤??è¨???o¨??ê??|ì???t???D?ê?y?ê?¨o¨2??à?|ì???ê?zhaoyiru,2017.05.09
  1200. int CPixMatrix::BadGridLineCorrect4( unsigned short *pImage, int nLineDirection, int nEntries, int nStartPoint, int nEndPoint, int nWidth, int nHeight)
  1201. {
  1202. if( !pImage || nEntries < m_nWOffset+3 || nEntries > nHeight -m_nWOffset-4 )
  1203. return -1;
  1204. if ( nStartPoint < m_nHOffset + 3 )
  1205. nStartPoint = m_nHOffset + 3;
  1206. if ( nEndPoint > m_nHOffset -4)
  1207. nEndPoint = nHeight-m_nHOffset -4;
  1208. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  1209. const float coef1=0.25f;
  1210. const float coef2=0.5f;//0.67f;
  1211. const float coef3=0.5f;//0.33f;
  1212. const float coef4=1.0f;
  1213. const float coef5=2.0f;
  1214. const float coef6=1.0f;
  1215. for( int i = nStartPoint; i < nEndPoint; i++ )
  1216. {
  1217. int nA,nB,nC,nA1, nB1,nC1;
  1218. nA1 = (int)(coef1*(m_TempImage[(i - 2)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i - 1)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i)*m_nWidth + nEntries - 1] * coef6 -
  1219. m_TempImage[(i)*m_nWidth + nEntries+1]*coef4 - m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i+2)*m_nWidth + nEntries +1]*coef6));
  1220. nB1 = (int)(coef1*(m_TempImage[(i - 1)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i + 1)*m_nWidth + nEntries - 1] * coef6 -
  1221. m_TempImage[(i-1)*m_nWidth + nEntries+1 ]*coef4 - m_TempImage[(i)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef6));
  1222. nC1 = (int)(coef1*(m_TempImage[(i)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i + 1)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i + 2)*m_nWidth + nEntries - 1] * coef6 -
  1223. m_TempImage[(i-2)*m_nWidth + nEntries+1 ]*coef4 - m_TempImage[(i-1)*m_nWidth + nEntries+1]*coef5 - m_TempImage[(i)*m_nWidth + nEntries+1]*coef6));
  1224. nA = nA1*nA1;
  1225. nB = nB1*nB1;
  1226. nC = nC1*nC1;
  1227. if( nA <= nB && nA<= nC )
  1228. {
  1229. pImage[(i)*m_nWidth + nEntries] = (unsigned short)(coef1*coef2*(m_TempImage[(i - 2)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i - 1)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i)*m_nWidth + nEntries - 1] * coef6) +
  1230. coef1*coef3*(m_TempImage[(i)*m_nWidth + nEntries+1]*coef4 + m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i+2)*m_nWidth + nEntries +1]*coef6));
  1231. }
  1232. else if( nB<= nC )
  1233. {
  1234. pImage[(i)*m_nWidth + nEntries] = (unsigned short)(coef1*coef2*(m_TempImage[(i - 1)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i + 1)*m_nWidth + nEntries - 1] * coef6) +
  1235. coef1*coef3*(m_TempImage[(i-1)*m_nWidth + nEntries+1 ]*coef4 + m_TempImage[(i)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i+1)*m_nWidth + nEntries+1]*coef6 ));
  1236. }
  1237. else
  1238. {
  1239. pImage[(i)*m_nWidth + nEntries] = (unsigned short)(coef1*coef2*(m_TempImage[(i)*m_nWidth + nEntries - 1] * coef4 + m_TempImage[(i + 1)*m_nWidth + nEntries - 1] * coef5 + m_TempImage[(i + 2)*m_nWidth + nEntries - 1] * coef6) +
  1240. coef1*coef3*(m_TempImage[(i-2)*m_nWidth + nEntries+1 ]*coef4 + m_TempImage[(i-2)*m_nWidth + nEntries+1]*coef5 + m_TempImage[(i)*m_nWidth + nEntries+1]*coef6));
  1241. }
  1242. }
  1243. return 0;
  1244. }
  1245. void CPixMatrix::BadLineRecognize()
  1246. {
  1247. //////////////////////////////////////////坏线自动识别
  1248. long BadPixNum;
  1249. long BadNum;
  1250. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1251. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1252. memset( m_datalen, -1, sizeof(int) * (int( (MAXI_BADPIX_COUNT - MAX_PIX_MAP_LINE) / m_nLinePoint)) );
  1253. for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
  1254. {
  1255. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  1256. if ( m_BadPixelMap[ i ].num_entries > 0 )
  1257. {
  1258. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  1259. {
  1260. BadPixNum = *m_pBadPixNum++;
  1261. BadNum = BadPixNum & OFFSET_MASK;
  1262. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  1263. m_TempImage[ i * m_nWidth + BadNum ] = 65535;
  1264. }
  1265. }
  1266. }
  1267. /////////////////////按行标记坏线,水平坏线
  1268. //char *data_out = NULL;改为m_datalen;
  1269. int linedirection;
  1270. //m_datalen = new char [ 40000 ];
  1271. int count = 0;
  1272. for ( int i = m_nHOffset; i < m_nHeight - m_nHOffset; i++ )
  1273. {
  1274. for ( int j = m_nWOffset + 2; j < m_nWidth - m_nWOffset - 2; j++ )
  1275. {
  1276. m_TempImage1[ i * m_nWidth + j ] = ( m_TempImage[ i * m_nWidth + j - 2 ] + m_TempImage[ i * m_nWidth + j - 1 ] + m_TempImage[ i * m_nWidth + j ] + m_TempImage[ i * m_nWidth + j + 1 ] + m_TempImage[ i * m_nWidth + j + 2 ] ) / 5;
  1277. if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
  1278. m_TempImage1[ i * m_nWidth + j ] = 0;
  1279. else
  1280. m_TempImage1[ i * m_nWidth + j ] = 65535;
  1281. }
  1282. int tempcount = 0;
  1283. for ( int p = m_nWOffset; p < m_nWidth - m_nWOffset; p++ )
  1284. {
  1285. if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
  1286. tempcount++;
  1287. }
  1288. int minX = 0;
  1289. int maxX = 0;
  1290. if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
  1291. {
  1292. for ( int k = m_nWOffset; k < m_nWidth - m_nWOffset; k++ )
  1293. {
  1294. if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
  1295. {
  1296. minX = k;
  1297. break;
  1298. }
  1299. }
  1300. for ( int k1 = m_nWidth - m_nWOffset - 1; k1 > m_nWOffset; k1-- )
  1301. {
  1302. if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
  1303. {
  1304. maxX = k1;
  1305. break;
  1306. }
  1307. }
  1308. linedirection = 0;
  1309. //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
  1310. //char_count += char_length;
  1311. //char_length = sprintf( m_datalen + char_count, "\n" );
  1312. //char_count += char_length;
  1313. m_datalen[ count ] = linedirection;
  1314. m_datalen[ count + 1 ] = i;
  1315. m_datalen[ count + 2 ] = minX - 2;
  1316. m_datalen[ count + 3 ] = maxX + 2;
  1317. count += 4;
  1318. }
  1319. }
  1320. ////////////////////////////按列标记坏线--竖直坏线
  1321. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1322. for ( int i = m_nWOffset; i < m_nWidth - m_nWOffset; i++ )
  1323. {
  1324. for ( int j = m_nHOffset + 2; j < m_nHeight - m_nHOffset - 2; j++ )
  1325. {
  1326. m_TempImage1[ j * m_nWidth + i ] = ( m_TempImage[ ( j - 2 ) * m_nWidth + i ] + m_TempImage[ ( j - 1 ) * m_nWidth + i ] + m_TempImage[ j * m_nWidth + i ] + m_TempImage[ ( j + 1 ) * m_nWidth + i ] + m_TempImage[ ( j + 2 ) * m_nWidth + i ] ) / 5;
  1327. if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
  1328. m_TempImage1[ j * m_nWidth + i ] = 0;
  1329. else
  1330. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1331. }
  1332. int tempcount = 0;
  1333. for ( int p = m_nHOffset; p < m_nHeight - m_nHOffset; p++ )
  1334. {
  1335. if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
  1336. tempcount++;
  1337. }
  1338. int minY = 0;
  1339. int maxY = 0;
  1340. if ( tempcount > m_nLinePoint )//add by chen 2015-1-13 judge condition change from 300 to 50
  1341. {
  1342. for ( int k = m_nHOffset; k < m_nHeight - m_nHOffset; k++ )
  1343. {
  1344. if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
  1345. {
  1346. minY = k;
  1347. break;
  1348. }
  1349. }
  1350. for ( int k1 = m_nHeight - m_nHOffset - 1; k1 > m_nHOffset; k1-- )
  1351. {
  1352. if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
  1353. {
  1354. maxY = k1;
  1355. break;
  1356. }
  1357. }
  1358. linedirection = 1;
  1359. //char_length = sprintf( m_datalen + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
  1360. //char_count += char_length;
  1361. //char_length = sprintf( m_datalen + char_count, "\n" );
  1362. //char_count += char_length;
  1363. m_datalen[ count ] = linedirection;
  1364. m_datalen[ count + 1 ] = i;
  1365. m_datalen[ count + 2 ] = minY - 2;
  1366. m_datalen[ count + 3 ] = maxY + 2;
  1367. count += 4;
  1368. }
  1369. }
  1370. }
  1371. ///////////////////生成坏线标记文件,by chen G N 2013 - 01 - 16
  1372. int CPixMatrix::SaveBadLineMap(const char *fileName)
  1373. {
  1374. ////////////////////////////新方法-基于二值图,返回值: -2 坏点文件未载入;-1 坏线写失败;0 无坏线;1有坏线
  1375. if(m_BadPixelMap == NULL)
  1376. {
  1377. return -2;
  1378. }
  1379. long BadPixNum;
  1380. long BadNum;
  1381. //////////////////////////标记坏点
  1382. //if ( nWidth != m_nWidth && nHeight != m_nHeight )
  1383. //{
  1384. // unsigned short *m_TempImage = new unsigned short [ m_nWidth * m_nHeight ];
  1385. // unsigned short *m_TempImage1 = new unsigned short [ m_nWidth * m_nHeight ];
  1386. // nWidth = m_nWidth;
  1387. // nHeight = m_nHeight;
  1388. //}
  1389. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1390. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1391. for ( int i = 2; i < m_nHeight - 2; i++ )
  1392. {
  1393. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  1394. if ( m_BadPixelMap[ i ].num_entries > 0 )
  1395. {
  1396. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  1397. {
  1398. BadPixNum = *m_pBadPixNum++;
  1399. BadNum = BadPixNum & OFFSET_MASK;
  1400. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  1401. m_TempImage[ i * m_nWidth + BadNum ] = 65535;
  1402. }
  1403. }
  1404. }
  1405. /////////////////////按行标记坏线,水平坏线
  1406. char *data_out = NULL;
  1407. long char_count = 0;
  1408. long char_length;
  1409. int linedirection;
  1410. data_out = new char [ 40000 ];
  1411. for ( int i = 0; i < m_nHeight; i++ )
  1412. {
  1413. for ( int j = 2; j < m_nWidth - 2; j++ )
  1414. {
  1415. m_TempImage1[ i * m_nWidth + j ] = ( m_TempImage[ i * m_nWidth + j - 2 ] + m_TempImage[ i * m_nWidth + j - 1 ] + m_TempImage[ i * m_nWidth + j ] + m_TempImage[ i * m_nWidth + j + 1 ] + m_TempImage[ i * m_nWidth + j + 2 ] ) / 5;
  1416. if ( m_TempImage1[ i * m_nWidth + j ] < 40000 )
  1417. m_TempImage1[ i * m_nWidth + j ] = 0;
  1418. else
  1419. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1420. }
  1421. int tempcount = 0;
  1422. for ( int p = 0; p < m_nWidth; p++ )
  1423. {
  1424. if ( m_TempImage1[ i * m_nWidth + p ] == 65535 )
  1425. tempcount++;
  1426. }
  1427. int minX = 0;
  1428. int maxX = 0;
  1429. if ( tempcount > 500 )
  1430. {
  1431. for ( int k = 0; k < m_nWidth; k++ )
  1432. {
  1433. if ( m_TempImage1[ i * m_nWidth + k ] == 65535 )
  1434. {
  1435. minX = k;
  1436. break;
  1437. }
  1438. }
  1439. for ( int k1 = m_nWidth - 1; k1 > 0; k1-- )
  1440. {
  1441. if ( m_TempImage1[ i * m_nWidth + k1 ] == 65535 )
  1442. {
  1443. maxX = k1;
  1444. break;
  1445. }
  1446. }
  1447. linedirection = 0;
  1448. char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minX - 2, maxX + 2 );
  1449. char_count += char_length;
  1450. char_length = sprintf( data_out + char_count, "\n" );
  1451. char_count += char_length;
  1452. }
  1453. }
  1454. ////////////////////////////按列标记坏线--竖直坏线
  1455. memset( m_TempImage1, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  1456. for ( int i = 0; i < m_nWidth; i++ )
  1457. {
  1458. for ( int j = 2; j < m_nHeight - 2; j++ )
  1459. {
  1460. m_TempImage1[ j * m_nWidth + i ] = ( m_TempImage[ ( j - 2 ) * m_nWidth + i ] + m_TempImage[ ( j - 1 ) * m_nWidth + i ] + m_TempImage[ j * m_nWidth + i ] + m_TempImage[ ( j + 1 ) * m_nWidth + i ] + m_TempImage[ ( j + 2 ) * m_nWidth + i ] ) / 5;
  1461. if ( m_TempImage1[ j * m_nWidth + i ] < 40000 )
  1462. m_TempImage1[ j * m_nWidth + i ] = 0;
  1463. else
  1464. m_TempImage1[ j * m_nWidth + i ] = 65535;
  1465. }
  1466. int tempcount = 0;
  1467. for ( int p = 0; p < m_nHeight; p++ )
  1468. {
  1469. if ( m_TempImage1[ p * m_nWidth + i ] == 65535 )
  1470. tempcount++;
  1471. }
  1472. int minY = 0;
  1473. int maxY = 0;
  1474. if ( tempcount > 500 )
  1475. {
  1476. for ( int k = 0; k < m_nHeight; k++ )
  1477. {
  1478. if ( m_TempImage1[ k * m_nWidth + i ] == 65535 )
  1479. {
  1480. minY = k;
  1481. break;
  1482. }
  1483. }
  1484. for ( int k1 = m_nHeight - 1; k1 > 0; k1-- )
  1485. {
  1486. if ( m_TempImage1[ k1 * m_nWidth + i ] == 65535 )
  1487. {
  1488. maxY = k1;
  1489. break;
  1490. }
  1491. }
  1492. linedirection = 1;
  1493. char_length = sprintf( data_out + char_count, "%d,%d:%d,%d", linedirection, i, minY - 2, maxY + 2 );
  1494. char_count += char_length;
  1495. char_length = sprintf( data_out + char_count, "\n" );
  1496. char_count += char_length;
  1497. }
  1498. }
  1499. ///////////////////////写坏线坐标/////////////////////////////
  1500. if ( char_count == 0 )
  1501. {
  1502. return 0;
  1503. }
  1504. else
  1505. {
  1506. FILE *fp;
  1507. fp = fopen(fileName, "wb");
  1508. if(m_BadPixelMap == NULL)
  1509. {
  1510. return -2;
  1511. }
  1512. if (fwrite(data_out,1,char_count,fp)!=char_count)
  1513. {
  1514. return -1;
  1515. }
  1516. fclose(fp);
  1517. }
  1518. if( data_out != NULL )
  1519. {
  1520. delete [] data_out;
  1521. data_out = NULL;
  1522. }
  1523. //if( m_TempImage != NULL )
  1524. //{
  1525. // delete [] m_TempImage;
  1526. // m_TempImage = NULL;
  1527. //}
  1528. //if( m_TempImage1 != NULL )
  1529. //{
  1530. // delete [] m_TempImage1;
  1531. // m_TempImage1 = NULL;
  1532. //}
  1533. return 1;
  1534. }
  1535. //////////////解决去栅影后的坏线振铃效应,返回-2为异常,返回1为正常去除坏线/////////////////////////////////////////
  1536. int CPixMatrix::LoadandCorrectBadLine( unsigned short *pImage, int GridDirection )
  1537. {
  1538. //FILE *fp;
  1539. int line_direction;
  1540. int line_entries;
  1541. int startpoint;
  1542. int endpoint;
  1543. //int char_count = 0;
  1544. //int char_length = 0;
  1545. int count = 0;
  1546. //char *data_in = NULL;
  1547. //data_in = new char [MAX_PIX_MAP_LINE];
  1548. if( m_datalen == NULL)
  1549. {
  1550. return -2;
  1551. }
  1552. //fp = fopen(fileName, "rt");
  1553. //if(fp == NULL)
  1554. //{
  1555. // return -2;
  1556. //}
  1557. // while( (m_datalen + char_count)!= NULL)
  1558. // {
  1559. //while( sscanf( m_datalen + char_count, "%d,%d:%d,%d", &line_direction, &line_entries, &startpoint, &endpoint ) == 4 )
  1560. //while( (m_datalen[ count ] != -1) && (count < 400) )
  1561. while( (m_datalen[ count ] != -1) && (count < m_nLineData) ) //???acoutD?óú×?′ó?μ????êy,zhaoyiru, 2017.05.09
  1562. {
  1563. line_direction = m_datalen[ count ] ;
  1564. line_entries = m_datalen[ count + 1 ];
  1565. startpoint = m_datalen[ count + 2 ];
  1566. endpoint = m_datalen[ count + 3 ];
  1567. count = count + 4;
  1568. if( line_direction == 0 && line_entries > 2 && line_entries < m_nHeight - 2 && GridDirection == 2 )//////////////////////去除水平坏线
  1569. {
  1570. int nTGray = 0, nBGray = 0, nGray = 0;
  1571. int nTDetails = 0, nBDetails = 0, nTAvg = 0, nBAvg = 0;
  1572. if ( startpoint < m_nWOffset + 2 )
  1573. startpoint = m_nWOffset + 2;
  1574. if ( endpoint > m_nWidth - m_nWOffset - 2 )
  1575. endpoint = m_nWidth - m_nWOffset - 2;
  1576. for ( int i = startpoint; i < endpoint; i++ )
  1577. {
  1578. nTAvg = 0;
  1579. for ( int j = -2; j < 3; j++ )
  1580. {
  1581. nTAvg += pImage[ ( line_entries - 1 ) * m_nWidth + i + j ];
  1582. }
  1583. nTAvg /= 5;
  1584. nTDetails = pImage[ ( line_entries - 1 ) * m_nWidth + i ] - nTAvg;
  1585. nTGray = (int)(0.5 * pImage[(line_entries - 2) * m_nWidth + i] + 0.25 * pImage[(line_entries - 2) * m_nWidth + i - 1] + 0.25 * pImage[(line_entries - 2) * m_nWidth + i + 1]);
  1586. nBGray = (int)(0.5 * pImage[(line_entries + 2) * m_nWidth + i] + 0.25 * pImage[(line_entries + 2) * m_nWidth + i - 1] + 0.25 * pImage[(line_entries + 2) * m_nWidth + i + 1]);
  1587. nGray = (int)(0.75 * nTGray + 0.25 * nBGray);
  1588. //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
  1589. if ( (nGray + nTDetails) < 0 )
  1590. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 0;
  1591. else if ( (nGray + nTDetails) > 65535 )
  1592. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = 65535;
  1593. else
  1594. pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nTDetails;
  1595. }
  1596. for ( int i = startpoint; i < endpoint; i++ )
  1597. {
  1598. nBAvg = 0;
  1599. for ( int j = -2; j < 3; j++ )
  1600. {
  1601. nBAvg += pImage[ ( line_entries + 1 ) * m_nWidth + i + j ];
  1602. }
  1603. nBAvg /= 5;
  1604. nBDetails = pImage[ ( line_entries + 1 ) * m_nWidth + i ] - nBAvg;
  1605. nTGray = (int)(0.5 * pImage[(line_entries - 2) * m_nWidth + i] + 0.25 * pImage[(line_entries - 2) * m_nWidth + i - 1] + 0.25 * pImage[(line_entries - 2) * m_nWidth + i + 1]);
  1606. nBGray = (int)(0.5 * pImage[(line_entries + 2) * m_nWidth + i] + 0.25 * pImage[(line_entries + 2) * m_nWidth + i - 1] + 0.25 * pImage[(line_entries + 2) * m_nWidth + i + 1]);
  1607. nGray = (int)(0.25 * nTGray + 0.75 * nBGray);
  1608. //pImage[ ( line_entries - 1 ) * m_nWidth + i ] = nGray + nBDetails;
  1609. if ( (nGray + nBDetails) < 0 )
  1610. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 0;
  1611. else if ( (nGray + nBDetails) > 65535 )
  1612. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = 65535;
  1613. else
  1614. pImage[ ( line_entries + 1 ) * m_nWidth + i ] = nGray + nBDetails;
  1615. }
  1616. for ( int i = startpoint; i < endpoint; i++ )
  1617. {
  1618. pImage[(line_entries)* m_nWidth + i] = (unsigned short)(0.25 * pImage[(line_entries + 1) * m_nWidth + i] + 0.125 * pImage[(line_entries + 1) * m_nWidth + i - 1] + 0.125 * pImage[(line_entries + 1) * m_nWidth + i + 1] + 0.25 * pImage[(line_entries - 1) * m_nWidth + i] + 0.125 * pImage[(line_entries - 1) * m_nWidth + i - 1] + 0.125 * pImage[(line_entries - 1) * m_nWidth + i + 1]);
  1619. }
  1620. }
  1621. if( line_direction == 1 && line_entries > 2 && line_entries < m_nWidth - 2 && GridDirection == 1 )//////////////////////去除竖直坏线
  1622. {
  1623. int nLGray = 0, nRGray = 0, nGray = 0;
  1624. int nLDetails = 0, nRDetails = 0, nLAvg = 0, nRAvg = 0;
  1625. if ( startpoint < m_nHOffset + 2 )
  1626. startpoint = m_nHOffset + 2;
  1627. if ( endpoint > m_nHeight - m_nHOffset - 2 )
  1628. endpoint = m_nHeight - m_nHOffset - 2;
  1629. for ( int i = startpoint; i < endpoint; i++ )
  1630. {
  1631. nLAvg = 0;
  1632. for ( int j = -2; j < 3; j++ )
  1633. {
  1634. nLAvg += pImage[ ( i + j ) * m_nWidth + line_entries - 1 ];
  1635. }
  1636. nLAvg /= 5;
  1637. nLDetails = pImage[ i * m_nWidth + line_entries - 1 ] - nLAvg;
  1638. nLGray = (int)(0.5 * pImage[i * m_nWidth + line_entries - 2] + 0.25 * pImage[(i - 1) * m_nWidth + line_entries - 2] + 0.25 * pImage[(i + 1) * m_nWidth + line_entries - 2]);
  1639. nRGray = (int)(0.5 * pImage[i * m_nWidth + line_entries + 2] + 0.25 * pImage[(i - 1) * m_nWidth + line_entries + 2] + 0.25 * pImage[(i + 1) * m_nWidth + line_entries + 2]);
  1640. nGray = (int)(0.75 * nLGray + 0.25 * nRGray);
  1641. //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
  1642. if ( (nGray + nLDetails) < 0 )
  1643. pImage[ i * m_nWidth + line_entries - 1 ] = 0;
  1644. else if ( (nGray + nLDetails) > 65535 )
  1645. pImage[ i * m_nWidth + line_entries - 1 ] = 65535;
  1646. else
  1647. pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nLDetails;
  1648. }
  1649. for ( int i = startpoint; i < endpoint; i++ )
  1650. {
  1651. nRAvg = 0;
  1652. for ( int j = -2; j < 3; j++ )
  1653. {
  1654. nRAvg += pImage[ ( i + j ) * m_nWidth + line_entries + 1 ];
  1655. }
  1656. nRAvg /= 5;
  1657. nRDetails = pImage[ i * m_nWidth + line_entries + 1 ] - nRAvg;
  1658. nLGray = (int)(0.5 * pImage[i * m_nWidth + line_entries - 2] + 0.25 * pImage[(i - 1) * m_nWidth + line_entries - 2] + 0.25 * pImage[(i + 1) * m_nWidth + line_entries - 2]);
  1659. nRGray = (int)(0.5 * pImage[i * m_nWidth + line_entries + 2] + 0.25 * pImage[(i - 1) * m_nWidth + line_entries + 2] + 0.25 * pImage[(i + 1) * m_nWidth + line_entries + 2]);
  1660. nGray = (int)(0.75 * nRGray + 0.25 * nLGray);
  1661. //pImage[ i * m_nWidth + line_entries - 1 ] = nGray + nRDetails;
  1662. if ( (nGray + nRDetails) < 0 )
  1663. pImage[ i * m_nWidth + line_entries + 1 ] = 0;
  1664. else if ( (nGray + nRDetails) > 65535 )
  1665. pImage[ i * m_nWidth + line_entries + 1 ] = 65535;
  1666. else
  1667. pImage[ i * m_nWidth + line_entries + 1 ] = nGray + nRDetails;
  1668. }
  1669. for ( int i = startpoint; i < endpoint; i++ )
  1670. {
  1671. pImage[i * m_nWidth + line_entries] = (unsigned short)(0.25 * pImage[i * m_nWidth + line_entries + 1] + 0.125 * pImage[(i - 1) * m_nWidth + line_entries + 1] + 0.125 * pImage[(i + 1) * m_nWidth + line_entries + 1] + 0.25 * pImage[i * m_nWidth + line_entries - 1] + 0.125 * pImage[(i - 1) * m_nWidth + line_entries - 1] + 0.125 * pImage[(i + 1) * m_nWidth + line_entries - 1]);
  1672. }
  1673. }
  1674. //char_count += 4;
  1675. //char_length = sprintf( m_datalen + char_count, "\n" );
  1676. //char_count += char_length;
  1677. }
  1678. // }
  1679. //if(fp != NULL)
  1680. //{
  1681. // fclose(fp);
  1682. //}
  1683. //if(data_in != NULL)
  1684. //{
  1685. // delete [] data_in;
  1686. //}
  1687. return 1;
  1688. }
  1689. bool CPixMatrix::AutoBadPixelMap(WORD *wImage)
  1690. {
  1691. char * m_charBadPixMap;
  1692. char * m_charLinePixMap;
  1693. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1694. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1695. BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
  1696. int i,j; // for loop
  1697. DWORD Hist[16384];
  1698. DWORD count = 0;
  1699. DWORD tempsum = 0;
  1700. WORD lowvalue, highvalue;
  1701. lowvalue = highvalue = 0;
  1702. bool state = true;
  1703. for(i = 0; i< 16384; i++)
  1704. {
  1705. Hist[i] = 0;
  1706. }
  1707. //统计直方图
  1708. for(i = m_nHeight/3; i<m_nHeight*2/3; i++)
  1709. {
  1710. for(j = m_nWidth/3; j<m_nWidth*2/3; j++)
  1711. {
  1712. Hist[*(wImage+i*m_nWidth+j)]++;
  1713. count ++;
  1714. }
  1715. }
  1716. count = count/10;
  1717. for(i = 0 ; i< 16384; i++)
  1718. {
  1719. tempsum += Hist[i];
  1720. if (tempsum> count)
  1721. break;
  1722. }
  1723. lowvalue = max(i-1500, 100);
  1724. tempsum = 0;
  1725. for(i = m_nPixelMax ; i>0; i--)
  1726. {
  1727. tempsum += Hist[i];
  1728. if (tempsum> count)
  1729. break;
  1730. }
  1731. highvalue = min(i+1500, m_nPixelMax);
  1732. for(i = 0; i<m_nHeight; i++)
  1733. {
  1734. for(j = 0; j<m_nWidth; j++)
  1735. {
  1736. if((*(wImage+i*m_nWidth+j)>lowvalue)&&(*(wImage+i*m_nWidth+j)<highvalue))
  1737. {
  1738. *(wImageWB+i*m_nWidth+j) = 255;
  1739. }
  1740. else
  1741. {
  1742. *(wImageWB+i*m_nWidth+j) = 0;
  1743. }
  1744. }
  1745. }
  1746. //查找空边
  1747. int x1,x2,y1,y2;
  1748. x1 = m_nWOffset;
  1749. x2 = m_nWidth-m_nWOffset;
  1750. y1 = m_nHOffset;
  1751. y2 = m_nHeight-m_nHOffset;
  1752. /* for(i = 0; i<m_nHeight; i++)
  1753. {
  1754. if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
  1755. {
  1756. y1 = i;
  1757. break;
  1758. }
  1759. }
  1760. for(i = m_nHeight-1; i>=0; i--)
  1761. {
  1762. if(!((*(wImageWB+m_nWidth*(m_nHeight-i-1)+m_nWidth/3)==0)&&(*(wImageWB+m_nWidth*(m_nHeight-i-1)+2*m_nWidth/3)==0)))
  1763. {
  1764. y2 = i;
  1765. break;
  1766. }
  1767. }
  1768. for(j = 0; j<m_nWidth; j++)
  1769. {
  1770. if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
  1771. {
  1772. x1 = j;
  1773. break;
  1774. }
  1775. }
  1776. for(j = m_nWidth-1; j>=0; j--)
  1777. {
  1778. if(!((*(wImageWB+m_nWidth*m_nHeight/3+j)==0)&&(*(wImageWB+2*m_nWidth*m_nHeight/3+j)==0)))
  1779. {
  1780. x2 = j;
  1781. break;
  1782. }
  1783. }
  1784. */
  1785. long char_count;
  1786. long char_length;
  1787. long nPixcount;
  1788. nPixcount=char_count = char_length = 0;
  1789. // 每列
  1790. for(i = 0; i<y1; i++)
  1791. {
  1792. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1793. }
  1794. for(i = y1; i<=y2; i++)
  1795. {
  1796. // 每行
  1797. nPixcount=char_length = 0;
  1798. for(j = x1; j <=x2; j++)
  1799. {
  1800. if(*(wImageWB+m_nWidth*i+j)==0){
  1801. //bad pix, add to charbuffer
  1802. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1803. nPixcount++;
  1804. }
  1805. }
  1806. //写入buffer
  1807. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1808. state = false;
  1809. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1810. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1811. break;
  1812. }
  1813. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1814. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1815. char_count += char_length;
  1816. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1817. }
  1818. if(state)
  1819. {
  1820. for(i = y2+1; i<m_nHeight; i++)
  1821. {
  1822. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1823. }
  1824. }
  1825. delete [] wImageWB;
  1826. if(state)
  1827. {
  1828. LoadBadPixelMapChar(m_charBadPixMap);
  1829. CombineBadPixelMap();
  1830. }
  1831. delete [] m_charLinePixMap;
  1832. delete [] m_charBadPixMap;
  1833. return state;
  1834. }
  1835. bool CPixMatrix::AutoBadPixelMap1(WORD *wImage)
  1836. {
  1837. int threshold1,threshold2;
  1838. char * m_charBadPixMap;
  1839. char * m_charLinePixMap;
  1840. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1841. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1842. BYTE *wImageWB = new BYTE [m_nWidth* m_nHeight];
  1843. int temp;
  1844. bool state = true;
  1845. int i, j;
  1846. DWORD dwMean = 0;
  1847. DWORD dwIndex = 0;
  1848. DWORD dwCts = 0;
  1849. for(i = 50; i < m_nHeight-50; i += 8) {
  1850. dwIndex = i*m_nWidth;
  1851. for(j = 50; j < m_nWidth-15; j += 8) {
  1852. dwMean += wImage[dwIndex+j];
  1853. dwCts++;
  1854. }
  1855. }
  1856. dwMean /= dwCts;
  1857. threshold1 = dwMean/3;
  1858. threshold2 = min(dwMean*2,dwMean+(m_nPixelMax-dwMean)/2);
  1859. for(i = 0; i<m_nWidth*m_nHeight; i++)
  1860. {
  1861. temp = wImage[i];// - wDarkImage[i];
  1862. if((temp<threshold1)||(temp>threshold2)) wImageWB[i] = 0;
  1863. else wImageWB[i] = 255;
  1864. }
  1865. //查找空边
  1866. int x1,x2,y1,y2;
  1867. x1 = m_nWOffset;
  1868. x2 = m_nWidth-m_nWOffset;
  1869. y1 = m_nHOffset;
  1870. y2 = m_nHeight-m_nHOffset;
  1871. long char_count;
  1872. long char_length;
  1873. long nPixcount;
  1874. nPixcount=char_count = char_length = 0;
  1875. // 每列
  1876. for(i = 0; i<y1; i++)
  1877. {
  1878. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1879. }
  1880. for(i = y1; i<=y2; i++)
  1881. {
  1882. // 每行
  1883. nPixcount=char_length = 0;
  1884. for(j = x1; j <=x2; j++)
  1885. {
  1886. if(*(wImageWB+m_nWidth*i+j)==0){
  1887. //bad pix, add to charbuffer
  1888. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1889. nPixcount++;
  1890. }
  1891. }
  1892. //写入buffer
  1893. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1894. state = false;
  1895. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1896. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1897. break;
  1898. }
  1899. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1900. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1901. char_count += char_length;
  1902. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1903. }
  1904. if(state)
  1905. {
  1906. for(i = y2+1; i<m_nHeight; i++)
  1907. {
  1908. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1909. }
  1910. }
  1911. delete [] wImageWB;
  1912. if(state)
  1913. {
  1914. LoadBadPixelMapChar(m_charBadPixMap,true);
  1915. CombineBadPixelMap();
  1916. }
  1917. delete [] m_charLinePixMap;
  1918. delete [] m_charBadPixMap;
  1919. return state;
  1920. }
  1921. bool CPixMatrix::AutoBadPixelMap2(WORD *wImage)
  1922. {
  1923. char * m_charBadPixMap;
  1924. char * m_charLinePixMap;
  1925. m_charLinePixMap = new char [MAX_PIX_MAP_LINE];
  1926. m_charBadPixMap = new char [MAXI_BADPIX_COUNT];
  1927. bool state = true;
  1928. int i, j;
  1929. //查找空边
  1930. int x1,x2,y1,y2;
  1931. x1 = m_nWOffset;
  1932. x2 = m_nWidth-m_nWOffset;
  1933. y1 = m_nHOffset;
  1934. y2 = m_nHeight-m_nHOffset;
  1935. long char_count;
  1936. long char_length;
  1937. long nPixcount;
  1938. nPixcount=char_count = char_length = 0;
  1939. // 每列
  1940. for(i = 0; i<y1; i++)
  1941. {
  1942. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1943. }
  1944. for(i = y1; i<y2; i++)
  1945. {
  1946. // 每行
  1947. nPixcount=char_length = 0;
  1948. for(j = x1; j <x2; j++)
  1949. {
  1950. if(*(wImage+m_nWidth*i+j)==0xffff){
  1951. //bad pix, add to charbuffer
  1952. char_length += sprintf(m_charLinePixMap+char_length,"%d,",j);
  1953. nPixcount++;
  1954. }
  1955. }
  1956. //写入buffer
  1957. if (char_count>(MAXI_BADPIX_COUNT-MAX_PIX_MAP_LINE)){
  1958. state = false;
  1959. // AfxMessageBox("Too Many bad point, maybe bad method to expose!");
  1960. //gfun_LogError("Too Many bad point, maybe bad method to expose!");
  1961. break;
  1962. }
  1963. char_count += sprintf(m_charBadPixMap+char_count,"%d,%d:",i,nPixcount);
  1964. memcpy(m_charBadPixMap+char_count,m_charLinePixMap,char_length);
  1965. char_count += char_length;
  1966. char_count += sprintf(m_charBadPixMap+char_count,"\n");
  1967. }
  1968. if(state)
  1969. {
  1970. for(i = y2; i<m_nHeight; i++)
  1971. {
  1972. char_count += sprintf(m_charBadPixMap+char_count,"%d,0:\n",i);
  1973. }
  1974. }
  1975. if(state)
  1976. {
  1977. LoadBadPixelMapChar(m_charBadPixMap,true);
  1978. CombineBadPixelMap();
  1979. }
  1980. if (m_charLinePixMap)
  1981. {
  1982. delete m_charLinePixMap;
  1983. m_charLinePixMap = NULL;
  1984. }
  1985. if (m_charBadPixMap)
  1986. {
  1987. delete m_charBadPixMap;
  1988. m_charBadPixMap = NULL;
  1989. }
  1990. return state;
  1991. }
  1992. void CPixMatrix::FreeBadPixelMap()
  1993. {
  1994. if(m_BadPixelMap != NULL) {
  1995. for(int i = 0 ; i < m_nHeight ; ++i) {
  1996. if(m_BadPixelMap[i].bad_pixel_num != NULL) {
  1997. try
  1998. {
  1999. delete [] m_BadPixelMap[i].bad_pixel_num;
  2000. }
  2001. catch(...)
  2002. {
  2003. }
  2004. m_BadPixelMap[i].bad_pixel_num = NULL;
  2005. }
  2006. }
  2007. delete m_BadPixelMap;
  2008. m_BadPixelMap = NULL;
  2009. }
  2010. }
  2011. void CPixMatrix::FreeDBadLineMap()
  2012. {
  2013. LINE_MAP2 *ptemp = NULL;
  2014. while(m_doubleLineMap)
  2015. {
  2016. ptemp = m_doubleLineMap;
  2017. m_doubleLineMap = m_doubleLineMap->nextline;
  2018. delete ptemp;
  2019. }
  2020. }
  2021. void CPixMatrix::FreeBadNewPixelMap()
  2022. {
  2023. if(m_BadNewPixelMap != NULL) {
  2024. for(int i = 0 ; i < m_nHeight ; ++i) {
  2025. if(m_BadNewPixelMap[i].bad_pixel_num != NULL) {
  2026. delete [] m_BadNewPixelMap[i].bad_pixel_num;
  2027. m_BadNewPixelMap[i].bad_pixel_num = NULL;
  2028. }
  2029. }
  2030. delete [] m_BadNewPixelMap;
  2031. m_BadNewPixelMap = NULL;
  2032. }
  2033. }
  2034. bool CPixMatrix::CombineBadPixelMap()
  2035. {
  2036. long line;
  2037. long bad_pix_num;
  2038. long bad_pix_numcount;
  2039. long bad_new_pix_num;
  2040. long bad_new_pix_numcount;
  2041. long bad_temp_pix_num;
  2042. // long bad_pix;
  2043. // long bad_new_pix;
  2044. long *p_bad_pix_num;
  2045. long *p_bad_new_pix_num;
  2046. long *p_bad_temp_pix;
  2047. if (m_BadNewPixelMap == NULL)
  2048. return TRUE;
  2049. if (m_BadPixelMap == NULL)
  2050. {
  2051. m_BadPixelMap = m_BadNewPixelMap;
  2052. m_BadNewPixelMap = NULL;
  2053. return TRUE;
  2054. }
  2055. for(line = 0 ; line < m_nHeight ; ++line)
  2056. {
  2057. //逐行合并
  2058. bad_pix_num = m_BadPixelMap[line].num_entries;
  2059. bad_new_pix_num = m_BadNewPixelMap[line].num_entries;
  2060. p_bad_pix_num = m_BadPixelMap[line].bad_pixel_num;
  2061. p_bad_new_pix_num = m_BadNewPixelMap[line].bad_pixel_num;
  2062. bad_temp_pix_num = bad_pix_numcount = bad_new_pix_numcount = 0;
  2063. if(bad_new_pix_num==0)
  2064. continue;
  2065. p_bad_temp_pix = new long [(bad_pix_num+bad_new_pix_num+1)*sizeof(long)];
  2066. memset( p_bad_temp_pix, 0, (bad_pix_num+bad_new_pix_num+1) * sizeof(long));
  2067. while (!((bad_pix_numcount>=bad_pix_num)&&(bad_new_pix_numcount>=bad_new_pix_num)))
  2068. {
  2069. if(bad_pix_numcount==bad_pix_num)
  2070. {
  2071. for(;bad_new_pix_numcount<bad_new_pix_num;bad_new_pix_numcount++)
  2072. {
  2073. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
  2074. bad_temp_pix_num++;
  2075. }
  2076. }
  2077. else if(bad_new_pix_numcount==bad_new_pix_num)
  2078. {
  2079. for(;bad_pix_numcount<bad_pix_num;bad_pix_numcount++)
  2080. {
  2081. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2082. bad_temp_pix_num++;
  2083. }
  2084. }
  2085. else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)> (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
  2086. {
  2087. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2088. bad_temp_pix_num++;
  2089. bad_pix_numcount++;
  2090. }
  2091. else if((*(p_bad_new_pix_num+bad_new_pix_numcount)& OFFSET_MASK)< (*(p_bad_pix_num+bad_pix_numcount)& OFFSET_MASK))
  2092. {
  2093. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_new_pix_num+bad_new_pix_numcount);
  2094. bad_temp_pix_num++;
  2095. bad_new_pix_numcount++;
  2096. }
  2097. else
  2098. {
  2099. *(p_bad_temp_pix+bad_temp_pix_num) = *(p_bad_pix_num+bad_pix_numcount);
  2100. bad_temp_pix_num++;
  2101. bad_pix_numcount++;
  2102. bad_new_pix_numcount++;
  2103. }
  2104. }
  2105. delete [] m_BadPixelMap[line].bad_pixel_num;
  2106. m_BadPixelMap[line].bad_pixel_num = new long [(bad_temp_pix_num+1)*sizeof(long)];
  2107. m_BadPixelMap[line].num_entries = bad_temp_pix_num;
  2108. memcpy(m_BadPixelMap[line].bad_pixel_num,p_bad_temp_pix,(bad_temp_pix_num+1)*sizeof(long));
  2109. delete [] p_bad_temp_pix;
  2110. }
  2111. FreeBadNewPixelMap();
  2112. return TRUE;
  2113. }
  2114. bool CPixMatrix::AutoBadPixelFixed(WORD *wImage, int threshold)
  2115. {
  2116. DWORD Temp,Sum;
  2117. //指向DIB图像中的数据;
  2118. WORD* pDataByte;
  2119. //差值,与阈值比较
  2120. int idelta;
  2121. // int count;
  2122. // WORD sourcetemp[9];
  2123. int i,j,x,y,k;//for loop
  2124. for (j = m_nHOffset+2; j < m_nHeight-m_nHOffset; j++)
  2125. {
  2126. Temp = (*(wImage + j*m_nWidth - 1) + *(wImage + j*m_nWidth) + *(wImage + j*m_nWidth + 1) + *(wImage + (j-1)*m_nWidth - 1) + *(wImage + (j-1)*m_nWidth) + *(wImage + (j-1)*m_nWidth + 1)+ *(wImage + (j+1)*m_nWidth - 1) + *(wImage + (j+1)*m_nWidth) + *(wImage + (j+1)*m_nWidth + 1))/9;
  2127. for (i = m_nWOffset+2; i < m_nWidth-m_nHOffset ; i++)
  2128. {
  2129. //如果差值小于阈值,则不改变此点
  2130. pDataByte = wImage + j * m_nWidth + i;
  2131. idelta = *pDataByte - Temp;
  2132. if (idelta<0) idelta = -1*idelta;
  2133. if (idelta > threshold)
  2134. {
  2135. //如果差值大于阈值,则在邻近区域3x3找到所有满足阈值条件的点,取均值
  2136. Sum = 0;
  2137. k = 0;
  2138. for (y = j - 1; y<= j+1; y++)
  2139. {
  2140. pDataByte = (unsigned short*)(wImage + y * m_nWidth + i - 1);
  2141. for(x = i - 1; x <= i+1; x++)
  2142. {
  2143. idelta = *pDataByte - Temp;
  2144. if (idelta<0) idelta = -1*idelta;
  2145. if (idelta > threshold) break;
  2146. Sum += *pDataByte;
  2147. pDataByte++;
  2148. k++;
  2149. }
  2150. }
  2151. if (k>3) *(wImage + j * m_nWidth + i) = (WORD)Sum/k;
  2152. }
  2153. Temp = *(wImage + j * m_nWidth + i);
  2154. }
  2155. }
  2156. return TRUE;
  2157. }
  2158. bool CPixMatrix::LoadLineMap( const char *fileName )
  2159. {
  2160. FILE *fp;
  2161. long *line_direction = new long;
  2162. long *line_entries = new long;
  2163. long *startpoint = new long;
  2164. long *endpoint = new long;
  2165. char *data_in = NULL;
  2166. data_in = new char [MAX_PIX_MAP_LINE];
  2167. LINE_MAP * m_xLineMapCur = NULL;
  2168. LINE_MAP * m_yLineMapCur = NULL;
  2169. if(data_in == NULL) {
  2170. return FALSE;
  2171. }
  2172. fp = fopen(fileName, "rb");
  2173. if(fp == NULL) {
  2174. return FALSE;
  2175. }
  2176. FreeLineMap(m_xLineMap);
  2177. m_xLineMap = NULL;
  2178. FreeLineMap(m_yLineMap);
  2179. m_yLineMap = NULL;
  2180. while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  2181. if(sscanf(data_in, "%d,%d:%d,%d", line_direction, line_entries, startpoint, endpoint) == 4) {
  2182. // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
  2183. if(*line_direction==0)
  2184. {
  2185. m_xLineMapCur = new LINE_MAP;
  2186. m_xLineMapCur->nextline = m_xLineMap;
  2187. m_xLineMapCur->lLine = FALSE;
  2188. m_xLineMapCur->rLine = FALSE;
  2189. m_xLineMapCur->line_entries = (WORD)*line_entries;
  2190. m_xLineMapCur->startpoint = (WORD)*startpoint;
  2191. m_xLineMapCur->endpoint = (WORD)*endpoint;
  2192. m_xLineMap = m_xLineMapCur;
  2193. }
  2194. else
  2195. {
  2196. m_yLineMapCur = new LINE_MAP;
  2197. m_yLineMapCur->nextline = m_yLineMap;
  2198. m_yLineMapCur->lLine = FALSE;
  2199. m_yLineMapCur->rLine = FALSE;
  2200. m_yLineMapCur->line_entries = (WORD)*line_entries;
  2201. m_yLineMapCur->startpoint = (WORD)*startpoint;
  2202. m_yLineMapCur->endpoint = (WORD)*endpoint;
  2203. m_yLineMap = m_yLineMapCur;
  2204. }
  2205. }
  2206. }
  2207. MarkAdjacentLine(m_xLineMap);
  2208. MarkAdjacentLine(m_yLineMap);
  2209. if(fp != NULL) {
  2210. fclose(fp);
  2211. }
  2212. if(data_in != NULL) {
  2213. delete [] data_in;
  2214. }
  2215. if(m_xLineMapCur){
  2216. FreeLineMap(m_xLineMapCur);
  2217. }
  2218. delete line_direction;
  2219. delete line_entries;
  2220. delete startpoint;
  2221. delete endpoint;
  2222. return TRUE;
  2223. }
  2224. void CPixMatrix::CorrectDBadLines(WORD *image)
  2225. {
  2226. LINE_MAP2 * m_DLineMapCur = m_doubleLineMap;
  2227. while(m_DLineMapCur)
  2228. {
  2229. if(m_DLineMapCur->nLineType == 0)
  2230. {
  2231. CorrectDXLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
  2232. }
  2233. else
  2234. {
  2235. CorrectDYLines(image, m_DLineMapCur->line_entries, m_DLineMapCur->startpoint, m_DLineMapCur->endpoint);
  2236. }
  2237. m_DLineMapCur = m_DLineMapCur->nextline;
  2238. }
  2239. }
  2240. void CPixMatrix::CorrectDXLines(WORD *image, int nentry, int nstart, int nend)
  2241. {
  2242. int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
  2243. int nMIN;//, MID, MAX;
  2244. for(int i = nstart; i<nend; i++)
  2245. {
  2246. nA1 = image[(nentry-2)*m_nWidth + i-2] - image[(nentry-1)*m_nWidth + i-1];
  2247. nB1 = image[(nentry-2)*m_nWidth + i] - image[(nentry-1)*m_nWidth + i];
  2248. nC1 = image[(nentry-2)*m_nWidth + i+2] - image[(nentry-1)*m_nWidth + i+1];
  2249. nA2 = image[(nentry-1)*m_nWidth + i-1] - image[(nentry+2)*m_nWidth + i+2];
  2250. nB2 = image[(nentry-1)*m_nWidth + i] - image[(nentry+2)*m_nWidth + i];
  2251. nC2 = image[(nentry-1)*m_nWidth + i+1] - image[(nentry+2)*m_nWidth + i-2];
  2252. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2253. nA = nA1*nA1 + nA2*nA2;
  2254. nB = nB1*nB1 + nB2*nB2;
  2255. nC = nC1*nC1 + nC2*nC2;
  2256. nMIN = nA;
  2257. if(nB<nMIN)
  2258. {
  2259. nMIN = nB;
  2260. }
  2261. if(nC<nMIN)
  2262. {
  2263. nMIN = nC;
  2264. }
  2265. if(nMIN == nA)
  2266. {
  2267. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-1]*2 + image[(nentry+2)*2880 + i+2])/3;
  2268. }
  2269. else if(nMIN == nB)
  2270. {
  2271. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i]*2 + image[(nentry+2)*2880 + i])/3;
  2272. }
  2273. else //nMIN == nC
  2274. {
  2275. image[nentry*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+1]*2 + image[(nentry+2)*2880 + i-2])/3;
  2276. }
  2277. nA1 = image[(nentry+2)*m_nWidth + i+1] - image[(nentry+3)*m_nWidth + i+2];
  2278. nB1 = image[(nentry+2)*m_nWidth + i] - image[(nentry+3)*m_nWidth + i];
  2279. nC1 = image[(nentry+2)*m_nWidth + i-1] - image[(nentry+3)*m_nWidth + i-2];
  2280. nA = nA1*nA1 + nA2*nA2;
  2281. nB = nB1*nB1 + nB2*nB2;
  2282. nC = nC1*nC1 + nC2*nC2;
  2283. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2284. nMIN = nA;
  2285. if(nB<nMIN)
  2286. {
  2287. nMIN = nB;
  2288. }
  2289. if(nC<nMIN)
  2290. {
  2291. nMIN = nC;
  2292. }
  2293. if(nMIN == nA)
  2294. {
  2295. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i-2] + image[(nentry+2)*m_nWidth + i+1]*2)/3;
  2296. }
  2297. else if(nMIN == nB)
  2298. {
  2299. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i] + image[(nentry+2)*m_nWidth + i]*2)/3;
  2300. }
  2301. else //nMIN == nC
  2302. {
  2303. image[(nentry+1)*m_nWidth + i] = (image[(nentry-1)*m_nWidth + i+2] + image[(nentry+2)*m_nWidth + i-1]*2)/3;
  2304. }
  2305. }
  2306. }
  2307. void CPixMatrix::CorrectDYLines(WORD *image, int nentry, int nstart, int nend)
  2308. {
  2309. int nA,nB,nC, nA1, nA2, nB1, nB2, nC1, nC2;
  2310. int nMIN;//, MID, MAX;
  2311. for(int i = nstart; i<nend; i++)
  2312. {
  2313. nA1 = image[(i-2)*m_nWidth + nentry-2] - image[(i-1)*m_nWidth + nentry-1];
  2314. nB1 = image[i*m_nWidth + nentry-2] - image[i*m_nWidth + nentry-1];
  2315. nC1 = image[(i+2)*m_nWidth + nentry-2] - image[(i+1)*m_nWidth + nentry-1];
  2316. nA2 = image[(i-1)*m_nWidth + nentry-1] - image[(i+2)*m_nWidth + nentry+2];
  2317. nB2 = image[i*m_nWidth + nentry-1] - image[i*m_nWidth + nentry+2];
  2318. nC2 = image[(i+1)*m_nWidth + nentry-1] - image[(i-2)*m_nWidth + nentry+2];
  2319. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2320. nA = nA1*nA1 + nA2*nA2;
  2321. nB = nB1*nB1 + nB2*nB2;
  2322. nC = nC1*nC1 + nC2*nC2;
  2323. nMIN = nA;
  2324. if(nB<nMIN)
  2325. {
  2326. nMIN = nB;
  2327. }
  2328. if(nC<nMIN)
  2329. {
  2330. nMIN = nC;
  2331. }
  2332. if(nMIN == nA)
  2333. {
  2334. image[i*m_nWidth + nentry] = (image[(i-1)*m_nWidth + nentry-1]*2 + image[(i+2)*2880 + nentry+2])/3;
  2335. }
  2336. else if(nMIN == nB)
  2337. {
  2338. image[i*m_nWidth + nentry] = (image[i*m_nWidth + nentry-1]*2 + image[i*2880 + nentry+2])/3;
  2339. }
  2340. else //nMIN == nC
  2341. {
  2342. image[i*m_nWidth + nentry] = (image[(i+1)*m_nWidth + nentry-1]*2 + image[(i-2)*2880 + nentry+2])/3;
  2343. }
  2344. nA1 = image[(i+1)*m_nWidth + nentry+2] - image[(i+2)*m_nWidth + nentry+3];
  2345. nB1 = image[i*m_nWidth + nentry+2] - image[i*m_nWidth + nentry+3];
  2346. nC1 = image[(i-1)*m_nWidth + nentry+2] - image[(i-2)*m_nWidth + nentry+3];
  2347. nA = nA1*nA1 + nA2*nA2;
  2348. nB = nB1*nB1 + nB2*nB2;
  2349. nC = nC1*nC1 + nC2*nC2;
  2350. //frame1[i*2880 + 1434] = (A + B + C)/3;
  2351. nMIN = nA;
  2352. if(nB<nMIN)
  2353. {
  2354. nMIN = nB;
  2355. }
  2356. if(nC<nMIN)
  2357. {
  2358. nMIN = nC;
  2359. }
  2360. if(nMIN == nA)
  2361. {
  2362. image[i*m_nWidth + nentry+1] = (image[(i-2)*m_nWidth + nentry-1] + image[(i+1)*m_nWidth + nentry+2]*2)/3;
  2363. }
  2364. else if(nMIN == nB)
  2365. {
  2366. image[i*m_nWidth + nentry+1] = (image[i*m_nWidth + nentry-1] + image[i*m_nWidth + nentry+2]*2)/3;
  2367. }
  2368. else //nMIN == nC
  2369. {
  2370. image[i*m_nWidth + nentry+1] = (image[(i+2)*m_nWidth + nentry-1] + image[(i-1)*m_nWidth + nentry+2]*2)/3;
  2371. }
  2372. }
  2373. }
  2374. bool CPixMatrix::LoadDLineMap( const char *fileName )
  2375. {
  2376. FILE *fp;
  2377. int *line_direction = new int;
  2378. int *line_entries = new int;
  2379. int *startpoint = new int;
  2380. int *endpoint = new int;
  2381. char *data_in = NULL;
  2382. data_in = new char [MAX_PIX_MAP_LINE];
  2383. LINE_MAP2 * m_DLineMapCur = NULL;
  2384. if(data_in == NULL) {
  2385. return FALSE;
  2386. }
  2387. fp = fopen(fileName, "rb");
  2388. if(fp == NULL) {
  2389. return FALSE;
  2390. }
  2391. FreeDBadLineMap();
  2392. m_doubleLineMap = NULL;
  2393. while(fgets(data_in, MAX_PIX_MAP_LINE, fp) != NULL) {
  2394. if(sscanf(data_in, "%d,%d:%d,%d", line_direction, line_entries, startpoint, endpoint) == 4) {
  2395. // line_direction = 0;line_entries=359, startpoint=45, endpoint=2700;
  2396. m_DLineMapCur = new LINE_MAP2;
  2397. m_DLineMapCur->nLineType = *line_direction;
  2398. m_DLineMapCur->startpoint = max(2, *startpoint);
  2399. if(m_DLineMapCur->nLineType == 0)
  2400. {
  2401. m_DLineMapCur->line_entries = min(m_nHeight-1, max(1,*line_entries));
  2402. m_DLineMapCur->endpoint = min(m_nWidth-2, *endpoint);
  2403. }
  2404. else
  2405. {
  2406. m_DLineMapCur->line_entries = min(m_nWidth-1, max(1,*line_entries));
  2407. m_DLineMapCur->endpoint = min(m_nHeight-2, *endpoint);
  2408. }
  2409. m_DLineMapCur->nextline = m_doubleLineMap;
  2410. m_doubleLineMap = m_DLineMapCur;
  2411. }
  2412. }
  2413. if(fp != NULL) {
  2414. fclose(fp);
  2415. }
  2416. if(data_in != NULL) {
  2417. delete [] data_in;
  2418. }
  2419. delete line_direction;
  2420. delete line_entries;
  2421. delete startpoint;
  2422. delete endpoint;
  2423. return TRUE;
  2424. }
  2425. bool CPixMatrix::MarkAdjacentLine( LINE_MAP *m_linemap)
  2426. {
  2427. LINE_MAP *temp_linemap;
  2428. WORD lineentry, llineentry, rlineentry;
  2429. while(m_linemap)
  2430. {
  2431. lineentry = m_linemap->line_entries;
  2432. llineentry = lineentry-1;
  2433. rlineentry = lineentry+1;
  2434. temp_linemap = m_linemap->nextline;
  2435. //check through the line to find out adjacent line
  2436. while(temp_linemap)
  2437. {
  2438. lineentry = temp_linemap->line_entries;
  2439. if(lineentry == llineentry)
  2440. {
  2441. m_linemap->lLine = TRUE;
  2442. temp_linemap->rLine = TRUE;
  2443. }
  2444. if(lineentry == rlineentry)
  2445. {
  2446. m_linemap->rLine = TRUE;
  2447. temp_linemap->lLine = TRUE;
  2448. }
  2449. temp_linemap = temp_linemap->nextline;
  2450. }
  2451. //next one
  2452. m_linemap = m_linemap->nextline;
  2453. }
  2454. return TRUE;
  2455. }
  2456. void CPixMatrix::CorrectLines(WORD* image)
  2457. {
  2458. int i,i1,i2,j,j1,j2;
  2459. DWORD avepoint, avesurrond;
  2460. WORD temp;
  2461. LINE_MAP *m_LineMapCur;
  2462. LINE_MAP *temp_linemap;
  2463. LINE_MAP *left_linemap = NULL;
  2464. //first cycle
  2465. m_LineMapCur = m_xLineMap;
  2466. while(m_LineMapCur)
  2467. {
  2468. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  2469. {
  2470. //line correction
  2471. i = m_LineMapCur->line_entries;
  2472. i1 = i-1;
  2473. i2 = i+1;
  2474. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2475. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2476. {
  2477. j1 = j-1;
  2478. j2 = j+1;
  2479. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2480. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2]+2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/8;
  2481. temp = image[i*m_nWidth+j];
  2482. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2483. }
  2484. }
  2485. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  2486. {
  2487. //line correction
  2488. i = m_LineMapCur->line_entries;
  2489. // i1 = i-1;
  2490. i2 = i+1;
  2491. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2492. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2493. {
  2494. j1 = j-1;
  2495. j2 = j+1;
  2496. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2497. avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
  2498. temp = image[i*m_nWidth+j];
  2499. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2500. }
  2501. }
  2502. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  2503. {
  2504. //line correction
  2505. i = m_LineMapCur->line_entries;
  2506. i1 = i-1;
  2507. // i2 = i+1;
  2508. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2509. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2510. {
  2511. j1 = j-1;
  2512. j2 = j+1;
  2513. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2514. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
  2515. temp = image[i*m_nWidth+j];
  2516. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2517. }
  2518. }
  2519. else
  2520. {
  2521. //skip, next cycle to correction
  2522. temp_linemap = new LINE_MAP;
  2523. temp_linemap->nextline = left_linemap;
  2524. temp_linemap->line_entries = m_LineMapCur->line_entries;
  2525. temp_linemap->lLine = FALSE;
  2526. temp_linemap->rLine = FALSE;
  2527. temp_linemap->startpoint = m_LineMapCur->startpoint;
  2528. temp_linemap->endpoint = m_LineMapCur->endpoint;
  2529. left_linemap = temp_linemap;
  2530. }
  2531. m_LineMapCur = m_LineMapCur->nextline;
  2532. }
  2533. while(left_linemap)
  2534. {
  2535. m_LineMapCur = left_linemap;
  2536. left_linemap = NULL;
  2537. MarkAdjacentLine(m_LineMapCur);
  2538. while(m_LineMapCur)
  2539. {
  2540. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  2541. {
  2542. i = m_LineMapCur->line_entries;
  2543. i1 = i-1;
  2544. i2 = i+1;
  2545. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2546. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2547. {
  2548. j1 = j-1;
  2549. j2 = j+1;
  2550. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2551. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2]+2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/8;
  2552. temp = image[i*m_nWidth+j];
  2553. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2554. }
  2555. }
  2556. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  2557. {
  2558. i = m_LineMapCur->line_entries;
  2559. // i1 = i-1;
  2560. i2 = i+1;
  2561. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2562. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2563. {
  2564. j1 = j-1;
  2565. j2 = j+1;
  2566. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2567. avesurrond = (2*image[i2*m_nWidth+j]+image[i2*m_nWidth+j1]+image[i2*m_nWidth+j2])/4;
  2568. temp = image[i*m_nWidth+j];
  2569. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2570. }
  2571. }
  2572. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  2573. {
  2574. i = m_LineMapCur->line_entries;
  2575. i1 = i-1;
  2576. // i2 = i+1;
  2577. temp = image[i*m_nWidth+(m_LineMapCur->startpoint)-1];
  2578. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2579. {
  2580. j1 = j-1;
  2581. j2 = j+1;
  2582. avepoint = (2*image[i*m_nWidth+j]+temp+image[i*m_nWidth+j2])/4;
  2583. avesurrond = (2*image[i1*m_nWidth+j]+image[i1*m_nWidth+j1]+image[i1*m_nWidth+j2])/4;
  2584. temp = image[i*m_nWidth+j];
  2585. image[i*m_nWidth+j] = WORD(image[i*m_nWidth+j]*avesurrond/avepoint);
  2586. }
  2587. }
  2588. else
  2589. {
  2590. temp_linemap = new LINE_MAP;
  2591. temp_linemap->nextline = left_linemap;
  2592. temp_linemap->line_entries = m_LineMapCur->line_entries;
  2593. temp_linemap->lLine = FALSE;
  2594. temp_linemap->rLine = FALSE;
  2595. temp_linemap->startpoint = m_LineMapCur->startpoint;
  2596. temp_linemap->endpoint = m_LineMapCur->endpoint;
  2597. left_linemap = temp_linemap;
  2598. }
  2599. temp_linemap = m_LineMapCur;
  2600. m_LineMapCur = m_LineMapCur->nextline;
  2601. delete temp_linemap;
  2602. }
  2603. }
  2604. //first cycle
  2605. m_LineMapCur = m_yLineMap;
  2606. while(m_LineMapCur)
  2607. {
  2608. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  2609. {
  2610. //line correction
  2611. i = m_LineMapCur->line_entries;
  2612. i1 = i-1;
  2613. i2 = i+1;
  2614. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2615. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2616. {
  2617. j1 = j-1;
  2618. j2 = j+1;
  2619. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2620. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1]+2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/8;
  2621. temp = image[j*m_nWidth+i];
  2622. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2623. }
  2624. }
  2625. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  2626. {
  2627. //line correction
  2628. i = m_LineMapCur->line_entries;
  2629. // i1 = i-1;
  2630. i2 = i+1;
  2631. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2632. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2633. {
  2634. j1 = j-1;
  2635. j2 = j+1;
  2636. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2637. avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
  2638. temp = image[j*m_nWidth+i];
  2639. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2640. }
  2641. }
  2642. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  2643. {
  2644. //line correction
  2645. i = m_LineMapCur->line_entries;
  2646. i1 = i-1;
  2647. // i2 = i+1;
  2648. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2649. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2650. {
  2651. j1 = j-1;
  2652. j2 = j+1;
  2653. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2654. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
  2655. temp = image[j*m_nWidth+i];
  2656. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2657. }
  2658. }
  2659. else
  2660. {
  2661. //skip, next cycle to correction
  2662. temp_linemap = new LINE_MAP;
  2663. temp_linemap->nextline = left_linemap;
  2664. temp_linemap->line_entries = m_LineMapCur->line_entries;
  2665. temp_linemap->lLine = FALSE;
  2666. temp_linemap->rLine = FALSE;
  2667. temp_linemap->startpoint = m_LineMapCur->startpoint;
  2668. temp_linemap->endpoint = m_LineMapCur->endpoint;
  2669. left_linemap = temp_linemap;
  2670. }
  2671. m_LineMapCur = m_LineMapCur->nextline;
  2672. }
  2673. while(left_linemap)
  2674. {
  2675. m_LineMapCur = left_linemap;
  2676. left_linemap = NULL;
  2677. MarkAdjacentLine(m_LineMapCur);
  2678. while(m_LineMapCur)
  2679. {
  2680. if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==FALSE))
  2681. {
  2682. i = m_LineMapCur->line_entries;
  2683. i1 = i-1;
  2684. i2 = i+1;
  2685. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2686. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2687. {
  2688. j1 = j-1;
  2689. j2 = j+1;
  2690. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2691. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1]+2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/8;
  2692. temp = image[j*m_nWidth+i];
  2693. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2694. }
  2695. }
  2696. else if((m_LineMapCur->lLine==TRUE)&&(m_LineMapCur->rLine==FALSE))
  2697. {
  2698. i = m_LineMapCur->line_entries;
  2699. // i1 = i-1;
  2700. i2 = i+1;
  2701. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2702. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2703. {
  2704. j1 = j-1;
  2705. j2 = j+1;
  2706. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2707. avesurrond = (2*image[j*m_nWidth+i2]+image[j1*m_nWidth+i2]+image[j2*m_nWidth+i2])/4;
  2708. temp = image[j*m_nWidth+i];
  2709. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2710. }
  2711. }
  2712. else if((m_LineMapCur->lLine==FALSE)&&(m_LineMapCur->rLine==TRUE))
  2713. {
  2714. i = m_LineMapCur->line_entries;
  2715. i1 = i-1;
  2716. // i2 = i+1;
  2717. temp = image[((m_LineMapCur->startpoint)-1)*m_nWidth+i];
  2718. for( j = (m_LineMapCur->startpoint); j<(m_LineMapCur->endpoint); j++)
  2719. {
  2720. j1 = j-1;
  2721. j2 = j+1;
  2722. avepoint = (2*image[j*m_nWidth+i]+temp+image[j2*m_nWidth+i])/4;
  2723. avesurrond = (2*image[j*m_nWidth+i1]+image[j1*m_nWidth+i1]+image[j2*m_nWidth+i1])/4;
  2724. temp = image[j*m_nWidth+i];
  2725. image[j*m_nWidth+i] = WORD(image[j*m_nWidth+i]*avesurrond/avepoint);
  2726. }
  2727. }
  2728. else
  2729. {
  2730. temp_linemap = new LINE_MAP;
  2731. temp_linemap->nextline = left_linemap;
  2732. temp_linemap->line_entries = m_LineMapCur->line_entries;
  2733. temp_linemap->lLine = FALSE;
  2734. temp_linemap->rLine = FALSE;
  2735. temp_linemap->startpoint = m_LineMapCur->startpoint;
  2736. temp_linemap->endpoint = m_LineMapCur->endpoint;
  2737. left_linemap = temp_linemap;
  2738. }
  2739. temp_linemap = m_LineMapCur;
  2740. m_LineMapCur = m_LineMapCur->nextline;
  2741. delete temp_linemap;
  2742. }
  2743. }
  2744. }
  2745. bool CPixMatrix::FreeLineMap(LINE_MAP *m_linemap)
  2746. {
  2747. LINE_MAP *temp_linemap;
  2748. while(m_linemap)
  2749. {
  2750. temp_linemap = m_linemap;
  2751. m_linemap = m_linemap->nextline;
  2752. delete temp_linemap;
  2753. }
  2754. m_linemap = NULL;
  2755. return TRUE;
  2756. }
  2757. //开始自动坏点校正
  2758. void CPixMatrix::BeginAutoBadPixels()
  2759. {
  2760. //if(m_lastImg)
  2761. //{
  2762. // delete [] m_lastImg;
  2763. // m_lastImg = NULL;
  2764. //}
  2765. if(m_mapImg)
  2766. {
  2767. delete [] m_mapImg;
  2768. m_mapImg = NULL;
  2769. }
  2770. //m_lastImg = new WORD[m_nWidth*m_nHeight];
  2771. m_mapImg = new WORD[m_nWidth*m_nHeight];
  2772. ZeroMemory(m_mapImg, m_nWidth*m_nHeight*sizeof(WORD));
  2773. m_curAvg = -1;
  2774. m_lastAvg = -1;
  2775. }
  2776. void CPixMatrix::AddImageforBadPixels(WORD *wImage)
  2777. {
  2778. if (wImage == NULL)
  2779. {
  2780. return;
  2781. }
  2782. //m_curImg = wImage;////////////////////////chenGN 2013.01.31
  2783. int ntempHOffset = max(0, m_nHOffset);
  2784. int ntempWOffset = max(0, m_nWOffset);
  2785. bool bFlag = DetBadPxlByMF(wImage, m_nWidth, m_nHeight, ntempWOffset, ntempHOffset, m_mapImg);////////////////////////chenGN 2013.01.31
  2786. if (!bFlag)
  2787. {
  2788. delete []m_mapImg;
  2789. return;
  2790. }
  2791. //old code delete 20100906
  2792. //calculate avg pv
  2793. /*DWORD dwSum = 0;
  2794. DWORD dwIndex = 0;
  2795. int i, j;
  2796. DWORD dwCts = 0;
  2797. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i += 8) {
  2798. dwIndex = i*m_nWidth;
  2799. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j+= 8) {
  2800. dwSum += (*(m_curImg + dwIndex + j));
  2801. dwCts++;
  2802. }
  2803. }
  2804. m_curAvg = dwSum/dwCts;
  2805. //find badpixel by thresdhold;
  2806. int thresdhold1, thresdhold2;
  2807. thresdhold1 = m_curAvg*0.6;
  2808. thresdhold2 = (m_curAvg*1.4>65535?65535:m_curAvg*1.4);
  2809. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
  2810. {
  2811. dwIndex = i*m_nWidth;
  2812. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
  2813. {
  2814. if(( *(m_curImg + dwIndex + j) < thresdhold1)||( *(m_curImg + dwIndex + j) > thresdhold2))
  2815. {
  2816. *(m_mapImg + dwIndex + j) = 0xFFFF;
  2817. }
  2818. }
  2819. }
  2820. AutoBadPointDec(m_curImg,m_nWidth, m_nHeight ,ntempHOffset, ntempWOffset,m_mapImg);
  2821. //check status;
  2822. if(m_lastAvg<0)
  2823. {
  2824. memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(WORD));
  2825. m_lastAvg = m_curAvg;
  2826. m_curAvg = -1;
  2827. //no last Image;
  2828. return;
  2829. }
  2830. else if(abs(m_curAvg - m_lastAvg)<500)
  2831. {
  2832. //not too many difference;
  2833. return;
  2834. }
  2835. else
  2836. {
  2837. double thresdhold1 = (((double) m_curAvg) / m_lastAvg)*0.9;
  2838. double thresdhold2 = (((double) m_curAvg) / m_lastAvg)*1.1;
  2839. double temp=0.0;
  2840. for(i = ntempHOffset; i < m_nHeight-ntempHOffset; i ++)
  2841. {
  2842. dwIndex = i*m_nWidth;
  2843. for(j = ntempWOffset; j < m_nWidth-ntempWOffset; j++)
  2844. {
  2845. if (*(m_lastImg + dwIndex + j)==0)
  2846. continue;
  2847. temp=((double)*(m_curImg + dwIndex + j))/(*(m_lastImg + dwIndex + j));
  2848. if((temp < thresdhold1)||(temp>thresdhold2))
  2849. {
  2850. *(m_mapImg + dwIndex + j) = 0xFFFF;
  2851. }
  2852. }
  2853. }
  2854. memcpy(m_lastImg, m_curImg, m_nHeight*m_nWidth*sizeof(WORD));
  2855. m_lastAvg = m_curAvg;
  2856. m_curAvg = -1;
  2857. }
  2858. */
  2859. }
  2860. //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
  2861. void CPixMatrix::EndAutoBadPixels(int nMode)
  2862. {
  2863. bool bReturn = true;
  2864. if(nMode == 0)
  2865. {
  2866. //0,替换原有map
  2867. FreeBadPixelMap();
  2868. bReturn = AutoBadPixelMap2(m_mapImg);
  2869. if (bReturn)
  2870. {
  2871. SaveBadPixelMap(m_charFilename);
  2872. }
  2873. }
  2874. else if(nMode == 1)
  2875. {
  2876. //1,合并原有map
  2877. bReturn = AutoBadPixelMap2(m_mapImg);
  2878. if (bReturn)
  2879. {
  2880. SaveBadPixelMap(m_charFilename);
  2881. }
  2882. }
  2883. else
  2884. {
  2885. //-1,放弃
  2886. }
  2887. //if(m_lastImg)////////////////////////chenGN 2013.01.31
  2888. //{
  2889. // delete [] m_lastImg;
  2890. // m_lastImg = NULL;
  2891. //}
  2892. if(m_mapImg)
  2893. {
  2894. delete [] m_mapImg;
  2895. m_mapImg = NULL;
  2896. }
  2897. m_curAvg = -1;
  2898. m_lastAvg = -1;
  2899. }
  2900. //add by wangyb 2013 for replace the previous function EndAutoBadPixels()
  2901. //the purpose is to carry on the same process with Gain Calibration;
  2902. //结束自动换点校正,-1,放弃;0,替换原有map;1,合并原有map
  2903. bool CPixMatrix::StoreBadPixels(const char * charFilename,int nMode)
  2904. {
  2905. bool bReturn = true;
  2906. if(nMode == 0)
  2907. {
  2908. //0,替换原有map
  2909. FreeBadPixelMap();
  2910. bReturn = AutoBadPixelMap2(m_mapImg);
  2911. if (bReturn)
  2912. {
  2913. SaveBadPixelMap(charFilename);
  2914. }
  2915. }
  2916. else if(nMode == 1)
  2917. {
  2918. //1,合并原有map
  2919. bReturn = AutoBadPixelMap2(m_mapImg);
  2920. if (bReturn)
  2921. {
  2922. SaveBadPixelMap(charFilename);
  2923. }
  2924. }
  2925. else
  2926. {
  2927. //-1,放弃
  2928. }
  2929. //if(m_lastImg)////////////////////////chenGN 2013.01.31
  2930. //{
  2931. // delete [] m_lastImg;
  2932. // m_lastImg = NULL;
  2933. //}
  2934. if(m_mapImg)
  2935. {
  2936. delete [] m_mapImg;
  2937. m_mapImg = NULL;
  2938. }
  2939. m_curAvg = -1;
  2940. m_lastAvg = -1;
  2941. return bReturn;
  2942. }
  2943. void CPixMatrix::CorrectButCross(WORD* image)
  2944. {
  2945. }
  2946. bool CPixMatrix::AutoBadPointDec(unsigned short* lpDetail,int lWidth, int lHeight ,int Xoffset, int Yoffset,unsigned short* pmap)
  2947. {
  2948. unsigned short* lpNewDataBits =lpDetail;
  2949. // new unsigned short[lWidth*lHeight];
  2950. // memcpy(lpNewDataBits,lpDetail,sizeof(unsigned short)*lWidth*lHeight);
  2951. int sort1[4];//big
  2952. int sort2[4];//small
  2953. int sort3[3];//big
  2954. int sort4[3];//small
  2955. double Neighbor_sum=0;
  2956. double Neighbor_avg=0;
  2957. long i,j; //for loop
  2958. int temp;
  2959. int locallength=50; //区域直方图长度
  2960. int start_avg;
  2961. start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,Xoffset ,Yoffset,locallength);
  2962. //
  2963. int last_avg=start_avg;
  2964. for(i = Xoffset; i<lHeight-Xoffset; i++)
  2965. {
  2966. // if (i+locallength+Xoffset>=lHeight)
  2967. // moveoff=lHeight-Xoffset-locallength;
  2968. // else
  2969. // moveoff=i;
  2970. // start_avg=GetAvg(lpNewDataBits,lWidth,lHeight,moveoff,Yoffset,locallength);
  2971. start_avg=last_avg;
  2972. for(j = Yoffset; j<lWidth-Yoffset; j++)
  2973. {
  2974. Neighbor_sum=0.0;
  2975. Neighbor_avg=0.0;
  2976. if(lpNewDataBits[(i-1)*lWidth+j-1]>lpNewDataBits[(i+1)*lWidth+j+1])
  2977. {
  2978. sort1[0] = lpNewDataBits[(i-1)*lWidth+j-1];
  2979. sort2[0] = lpNewDataBits[(i+1)*lWidth+j+1];
  2980. }
  2981. else
  2982. {
  2983. sort1[0] = lpNewDataBits[(i+1)*lWidth+j+1];
  2984. sort2[0] = lpNewDataBits[(i-1)*lWidth+j-1];
  2985. }
  2986. if(lpNewDataBits[(i-1)*lWidth+j]>lpNewDataBits[(i+1)*lWidth+j])
  2987. {
  2988. sort1[1] = lpNewDataBits[(i-1)*lWidth+j];
  2989. sort2[1] = lpNewDataBits[(i+1)*lWidth+j];
  2990. }
  2991. else
  2992. {
  2993. sort1[1] = lpNewDataBits[(i+1)*lWidth+j];
  2994. sort2[1] = lpNewDataBits[(i-1)*lWidth+j];
  2995. }
  2996. if(lpNewDataBits[(i-1)*lWidth+j+1]>lpNewDataBits[(i+1)*lWidth+j-1])
  2997. {
  2998. sort1[2] = lpNewDataBits[(i-1)*lWidth+j+1];
  2999. sort2[2] = lpNewDataBits[(i+1)*lWidth+j-1];
  3000. }
  3001. else
  3002. {
  3003. sort1[2] = lpNewDataBits[(i+1)*lWidth+j-1];
  3004. sort2[2] = lpNewDataBits[(i-1)*lWidth+j+1];
  3005. }
  3006. if(lpNewDataBits[i*lWidth+j-1]>lpNewDataBits[i*lWidth+j+1])
  3007. {
  3008. sort1[3] = lpNewDataBits[i*lWidth+j-1];
  3009. sort2[3] = lpNewDataBits[i*lWidth+j+1];
  3010. }
  3011. else
  3012. {
  3013. sort1[3] = lpNewDataBits[i*lWidth+j+1];
  3014. sort2[3] = lpNewDataBits[i*lWidth+j-1];
  3015. }
  3016. //delete the max of the sort1[4];
  3017. if (sort1[0]>sort1[2])
  3018. {
  3019. temp=sort1[2];
  3020. sort1[2]=sort1[0];
  3021. sort1[0]=temp;
  3022. }
  3023. if (sort1[1]>sort1[3])
  3024. {
  3025. temp=sort1[3];
  3026. sort1[3]=sort1[1];
  3027. sort1[1]=temp;
  3028. }
  3029. if (sort1[2]>sort1[3])
  3030. {
  3031. sort1[2]=sort1[3];
  3032. }
  3033. if (sort2[0]<sort2[2])
  3034. {
  3035. temp=sort2[2];
  3036. sort2[2]=sort2[0];
  3037. sort2[0]=temp;
  3038. }
  3039. if (sort2[1]<sort2[3])
  3040. {
  3041. temp=sort2[3];
  3042. sort2[3]=sort2[1];
  3043. sort2[1]=temp;
  3044. }
  3045. if (sort2[2]<sort2[3])
  3046. {
  3047. sort2[2]=sort2[3];
  3048. }
  3049. //在剩下的中找最值,重新生成三组值.
  3050. if (sort1[0]>sort2[0])
  3051. {
  3052. sort3[0]=sort1[0];
  3053. sort4[0]=sort2[0];
  3054. }
  3055. else
  3056. {
  3057. sort3[0]=sort2[0];
  3058. sort4[0]=sort1[0];
  3059. }
  3060. if (sort1[1]>sort2[1])
  3061. {
  3062. sort3[1]=sort1[1];
  3063. sort4[1]=sort2[1];
  3064. }
  3065. else
  3066. {
  3067. sort3[1]=sort2[1];
  3068. sort4[1]=sort1[1];
  3069. }
  3070. if (sort1[2]>sort2[2])
  3071. {
  3072. sort3[2]=sort1[2];
  3073. sort4[2]=sort2[2];
  3074. }
  3075. else
  3076. {
  3077. sort3[2]=sort2[2];
  3078. sort4[2]=sort1[2];
  3079. }
  3080. //将这六个值相加,后减去一个最大值和最小值.
  3081. Neighbor_sum=sort3[0]+sort3[1]+sort3[2]+sort4[0]+sort4[1]+sort4[2];
  3082. //剩余三个点计算均值
  3083. if (sort3[0]>sort3[1])
  3084. {
  3085. if (sort3[0]>sort3[2])
  3086. Neighbor_sum= Neighbor_sum-sort3[0];
  3087. else
  3088. Neighbor_sum= Neighbor_sum-sort3[2];
  3089. }
  3090. else
  3091. {
  3092. if (sort3[1]>sort3[2])
  3093. Neighbor_sum= Neighbor_sum-sort3[1];
  3094. else
  3095. Neighbor_sum= Neighbor_sum-sort3[2];
  3096. }
  3097. //找最小值
  3098. if (sort4[0]<sort4[1])
  3099. {
  3100. if (sort4[0]<sort4[2])
  3101. Neighbor_sum= Neighbor_sum-sort4[0];
  3102. else
  3103. Neighbor_sum= Neighbor_sum-sort4[2];
  3104. }
  3105. else
  3106. {
  3107. if (sort4[1]<sort4[2])
  3108. Neighbor_sum= Neighbor_sum-sort4[1];
  3109. else
  3110. Neighbor_sum= Neighbor_sum-sort4[2];
  3111. }
  3112. Neighbor_avg=Neighbor_sum/4.0;
  3113. if ((Neighbor_avg<start_avg*1.3)&&(Neighbor_avg>start_avg*0.8))
  3114. {
  3115. start_avg=(int)Neighbor_avg;
  3116. }
  3117. else
  3118. {
  3119. int tempccc = 0;
  3120. }
  3121. if((*(lpNewDataBits+i*lWidth+j)>start_avg*1.2)||(*(lpNewDataBits+i*lWidth+j)<start_avg*0.8))
  3122. {
  3123. *(pmap+i*lWidth+j)=0xFFFF;
  3124. }
  3125. if (j==Yoffset)
  3126. {
  3127. last_avg=start_avg;
  3128. }
  3129. }
  3130. }
  3131. return true;
  3132. }
  3133. int CPixMatrix::GetAvg(unsigned short* pData,int Width, int Height ,int Xoffset, int Yoffset, int length)
  3134. {
  3135. int i,j;
  3136. int index;
  3137. int *hist = new int[PIXEL_MAX_VALUE];
  3138. for (i=0;i<65535;++i)
  3139. hist[i]=0;
  3140. double sum=0.0;
  3141. double sumsum=0.0;
  3142. int avg=0;
  3143. double rate;
  3144. int count=0;
  3145. for (i=Xoffset; i<Xoffset+length;++i)
  3146. {
  3147. index=i*Width;
  3148. for (j=Yoffset ; j<Yoffset+length;++j)
  3149. {
  3150. ++hist[*(pData+index+j)];
  3151. }
  3152. }
  3153. for (i=0;i<65535;++i)
  3154. {
  3155. sum+=hist[i];
  3156. rate=sum/(length*length);
  3157. if ((rate>0.2)&&(rate<0.8))
  3158. {
  3159. count+=hist[i];
  3160. sumsum+=hist[i]*i;
  3161. }
  3162. }
  3163. avg=(int)sumsum/count;
  3164. delete []hist;
  3165. return avg;
  3166. }
  3167. //code begin 20100906
  3168. /************************************************************************
  3169. FUNCTION NAME: Mean7
  3170. DESCRIPTION: Filter raw image by 7 * 7 mean filter
  3171. RETURN VALUE:
  3172. PARA: [IN\OUT] pImgData: raw image data
  3173. [IN] nWidth: raw image width
  3174. [IN] nHeight: raw image height
  3175. [IN] nXOffset: raw image's offset in x direction
  3176. [IN] nYOffset: raw image's offset in y direction
  3177. HISTORY: Aug\24\2010 written by Alex Stocks
  3178. ************************************************************************/
  3179. /*
  3180. int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3181. {
  3182. if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
  3183. || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
  3184. {
  3185. return -1;
  3186. }
  3187. HGLOBAL hImgBuffer = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3188. if (NULL == hImgBuffer)
  3189. {
  3190. ::GlobalUnlock(hImgBuffer);
  3191. return -1;
  3192. }
  3193. ushort_t* pImgBuffer = (ushort_t*) ::GlobalLock(hImgBuffer);
  3194. int nMinXIdx = nXOffset;
  3195. int nMaxXIdx = nWidth - nXOffset;
  3196. int nMinYIdx = nYOffset;
  3197. int nMaxYIdx = nHeight - nYOffset;
  3198. int nIdxI = 0;
  3199. int nIdxJ = 0;
  3200. int nIndex = 0;
  3201. ushort_t* pBuffer = NULL;
  3202. ushort_t* pData = NULL;
  3203. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3204. {
  3205. nIndex = nIdxI * nWidth + nMinXIdx;
  3206. pBuffer = pImgBuffer + nIndex;
  3207. pData = pImgData + nIndex;
  3208. *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
  3209. *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
  3210. *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
  3211. nIndex -= nMinXIdx;
  3212. for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
  3213. {
  3214. *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
  3215. + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
  3216. + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
  3217. + *(pData + nIdxJ + 3)) / 7;
  3218. }
  3219. nIndex += nMaxXIdx;
  3220. pBuffer = pImgBuffer + nIndex;
  3221. pData = pImgData + nIndex;
  3222. *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
  3223. + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
  3224. *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
  3225. + 2 * (*(pData -2)) + *(pData -1)) / 7;
  3226. *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
  3227. + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
  3228. }
  3229. int nInc1 = nWidth * 1;
  3230. int nInc2 = nWidth * 2;
  3231. int nInc3 = nWidth * 3;
  3232. int nInc4 = nWidth * 4;
  3233. int nInc5 = nWidth * 5;
  3234. int nInc6 = nWidth * 6;
  3235. int nGVSum = 0;
  3236. int nStartYPos = 0;
  3237. int nEndYPos = 0;
  3238. for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
  3239. {
  3240. nIndex = nMinYIdx * nWidth + nIdxI;
  3241. pBuffer = pImgBuffer + nIndex;
  3242. pData = pImgData + nIndex;
  3243. *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
  3244. *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
  3245. *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
  3246. nIndex -= nMinYIdx * nWidth;
  3247. nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
  3248. nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
  3249. nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
  3250. pBuffer = pImgBuffer;
  3251. pData = pImgData;
  3252. for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
  3253. {
  3254. nGVSum += *(pData + nIdxJ + nInc3);
  3255. *(pBuffer + nIdxJ) = nGVSum / 7;
  3256. nGVSum -= *(pBuffer + nIdxJ - nInc3);
  3257. }
  3258. nIndex = nMaxYIdx * nWidth + nIdxI;
  3259. pBuffer = pImgBuffer + nIndex;
  3260. pData = pImgData + nIndex;
  3261. *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3262. *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3263. *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
  3264. }
  3265. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3266. ::GlobalUnlock(hImgBuffer);
  3267. ::GlobalFree(hImgBuffer);
  3268. return 1;
  3269. }
  3270. */
  3271. int CPixMatrix::Mean7(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3272. {
  3273. if (NULL == pImgData || nWidth <= 2 * nXOffset + 7 || nXOffset < 0
  3274. || nHeight <= 2 * nYOffset + 7 || nYOffset < 0)
  3275. {
  3276. return -1;
  3277. }
  3278. HGLOBAL hImgBuffer = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3279. if (NULL == hImgBuffer)
  3280. {
  3281. ::GlobalUnlock(hImgBuffer);
  3282. return -1;
  3283. }
  3284. ushort_t* pImgBuffer = (ushort_t*) ::GlobalLock(hImgBuffer);
  3285. int nMinXIdx = nXOffset;
  3286. int nMaxXIdx = nWidth - nXOffset;
  3287. int nMinYIdx = nYOffset;
  3288. int nMaxYIdx = nHeight - nYOffset;
  3289. int nIdxI = 0;
  3290. int nIdxJ = 0;
  3291. int nIndex = 0;
  3292. ushort_t* pBuffer = NULL;
  3293. ushort_t* pData = NULL;
  3294. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3295. {
  3296. nIndex = nIdxI * nWidth + nMinXIdx;
  3297. pBuffer = pImgBuffer + nIndex;
  3298. pData = pImgData + nIndex;
  3299. *(pBuffer) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + 2 * (*(pData + 3))) / 7;
  3300. *(pBuffer + 1) = (*(pData) + 2 * (*(pData + 1)) + 2 * (*(pData + 2)) + *(pData + 3) + *(pData + 4)) / 7;
  3301. *(pBuffer + 2) = (*(pData) + 2 *(*(pData + 1)) + *(pData + 2) + *(pData + 3) + *(pData + 4) + *(pData + 5)) / 7;
  3302. nIndex -= nMinXIdx;
  3303. pBuffer = pImgBuffer + nIndex;
  3304. pData = pImgData + nIndex;
  3305. for (nIdxJ = nMinXIdx + 3; nIdxJ < nMaxXIdx - 3; ++nIdxJ)
  3306. {
  3307. *(pBuffer + nIdxJ) = (*(pData + nIdxJ - 3) + *(pData + nIdxJ - 2)
  3308. + *(pData + nIdxJ - 1) + *(pData + nIdxJ)
  3309. + *(pData + nIdxJ + 1) + *(pData + nIdxJ + 2)
  3310. + *(pData + nIdxJ + 3)) / 7;
  3311. }
  3312. nIndex += nMaxXIdx;
  3313. pBuffer = pImgBuffer + nIndex;
  3314. pData = pImgData + nIndex;
  3315. *(pBuffer - 3) = (*(pData - 6) + *(pData - 5) + *(pData - 4)
  3316. + *(pData - 3) + 2 * (*(pData - 2)) + *(pData - 1)) / 7;
  3317. *(pBuffer - 2) = (*(pData -5) + *(pData -4) + 2 * (*(pData -3))
  3318. + 2 * (*(pData -2)) + *(pData -1)) / 7;
  3319. *(pBuffer - 1) = (2 * (*(pData - 4)) + 2 * (*(pData - 3))
  3320. + 2 * (*(pData - 2)) + *(pData - 1) ) / 7;
  3321. }
  3322. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3323. int nInc1 = nWidth * 1;
  3324. int nInc2 = nWidth * 2;
  3325. int nInc3 = nWidth * 3;
  3326. int nInc4 = nWidth * 4;
  3327. int nInc5 = nWidth * 5;
  3328. int nInc6 = nWidth * 6;
  3329. int nGVSum = 0;
  3330. int nStartYPos = 0;
  3331. int nEndYPos = 0;
  3332. for (nIdxI = nMinXIdx; nIdxI < nMaxXIdx; ++nIdxI)
  3333. {
  3334. nIndex = nMinYIdx * nWidth + nIdxI;
  3335. pBuffer = pImgBuffer + nIndex;
  3336. pData = pImgData + nIndex;
  3337. *(pBuffer) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + 2 * (*(pData + nInc3))) / 7;
  3338. *(pBuffer + nInc1) = (*(pData) + 2 * (*(pData + nInc1)) + 2 * (*(pData + nInc2)) + *(pData + nInc3) + *(pData + nInc4)) / 7;
  3339. *(pBuffer + nInc2) = (*(pData) + 2 *(*(pData + nInc1)) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5)) / 7;
  3340. nIndex -= nMinYIdx * nWidth;
  3341. nGVSum = *(pData) + *(pData + nInc1) + *(pData + nInc2) + *(pData + nInc3) + *(pData + nInc4) + *(pData + nInc5);
  3342. nStartYPos = (nMinYIdx + 3) * nWidth + nIdxI;
  3343. nEndYPos = (nMaxYIdx - 3) * nWidth + nIdxI;
  3344. pBuffer = pImgBuffer;
  3345. pData = pImgData;
  3346. for (nIdxJ = nStartYPos; nIdxJ < nEndYPos; nIdxJ += nWidth)
  3347. {
  3348. nGVSum += *(pData + nIdxJ + nInc3);
  3349. *(pBuffer + nIdxJ) = nGVSum / 7;
  3350. nGVSum -= *(pBuffer + nIdxJ - nInc3);
  3351. }
  3352. nIndex = nMaxYIdx * nWidth + nIdxI;
  3353. pBuffer = pImgBuffer + nIndex;
  3354. pData = pImgData + nIndex;
  3355. *(pBuffer - nInc3) = (*(pData - nInc6) + *(pData - nInc5) + *(pData - nInc4) + *(pData - nInc3) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3356. *(pBuffer - nInc2) = (*(pData - nInc5) + *(pData - nInc4) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1)) / 7;
  3357. *(pBuffer - nInc1) = (2 * (*(pData - nInc4)) + 2 * (*(pData - nInc3)) + 2 * (*(pData - nInc2)) + *(pData - nInc1) ) / 7;
  3358. }
  3359. memcpy(pImgData, pImgBuffer, nHeight * nWidth * sizeof(ushort_t));
  3360. // CFile file;
  3361. // CString strDignosisPath = _T("e:\\Varian.raw");
  3362. // file.Open(strDignosisPath, CFile::modeCreate | CFile::modeWrite );
  3363. // file.Write(pImgBuffer, m_nWidth*m_nHeight*sizeof(WORD));
  3364. // file.Close();
  3365. ::GlobalUnlock(hImgBuffer);
  3366. ::GlobalFree(hImgBuffer);
  3367. return 1;
  3368. }
  3369. /************************************************************************
  3370. FUNCTION NAME: CalcGlbAvgPxlValueBySamp
  3371. DESCREPTION: Calculate global average pixel value by sampling method
  3372. RETURN VALUE: Global average pixel value.
  3373. PARA: [IN]pImgData: raw image data array
  3374. [IN]nWidth: raw image's width
  3375. [IN]nHeight: raw image's height
  3376. [IN]nXOffset: raw image's offset in x direction
  3377. [IN]nYOffset: raw image's offset in y direction
  3378. HISTORY: August/2/2010 written by Alex Stocks
  3379. ************************************************************************/
  3380. double CPixMatrix::CalcGlbAvgPxlValueBySamp(unsigned short* pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset)
  3381. {
  3382. if (NULL == pImgData || nWidth < 0 || nHeight < 0 || nXOffset < 0
  3383. || nYOffset < 0 || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  3384. {
  3385. return -1.0f;
  3386. }
  3387. int nIdxI = 0;
  3388. int nIdxJ = 0;
  3389. int nIndex = 0;
  3390. int nHist[PIXEL_MAX_VALUE] = { 0 };
  3391. int nMinYIdx = nYOffset;
  3392. int nMaxYIdx = nHeight - nYOffset;
  3393. int nMinXIdx = nXOffset;
  3394. int nMaxXIdx = nWidth - nXOffset;
  3395. unsigned short *pImage = pImgData;
  3396. int nPxlNum = 0;
  3397. for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; nIdxI += 8)
  3398. {
  3399. pImage = pImgData + nIdxI * nWidth;
  3400. for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; nIdxJ += 8)
  3401. {
  3402. ++nHist[*(pImage+nIdxJ)];
  3403. ++nPxlNum;
  3404. }
  3405. }
  3406. //code delete 20110125
  3407. // double fSum=0.0f;
  3408. // double fGVSum = 0.0f;
  3409. // double fGVAvg=0;
  3410. // double fRatio = 0;
  3411. // int nCount=0;
  3412. // for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
  3413. // {
  3414. // fSum += nHist[nIdxI];
  3415. // fRatio = fSum / nPxlNum;
  3416. // if (0.15f < fRatio && fRatio < 0.85f)
  3417. // {
  3418. // nCount += nHist[nIdxI];
  3419. // fGVSum += nHist[nIdxI] * nIdxI;
  3420. // }
  3421. // }
  3422. // if (nCount <= 0)
  3423. // {
  3424. // return -1.0f;
  3425. // }
  3426. //code begin 20110125
  3427. double fSum=0.0f;
  3428. double fGVSum = 0.0f;
  3429. double fGVAvg=0;
  3430. double fRatio = 0;
  3431. int nCount=0;
  3432. float fTempGVSum = 0.0f;
  3433. for (nIdxI = 0; nIdxI < 65535; ++nIdxI)
  3434. {
  3435. fSum += nHist[nIdxI];
  3436. fRatio = fSum / nPxlNum;
  3437. fTempGVSum += nHist[nIdxI] * nIdxI;
  3438. if (0.15f < fRatio && fRatio < 0.85f)
  3439. {
  3440. nCount += nHist[nIdxI];
  3441. fGVSum += nHist[nIdxI] * nIdxI;
  3442. }
  3443. }
  3444. if (nCount <= 0)
  3445. {
  3446. nCount = nPxlNum;
  3447. fGVSum = fTempGVSum;
  3448. }
  3449. //code end 20110125
  3450. fGVAvg = fGVSum / nCount;
  3451. return fGVAvg;
  3452. }
  3453. ///////////////////////////////////redesigned DetBadPxlByMF by CGN 2013.01.30
  3454. bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
  3455. {
  3456. if (NULL == pImgData || NULL == pMap
  3457. || nWidth <= 0 || nHeight <= 0
  3458. || nXOffset < 0 || nYOffset < 0
  3459. || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  3460. {
  3461. return false;
  3462. }
  3463. ///////////////////////////全图阈值计算,挑出黑白坏点
  3464. float fGlbAvgPxlValue = (float)CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
  3465. float fMinThreshold = 0.5f * fGlbAvgPxlValue;
  3466. float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
  3467. if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
  3468. {
  3469. fMaxThreshold = 65535.0f;
  3470. }
  3471. int nMinXIdx = nXOffset;
  3472. int nMaxXIdx = nWidth - nXOffset;
  3473. int nMinYIdx = nYOffset;
  3474. int nMaxYIdx = nHeight - nYOffset;
  3475. int bcount = 0;
  3476. for ( int i = nMinYIdx; i < nMaxYIdx; ++i )
  3477. {
  3478. for ( int j = nMinXIdx; j < nMaxXIdx; ++j )
  3479. {
  3480. if ( pImgData[ i * nWidth + j ] < fMinThreshold || fMaxThreshold < pImgData[ i * nWidth + j ] )
  3481. {
  3482. pMap[ i * nWidth + j ] = 65535;
  3483. bcount++;
  3484. }
  3485. }
  3486. }
  3487. /////////////////////////////////将图像分成4x4区域,在每个区域中用7x7像素模版进行坏点判断
  3488. /*int Heightbegin = nYOffset;
  3489. int Heightend = nYOffset + ( nHeight - 2 * nYOffset ) / 4;
  3490. int Widthbegin = nXOffset;
  3491. int Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
  3492. int nsum = 0;
  3493. int ncount = 0;
  3494. int navg = 0;
  3495. int nthres = 0;
  3496. for ( int i = 0; i < 4; i++ )
  3497. {
  3498. Widthbegin = nXOffset;
  3499. Widthend = nXOffset + ( nWidth - 2 * nXOffset ) / 4;
  3500. for ( int j = 0; j < 4; j++ )
  3501. {
  3502. for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
  3503. {
  3504. for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
  3505. {
  3506. ncount = 0;
  3507. nsum = 0;
  3508. navg = 0;
  3509. nthres = 0;
  3510. if ( pMap[ k * nWidth + p ] != 65535 )
  3511. {
  3512. for ( int m = -3; m < 4; m++ )
  3513. {
  3514. for ( int n = -3; n < 4; n++ )
  3515. {
  3516. if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
  3517. {
  3518. nsum += pImgData[ ( k + m ) * nWidth + p + n ];
  3519. ncount++;
  3520. }
  3521. }
  3522. }
  3523. navg = nsum / ncount;
  3524. nthres = navg / 10;
  3525. if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
  3526. {
  3527. pMap[ k * nWidth + p ] = 65535;
  3528. bcount++;
  3529. }
  3530. }
  3531. }
  3532. }
  3533. Widthbegin += ( nWidth - 2 * nXOffset ) / 4;
  3534. Widthend += ( nWidth - 2 * nXOffset ) / 4;
  3535. }
  3536. Heightbegin += ( nHeight - 2 * nYOffset ) / 4;
  3537. Heightend += ( nHeight - 2 * nYOffset ) / 4;
  3538. }*/
  3539. int Heightbegin = nYOffset;
  3540. int Heightend = nHeight - nYOffset;
  3541. int Widthbegin = nXOffset;
  3542. int Widthend = nWidth - nXOffset;
  3543. int nsum = 0;
  3544. int ncount = 0;
  3545. int navg = 0;
  3546. int nthres = 0;
  3547. for ( int k = Heightbegin + 3; k < Heightend - 3 ; k++ )
  3548. {
  3549. for ( int p = Widthbegin + 3; p < Widthend - 3; p++ )
  3550. {
  3551. ncount = 0;
  3552. nsum = 0;
  3553. navg = 0;
  3554. nthres = 0;
  3555. if ( pMap[ k * nWidth + p ] != 65535 )
  3556. {
  3557. for ( int m = -3; m < 4; m++ )
  3558. {
  3559. for ( int n = -3; n < 4; n++ )
  3560. {
  3561. if ( pMap[ ( k + m ) * nWidth + p + n ] != 65535 )
  3562. {
  3563. nsum += pImgData[ ( k + m ) * nWidth + p + n ];
  3564. ncount++;
  3565. }
  3566. }
  3567. }
  3568. navg = nsum / ncount;
  3569. nthres = navg / 10;
  3570. if ( abs( pImgData[ k * nWidth + p ] - navg ) > nthres )
  3571. {
  3572. pMap[ k * nWidth + p ] = 65535;
  3573. bcount++;
  3574. }
  3575. }
  3576. }
  3577. }
  3578. //FILE *f1;
  3579. //f1=fopen("D:\\my study\\program\\grid suppression image\\careray1500p test\\badmap.raw","wb");
  3580. //
  3581. // fwrite( pMap, sizeof(unsigned short), nHeight * nWidth, f1);
  3582. // fclose( f1 );
  3583. return true;
  3584. }
  3585. /************************************************************************
  3586. FUNCTION NAME: DetBadPxlByMF
  3587. DESCRIPTION: detect bad pixels by 7 * 7 mean filter
  3588. RETURN VALUE:
  3589. PARA: [IN] pImageData: primary raw image data array
  3590. [IN] nWidth: raw image width
  3591. [IN] nHeight: raw image height
  3592. [IN] nXOffset: raw image's offset in x direction
  3593. [IN] nYOffset: raw image's offset in y direction
  3594. [IN\OUT] pMap: raw image's bad pixel array
  3595. HISTORY: Aug\23\2010 written by Alex Stocks
  3596. ************************************************************************/
  3597. //bool CPixMatrix::DetBadPxlByMF(ushort_t *pImgData, int nWidth, int nHeight, int nXOffset, int nYOffset, ushort_t *pMap)
  3598. //{
  3599. // if (NULL == pImgData || NULL == pMap
  3600. // || nWidth <= 0 || nHeight <= 0
  3601. // || nXOffset < 0 || nYOffset < 0
  3602. // || nWidth <= 2 * nXOffset || nHeight <= 2 * nYOffset)
  3603. // {
  3604. // return false;
  3605. // }
  3606. //
  3607. // //////////////////////////////////////////////////////////////////////////
  3608. // //step1: detect bad pixels by global threshold
  3609. // //////////////////////////////////////////////////////////////////////////
  3610. // float fGlbAvgPxlValue = CalcGlbAvgPxlValueBySamp(pImgData, nWidth, nHeight, nXOffset, nYOffset);
  3611. // float fMinThreshold = 0.5f * fGlbAvgPxlValue;
  3612. // float fMaxThreshold = 1.5f * fGlbAvgPxlValue;
  3613. // if (65535.0f < fMaxThreshold) //可能是导致16位图像坏点过多判断的原因,16383修改为65535,by陈冠男,2012-12-20
  3614. // {
  3615. // fMaxThreshold = 65535.0f;
  3616. // }
  3617. // int nMinXIdx = nXOffset;
  3618. // int nMaxXIdx = nWidth - nXOffset;
  3619. // int nMinYIdx = nYOffset;
  3620. // int nMaxYIdx = nHeight - nYOffset;
  3621. // int nIdxI = 0;
  3622. // int nIdxJ = 0;
  3623. // int nIndex = 0;
  3624. //
  3625. // HGLOBAL hMdfPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3626. // if (NULL == hMdfPic)
  3627. // {
  3628. // ::GlobalUnlock((HGLOBAL)hMdfPic);
  3629. // return false;
  3630. // }
  3631. // ushort_t *pMdfPic = (ushort_t*) ::GlobalLock((HGLOBAL)hMdfPic);
  3632. // memcpy(pMdfPic, pImgData, nHeight * nWidth * sizeof(ushort_t));
  3633. //
  3634. // //use average pixel value in place of every bad pixel's pixel value
  3635. // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3636. // {
  3637. // nIndex = nIdxI * nWidth;
  3638. // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
  3639. // {
  3640. // if (*(pMap + nIndex + nIdxJ) == 0xFFFF)
  3641. // {
  3642. // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
  3643. // continue;
  3644. // }
  3645. // if (*(pImgData + nIndex + nIdxJ) < fMinThreshold || fMaxThreshold < *(pImgData + nIndex + nIdxJ))
  3646. // {
  3647. // *(pMap + nIndex + nIdxJ) = 0xFFFF;
  3648. // *(pMdfPic + nIndex + nIdxJ) = (int)fGlbAvgPxlValue;
  3649. // }
  3650. // }
  3651. // }
  3652. //
  3653. // //////////////////////////////////////////////////////////////////////////
  3654. // //step2: detect bad pixels by mean filter
  3655. // //////////////////////////////////////////////////////////////////////////
  3656. // HGLOBAL hImpPic = ::GlobalAlloc(GHND, nHeight * nWidth * sizeof(ushort_t));
  3657. // if (NULL == hImpPic)
  3658. // {
  3659. // ::GlobalUnlock(hImpPic);
  3660. // return false;
  3661. // }
  3662. // ushort_t* pImpPic = (ushort_t*) ::GlobalLock(hImpPic);
  3663. // memcpy(pImpPic, pMdfPic, nHeight * nWidth * sizeof(ushort_t));
  3664. // ::GlobalUnlock(hMdfPic);
  3665. // ::GlobalFree(hMdfPic);
  3666. // int nAns = Mean7(pImpPic, nWidth, nHeight, nXOffset, nYOffset);
  3667. // if (nAns < 0)
  3668. // {
  3669. // ::GlobalUnlock(hImpPic);
  3670. // ::GlobalFree(hImpPic);
  3671. // //::GlobalUnlock(hMdfPic);
  3672. // //::GlobalFree(hMdfPic);
  3673. // return false;
  3674. // }
  3675. // int nDiff = 0;
  3676. // float fAbsDiff = fGlbAvgPxlValue * 0.10f;
  3677. // for (nIdxI = nMinYIdx; nIdxI < nMaxYIdx; ++nIdxI)
  3678. // {
  3679. // nIndex = nIdxI * nWidth;
  3680. // for (nIdxJ = nMinXIdx; nIdxJ < nMaxXIdx; ++nIdxJ)
  3681. // {
  3682. // if (*(pMap + nIndex + nIdxJ) != 0xFFFF)
  3683. // {
  3684. // nDiff = *(pImgData + nIndex + nIdxJ) - *(pImpPic + nIndex + nIdxJ);
  3685. // if (fAbsDiff < abs(nDiff))
  3686. // {
  3687. // *(pMap + nIndex + nIdxJ) = 0xFFFF;
  3688. // }
  3689. // }
  3690. // }
  3691. // }
  3692. //
  3693. // ::GlobalUnlock(hImpPic);
  3694. // ::GlobalFree(hImpPic);
  3695. // //::GlobalUnlock(hMdfPic);
  3696. // //::GlobalFree(hMdfPic);
  3697. // return true;
  3698. //}
  3699. int CPixMatrix::BadGridLineCorrect( unsigned short *pImage)
  3700. {
  3701. long BadPixNum;
  3702. long BadNum;
  3703. memset( m_TempImage, 0, sizeof(unsigned short) * m_nWidth * m_nHeight );
  3704. memcpy( m_TempImage, pImage,sizeof(unsigned short) * m_nWidth * m_nHeight );
  3705. for ( int i = m_nHOffset + 2; i < m_nHeight - m_nHOffset - 2; i++ )
  3706. {
  3707. m_pBadPixNum = m_BadPixelMap[ i ].bad_pixel_num;
  3708. if ( m_BadPixelMap[ i ].num_entries > 0 )
  3709. {
  3710. for ( int j = 0; j < m_BadPixelMap[ i ].num_entries; j++ )
  3711. {
  3712. BadPixNum = *m_pBadPixNum++;
  3713. BadNum = BadPixNum & OFFSET_MASK;
  3714. if ( BadNum > 1 && BadNum < m_nWidth - 1 )
  3715. pImage[ i * m_nWidth + BadNum ] = ( m_TempImage[ ( i - 2 ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i - 2 ) * m_nWidth + BadNum ] + m_TempImage[ ( i - 2 ) * m_nWidth + BadNum + 2 ] +
  3716. m_TempImage[ ( i ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i ) * m_nWidth + BadNum + 2 ] +
  3717. m_TempImage[ ( i + 2 ) * m_nWidth + BadNum - 2 ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum ] + m_TempImage[ ( i + 2 ) * m_nWidth + BadNum + 2 ] ) / 8;
  3718. }
  3719. }
  3720. }
  3721. return 1;
  3722. }