xref: /utopia/UTPA2-700.0.x/modules/dlc/hal/mooney/dlc/MsDlc.c (revision 53ee8cc121a030b8d368113ac3e966b4705770ef)
1 //<MStar Software>
2 //******************************************************************************
3 // MStar Software
4 // Copyright (c) 2010 - 2012 MStar Semiconductor, Inc. All rights reserved.
5 // All software, firmware and related documentation herein ("MStar Software") are
6 // intellectual property of MStar Semiconductor, Inc. ("MStar") and protected by
7 // law, including, but not limited to, copyright law and international treaties.
8 // Any use, modification, reproduction, retransmission, or republication of all
9 // or part of MStar Software is expressly prohibited, unless prior written
10 // permission has been granted by MStar.
11 //
12 // By accessing, browsing and/or using MStar Software, you acknowledge that you
13 // have read, understood, and agree, to be bound by below terms ("Terms") and to
14 // comply with all applicable laws and regulations:
15 //
16 // 1. MStar shall retain any and all right, ownership and interest to MStar
17 //    Software and any modification/derivatives thereof.
18 //    No right, ownership, or interest to MStar Software and any
19 //    modification/derivatives thereof is transferred to you under Terms.
20 //
21 // 2. You understand that MStar Software might include, incorporate or be
22 //    supplied together with third party`s software and the use of MStar
23 //    Software may require additional licenses from third parties.
24 //    Therefore, you hereby agree it is your sole responsibility to separately
25 //    obtain any and all third party right and license necessary for your use of
26 //    such third party`s software.
27 //
28 // 3. MStar Software and any modification/derivatives thereof shall be deemed as
29 //    MStar`s confidential information and you agree to keep MStar`s
30 //    confidential information in strictest confidence and not disclose to any
31 //    third party.
32 //
33 // 4. MStar Software is provided on an "AS IS" basis without warranties of any
34 //    kind. Any warranties are hereby expressly disclaimed by MStar, including
35 //    without limitation, any warranties of merchantability, non-infringement of
36 //    intellectual property rights, fitness for a particular purpose, error free
37 //    and in conformity with any international standard.  You agree to waive any
38 //    claim against MStar for any loss, damage, cost or expense that you may
39 //    incur related to your use of MStar Software.
40 //    In no event shall MStar be liable for any direct, indirect, incidental or
41 //    consequential damages, including without limitation, lost of profit or
42 //    revenues, lost or damage of data, and unauthorized system use.
43 //    You agree that this Section 4 shall still apply without being affected
44 //    even if MStar Software has been modified by MStar in accordance with your
45 //    request or instruction for your use, except otherwise agreed by both
46 //    parties in writing.
47 //
48 // 5. If requested, MStar may from time to time provide technical supports or
49 //    services in relation with MStar Software to you for your use of
50 //    MStar Software in conjunction with your or your customer`s product
51 //    ("Services").
52 //    You understand and agree that, except otherwise agreed by both parties in
53 //    writing, Services are provided on an "AS IS" basis and the warranty
54 //    disclaimer set forth in Section 4 above shall apply.
55 //
56 // 6. Nothing contained herein shall be construed as by implication, estoppels
57 //    or otherwise:
58 //    (a) conferring any license or right to use MStar name, trademark, service
59 //        mark, symbol or any other identification;
60 //    (b) obligating MStar or any of its affiliates to furnish any person,
61 //        including without limitation, you and your customers, any assistance
62 //        of any kind whatsoever, or any information; or
63 //    (c) conferring any license or right under any intellectual property right.
64 //
65 // 7. These terms shall be governed by and construed in accordance with the laws
66 //    of Taiwan, R.O.C., excluding its conflict of law rules.
67 //    Any and all dispute arising out hereof or related hereto shall be finally
68 //    settled by arbitration referred to the Chinese Arbitration Association,
69 //    Taipei in accordance with the ROC Arbitration Law and the Arbitration
70 //    Rules of the Association by three (3) arbitrators appointed in accordance
71 //    with the said Rules.
72 //    The place of arbitration shall be in Taipei, Taiwan and the language shall
73 //    be English.
74 //    The arbitration award shall be final and binding to both parties.
75 //
76 //******************************************************************************
77 //<MStar Software>
78 /******************************************************************************
79  Copyright (c) 2008 MStar Semiconductor, Inc.
80  All rights reserved.
81 
82  [Module Name]: MsDLC.c
83  [Date]: 2009-03-20
84  [Comment]:
85    MST DLC subroutines.
86  [Reversion History]:
87 *******************************************************************************/
88 
89 #define _MSDLC_C_
90 
91 #include "MsDlc_LIB.h"
92 #include "MsDlc.h"
93 
94 #ifdef MSOS_TYPE_LINUX
95     #include <sys/ioctl.h>
96     #include <unistd.h>
97     #include <fcntl.h> // O_RDWR
98     #include "../../../graphic/drv/gop/mdrv_gflip_io.h"
99 #endif
100 
101 // Compiler option
102 #define DLC_LIB_VERSION             0x3300 //v32.61 // fix bugs of u8_L_H_U & u8_L_H_D
103                                            //v32.59 // 1. Add guard condition to msDlcOnOff & msDlc_CGC_Init. msDlcOnOff & msDlc_CGC_Init will check DLC On/Off status before execution.
104                                            //v32.58 // 1. change register data width from WORD -> DWORD 2. Fixs float point issue.
105                                            //v32.57 // fix bug imgae flick while curve change, fix bug for g_DlcParameters.ucDlcAvgDeltaStill & g_DlcParameters.ucDlcAvgDelta
106                                            //v32.56 // fix bug for g_DlcParameters.ucDlcAvgDeltaStill
107                                            //v32.55 // change the resolution of Yavg for g_DlcParameters.ucDlcAvgDeltaStill
108                                            //v32.54 // add C Gain (CGC) On Off
109                                            //v32.53 // modify msDlc_AP_ReinitWithoutPara() for debug command
110                                            //v32.52 // Include DebugTool Command + move g_u8Yth to CGC parameter groups
111                                            //v32.51 // Modify CGC initial flow
112                                            //v32.50 // Fix CGC Limit definition
113                                            //v32.49 // add Cgain control by Jason.Chen, add protect when AvgMax < Avg Min for pure image
114                                            //v32.48 // reserved for Paulo/Lomeo version number.
115                                            //v32.47 // fix Yavg threshold problem
116                                            //v32.46 // rename parameters, add Yavg threshold, g_DlcParameters.ucDlcYAvgThresholdH & g_DlcParameters.ucDlcYAvgThresholdL
117                                            //v32.45 // fix level[14] over flow issue & unstable when LH2(g_DlcParameters.u8_L_H_D) is high
118                                            //v32.44 // finetune v32.43
119                                            //v32.43 // (test) add new parameter for stop curve cgange when Avg Delta <= g_DlcParameters.ucDlcAvgDeltaStill
120                                            //v32.42 // remove Delay1ms() and replace to a Macro msDlc_Delay1ms
121                                            //v32.41 // change usage of timeout event, use Delay1ms(n);
122                                            //v32.40 // add new parameter for WLE/BLE g_DlcParameters.ucDlcBLEPoint, g_DlcParameters.ucDlcWLEPoint, add new parameter for Avg offset (Compal), add timeout in initial stage
123                                            //v32.39 // modify for get stable pixel count "g_wLumiTotalCount"
124                                            //v32.38 // fix re-init error in Histogram request bit (bit2)
125                                            //v32.37 // seperate code msDlcLumiDiffCtrl()
126                                            //v32.36 // add more overflow/underflow protection
127                                            //v32.35 // to fix the bug => luma curve (0) is almost  "0x00" for all kind of pattern.
128                                            //v32.34 // add compiler option ENABLE_HISTOGRAM_8 and function msDlcSetVarHistogramRange()
129                                            //v32.33 // add histogram range of H
130                                            //v32.32 // add registers initial table
131                                            //v32.31 // add some definition & add function enter and exit
132                                            //v32.30 // modify for ATV/DTV are compatible
133                                            //v32.23 // change definition of g_wAvgDelta =>  Delta = n * 3 , add function msGetAverageLuminous()
134                                            //v32.22 // add g_DlcParameters.ucDlcFastAlphaBlending, update g_wAvgDelta
135                                            //v32.21 // add g_wAvgDelta
136                                            //v32.20 // add level limit
137                                            //v32.19 // modify de-flick function, add parameter for pure image "g_DlcParameters.ucDlcPureImageMode", remove msGetHistogramHandler from DLC handler, finetune the process of pure image
138                                            //v32.18 // modify de-flick function (g_ucUpdateCnt & g_ucDlcFlickAlpha)
139                                            //v32.17 // fix bug of de-flick function
140                                            //v32.16 // modify de-flick function
141                                            //v32.15 // modify alpha belnding & add g_DlcParameters.ucDlcFlickAlphaStart after mode change
142                                            //v32.14 // Remove garbage (L_L_U...)
143                                            //v32.13 // Protect Histogram Overflow
144                                            //v32.12 // modify for tool finetune
145                                            //v32.11 // modify init static curve & for tool finetune
146                                            //v32.10 // Lopez & Raphael series 32-seg DLC & version 10
147 
148 #define DLC_CGC_LIB_VERSION         0x0104 //v1.04 // add C Gain (CGC) On Off
149                                            //v1.03 // move g_u8Yth to CGC parameter groups
150                                            //v1.02 // Modify CGC initial flow
151                                            //v1.01 // Fix CGC Limit definition
152                                            //v1.00 // Initial
153 
154 
155 XDATA StuDlc_FinetuneParamaters g_DlcParameters;
156 
157 #define DLC_DEBUG(x)        //(x)
158 
159 #define DEFAULT_BLE_UPPER_BOND      0x08
160 #define DEFAULT_BLE_LOWER_BOND      0x04
161 
162 #define DLC_DEFLICK_PRECISION_SHIFT   64UL
163 #define DLC_DEFLICK_BLEND_FACTOR      32UL
164 
165 #define DLC_PURE_IMAGE_DLC_CURVE        0L
166 #define DLC_PURE_IMAGE_LINEAR_CURVE     1L
167 #define DLC_PURE_IMAGE_DO_NOTHING       2L
168 
169 #define XC_DLC_ALGORITHM_OLD       0
170 #define XC_DLC_ALGORITHM_NEW       1
171 #define XC_DLC_ALGORITHM_KERNEL    2
172 
173 #define XC_DLC_SET_DLC_CURVE_BOTH_SAME    0   // 0:Main and Sub use the same DLC curve
174 
175 #ifdef MSOS_TYPE_LINUX
176 XDATA MS_S32   s32FdDlcBle= -1 ;
177 #endif
178 
179 XDATA WORD g_wLumaHistogram32H[32];
180 XDATA BYTE g_ucHistogramMax, g_ucHistogramMin;
181 XDATA WORD g_wLumiAverageTemp;
182 XDATA BYTE g_ucHistogramTotal_H;
183 XDATA BYTE g_ucHistogramTotal_L;
184 XDATA BYTE g_ucDlcFlickAlpha;
185 XDATA BYTE g_ucTable[16];     // Final target curve (8 bit)
186 XDATA BYTE g_pre_ucTable[16]; // Final target curve (8 bit)
187 XDATA WORD g_uwTable[16];     // Final target curve (10 bit)
188 XDATA BYTE g_ucCurveYAvg[16];
189 XDATA DWORD g_uwCurveHistogram[16];
190 XDATA WORD g_uwPre_CurveHistogram[16];
191 XDATA WORD g_uwPreTable[16]; // New de-Flick
192 XDATA BYTE g_ucUpdateCnt;
193 XDATA WORD g_wLumiTotalCount;
194 XDATA BYTE g_ucTmpAvgN_1; // Avg value (N-1)
195 XDATA BYTE g_ucTmpAvgN; // Avg value (current N)
196 XDATA WORD g_wTmpAvgN_1_x10; // Avg value (N-1) x 10
197 XDATA WORD g_wTmpAvgN_x10; // Avg value (current N) x 10
198 XDATA BYTE g_ucDlcFastLoop; // for pulse ripple
199 
200 #define FLICK_ALPHA_START_COUNTER   40
201 
202 XDATA DWORD g_dwFactory;
203 XDATA DWORD g_dwFactory_7;
204 XDATA DWORD g_dwFactory_9;
205 XDATA DWORD g_dwLstep;
206 XDATA DWORD g_dwHstep;
207 
208 XDATA BYTE s_Slope[8] = {0x17,0x27,0x47,0x67,0x97,0xB7,0xD7,0xE7};
209 
210 static MS_BOOL  g_bSetDlcBleOn = TRUE;
211 static WORD g_wPIAlpha=0;
212 static WORD g_wPrePIAlpha=0;
213 
214 #define LA1 g_DlcParameters.u8_L_L_U
215 #define LA2 g_DlcParameters.u8_L_L_D
216 #define LB1 g_DlcParameters.u8_L_L_U
217 #define LB2 g_DlcParameters.u8_L_L_D
218 #define LC1 g_DlcParameters.u8_L_L_U
219 #define LC2 g_DlcParameters.u8_L_L_D
220 #define LD1 g_DlcParameters.u8_L_L_U
221 #define LD2 g_DlcParameters.u8_L_L_D
222 #define LE1 g_DlcParameters.u8_L_H_U
223 #define LE2 g_DlcParameters.u8_L_H_D
224 #define LF1 g_DlcParameters.u8_L_H_U
225 #define LF2 g_DlcParameters.u8_L_H_D
226 #define LG1 g_DlcParameters.u8_L_H_U
227 #define LG2 g_DlcParameters.u8_L_H_D
228 #define LH1 g_DlcParameters.u8_L_H_U
229 #define LH2 g_DlcParameters.u8_L_H_D
230 
231 #define SA1 g_DlcParameters.u8_S_L_U
232 #define SA2 g_DlcParameters.u8_S_L_D
233 #define SB1 g_DlcParameters.u8_S_L_U
234 #define SB2 g_DlcParameters.u8_S_L_D
235 #define SC1 g_DlcParameters.u8_S_L_U
236 #define SC2 g_DlcParameters.u8_S_L_D
237 #define SD1 g_DlcParameters.u8_S_L_U
238 #define SD2 g_DlcParameters.u8_S_L_D
239 #define SE1 g_DlcParameters.u8_S_H_U
240 #define SE2 g_DlcParameters.u8_S_H_D
241 #define SF1 g_DlcParameters.u8_S_H_U
242 #define SF2 g_DlcParameters.u8_S_H_D
243 #define SG1 g_DlcParameters.u8_S_H_U
244 #define SG2 g_DlcParameters.u8_S_H_D
245 #define SH1 g_DlcParameters.u8_S_H_U
246 #define SH2 g_DlcParameters.u8_S_H_D
247 
248 typedef enum
249 {
250     Slope_1,
251     Slope_2,
252     Slope_4,
253     Slope_6,
254     Slope_9,
255     Slope_11,
256     Slope_13,
257     Slope_14,
258     Slope_Max
259 } Slop_Type;
260 
261 #define Lstep           g_dwLstep
262 #define Hstep           g_dwHstep
263 
264 //=========================================================
265 
266 #define TOTAL_PIXEL     ((DWORD)(wVEnd-wVStart)*(wHEnd-wHStart)*7*4/(160*3))  // ((DWORD)(wVEnd-wVStart)*0.04375*4/3*(wHEnd-wHStart))
267 
268 void msWriteDLCRegsTbl(DlcRegUnitType code *pTable);
269 void msDlcWriteCurve(BOOL bWindow);
270 void msDlcWriteCurveLSB(BOOL bWindow, BYTE ucIndex, BYTE ucValue);
271 void msDlcLumiDiffCtrl(void);
272 void msDlcSetVarHistogramRange(BYTE *pRangeArray);
273 
274 /******************************************************************************/
275 /*                           CGC                                         */
276 /******************************************************************************/
277 // Constant
278 #define Yvar_Th       4                         // Change threshold for DLC luma curve
279 #define Timer_CountU   3                   // Increasing Chroma Gain change speed
280 #define Timer_CountD   1                   // Decreasing Chroma Gain change speed
281 #define CGain_StepU     1                   // Increasing Chroma Gain step
282 #define CGain_StepD     1                   // Decreasing Chroma Gain step
283 
284 #define Y01_weight    5                      // Luma Curve Y[0], Y[1] weighting
285 #define Y23_weight    3                      // Luma Curve Y[2], Y[3] weighting
286 #define Y45_weight    1                      // Luma Curve Y[4], Y[5] weighting
287 #define Y67_weight    1                      // Luma Curve Y[6], Y[7] weighting
288 
289 // Local
290 XDATA BYTE g_u8Chroma_OriGain = 0x40;
291 XDATA BYTE g_u8Chroma_target;
292 XDATA WORD g_u16Ydark, g_u16Ydark_pre;
293 XDATA BYTE g_u8TCount = 0;
294 XDATA BYTE g_u8CGC_UpperBond, g_u8CGC_LowerBond;
295 XDATA BOOL g_bSetDLCCurveBoth = 0;//We will set main/sub DLC curve at the same time in 3D side-side ap.
296 
297 /******************************************************************************/
298 
299 //////////////////////////////////////////////////////////////
300 //
301 //////////////////////////////////////////////////////////////
msDlcInitWithCurve(WORD wHStart,WORD wHEnd,WORD wVStart,WORD wVEnd)302 void msDlcInitWithCurve( WORD wHStart, WORD wHEnd, WORD wVStart, WORD wVEnd )
303 {
304     BYTE ucTmp, ucTimeout;
305 
306     msDlc_FunctionEnter();
307 
308     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
309     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
310         g_ucDlcFastLoop = 255;
311 
312     g_ucHistogramTotal_L = 0;
313     g_ucHistogramTotal_H = 0;
314 
315     //putstr("\r\nDLC Initilize use HK MCU");
316 
317     g_dwFactory = TOTAL_PIXEL/32/2;
318     g_dwFactory_7 = (g_dwFactory - (g_dwFactory/8));
319     g_dwFactory_9 = (g_dwFactory + (g_dwFactory/8));
320 
321     g_dwLstep = g_dwFactory*1/16*12/10;
322     g_dwHstep = g_dwFactory*3/8;
323 
324     // Registers Init
325     msWriteDLCRegsTbl(tDLC_Initialize);
326 
327     // Set histogram method/dither
328     if (msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT2)
329     {
330         ucTimeout = 60;
331         while(!(msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT3) && (ucTimeout--))
332         {
333             msDlc_Delay1ms(1);
334         }
335     }
336 
337     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE)|_BIT5|_BIT4 );
338 
339     // Set histogram range - H
340     if (REG_ADDR_HISTOGRAM_RANGE_M_HST != REG_NULL)
341     {
342         // Main Window
343         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HST, wHStart/8);
344         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, wHEnd/8);
345         // MWE/Sub Window
346         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_HST, wHStart/8);
347         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_HEN, wHEnd/8);
348     }
349     // Set histogram range - V
350     // Main Window
351     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VST, wVStart/8);
352     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VEN, wVEnd/8);
353     // MWE/Sub Window
354     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_VST, wVStart/8);
355     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_VEN, wVEnd/8);
356 
357     msWriteByte(REG_ADDR_BLE_UPPER_BOND, DEFAULT_BLE_UPPER_BOND);
358     msWriteByte(REG_ADDR_BLE_LOWER_BOND, DEFAULT_BLE_LOWER_BOND);
359 
360     // Enable range for Histogram
361     msWriteByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE, _BIT7|msReadByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE) ); // Enable MAIN_WINDOW histogram
362 
363     // Init Luma Curve
364     //putstr("[DLC]: Disable the Burst Write 16 Luma Curve\r\n");
365     for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
366     {
367         g_ucTable[ucTmp] = g_DlcParameters.ucLumaCurve[ucTmp];
368         g_uwPreTable[ucTmp] = (WORD)g_DlcParameters.ucLumaCurve[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT; // New de-Flick
369 	    msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
370         msDlcWriteCurveLSB(MAIN_WINDOW, ucTmp, 0);
371         msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
372         msDlcWriteCurveLSB(SUB_WINDOW, ucTmp, 0);
373     }
374     // init DLC curve index N0 & 16
375     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_ucTable[0]);
376     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
377     msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, 0);
378     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-g_ucTable[15]);
379     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
380     msDlcWriteCurveLSB(MAIN_WINDOW, 16, 0);
381     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_ucTable[0]);
382     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
383     msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, 0);
384     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-g_ucTable[15]);
385     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
386     msDlcWriteCurveLSB(SUB_WINDOW, 16, 0);
387 
388     // Get Total Pixel Count
389     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF0);
390     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 | _BIT1);
391 
392     g_wLumiTotalCount = msReadByte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_H);
393     g_wLumiTotalCount = (g_wLumiTotalCount << 8) + (WORD)msReadByte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L);
394 
395     g_DlcParameters.ucDlcTimeOut = 0;
396     g_DlcParameters.ucDlcFlickAlphaStart = FLICK_ALPHA_START_COUNTER;
397     g_DlcParameters.ucDlcSlowEvent = 0;
398     g_ucUpdateCnt = 0;
399 
400     msDlc_FunctionExit();
401 }
402 
msDlc_SetCaptureRange(MS_U16 wHStart,MS_U16 wHEnd,MS_U16 wVStart,MS_U16 wVEnd)403 void msDlc_SetCaptureRange(MS_U16 wHStart,MS_U16 wHEnd, MS_U16 wVStart, MS_U16 wVEnd)
404 {
405     msDlc_FunctionEnter();
406 
407     if (REG_ADDR_HISTOGRAM_RANGE_M_HST != REG_NULL)
408     {
409         if(msReadByte(REG_ADDR_VIP_HISTOGRAM_4K2K_MODE) & BIT(2)||((wHEnd-wHStart)>=3000))
410         {
411             // Set histogram range - H
412             // Main Window
413             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HST, wHStart/8/2);
414             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, wHEnd/8/2);
415 
416             // Virtical size need to change by display
417             // Set histogram range - V
418             // Main Window
419             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VST, wVStart/8/2);
420             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VEN, wVEnd/8/2);
421         }
422         else
423         {
424             // Set histogram range - H
425             // Main Window
426             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HST, wHStart/8);
427             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_HEN, wHEnd/8);
428 
429             // Virtical size need to change by display
430             // Set histogram range - V
431             // Main Window
432             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VST, wVStart/8);
433             msWriteByte(REG_ADDR_HISTOGRAM_RANGE_M_VEN, wVEnd/8);
434         }
435 
436         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
437         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
438     }
439     msDlc_FunctionExit();
440 }
441 
msDlc_SetCaptureRangeSub(MS_U16 wHStart,MS_U16 wHEnd,MS_U16 wVStart,MS_U16 wVEnd)442 void msDlc_SetCaptureRangeSub(MS_U16 wHStart,MS_U16 wHEnd, MS_U16 wVStart, MS_U16 wVEnd)
443 {
444     msDlc_FunctionEnter();
445 
446     if (REG_ADDR_HISTOGRAM_RANGE_S_HST != REG_NULL)
447     {
448         // Set histogram range - H
449         // Sub Window
450         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_HST, wHStart/8);
451         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_HEN, wHEnd/8);
452 
453         // Virtical size need to change by display
454         // Set histogram range - V
455         // Sub Window
456         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_VST, wVStart/8);
457         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_S_VEN, wVEnd/8);
458 
459         // Enable SUB_WINDOW histogram
460         msWriteByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE, _BIT3|msReadByte(REG_ADDR_HISTOGRAM_RANGE_ENABLE) );
461 
462         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
463         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
464     }
465     msDlc_FunctionExit();
466 }
467 
msDlcEnableSetDLCCurveBoth(BOOL bEnable)468 void msDlcEnableSetDLCCurveBoth(BOOL bEnable)
469 {
470     g_bSetDLCCurveBoth = bEnable;
471 }
472 
473 //////////////////////////////////////////////////////////////
474 //
475 //////////////////////////////////////////////////////////////
msDlcOnOff(BOOL bSwitch,BOOL bWindow)476 void msDlcOnOff(BOOL bSwitch, BOOL bWindow)
477 {
478     BYTE ucCtlBit;
479 
480     msDlc_FunctionEnter();
481 
482     if( MAIN_WINDOW == bWindow )
483         ucCtlBit = _BIT7;
484     else // Sub window
485         ucCtlBit = _BIT6;
486 
487     if( bSwitch ) // Enable
488     {
489         if((msReadByte(REG_ADDR_DLC_HANDSHAKE) & ucCtlBit) )// Shall not turn DLC on when it is on already.
490         {
491            msDlc_FunctionExit();
492            return;
493         }
494 
495         msWriteBit(REG_ADDR_DLC_HANDSHAKE, TRUE, ucCtlBit);
496 
497         // Request HW to do histogram
498         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
499 
500         msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
501 
502         g_DlcParameters.ucDlcFlickAlphaStart = FLICK_ALPHA_START_COUNTER;
503         g_DlcParameters.ucDlcSlowEvent = 0;
504     }
505     else // Disable
506     {
507         msWriteBit(REG_ADDR_DLC_HANDSHAKE, FALSE, ucCtlBit);
508     }
509 
510     msDlc_FunctionExit();
511 }
512 
513 //////////////////////////////////////////////////////////////
514 //
515 //////////////////////////////////////////////////////////////
msDlcSetBleOnOff(BOOL bSwitch)516 void msDlcSetBleOnOff(BOOL bSwitch)
517 {
518     g_bSetDlcBleOn= bSwitch ;
519 }
520 
521 //////////////////////////////////////////////////////////////
522 //
523 //////////////////////////////////////////////////////////////
msIsBlackVideoEnable(MS_BOOL bWindow)524 MS_BOOL msIsBlackVideoEnable( MS_BOOL bWindow )
525 {
526     MS_BOOL bReturn;
527 
528     msVop_FunctionEnter();
529 
530     if( MAIN_WINDOW == bWindow)
531     {
532         if(msReadByte(REG_ADDR_VOP_SCREEN_CONTROL) & BIT(1))
533         {
534             bReturn = TRUE;
535         }
536         else
537         {
538             bReturn = FALSE;
539         }
540     }
541     else
542     {
543         if(msReadByte(REG_ADDR_VOP_SCREEN_CONTROL) & BIT(5))
544         {
545             bReturn = TRUE;
546         }
547         else
548         {
549             bReturn = FALSE;
550         }
551     }
552 
553     msVop_FunctionExit();
554     return bReturn;
555 
556 }
557 
558 //////////////////////////////////////////////////////////////
559 //
560 //////////////////////////////////////////////////////////////
msGetAverageLuminous(void)561 BYTE msGetAverageLuminous(void) // = external msGetAverageLuminousValue()
562 {
563     WORD uwTmpAvr;
564 
565     // Normalize
566     if (g_wLumiTotalCount!=0)
567         uwTmpAvr = (256 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
568     else
569         uwTmpAvr = 0;
570 
571     if (uwTmpAvr >= 255)
572         uwTmpAvr = 255;
573 
574     return (BYTE)uwTmpAvr;
575 }
576 
577 //////////////////////////////////////////////////////////////
578 //
579 //////////////////////////////////////////////////////////////
msGetAverageLuminous_x4(void)580 WORD msGetAverageLuminous_x4(void) // = external msGetAverageLuminousValue()
581 {
582     WORD uwTmpAvr;
583 
584     // Normalize
585     if (g_wLumiTotalCount!=0)
586         uwTmpAvr = (1024 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
587     else
588         uwTmpAvr = 0;
589 
590     if (uwTmpAvr >= 1024- 1)
591         uwTmpAvr = 1024- 1;
592 
593     return uwTmpAvr;
594 }
595 
596 //////////////////////////////////////////////////////////////
597 //
598 //////////////////////////////////////////////////////////////
msGetAverageLuminous_x10(void)599 WORD msGetAverageLuminous_x10(void)
600 {
601     WORD uwTmpAvr;
602 
603     // Normalize
604     if (g_wLumiTotalCount!=0)
605         uwTmpAvr = (256 * 10 * (DWORD)g_wLumiAverageTemp + (g_wLumiTotalCount/2)) / g_wLumiTotalCount;
606     else
607         uwTmpAvr = 0;
608 
609     if (uwTmpAvr >= (256 * 10 - 1))
610         uwTmpAvr = (256 * 10 - 1);
611 
612     return uwTmpAvr;
613 }
614 
615 //////////////////////////////////////////////////////////////
616 //
617 //////////////////////////////////////////////////////////////
msGetAverageLuminousSub(void)618 BYTE msGetAverageLuminousSub(void)
619 {
620 
621     WORD uwTmpAvr=0, uwLumiTotalCount=0, uwLumiAverageTemp=0 ;
622 
623     msDlc_FunctionEnter();
624 
625     uwLumiTotalCount = msReadByte(REG_ADDR_HISTOGRAM_SUB_TOTAL_COUNT_H);
626     uwLumiTotalCount = (uwLumiTotalCount << 8) + (WORD)msReadByte(REG_ADDR_HISTOGRAM_SUB_TOTAL_COUNT_L);
627 
628     uwLumiAverageTemp = msReadByte(REG_ADDR_HISTOGRAM_SUB_TOTAL_SUM_H);
629     uwLumiAverageTemp = (uwLumiAverageTemp << 8) + (WORD)msReadByte(REG_ADDR_HISTOGRAM_SUB_TOTAL_SUM_L);
630 
631     msDlc_FunctionExit();
632 
633     // Normalize
634     if (uwLumiTotalCount!=0)
635         uwTmpAvr = (256 * (DWORD)uwLumiAverageTemp + (uwLumiTotalCount/2)) / uwLumiTotalCount;
636     else
637         uwTmpAvr = 0;
638 
639     if (uwTmpAvr >= 255)
640         uwTmpAvr = 255;
641 
642     return (BYTE)uwTmpAvr;
643 }
644 
645 //////////////////////////////////////////////////////////////
646 //
647 //////////////////////////////////////////////////////////////
msGetHistogramHandler(BOOL bWindow)648 BOOL msGetHistogramHandler(BOOL bWindow)
649 {
650     BYTE ucTmp;
651     WORD wTmpHistogramSum; // for protect histogram overflow
652     BYTE ucHistogramOverflowIndex;
653     BYTE ucReturn;
654 
655     msDlc_FunctionEnter();
656 
657 #if 0 //Have double buffer, does not need to wait.
658     // ENABLE_FAST_HISTOGRAM_CATCH
659     if( !(msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT3) )  // Wait until ready
660     {
661         msDlc_FunctionExit();
662         return FALSE;
663     }
664 #endif
665 
666     if (g_DlcParameters.ucDlcTimeOut)
667     {
668         // Get histogram 32 data
669         // Total/BW_ref histogram count
670         // putstr("[DLC]: Disable the Burst Read 32-section Histogram\r\n");
671         //dwTotalCount = 0;
672         for (ucTmp = 0, wTmpHistogramSum = 0, ucHistogramOverflowIndex = 0xFF; ucTmp < 32; ucTmp++)
673         {
674             g_wLumaHistogram32H[ucTmp] = msReadByte(REG_ADDR_HISTOGRAM_DATA_32+(2*ucTmp)+1);
675             g_wLumaHistogram32H[ucTmp] <<= 8;
676             g_wLumaHistogram32H[ucTmp] |= msReadByte(REG_ADDR_HISTOGRAM_DATA_32+(2*ucTmp));
677             //printf(" %d",g_wLumaHistogram32H[ucTmp]);
678             //dwTotalCount += g_wLumaHistogram32H[ucTmp];
679 
680              // protect histogram overflow
681             if (g_wLumaHistogram32H[ucTmp] >= 65535)
682             {
683                 ucHistogramOverflowIndex = ucTmp;
684                 //printf("\r\n overflow %d",ucHistogramOverflowIndex);
685             }
686             g_wLumaHistogram32H[ucTmp] >>= 1; // protect histogram overflow
687             wTmpHistogramSum += g_wLumaHistogram32H[ucTmp];
688         }
689 
690         //Get the histogram report sum of pixel number .
691         g_wLumiTotalCount = msReadByte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_H);
692         g_wLumiTotalCount = (g_wLumiTotalCount << 8) + (WORD)msReadByte(REG_ADDR_HISTOGRAM_TOTAL_COUNT_L);
693 
694         // protect histogram overflow
695         if (ucHistogramOverflowIndex != 0xFF)
696         {
697             if (wTmpHistogramSum > g_wLumiTotalCount)
698                 wTmpHistogramSum = g_wLumiTotalCount;
699             g_wLumaHistogram32H[ucHistogramOverflowIndex] += (g_wLumiTotalCount - wTmpHistogramSum);
700         }
701 
702         g_ucHistogramTotal_L = msReadByte( REG_ADDR_HISTOGRAM_TOTAL_SUM_L );
703         g_ucHistogramTotal_H = msReadByte( REG_ADDR_HISTOGRAM_TOTAL_SUM_H );
704 
705         // Get MIN. & MAX VALUE
706         if( MAIN_WINDOW == bWindow )   // Main window
707         {
708             g_ucHistogramMin = msReadByte(REG_ADDR_MAIN_MIN_VALUE);
709             g_ucHistogramMax = msReadByte(REG_ADDR_MAIN_MAX_VALUE);
710         }
711         else // Sub window
712         {
713             g_ucHistogramMin = msReadByte(REG_ADDR_SUB_MIN_VALUE);
714             g_ucHistogramMax = msReadByte(REG_ADDR_SUB_MAX_VALUE);
715         }
716 
717         g_ucTmpAvgN_1 = msGetAverageLuminous();
718         g_wTmpAvgN_1_x10 = msGetAverageLuminous_x10();
719 
720         g_wLumiAverageTemp = ((WORD)g_ucHistogramTotal_H<<8) + g_ucHistogramTotal_L;
721         g_ucTmpAvgN = msGetAverageLuminous();
722         g_wTmpAvgN_x10 = msGetAverageLuminous_x10();
723 
724         ucReturn = TRUE;
725     }
726     else
727     {
728         ucReturn = FALSE;
729         //putstr("\r\n!!!!! DLC Time Out !!!!!");
730     }
731 
732     // After read histogram, request HW to do histogram
733     // Request HW to do histogram
734     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) & 0xF2);
735 
736     g_DlcParameters.ucDlcTimeOut = 150; // 150ms
737 
738     msWriteByte(REG_ADDR_DLC_HANDSHAKE, msReadByte(REG_ADDR_DLC_HANDSHAKE) | _BIT2 );
739 
740     msDlc_FunctionExit();
741 
742     return ucReturn;
743 }
744 
745 //////////////////////////////////////////////////////////////
746 //
747 //////////////////////////////////////////////////////////////
msDlcSetVarHistogramRange(BYTE * pRangeArray)748 void msDlcSetVarHistogramRange(BYTE *pRangeArray)
749 {
750     BYTE ucTmp;
751     DWORD wReg;
752 
753     wReg = REG_ADDR_HISTOGRAM_8_RANGE_START;
754     for (ucTmp=0; ucTmp<7 ; ucTmp++)
755     {
756         msWriteByte(wReg, *(pRangeArray + ucTmp));
757         wReg += 1;
758     }
759 }
760 
761 //=========================================================
762 //
763 //=========================================================
msDlcLumiDiffCtrl(void)764 void msDlcLumiDiffCtrl(void)
765 {
766     if (g_DlcParameters.ucDlcFlickAlphaStart)
767     {
768         g_DlcParameters.ucDlcFlickAlphaStart--;
769     }
770 
771     if (g_DlcParameters.ucDlcSlowEvent)
772     {
773         g_DlcParameters.ucDlcSlowEvent--;
774         g_ucDlcFlickAlpha = 1; // it mean the curve change slowly
775         g_ucDlcFastLoop = 0;
776     }
777     else
778     {
779         if (g_wTmpAvgN_1_x10 >= g_wTmpAvgN_x10)
780         {
781             if ((g_ucDlcFastLoop > 0) || ((g_wTmpAvgN_1_x10 - g_wTmpAvgN_x10 ) >= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)))
782             {
783                 //printf("\r\n1H:Avg = %d",g_ucTmpAvgN_1 - g_ucTmpAvgN);
784                 //printf(" g_ucDlcFlickAlpha = %d\r\n",g_ucDlcFlickAlpha);
785 
786                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR) // it mean the curve change quickly
787                 if (g_ucDlcFastLoop == 0)
788                 {
789                     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
790                     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
791                     {
792                         g_ucDlcFastLoop = 255;
793                     }
794                 }
795             }
796             else if ((g_DlcParameters.ucDlcAvgDeltaStill <= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)) && (g_DlcParameters.ucDlcAvgDeltaStill != 0) && ((g_wTmpAvgN_1_x10 - g_wTmpAvgN_x10) <= g_DlcParameters.ucDlcAvgDeltaStill)) // if (Yavg is less than g_DlcParameters.ucDlcAvgDeltaStill) & (Fast change is finish) does NOT do curve change
797             {
798                 g_ucDlcFlickAlpha = 0; // (0/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve will be still
799             }
800             else // if ((g_ucTmpAvgN_1 - g_ucTmpAvgN) < g_DlcParameters.ucDlcAvgDelta) // if (Yavg is less than g_DlcParameters.ucDlcAvgDelta) & (Fast change is finish) may do curve change slowly
801             {
802                 if ( g_DlcParameters.ucDlcFlickAlphaStart == 0) // chagne source and it's stable
803                 {
804                     g_ucDlcFlickAlpha = 1; // (1/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change slowly
805                 }
806                 else
807                 {
808                     g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR) // it mean the curve change quickly
809                 }
810             }
811         }
812         else if (g_wTmpAvgN_1_x10 < g_wTmpAvgN_x10)
813         {
814              if ((g_ucDlcFastLoop > 0) || (( g_wTmpAvgN_x10 - g_wTmpAvgN_1_x10 ) >= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)))
815             {
816                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change quickly
817                 if (g_ucDlcFastLoop == 0)
818                 {
819                     g_ucDlcFastLoop = (g_DlcParameters.ucDlcFastAlphaBlending/2) * 16;
820                     if (g_DlcParameters.ucDlcFastAlphaBlending == 32) // 32 / 2 * 16 = 0 overflow
821                     {
822                         g_ucDlcFastLoop = 255;
823                     }
824                 }
825             }
826             else if ((g_DlcParameters.ucDlcAvgDeltaStill <= ((WORD)g_DlcParameters.ucDlcAvgDelta * 10)) && (g_DlcParameters.ucDlcAvgDeltaStill != 0) && ((g_wTmpAvgN_x10 - g_wTmpAvgN_1_x10) <= g_DlcParameters.ucDlcAvgDeltaStill)) // if (Yavg is less than g_DlcParameters.ucDlcAvgDeltaStill) & (Fast change is finish) does NOT do curve change
827             {
828                 g_ucDlcFlickAlpha = 0; // (0/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve will be still
829             }
830             else // if ((g_ucTmpAvgN - g_ucTmpAvgN_1) < g_DlcParameters.ucDlcAvgDelta) // if (Yavg is less than g_DlcParameters.ucDlcAvgDelta) & (Fast change is finish) may do curve change slowly
831             {
832                 if (g_DlcParameters.ucDlcFlickAlphaStart == 0) // chagne source and it's stable
833                 {
834                     g_ucDlcFlickAlpha = 1; // (1/32) = (1/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change slowly
835                 }
836                 else
837                 {
838                     g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // (32/32) = (32/DLC_DEFLICK_BLEND_FACTOR)  // it mean the curve change quickly
839                 }
840             }
841         }
842     }
843 }
844 
845 //=========================================================
846 //
847 //=========================================================
msDlcHandler(BOOL bWindow)848 void msDlcHandler(BOOL bWindow)
849 {
850     BYTE ucTmp,Tmp,sepPoint =0,sepPoint0 =0,sepPoint1 =0,sepPoint2 =0,ucYAvgNormalize=0,belnging_factor;
851     WORD ucYAvg=0;
852     WORD uwHistogramMax, uwHistogramMax2nd,uwHistogramSlopRatioL, uwHistogramSlopRatioH; // 20110406 Ranma add
853     DWORD uwHistogramAreaSum[32];//,uwPre_Histogram_ShiftR[32],uwPre_Histogram_ShiftL[32]; // 20110406 Ranma add
854     WORD uwHistogramForBleSum=0;
855     WORD uwHistogram_BLE_AreaSum[32];
856     WORD BLE_StartPoint=0,BLE_EndPoint=0,BLE_StartPoint_Correction=0,BLE_EndPoint_Correction=0;
857     WORD BLE_Slop_400,BLE_Slop_480,BLE_Slop_500,BLE_Slop_600,BLE_Slop_800,Dark_BLE_Slop_Min;
858 
859     static WORD Pre_BLE_Slop=0x4000;
860     static WORD Pre_g_ucDlcFlickAlpha = 0,Pre_TotalYSum = 0,Pre_BLE_StartPoint_Correction=0xFFF;
861     static BYTE ucPre_YAvg =0x00;
862     static BOOL bPreSubWindowEnable=FALSE;
863     static BYTE ucSubWindowChange=0 ;
864     static WORD uwSubChangeDelayCount=0;
865     BOOL bSubWindowEnable = FALSE;
866     WORD uwBLE_Blend=0,uwBLE_StartPoint_Blend=0,CurveDiff=0,CurveDiff_Coring=0,CurveDiff_base=0,YAvg_base=0;
867     BYTE g_ucDlcFlickAlpha_Max=0,g_ucDlcFlickAlpha_Min=0,YAvg_TH_H,YAvg_TH_L,g_ucDlcFlickAlpha_temp,ucDlcFlickAlpha_Diff=0;
868     DWORD dwSumBelowAvg=0, dwSumAboveAvg=0,dwHistogramDlcSlop[17],BLE_Slop=0x400,BLE_Slop_tmp=0x400;
869     WORD uwArrayComp, uwArraySum, Delta_YAvg=0,coeff0,coeff1,coeff2;
870     DWORD His_ratio_BelowAvg,His_ratio_AboveAvg;
871     DWORD g_uwCurveHistogram0[16],g_uwCurveHistogram1[16],g_uwCurveHistogram2[16];
872     DWORD BLE_sum=0;
873     WORD Pre_YAvg_base,Diff_L,Diff_M,Diff_H,Pre_CurveDiff_base;
874 
875     //When DLC algorithm is from kernel, force return.
876     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_KERNEL)
877     {
878         return ;
879     }
880 
881     msDlc_FunctionEnter();
882 
883     msDlcLumiDiffCtrl();
884 
885     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_OLD)
886     {
887         // If difference is too small, don't do dlc
888         if( ((g_ucHistogramMax-g_ucHistogramMin) < 0x0A) || (g_ucHistogramMin >= g_ucHistogramMax) )
889         {
890             if (g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_DO_NOTHING)
891             {
892                 msDlc_FunctionExit();
893                 return;
894             }
895             else if (g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_LINEAR_CURVE)
896             {
897                 BYTE ucLuma = 0x07;
898                 for( ucTmp = 0; ucTmp < 16; ++ ucTmp )
899                 {
900                     g_ucTable[ucTmp] = ucLuma;
901                     ucLuma += 0x10;
902                 }
903 
904                 g_ucDlcFlickAlpha = g_DlcParameters.ucDlcFastAlphaBlending; // it mean the curve change quickly
905 
906                 // Write data to luma curve ...
907                 msDlcWriteCurve(bWindow);
908                 if(g_bSetDLCCurveBoth)
909                 {
910                     msDlcWriteCurve(SUB_WINDOW);
911                 }
912                 msDlc_FunctionExit();
913                 return;
914             }
915             else // g_DlcParameters.ucDlcPureImageMode == DLC_PURE_IMAGE_DLC_CURVE
916                 ;
917         }
918 
919         //=========================================================
920         // LC[6] - H0 ~ H12
921         //=========================================================
922         uwArraySum=0;
923         for(ucTmp=0;ucTmp<13;ucTmp++)
924             uwArraySum+= g_wLumaHistogram32H[ucTmp];
925         uwArrayComp = uwArraySum / 13;
926 
927         if (uwArrayComp < g_dwFactory_7)
928         {
929             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SD2 / (Lstep*128))+1;
930             if (ucTmp > LD2)
931                 ucTmp = LD2;
932             g_ucTable[6] = 0x67 - ucTmp;
933         }
934         else if (uwArrayComp > g_dwFactory_9)
935         {
936             ucTmp = ((uwArrayComp - g_dwFactory_9) * SD1 / (Hstep*128))+1;
937             if (ucTmp > LD1)
938                 ucTmp = LD1;
939             g_ucTable[6] = 0x67 + ucTmp;
940         }
941         else
942             g_ucTable[6] = 0x67;
943 
944         if ((g_ucTable[6]> s_Slope[Slope_6])&&((g_ucTable[6] - s_Slope[Slope_6])>=2))
945             g_ucTable[6] = g_ucTable[6] - 1;
946         else if ((s_Slope[Slope_6]> g_ucTable[6])&&((s_Slope[Slope_6] - g_ucTable[6])>=2))
947             g_ucTable[6] = g_ucTable[6] + 1;
948         else
949             g_ucTable[6] = s_Slope[Slope_6];
950 
951         //=========================================================
952         // LC[4] - H0 ~ H8
953         //=========================================================
954 
955         g_ucTable[4] = ((g_ucTable[6] - 0x07) * 2 / 3) + 0x07;
956 
957         uwArraySum = uwArraySum - (g_wLumaHistogram32H[12] + g_wLumaHistogram32H[11] + g_wLumaHistogram32H[10] + g_wLumaHistogram32H[9]);
958         uwArrayComp = uwArraySum/9;
959 
960         if (uwArrayComp < g_dwFactory_7)
961         {
962             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SC2 / (Lstep*128))+1;
963             if (ucTmp > LC2)
964                 ucTmp = LC2;
965             g_ucTable[4] = g_ucTable[4] - ucTmp;
966         }
967         else if (uwArrayComp > g_dwFactory_9)
968         {
969             ucTmp = ((uwArrayComp - g_dwFactory_9) * SC1 / (Hstep*128))+1;
970             if (ucTmp > LC1)
971                 ucTmp = LC1;
972             g_ucTable[4] = g_ucTable[4] + ucTmp;
973         }
974 
975         if ((g_ucTable[4]> s_Slope[Slope_4])&&((g_ucTable[4]- s_Slope[Slope_4])>=2))
976             g_ucTable[4] = g_ucTable[4] - 1;
977         else if ((s_Slope[Slope_4]> g_ucTable[4])&&((s_Slope[Slope_4]- g_ucTable[4])>=2))
978             g_ucTable[4] = g_ucTable[4] + 1;
979         else
980             g_ucTable[4] = s_Slope[Slope_4];
981 
982         //=========================================================
983         // LC[2] - H0 ~ H4
984         //=========================================================
985 
986         g_ucTable[2] = ((g_ucTable[4] - 0x07) * 1 / 2) + 0x07;
987         uwArraySum = uwArraySum - (g_wLumaHistogram32H[8] + g_wLumaHistogram32H[7] + g_wLumaHistogram32H[6] + g_wLumaHistogram32H[5]);
988         uwArrayComp = uwArraySum / 5;
989 
990         if (uwArrayComp < g_dwFactory_7)
991         {
992             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SB2 / (Lstep*128))+1;
993             if (ucTmp > LB2)
994                 ucTmp = LB2;
995             g_ucTable[2] = g_ucTable[2] - ucTmp;
996         }
997         else if (uwArrayComp > g_dwFactory_9)
998         {
999             ucTmp = ((uwArrayComp - g_dwFactory_9) * SB1 / (Hstep*128))+1;
1000             if (ucTmp > LB1)
1001                 ucTmp = LB1;
1002             g_ucTable[2] = g_ucTable[2] + ucTmp;
1003         }
1004 
1005         if ((g_ucTable[2]> s_Slope[Slope_2])&&((g_ucTable[2]- s_Slope[Slope_2])>=2))
1006             g_ucTable[2] = g_ucTable[2] - 1;
1007         else if ((s_Slope[Slope_2]> g_ucTable[2])&&((s_Slope[Slope_2]- g_ucTable[2])>=2))
1008             g_ucTable[2] = g_ucTable[2] + 1;
1009         else
1010             g_ucTable[2] = s_Slope[Slope_2];
1011 
1012         //=========================================================
1013         // LC[1] - H0 ~ H2
1014         //=========================================================
1015 
1016         g_ucTable[1] = ((g_ucTable[2] - 0x07) * 1 / 2) + 0x07;
1017         uwArraySum = uwArraySum - (g_wLumaHistogram32H[4] + g_wLumaHistogram32H[3]);
1018         uwArrayComp = uwArraySum / 3;
1019 
1020         if (uwArrayComp < g_dwFactory_7)
1021         {
1022             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SA2 / (Lstep*128))+1;
1023             if (ucTmp > LA2)
1024                 ucTmp = LA2;
1025 
1026             if (g_ucTable[1] <= ucTmp)
1027                 g_ucTable[1] = 0x00;
1028             else
1029                 g_ucTable[1] = g_ucTable[1] - ucTmp;
1030         }
1031         else if (uwArrayComp > g_dwFactory_9)
1032         {
1033             ucTmp = ((uwArrayComp - g_dwFactory_9) * SA1 / (Hstep*128))+1;
1034             if (ucTmp > LA1)
1035                 ucTmp = LA1;
1036             g_ucTable[1] = g_ucTable[1] + ucTmp;
1037         }
1038 
1039         if ((g_ucTable[1]> s_Slope[Slope_1])&&((g_ucTable[1]- s_Slope[Slope_1])>=1))
1040             g_ucTable[1] = g_ucTable[1] - 1;
1041         else if ((s_Slope[Slope_1]> g_ucTable[1])&&((s_Slope[Slope_1]- g_ucTable[1])>=2))
1042             g_ucTable[1] = g_ucTable[1] + 1;
1043         else
1044             g_ucTable[1] = s_Slope[Slope_1];
1045 
1046         //=========================================================
1047         // LC[9] - H19 ~ H31
1048         //=========================================================
1049 
1050         uwArraySum=0;
1051         for (ucTmp=19; ucTmp<32; ucTmp++)
1052             uwArraySum+= g_wLumaHistogram32H[ucTmp];
1053 
1054         uwArrayComp = uwArraySum / 13;
1055 
1056         if (uwArrayComp < g_dwFactory_7)
1057         {
1058             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SE1 / (Lstep*128))+1;
1059             if (ucTmp > LE1)
1060                 ucTmp = LE1;
1061             g_ucTable[9] = 0x97 + ucTmp;
1062         }
1063         else if (uwArrayComp > g_dwFactory_9)
1064         {
1065             ucTmp = ((uwArrayComp - g_dwFactory_9) * SE2 / (Hstep*128))+1;
1066             if (ucTmp > LE2)
1067                 ucTmp = LE2;
1068             g_ucTable[9] = 0x97 - ucTmp;
1069         }
1070         else
1071             g_ucTable[9] = 0x97;
1072 
1073         //=========================================================
1074         // LC[11] - H23 ~ H31
1075         //=========================================================
1076 
1077         g_ucTable[11] = 0xF7 - ((0xF7 - g_ucTable[9]) * 2 / 3);
1078         uwArraySum = uwArraySum - (g_wLumaHistogram32H[19] + g_wLumaHistogram32H[20] + g_wLumaHistogram32H[21] + g_wLumaHistogram32H[22]);
1079         uwArrayComp = uwArraySum / 9;
1080 
1081         if (uwArrayComp< g_dwFactory_7)
1082         {
1083             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SF1 / (Lstep*128))+1;
1084             if (ucTmp > LF1)
1085                 ucTmp = LF1;
1086             g_ucTable[11] = g_ucTable[11] + ucTmp;
1087         }
1088         else if (uwArrayComp > g_dwFactory_9)
1089         {
1090             ucTmp = ((uwArrayComp - g_dwFactory_9) * SF2 / (Hstep*128))+1;
1091             if (ucTmp > LF2)
1092                 ucTmp = LF2;
1093             g_ucTable[11] = g_ucTable[11] - ucTmp;
1094         }
1095 
1096         //=========================================================
1097         // LC[13] - H27 ~ H31
1098         //=========================================================
1099 
1100         g_ucTable[13] = 0xF7 - ((0xF7 - g_ucTable[11]) * 1 / 2);
1101         uwArraySum = uwArraySum - (g_wLumaHistogram32H[23] + g_wLumaHistogram32H[24] + g_wLumaHistogram32H[25] + g_wLumaHistogram32H[26]);
1102         uwArrayComp = uwArraySum / 5;
1103 
1104         if (uwArrayComp< g_dwFactory_7)
1105         {
1106             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SG1 / (Lstep*128))+1;
1107             if (ucTmp > LG1)
1108                 ucTmp = LG1;
1109 
1110             if ((0xFF - g_ucTable[13]) <= ucTmp)
1111                 g_ucTable[13] = 0xFF;
1112             else
1113                 g_ucTable[13] = g_ucTable[13] + ucTmp;
1114         }
1115         else if (uwArrayComp > g_dwFactory_9)
1116         {
1117             ucTmp = ((uwArrayComp - g_dwFactory_9) * SG2 / (Hstep*128))+1;
1118             if (ucTmp > LG2)
1119                 ucTmp = LG2;
1120             g_ucTable[13] = g_ucTable[13] - ucTmp;
1121         }
1122 
1123         //=========================================================
1124         // LC[14] - H29 ~ H31
1125         //=========================================================
1126 
1127         g_ucTable[14] = 0xF7 - ((0xF7 - g_ucTable[13]) * 1 / 2);
1128         uwArraySum = uwArraySum - (g_wLumaHistogram32H[27] + g_wLumaHistogram32H[28]);
1129         uwArrayComp = uwArraySum / 3;
1130 
1131         if (uwArrayComp < g_dwFactory_7)
1132         {
1133             ucTmp = ((g_dwFactory_7 - uwArrayComp) * SH1 / (Lstep*128))+1;
1134             if (ucTmp > LH1)
1135             ucTmp = LH1;
1136 
1137             if ((0xFF - g_ucTable[14]) <= ucTmp)
1138                 g_ucTable[14] = 0xFF;
1139             else
1140                 g_ucTable[14] = g_ucTable[14] + ucTmp;
1141         }
1142         else if (uwArrayComp > g_dwFactory_9)
1143         {
1144             ucTmp = ((uwArrayComp - g_dwFactory_9) * SH2 / (Hstep*128))+1;
1145             if (ucTmp > LH2)
1146             ucTmp = LH2;
1147             g_ucTable[14] =g_ucTable[14] - ucTmp;
1148         }
1149 
1150         s_Slope[Slope_1] = g_ucTable[1];
1151         s_Slope[Slope_2] = g_ucTable[2];
1152         s_Slope[Slope_4] = g_ucTable[4];
1153         s_Slope[Slope_6] = g_ucTable[6];
1154 
1155         g_ucTable[3] = g_ucTable[2] + ((g_ucTable[4] - g_ucTable[2]) * 1 / 2);
1156         g_ucTable[5] = g_ucTable[4] + ((g_ucTable[6] - g_ucTable[4]) * 1 / 2);
1157         g_ucTable[7] = g_ucTable[6] + ((g_ucTable[9] - g_ucTable[6]) * 1 / 3);
1158         g_ucTable[8] = g_ucTable[6] + ((g_ucTable[9] - g_ucTable[6]) * 2 / 3);
1159         g_ucTable[10] = g_ucTable[9] + ((g_ucTable[11] - g_ucTable[9]) * 1 / 2);
1160         g_ucTable[12] = g_ucTable[11] + ((g_ucTable[13] - g_ucTable[11]) * 1 / 2);
1161         g_ucTable[0] = g_ucTable[1]/3; // Joseph 20081014
1162         g_ucTable[15] = 0xFF - ((0xFF - g_ucTable[14]) / 3);
1163         g_ucTable[15]= ((g_ucTable[15]) / 2) + (0xf7 / 2);
1164 
1165         // Get Average Value
1166         ucYAvg = msGetAverageLuminous();
1167 
1168         //printf("\r\n ucYAvg = %d",ucYAvg);
1169         if (ucYAvg >= g_DlcParameters.ucDlcYAvgThresholdH)
1170         {
1171             for (ucTmp=0; ucTmp<16; ucTmp++)
1172             {
1173                 g_DlcParameters.ucLumaCurve2[ucTmp] = g_DlcParameters.ucLumaCurve2_a[ucTmp];
1174             }
1175         }
1176         else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1177         {
1178             for (ucTmp=0; ucTmp<16; ucTmp++)
1179             {
1180                 g_DlcParameters.ucLumaCurve2[ucTmp] = g_DlcParameters.ucLumaCurve2_b[ucTmp];
1181             }
1182         }
1183         else // (ucYAvg > g_DlcParameters.ucDlcYAvgThresholdL) && (ucYAvg < g_DlcParameters.ucDlcYAvgThresholdH)
1184         {
1185             WORD wYavgW;
1186 
1187             wYavgW = ((WORD)(ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL) * 128) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdL);
1188 
1189             for (ucTmp=0; ucTmp<16; ucTmp++)
1190             {
1191                 g_DlcParameters.ucLumaCurve2[ucTmp] = ((WORD)(wYavgW * g_DlcParameters.ucLumaCurve2_a[ucTmp]) + (WORD)((128 - wYavgW) * g_DlcParameters.ucLumaCurve2_b[ucTmp])) / 128;
1192             }
1193         }
1194 
1195         if (((WORD)g_ucTable[0] + g_DlcParameters.ucLumaCurve[0] + g_DlcParameters.ucLumaCurve2[0]) >= (0x07 + 0x07))
1196             g_ucTable[0] = g_ucTable[0]+(g_DlcParameters.ucLumaCurve[0]-0x07)+(g_DlcParameters.ucLumaCurve2[0]-0x07);
1197         else
1198             g_ucTable[0] = 0;
1199 
1200         g_ucTable[1] = g_ucTable[1]+(g_DlcParameters.ucLumaCurve[1]-0x17)+(g_DlcParameters.ucLumaCurve2[1]-0x17);
1201         g_ucTable[2] = g_ucTable[2]+(g_DlcParameters.ucLumaCurve[2]-0x27)+(g_DlcParameters.ucLumaCurve2[2]-0x27);
1202         g_ucTable[3] = g_ucTable[3]+(g_DlcParameters.ucLumaCurve[3]-0x37)+(g_DlcParameters.ucLumaCurve2[3]-0x37);
1203         g_ucTable[4] = g_ucTable[4]+(g_DlcParameters.ucLumaCurve[4]-0x47)+(g_DlcParameters.ucLumaCurve2[4]-0x47);
1204         g_ucTable[5] = g_ucTable[5]+(g_DlcParameters.ucLumaCurve[5]-0x57)+(g_DlcParameters.ucLumaCurve2[5]-0x57);
1205         g_ucTable[6] = g_ucTable[6]+(g_DlcParameters.ucLumaCurve[6]-0x67)+(g_DlcParameters.ucLumaCurve2[6]-0x67);
1206         g_ucTable[7] = g_ucTable[7]+(g_DlcParameters.ucLumaCurve[7]-0x77)+(g_DlcParameters.ucLumaCurve2[7]-0x77);
1207         g_ucTable[8] = g_ucTable[8]+(g_DlcParameters.ucLumaCurve[8]-0x87)+(g_DlcParameters.ucLumaCurve2[8]-0x87);
1208         g_ucTable[9] = g_ucTable[9]+(g_DlcParameters.ucLumaCurve[9]-0x97)+(g_DlcParameters.ucLumaCurve2[9]-0x97);
1209         g_ucTable[10] = g_ucTable[10]+(g_DlcParameters.ucLumaCurve[10]-0xa7)+(g_DlcParameters.ucLumaCurve2[10]-0xa7);
1210         g_ucTable[11] = g_ucTable[11]+(g_DlcParameters.ucLumaCurve[11]-0xb7)+(g_DlcParameters.ucLumaCurve2[11]-0xb7);
1211         g_ucTable[12] = g_ucTable[12]+(g_DlcParameters.ucLumaCurve[12]-0xc7)+(g_DlcParameters.ucLumaCurve2[12]-0xc7);
1212         g_ucTable[13] = g_ucTable[13]+(g_DlcParameters.ucLumaCurve[13]-0xd7)+(g_DlcParameters.ucLumaCurve2[13]-0xd7);
1213         g_ucTable[14] = g_ucTable[14]+(g_DlcParameters.ucLumaCurve[14]-0xe7)+(g_DlcParameters.ucLumaCurve2[14]-0xe7);
1214         if (((WORD)g_ucTable[15] + g_DlcParameters.ucLumaCurve[15] + g_DlcParameters.ucLumaCurve2[15] - 0xF7 - 0xF7) < 0xFF)
1215             g_ucTable[15] = g_ucTable[15]+(g_DlcParameters.ucLumaCurve[15]-0xf7)+(g_DlcParameters.ucLumaCurve2[15]-0xf7);
1216         else
1217             g_ucTable[15] = 0xFF;
1218 
1219         // Check Over Flow & Under Flow
1220         if (g_ucTable[15] < 0xD0)
1221             g_ucTable[15] = 0xFF;
1222         if (g_ucTable[14] < 0x90)
1223             g_ucTable[14] = 0xFF;
1224         if (g_ucTable[13] < 0x70)
1225             g_ucTable[13] = 0xFF;
1226         if (g_ucTable[12] < 0x60)
1227             g_ucTable[12] = 0xFF;
1228         if (g_ucTable[11] < 0x50)
1229             g_ucTable[11] = 0xFF;
1230         if (g_ucTable[2] > 0x90)
1231             g_ucTable[2] = 0x0;
1232         if (g_ucTable[1] > 0x60)
1233             g_ucTable[1] = 0x0;
1234         if (g_ucTable[0] > 0x30)
1235             g_ucTable[0] = 0x0;
1236     }
1237     else  //New DLC  Algorithm
1238     {
1239 
1240 #if 0 //For customer require. Run DLC algorithm when static field .
1241         if((g_wLumiAverageTemp-Pre_TotalYSum)==0)
1242         {
1243             return;
1244         }
1245         else
1246 #endif
1247         {
1248             ucYAvg = msGetAverageLuminous();
1249 
1250             for (ucTmp=0; ucTmp<32; ucTmp++)
1251             {
1252                 if(ucTmp==0)
1253                 {
1254                     uwHistogramAreaSum[ucTmp]=g_wLumaHistogram32H[ucTmp];
1255                 }
1256                 else
1257                 {
1258                     uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp-1] + g_wLumaHistogram32H[ucTmp];
1259                 }
1260             }
1261 
1262             //Mantis issue of 0232938:Hsense A3 DLC uwHistogramAreaSum[31]=0 will code dump, the divisor can't to be equal to zero .
1263             if(uwHistogramAreaSum[31]==0)
1264             {
1265                 DLC_DEBUG(printf("\n Error in [New DLC Algorithm][ %s  , %d ]  uwHistogramAreaSum[31]=0 !!!\n", __FUNCTION__,__LINE__));
1266                 return;
1267             }
1268 
1269 
1270             // ~~~~~~~~~~~~~~~~~~~~~~~~Pure Image Detect  ~~~~~~~~~~~~~~~~~~~~~~~~
1271             if( ((g_ucHistogramMax-g_ucHistogramMin) <= 10) || (g_ucHistogramMin >= g_ucHistogramMax) )
1272             {
1273                 g_wPIAlpha = 256;
1274             }
1275             else if ((g_ucHistogramMax-g_ucHistogramMin) >= 26)
1276             {
1277                 g_wPIAlpha = 0;
1278             }
1279             else
1280             {
1281                 g_wPIAlpha = ((26-(g_ucHistogramMax-g_ucHistogramMin))*256+8)/16;
1282             }
1283 
1284             if(g_wPIAlpha <= 0)
1285             {
1286                 g_wPIAlpha = 0;
1287             }
1288             else if (g_wPIAlpha >= 256)
1289             {
1290                 g_wPIAlpha = 256;
1291             }
1292             else
1293             {
1294                 g_wPIAlpha = g_wPIAlpha;
1295             }
1296             // ~~~~~~~~~~~~~~~~~~~~~~~~Pure Image Detect End   ~~~~~~~~~~~~~~~~~~~~~~~~
1297 
1298             if(ucYAvg >= g_DlcParameters.ucDlcYAvgThresholdH)
1299             {
1300                 ucYAvgNormalize=g_DlcParameters.ucDlcSepPointL;
1301             }
1302             else if(ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1303             {
1304                 ucYAvgNormalize=g_DlcParameters.ucDlcSepPointH;
1305             }
1306             else
1307             {
1308                 ucYAvgNormalize=((ucYAvg-g_DlcParameters.ucDlcYAvgThresholdL)*g_DlcParameters.ucDlcSepPointL
1309                                 +(g_DlcParameters.ucDlcYAvgThresholdH-ucYAvg)*g_DlcParameters.ucDlcSepPointH)
1310                                 /(g_DlcParameters.ucDlcYAvgThresholdH-g_DlcParameters.ucDlcYAvgThresholdL);
1311             }
1312 
1313             for (ucTmp=0; ucTmp<31; ucTmp++)
1314             {
1315                 if(((ucTmp+1)*8)<ucYAvgNormalize)
1316                 {
1317                     dwSumBelowAvg = uwHistogramAreaSum[ucTmp]
1318                                    +((ucYAvgNormalize-(ucTmp+1)*8)*g_wLumaHistogram32H[(ucTmp+1)]+4)/8;
1319                 }
1320             }
1321 
1322             dwSumAboveAvg = uwHistogramAreaSum[31] - dwSumBelowAvg ;
1323 
1324             His_ratio_BelowAvg = (4096*dwSumBelowAvg + (uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
1325             His_ratio_AboveAvg = (4096*dwSumAboveAvg + (uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
1326 
1327             if(His_ratio_BelowAvg > 128)
1328             {
1329                 His_ratio_BelowAvg = 128;
1330             }
1331             if(His_ratio_AboveAvg > 128)
1332             {
1333                 His_ratio_AboveAvg = 128;
1334             }
1335 
1336             for (ucTmp=0; ucTmp<32; ucTmp++)
1337             {
1338                 uwHistogram_BLE_AreaSum[ucTmp]=((1024*uwHistogramAreaSum[ucTmp])+uwHistogramAreaSum[31]/2)/uwHistogramAreaSum[31];
1339 
1340                 if(uwHistogram_BLE_AreaSum[ucTmp]>1024)
1341                 {
1342                     uwHistogram_BLE_AreaSum[ucTmp]=1024;
1343                 }
1344             }
1345             //uwHistogram_BLE_AreaSum[ucTmp] = histogram Add to normalize 1024
1346 
1347             if ((dwSumBelowAvg == 0)&&(dwSumAboveAvg != 0))
1348             {
1349                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1350                 {
1351                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1352                     {
1353                          uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1354                     }
1355                     else
1356                     {
1357                         uwHistogramAreaSum[ucTmp] = ucYAvgNormalize*4 + ((DWORD)uwHistogramAreaSum[ucTmp]*(1024-ucYAvgNormalize*4)+(dwSumAboveAvg/2))/dwSumAboveAvg;
1358                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1359                         {
1360                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_AboveAvg)/128);
1361                         }
1362                         else
1363                         {
1364                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_AboveAvg)/128);
1365                         }
1366                     }
1367                 }
1368             }
1369             else if ((dwSumBelowAvg != 0)&&(dwSumAboveAvg == 0))
1370             {
1371                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1372                 {
1373                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1374                     {
1375                         uwHistogramAreaSum[ucTmp] = ((DWORD)uwHistogramAreaSum[ucTmp]*ucYAvgNormalize*4+(dwSumBelowAvg/2))/dwSumBelowAvg;
1376                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1377                         {
1378                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_BelowAvg)/128);
1379                         }
1380                         else
1381                         {
1382                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_BelowAvg)/128);
1383                         }
1384                     }
1385                     else
1386                     {
1387                          uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1388                     }
1389                 }
1390             }
1391             else if((dwSumBelowAvg == 0)&&(dwSumAboveAvg == 0))
1392             {
1393                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1394                 {
1395                     uwHistogramAreaSum[ucTmp] = (ucTmp+1)*8*4;
1396                 }
1397             }
1398             else
1399             {
1400                 for (ucTmp = 0; ucTmp < 32; ucTmp++)
1401                 {
1402                     if (ucYAvgNormalize >= (((WORD)ucTmp+1)*8))
1403                     {
1404                          uwHistogramAreaSum[ucTmp] = ((DWORD)uwHistogramAreaSum[ucTmp]*ucYAvgNormalize*4+(dwSumBelowAvg/2))/dwSumBelowAvg;
1405                          if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1406                          {
1407                              uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_BelowAvg)/128);
1408                          }
1409                          else
1410                          {
1411                              uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_BelowAvg)/128);
1412                          }
1413                     }
1414                     else
1415                     {
1416                         uwHistogramAreaSum[ucTmp] = ucYAvgNormalize*4 + ((uwHistogramAreaSum[ucTmp]-dwSumBelowAvg)*(1024-ucYAvgNormalize*4)+(dwSumAboveAvg/2))/dwSumAboveAvg;
1417 
1418                         if(uwHistogramAreaSum[ucTmp] >= ((ucTmp+1)*32))
1419                         {
1420                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) + (((uwHistogramAreaSum[ucTmp] - ((ucTmp+1)*32))*His_ratio_AboveAvg)/128);
1421                         }
1422                         else
1423                         {
1424                             uwHistogramAreaSum[ucTmp] = ((ucTmp+1)*32) - (((((ucTmp+1)*32) - uwHistogramAreaSum[ucTmp])*His_ratio_AboveAvg)/128);
1425                         }
1426                     }
1427                 }
1428             }
1429 
1430             //down sampling to 16 numbers (store to uwHistogramAreaSum[0~15])
1431 
1432             for (ucTmp=0; ucTmp<16; ucTmp++)
1433             {
1434                 uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp*2]; //
1435             }
1436 
1437             //histogram_DLC limit by slope
1438             uwHistogramMax = 0;
1439 
1440             for (ucTmp=0; ucTmp<=16; ucTmp++)
1441             {
1442                 if (ucTmp == 0)
1443                 {
1444                     dwHistogramDlcSlop[ucTmp] = ((uwHistogramAreaSum[ucTmp] - 0) * 256 + 16) / 64;
1445 
1446                     if (dwHistogramDlcSlop[ucTmp] < 257)
1447                     {
1448                         dwHistogramDlcSlop[ucTmp] = 257;
1449                     }
1450                 }
1451                 else if (ucTmp == 16)
1452                 {
1453                     dwHistogramDlcSlop[ucTmp] = ((1024 - uwHistogramAreaSum[ucTmp-1]) * 256 + 16) / 64;
1454 
1455                     if (dwHistogramDlcSlop[ucTmp] < 257)
1456                     {
1457                         dwHistogramDlcSlop[ucTmp] = 257;
1458                     }
1459                 }
1460                 else
1461                 {
1462                     dwHistogramDlcSlop[ucTmp] = ((uwHistogramAreaSum[ucTmp] - uwHistogramAreaSum[ucTmp-1]) * 256 + 32) / 64; //find slop
1463 
1464                     if (dwHistogramDlcSlop[ucTmp] < 257)
1465                     {
1466                         dwHistogramDlcSlop[ucTmp] = 257; //(only take the slop > 1)
1467                     }
1468                 }
1469 
1470                 dwHistogramDlcSlop[ucTmp] = ((g_DlcParameters.ucDlcHistogramLimitCurve[ucTmp]*512)+((dwHistogramDlcSlop[ucTmp]-256)/2)) / (dwHistogramDlcSlop[ucTmp]-256) ; //SlopRatio 8bit 256=1x
1471 
1472                 if(dwHistogramDlcSlop[ucTmp] > 0xFFFF)
1473                 {
1474                     dwHistogramDlcSlop[ucTmp] = 0xFFFF;
1475                 }
1476             }
1477 
1478             sepPoint = (ucYAvgNormalize-8)/16;
1479             sepPoint0 = sepPoint-1;
1480             sepPoint1 = sepPoint;
1481             sepPoint2 = sepPoint+1;
1482 
1483             if(ucYAvgNormalize < (sepPoint2*16))
1484             {
1485                 //sepPoint0 = sepPoint;
1486                 //sepPoint1 = sepPoint+1;
1487                 //sepPoint2 = sepPoint+2;
1488                 coeff0 = (((3*((sepPoint2*16)-ucYAvgNormalize)*((sepPoint2*16)-ucYAvgNormalize))+64*((sepPoint2*16)-ucYAvgNormalize)+320));
1489                 coeff1 = ((1408-6*((sepPoint2*16)-ucYAvgNormalize)*((sepPoint2*16)-ucYAvgNormalize)));
1490                 coeff2 = 2048-coeff0-coeff1;
1491             }
1492             else
1493             {
1494                 //sepPoint0 = sepPoint-1;
1495                 //sepPoint1 = sepPoint;
1496                 //sepPoint2 = sepPoint+1;
1497                 coeff0 = (((3*(ucYAvgNormalize-(sepPoint2*16))*(ucYAvgNormalize-(sepPoint2*16)))-64*(ucYAvgNormalize-(sepPoint2*16))+320));
1498                 coeff1 = ((1408-6*(ucYAvgNormalize-(sepPoint2*16))*(ucYAvgNormalize-(sepPoint2*16))));
1499                 coeff2 = 2048-coeff0-coeff1;
1500             }
1501 
1502             //Calculate for sepPoint0~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1503             uwHistogramSlopRatioL = 256;
1504             uwHistogramSlopRatioH = 256;
1505 
1506             for (ucTmp = 0; ucTmp<= (sepPoint0+1); ucTmp++)
1507             {
1508                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1509 
1510                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1511                 {
1512                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1513                 }
1514             }
1515 
1516             for (ucTmp = sepPoint0; ucTmp<=16; ucTmp++)
1517             {
1518                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1519 
1520                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1521                 {
1522                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1523                 }
1524             }
1525 
1526             // calcute final target curve for sepPoint0
1527             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1528             {
1529                 if (sepPoint0 >= (WORD)ucTmp)
1530                 {
1531                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1532                     {
1533                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 ) ;
1534                     }
1535                     else
1536                     {
1537                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256);
1538                     }
1539                 }
1540                 else
1541                 {
1542                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1543                     {
1544                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 ) ;
1545                     }
1546                     else
1547                     {
1548                         g_uwCurveHistogram0[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256);
1549                     }
1550                 }
1551             }
1552 
1553             //Calculate for sepPoint1~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1554             uwHistogramSlopRatioL = 256;
1555             uwHistogramSlopRatioH = 256;
1556 
1557             for (ucTmp = 0; ucTmp<= (sepPoint1+1); ucTmp++)
1558             {
1559                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1560 
1561                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1562                 {
1563                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1564                 }
1565             }
1566 
1567             for (ucTmp = sepPoint1; ucTmp<=16; ucTmp++)
1568             {
1569                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1570 
1571                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1572                 {
1573                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1574                 }
1575             }
1576 
1577             // calcute final target curve for sepPoint1
1578             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1579             {
1580                 if (sepPoint1 >= (WORD)ucTmp)
1581                 {
1582                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1583                     {
1584                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 );
1585                     }
1586                     else
1587                     {
1588                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256 );
1589                     }
1590                 }
1591                 else
1592                 {
1593                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1594                     {
1595                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 );
1596                     }
1597                     else
1598                     {
1599                         g_uwCurveHistogram1[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256 );
1600                     }
1601                 }
1602             }
1603 
1604             //Calculate for sepPoint2~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1605             uwHistogramSlopRatioL = 256;
1606             uwHistogramSlopRatioH = 256;
1607 
1608             for (ucTmp = 0; ucTmp<= (sepPoint2+1); ucTmp++)
1609             {
1610                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1611 
1612                 if (uwHistogramMax2nd < uwHistogramSlopRatioL)
1613                 {
1614                     uwHistogramSlopRatioL = uwHistogramMax2nd;
1615                 }
1616             }
1617 
1618             for (ucTmp = sepPoint2; ucTmp<=16; ucTmp++)
1619             {
1620                 uwHistogramMax2nd = dwHistogramDlcSlop[ucTmp];
1621 
1622                 if (uwHistogramMax2nd < uwHistogramSlopRatioH)
1623                 {
1624                     uwHistogramSlopRatioH = uwHistogramMax2nd;
1625                 }
1626             }
1627 
1628             // calcute final target curve for sepPoint2
1629             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1630             {
1631                 if (sepPoint2 >= (WORD)ucTmp)
1632                 {
1633                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1634                     {
1635                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioL + 128) / 256 );
1636                     }
1637                     else
1638                     {
1639                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioL + 128) / 256 );
1640                     }
1641                 }
1642                 else
1643                 {
1644                     if(uwHistogramAreaSum[ucTmp]>(ucTmp * 64 + 32))
1645                     {
1646                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) + (((uwHistogramAreaSum[ucTmp] - (ucTmp * 64 + 32)) * uwHistogramSlopRatioH + 128) / 256 );
1647                     }
1648                     else
1649                     {
1650                         g_uwCurveHistogram2[ucTmp] = (ucTmp * 64 + 32) - ((((ucTmp * 64 + 32)- uwHistogramAreaSum[ucTmp]) * uwHistogramSlopRatioH + 128) / 256);
1651                     }
1652                 }
1653             }
1654 
1655             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1656             {
1657                 g_uwCurveHistogram[ucTmp] = (coeff0*g_uwCurveHistogram0[ucTmp]+coeff1*g_uwCurveHistogram1[ucTmp]+coeff2*g_uwCurveHistogram2[ucTmp]+1024)/2048;
1658             }
1659             ///////////// Histogram_DLC add Low_Pass_filter /////////////
1660 
1661             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1662             {
1663                 if (ucTmp == 0)
1664                 {
1665                     g_uwCurveHistogram[ucTmp] = (5*g_uwCurveHistogram[ucTmp] + g_uwCurveHistogram[ucTmp+1]+4)/8;
1666                 }
1667                 else if (ucTmp == 15)
1668                 {
1669                     g_uwCurveHistogram[ucTmp] = (g_uwCurveHistogram[ucTmp-1] + 5*g_uwCurveHistogram[ucTmp] + 2048 + 4)/8;
1670                 }
1671                 else
1672                 {
1673                     g_uwCurveHistogram[ucTmp] = (g_uwCurveHistogram[ucTmp-1] + 6*g_uwCurveHistogram[ucTmp] + g_uwCurveHistogram[ucTmp+1]+4)/8;
1674                 }
1675             }
1676 
1677 
1678 #if 0  //Ranma Histogram DLC low pass
1679             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1680             {
1681                 uwHistogramAreaSum[ucTmp+16] = g_uwCurveHistogram[ucTmp];
1682 
1683                 if (ucTmp == sepPoint0)
1684                 {
1685                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-1]+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]+2)/4;
1686                     uwHistogramAreaSum[ucTmp] = coeff0*uwHistogramAreaSum[ucTmp+16]+(2048-coeff0)*uwHistogramAreaSum[ucTmp];
1687                 }
1688                 if (ucTmp == sepPoint1)
1689                 {
1690                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-2]+g_uwCurveHistogram[ucTmp-1]*2+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]*2+g_uwCurveHistogram[ucTmp+2]+4)/8;
1691                     uwHistogramAreaSum[ucTmp] = coeff1*uwHistogramAreaSum[ucTmp+16]+(2048-coeff1)*uwHistogramAreaSum[ucTmp];
1692                 }
1693                 if (ucTmp == sepPoint2)
1694                 {
1695                     uwHistogramAreaSum[ucTmp+16] = (g_uwCurveHistogram[ucTmp-1]+g_uwCurveHistogram[ucTmp]*2+g_uwCurveHistogram[ucTmp+1]+2)/4;
1696                     uwHistogramAreaSum[ucTmp] = coeff2*uwHistogramAreaSum[ucTmp+16]+(2048-coeff2)*uwHistogramAreaSum[ucTmp];
1697                 }
1698             }
1699 
1700             LpfBlendAlpha_temp1 = abs(g_uwCurveHistogram[sepPoint0]-g_uwCurveHistogram[sepPoint1]*2+g_uwCurveHistogram[sepPoint2]);
1701             LpfBlendAlpha_temp2 = abs(g_uwCurveHistogram[sepPoint1]-g_uwCurveHistogram[sepPoint2]*2+g_uwCurveHistogram[sepPoint2+1]);
1702 
1703             if(LpfBlendAlpha_temp1 > LpfBlendAlpha_temp2)
1704             {
1705                 LpfBlendAlpha = LpfBlendAlpha_temp1;
1706             }
1707             else
1708             {
1709                 LpfBlendAlpha = LpfBlendAlpha_temp2;
1710             }
1711 
1712             if (LpfBlendAlpha >64)
1713             {
1714                 LpfBlendAlpha = 64;
1715             }
1716 
1717             for (ucTmp = 0; ucTmp < 16; ucTmp++)
1718             {
1719                 g_uwCurveHistogram[ucTmp] = (uwHistogramAreaSum[ucTmp+16]*LpfBlendAlpha + (64-LpfBlendAlpha)*g_uwCurveHistogram[ucTmp]+32)/64;
1720 
1721                 if(g_uwCurveHistogram[ucTmp]>1023)
1722                 {
1723                     g_uwCurveHistogram[ucTmp]=1023;
1724                 }
1725             }
1726 
1727 #endif
1728             // Calculate curve by Yavg
1729 
1730             //DLC_PRINTF("\r\n ucYAvg = %d",ucYAvg);
1731             if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdL)
1732             {
1733                 for (ucTmp=0; ucTmp<16; ucTmp++)
1734                 {
1735                     g_ucCurveYAvg[ucTmp] = g_DlcParameters.ucLumaCurve2_a[ucTmp];
1736                 }
1737             }
1738             else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdM)
1739             {
1740                 for (ucTmp=0; ucTmp<16; ucTmp++)
1741                 {
1742                     g_ucCurveYAvg[ucTmp] = (((WORD)g_DlcParameters.ucLumaCurve[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL)) / (g_DlcParameters.ucDlcYAvgThresholdM - g_DlcParameters.ucDlcYAvgThresholdL)) +
1743                                            (g_DlcParameters.ucLumaCurve2_a[ucTmp] - (((WORD)g_DlcParameters.ucLumaCurve2_a[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdL)) / ( g_DlcParameters.ucDlcYAvgThresholdM - g_DlcParameters.ucDlcYAvgThresholdL)));
1744                 }
1745             }
1746             else if (ucYAvg <= g_DlcParameters.ucDlcYAvgThresholdH)
1747             {
1748                 for (ucTmp=0; ucTmp<16; ucTmp++)
1749                 {
1750                     g_ucCurveYAvg[ucTmp] = ((WORD)g_DlcParameters.ucLumaCurve2_b[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdM) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM)) +
1751                                            (g_DlcParameters.ucLumaCurve[ucTmp] - ((WORD)g_DlcParameters.ucLumaCurve[ucTmp] * (ucYAvg - g_DlcParameters.ucDlcYAvgThresholdM) / (g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM)));
1752                 }
1753             }
1754             else // if (ucYAvg > g_DlcParameters.ucDlcYAvgThresholdH)
1755             {
1756                 for (ucTmp=0; ucTmp<16; ucTmp++)
1757                 {
1758                     g_ucCurveYAvg[ucTmp] = g_DlcParameters.ucLumaCurve2_b[ucTmp];
1759                 }
1760             }
1761 
1762             belnging_factor = g_DlcParameters.ucDlcCurveModeMixAlpha;
1763 
1764             // Decide Target Curve
1765             Diff_L =g_DlcParameters.ucDlcDiffBase_L;
1766             Diff_M =g_DlcParameters.ucDlcDiffBase_M;
1767             Diff_H =g_DlcParameters.ucDlcDiffBase_H;
1768 
1769             if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1770             {
1771                 Pre_YAvg_base = Diff_L*4;
1772             }
1773             else if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1774             {
1775                 if(Diff_M >= Diff_L)
1776                 {
1777                     Pre_YAvg_base = Diff_L*4 + (((Diff_M-Diff_L)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1778                 }
1779                 else
1780                 {
1781                     Pre_YAvg_base = Diff_L*4 - (((Diff_L-Diff_M)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1782                 }
1783             }
1784             else if (ucPre_YAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1785             {
1786                 if(Diff_H >= Diff_M)
1787                 {
1788                     Pre_YAvg_base = Diff_M*4 + (((Diff_H- Diff_M)*(ucPre_YAvg*4- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1789                 }
1790                 else
1791                 {
1792                     Pre_YAvg_base = Diff_M*4 - (((Diff_M-Diff_H)*(ucPre_YAvg*4-g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH - g_DlcParameters.ucDlcYAvgThresholdM));
1793                 }
1794             }
1795             else
1796             {
1797                 Pre_YAvg_base = Diff_H*4;
1798             }
1799 
1800             if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1801             {
1802                 YAvg_base = Diff_L*4;
1803             }
1804             else if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1805             {
1806                 if(Diff_M >= Diff_L)
1807                 {
1808                     YAvg_base = Diff_L*4 + (((Diff_M-Diff_L)*( ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1809                 }
1810                 else
1811                 {
1812                     YAvg_base = Diff_L*4 - (((Diff_L-Diff_M)*( ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1813                 }
1814             }
1815             else if (ucYAvg*4 <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1816             {
1817                 if(Diff_H >= Diff_M)
1818                 {
1819                     YAvg_base = Diff_M*4 + (((Diff_H- Diff_M)*( ucYAvg*4- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1820                 }
1821                 else
1822                 {
1823                     YAvg_base = Diff_M*4 - (((Diff_M-Diff_H)*(ucYAvg*4-g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH -g_DlcParameters.ucDlcYAvgThresholdM));
1824                 }
1825             }
1826             else
1827             {
1828                 YAvg_base = Diff_H*4;
1829             }
1830 
1831             YAvg_base = (Pre_YAvg_base + YAvg_base +1)/2;
1832 
1833             //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
1834             if(YAvg_base==0)
1835             {
1836                 DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  YAvg_base=0 !!!\n", __FUNCTION__,__LINE__));
1837                 return;
1838             }
1839 
1840             if (g_DlcParameters.ucDlcCurveMode == 0 ) //DLC_CURVE_MODE_BY_YAVG)
1841             {
1842                 CurveDiff = g_DlcParameters.ucDlcCurveDiff_H_TH;
1843                 for (ucTmp=0; ucTmp<16; ucTmp++)
1844                 {
1845                     g_uwTable[ucTmp] = (g_ucCurveYAvg[ucTmp]<<2);
1846                 }
1847             }
1848             else if (g_DlcParameters.ucDlcCurveMode == 1 ) //DLC_CURVE_MODE_BY_HISTOGRAM)
1849             {
1850                 CurveDiff = g_DlcParameters.ucDlcCurveDiff_H_TH;
1851                 for (ucTmp=0; ucTmp<16; ucTmp++)
1852                 {
1853                     g_uwTable[ucTmp] = g_uwCurveHistogram[ucTmp];
1854                 }
1855             }
1856             else
1857             {
1858                 CurveDiff = 0;
1859                 CurveDiff_Coring = 0;
1860                 for (ucTmp=0; ucTmp<16; ucTmp++)
1861                 {
1862                     g_uwTable[ucTmp] = (((WORD)(g_ucCurveYAvg[ucTmp]<<2) * belnging_factor) + ((g_uwCurveHistogram[ucTmp]) * (128 - belnging_factor))) / 128;
1863 
1864                     if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1865                     {
1866                         Pre_CurveDiff_base = Diff_L*4;
1867                     }
1868                     else if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1869                     {
1870                         if(Diff_M >= Diff_L)
1871                         {
1872                             Pre_CurveDiff_base = Diff_L*4 + (((Diff_M-Diff_L)*(g_uwPre_CurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1873                         }
1874                         else
1875                         {
1876                             Pre_CurveDiff_base = Diff_L*4 - (((Diff_L-Diff_M)*(g_uwPre_CurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))/(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1877                         }
1878                     }
1879                     else if (g_uwPre_CurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1880                     {
1881                         if(Diff_H >= Diff_M)
1882                         {
1883                             Pre_CurveDiff_base = Diff_M*4 + (((Diff_H- Diff_M)*(g_uwPre_CurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1884                         }
1885                         else
1886                         {
1887                             Pre_CurveDiff_base = Diff_M*4 -(((Diff_M- Diff_H)*(g_uwPre_CurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))/(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1888                         }
1889                     }
1890                     else
1891                     {
1892                         Pre_CurveDiff_base = Diff_H*4;
1893                     }
1894 
1895                     if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdL*4)
1896                     {
1897                         CurveDiff_base = Diff_L*4;
1898                     }
1899                     else if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdM*4)
1900                     {
1901                         if(Diff_M >= Diff_L)
1902                         {
1903                             CurveDiff_base = Diff_L*4 + (((Diff_M-Diff_L)*(g_uwCurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))
1904                                         /(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1905                         }
1906                         else
1907                         {
1908                             CurveDiff_base = Diff_L*4 - (((Diff_L-Diff_M)*(g_uwCurveHistogram[ucTmp]-g_DlcParameters.ucDlcYAvgThresholdL*4))
1909                                         /(g_DlcParameters.ucDlcYAvgThresholdM- g_DlcParameters.ucDlcYAvgThresholdL));
1910                         }
1911                     }
1912                     else if (g_uwCurveHistogram[ucTmp] <= g_DlcParameters.ucDlcYAvgThresholdH*4)
1913                     {
1914                         if(Diff_H >= Diff_M)
1915                         {
1916                             CurveDiff_base = Diff_M*4 + (((Diff_H- Diff_M)*(g_uwCurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))
1917                                         /(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1918                         }
1919                         else
1920                         {
1921                             CurveDiff_base = Diff_M*4 -(((Diff_M- Diff_H)*(g_uwCurveHistogram[ucTmp]- g_DlcParameters.ucDlcYAvgThresholdM*4))
1922                                         /(g_DlcParameters.ucDlcYAvgThresholdH- g_DlcParameters.ucDlcYAvgThresholdM));
1923                         }
1924                     }
1925                     else
1926                     {
1927                         CurveDiff_base = Diff_H*4;
1928                     }
1929 
1930                     CurveDiff_base = (Pre_CurveDiff_base + CurveDiff_base+1)/2;
1931 
1932                     //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
1933                     if(CurveDiff_base==0)
1934                     {
1935                         DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  CurveDiff_base=0 !!!\n", __FUNCTION__,__LINE__));
1936                         return;
1937                     }
1938 
1939                     if(ucTmp*16+8 <= ucYAvgNormalize)
1940                     {
1941                         if (ucTmp == 0)
1942                         {
1943                             CurveDiff_Coring = (His_ratio_BelowAvg*(1024-uwHistogram_BLE_AreaSum[1])*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1944                         }
1945                         else
1946                         {
1947                             CurveDiff_Coring = (His_ratio_BelowAvg*(1024-(uwHistogram_BLE_AreaSum[ucTmp*2+1]- uwHistogram_BLE_AreaSum[ucTmp*2-1]))*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1948                         }
1949                     }
1950                     else
1951                     {
1952                         CurveDiff_Coring = (His_ratio_AboveAvg*(1024-(uwHistogram_BLE_AreaSum[ucTmp*2+1]- uwHistogram_BLE_AreaSum[ucTmp*2-1]))*abs(g_uwCurveHistogram[ucTmp]-g_uwPre_CurveHistogram[ucTmp]))/(15*CurveDiff_base*128);
1953                     }
1954 
1955                     CurveDiff_Coring = CurveDiff_Coring/6;
1956 
1957                     CurveDiff = CurveDiff + CurveDiff_Coring ;
1958                     g_uwPre_CurveHistogram[ucTmp] = g_uwCurveHistogram[ucTmp];
1959                 }
1960             }
1961 
1962             if(CurveDiff <= g_DlcParameters.ucDlcCurveDiff_L_TH)
1963             {
1964                 g_ucDlcFlickAlpha_Max = g_DlcParameters.ucDlcAlphaBlendingMin;
1965             }
1966             else if(CurveDiff >= g_DlcParameters.ucDlcCurveDiff_H_TH)
1967             {
1968                 g_ucDlcFlickAlpha_Max = g_DlcParameters.ucDlcAlphaBlendingMax; ;
1969             }
1970             else
1971             {
1972                 g_ucDlcFlickAlpha_Max = (((g_DlcParameters.ucDlcAlphaBlendingMax-g_DlcParameters.ucDlcAlphaBlendingMin)*(CurveDiff - g_DlcParameters.ucDlcCurveDiff_L_TH))/
1973                                         (g_DlcParameters.ucDlcCurveDiff_H_TH - g_DlcParameters.ucDlcCurveDiff_L_TH))+g_DlcParameters.ucDlcAlphaBlendingMin;
1974             }
1975 
1976             Delta_YAvg = (96*4*abs(ucYAvg - ucPre_YAvg)+ YAvg_base/2)/YAvg_base;
1977 
1978             if(Delta_YAvg > 256)
1979             {
1980                 Delta_YAvg = 256;
1981             }
1982             else
1983             {
1984                 Delta_YAvg = Delta_YAvg;
1985             }
1986 
1987             YAvg_TH_H = g_DlcParameters.ucDlcYAVG_H_TH;
1988             YAvg_TH_L = g_DlcParameters.ucDlcYAVG_L_TH;
1989 
1990             if(Delta_YAvg < YAvg_TH_L)
1991             {
1992                 g_ucDlcFlickAlpha_Min = g_DlcParameters.ucDlcAlphaBlendingMin;
1993             }
1994             else if(Delta_YAvg >= YAvg_TH_H)
1995             {
1996                 g_ucDlcFlickAlpha_Min = g_DlcParameters.ucDlcAlphaBlendingMax;
1997             }
1998             else
1999             {
2000                 g_ucDlcFlickAlpha_Min = ((Delta_YAvg- YAvg_TH_L)*( g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin)
2001                                         + ( YAvg_TH_H-YAvg_TH_L)/2)/( YAvg_TH_H-YAvg_TH_L)+g_DlcParameters.ucDlcAlphaBlendingMin;
2002             }
2003 
2004             ucPre_YAvg = ucYAvg;
2005 
2006             if (g_ucDlcFlickAlpha_Max > g_ucDlcFlickAlpha_Min)
2007             {
2008                 g_ucDlcFlickAlpha_Max = g_ucDlcFlickAlpha_Max;
2009                 g_ucDlcFlickAlpha_Min = g_ucDlcFlickAlpha_Min;
2010             }
2011             else
2012             {
2013                 g_ucDlcFlickAlpha_temp = g_ucDlcFlickAlpha_Max;
2014                 g_ucDlcFlickAlpha_Max = g_ucDlcFlickAlpha_Min;
2015                 g_ucDlcFlickAlpha_Min = g_ucDlcFlickAlpha_temp;
2016             }
2017 
2018             g_ucDlcFlickAlpha  = ( g_DlcParameters.ucDlcFlicker_alpha*g_ucDlcFlickAlpha_Max + (256- g_DlcParameters.ucDlcFlicker_alpha)*g_ucDlcFlickAlpha_Min+128)/256;
2019 
2020         }
2021     }
2022 
2023     if( g_DlcParameters.bEnableBLE )
2024     {
2025         BYTE uc_cut,uc_lowbound,uc_highbound,i;
2026 
2027         // combine BLE with software
2028         // BLE - get cut point
2029         uc_cut = g_ucHistogramMin;
2030 
2031         uc_highbound = msReadByte( REG_ADDR_BLE_UPPER_BOND );
2032         uc_lowbound = msReadByte( REG_ADDR_BLE_LOWER_BOND);
2033 
2034         if (uc_cut < uc_lowbound )
2035         {
2036             uc_cut = uc_lowbound;
2037         }
2038         else if ( uc_cut > uc_highbound )
2039         {
2040             uc_cut = uc_highbound;
2041         }
2042         else
2043         {
2044             uc_cut = uc_cut;
2045         }
2046 
2047         // BLE - calculation
2048         for(i=0; i<8; ++i)
2049         {
2050             if (g_ucTable[i] < uc_cut)
2051             {
2052                 g_ucTable[i] = 0;
2053             }
2054             else if (g_ucTable[i] > (uc_cut+g_DlcParameters.ucDlcBLEPoint))
2055             {
2056                 ;
2057             }
2058             else
2059             {
2060                 g_ucTable[i] = ((WORD)(g_ucTable[i]-uc_cut))*(uc_cut+g_DlcParameters.ucDlcBLEPoint)/g_DlcParameters.ucDlcBLEPoint;
2061             }
2062         }
2063     }
2064 
2065     if( g_DlcParameters.bEnableWLE )
2066     {
2067         BYTE i,uc_wcut,uc_wlowbound,uc_whighbound;
2068         // combine BLE with software
2069         // WLE - get cut point
2070         uc_wcut = g_ucHistogramMax;
2071 
2072         uc_whighbound = (msReadByte( REG_ADDR_WLE_UPPER_BOND ) & 0x3F) + 0xC0; // bit offset 0x00~0x3F -> 0xC0~0xFF
2073         uc_wlowbound = (msReadByte( REG_ADDR_WLE_LOWER_BOND) & 0x3F) + 0xC0; // bit offset 0x00~0x3F -> 0xC0~0xFF
2074 
2075         if (uc_wcut < uc_wlowbound )
2076         {
2077             uc_wcut = uc_wlowbound;
2078         }
2079         else if ( uc_wcut > uc_whighbound )
2080         {
2081             uc_wcut = uc_whighbound;
2082         }
2083         else
2084         {
2085             uc_wcut = uc_wcut;
2086         }
2087 
2088         // WLE - calculation
2089         for(i=0; i<8; ++i)
2090         {
2091             if (g_ucTable[15-i] > uc_wcut)
2092             {
2093                 g_ucTable[15-i] = 0xFF;
2094             }
2095             else if (g_ucTable[15-i] < (uc_wcut-g_DlcParameters.ucDlcWLEPoint))
2096             {
2097                 ;
2098             }
2099             else
2100             {
2101                 g_ucTable[15-i] = 0xFF - (((WORD)(uc_wcut-g_ucTable[15-i]))*(0xFF-uc_wcut+g_DlcParameters.ucDlcWLEPoint)/g_DlcParameters.ucDlcWLEPoint);
2102             }
2103         }
2104     }
2105 
2106     // Level Limit
2107     if (g_DlcParameters.ucDlcLevelLimit)
2108     {
2109         if ((g_DlcParameters.ucDlcLevelLimit >= 1) && (g_ucTable[15] >= 0xF7))
2110         {
2111             g_ucTable[15] = 0xF7;
2112             if (g_ucTable[14] > 0xF7)
2113             {
2114                 g_ucTable[14] = 0xF7;
2115             }
2116 
2117             if (g_ucTable[13] > 0xF7)
2118             {
2119                 g_ucTable[13] = 0xF7;
2120             }
2121         }
2122 
2123         if ((g_DlcParameters.ucDlcLevelLimit >= 2) && (g_ucTable[14] >= 0xE7))
2124         {
2125             g_ucTable[14] = 0xE7;
2126             if (g_ucTable[13] > 0xE7)
2127             {
2128                 g_ucTable[13] = 0xE7;
2129             }
2130 
2131             if (g_ucTable[12] > 0xE7)
2132             {
2133                 g_ucTable[12] = 0xE7;
2134             }
2135         }
2136 
2137         if ((g_DlcParameters.ucDlcLevelLimit >= 3) && (g_ucTable[13] >= 0xD7))
2138         {
2139             g_ucTable[13] = 0xD7;
2140             if (g_ucTable[12] > 0xD7)
2141             {
2142                 g_ucTable[12] = 0xD7;
2143             }
2144 
2145             if (g_ucTable[11] > 0xD7)
2146             {
2147                 g_ucTable[11] = 0xD7;
2148             }
2149         }
2150 
2151         if ((g_DlcParameters.ucDlcLevelLimit >= 4) && (g_ucTable[12] >= 0xC7))
2152         {
2153             g_ucTable[12] = 0xC7;
2154             if (g_ucTable[11] > 0xC7)
2155             {
2156                 g_ucTable[11] = 0xC7;
2157             }
2158 
2159             if (g_ucTable[10] > 0xC7)
2160             {
2161                 g_ucTable[10] = 0xC7;
2162             }
2163         }
2164     }
2165 #if 0 //For customer require. Run DLC algorithm when static field .
2166     if((g_wLumiAverageTemp-Pre_TotalYSum)!=0)
2167 #endif
2168     {
2169 
2170         if ((g_ucDlcFlickAlpha < (5*Pre_g_ucDlcFlickAlpha /8)) && (ucDlcFlickAlpha_Diff < (g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin)/2))
2171         {
2172             g_ucDlcFlickAlpha = 2*(ucDlcFlickAlpha_Diff* g_ucDlcFlickAlpha +(( g_DlcParameters.ucDlcAlphaBlendingMax
2173                                   - g_DlcParameters.ucDlcAlphaBlendingMin)/2- ucDlcFlickAlpha_Diff)* (5*Pre_g_ucDlcFlickAlpha /8))
2174                                   /( g_DlcParameters.ucDlcAlphaBlendingMax- g_DlcParameters.ucDlcAlphaBlendingMin);
2175         }
2176 
2177         Pre_g_ucDlcFlickAlpha = g_ucDlcFlickAlpha;
2178         ucDlcFlickAlpha_Diff = g_ucDlcFlickAlpha_Max-g_ucDlcFlickAlpha_Min;
2179 
2180         // ~~~~~~~~~~~~~~~~~~~~~~~~ Sub window open PATCH Start  ~~~~~~~~~~~~~~~~~~~~~~~~
2181         bSubWindowEnable = ((msReadByte(REG_ADDR_OP_SW_SUB_ENABLE) & BIT(1)) ==  BIT(1));
2182 
2183         if(bPreSubWindowEnable != bSubWindowEnable)
2184         {
2185         	ucSubWindowChange = 1;
2186             bPreSubWindowEnable = bSubWindowEnable;
2187             uwSubChangeDelayCount=0;
2188         }
2189 
2190         if(ucSubWindowChange > 0)
2191         {
2192             uwSubChangeDelayCount++;
2193             if(((g_ucDlcFlickAlpha > 48) &&(uwSubChangeDelayCount>=100))
2194                 ||((g_ucDlcFlickAlpha > 24) &&(uwSubChangeDelayCount>=200)))
2195             {
2196                 ucSubWindowChange = ucSubWindowChange-1;
2197             }
2198 
2199             if(ucSubWindowChange == 0)
2200             {
2201                 uwSubChangeDelayCount=0;
2202             }
2203             else
2204             {
2205                 g_ucDlcFlickAlpha = 0;
2206             }
2207         }
2208         // ~~~~~~~~~~~~~~~~~~~~~~~~ Sub window open PATCH End  ~~~~~~~~~~~~~~~~~~~~~~~~
2209 
2210         // ~~~~~~~~~~~~~~~~~~~~~~~~ PI_Alpha IIR Start  ~~~~~~~~~~~~~~~~~~~~~~~~
2211         if ( g_wPIAlpha >= g_wPrePIAlpha)
2212         {
2213             g_wPIAlpha = ((g_ucDlcFlickAlpha * g_wPIAlpha) +((128-g_ucDlcFlickAlpha)*g_wPrePIAlpha)+127)/128;
2214         }
2215         else
2216         {
2217             g_wPIAlpha = ((g_ucDlcFlickAlpha * g_wPIAlpha) +((128-g_ucDlcFlickAlpha)*g_wPrePIAlpha))/128;
2218         }
2219 
2220         g_wPrePIAlpha = g_wPIAlpha;
2221         // ~~~~~~~~~~~~~~~~~~~~~~~~ PI_Alpha IIR End  ~~~~~~~~~~~~~~~~~~~~~~~~
2222 
2223         // Write data to luma curve ...
2224         msDlcWriteCurve(bWindow);
2225         if(g_bSetDLCCurveBoth)
2226         {
2227             msDlcWriteCurve(SUB_WINDOW);
2228         }
2229 
2230         if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_NEW)
2231         {
2232             //Dyanmic BLE Function
2233             //~~~~~~~~~~~~~~~~~Calculate BLE_StartPoint~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2234             if(((ucYAvg*4) + g_DlcParameters.uwDlcBleStartPointTH)>512)
2235             {
2236                 BLE_StartPoint = ((ucYAvg*4) + g_DlcParameters.uwDlcBleStartPointTH - 512);
2237             }
2238             else
2239             {
2240                 BLE_StartPoint = 0;
2241             }
2242 
2243             BLE_StartPoint_Correction = (BLE_StartPoint+2)/4;
2244 
2245             if(BLE_StartPoint_Correction >(Pre_BLE_StartPoint_Correction/16))
2246             {
2247                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2248                 {
2249                     uwBLE_StartPoint_Blend = (WORD)(((16*(DWORD)BLE_StartPoint_Correction)+(63*(DWORD)Pre_BLE_StartPoint_Correction)+63)/64);
2250                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend;
2251                     uwBLE_StartPoint_Blend = (uwBLE_StartPoint_Blend+8)/16;
2252                 }
2253                 else
2254                 {
2255                     uwBLE_StartPoint_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_StartPoint_Correction) +
2256                                              ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_StartPoint_Correction/16))+127)/128);
2257                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend*16;
2258                 }
2259             }
2260             else
2261             {
2262                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2263                 {
2264                     uwBLE_StartPoint_Blend = (WORD)(((16*(DWORD)BLE_StartPoint_Correction)+(63*(DWORD)Pre_BLE_StartPoint_Correction))/64);
2265                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend;
2266                     uwBLE_StartPoint_Blend = (uwBLE_StartPoint_Blend+8)/16;
2267                 }
2268                 else
2269                 {
2270                     uwBLE_StartPoint_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_StartPoint_Correction) +
2271                                              ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_StartPoint_Correction/16)))/128);
2272                     Pre_BLE_StartPoint_Correction = uwBLE_StartPoint_Blend*16;
2273                 }
2274             }
2275 
2276             //~~~~~~~~~output = uwBLE_StartPoint_Blend & Pre_BLE_StartPoint_Correction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2277 
2278             //~~~~~~~~~~~~~~~~~Calculate  BLE_EndPoint ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2279 
2280             for (ucTmp=0; ucTmp<32; ucTmp++)
2281             {
2282                 if(ucTmp==0)
2283                 {
2284                     uwHistogramAreaSum[ucTmp]=g_wLumaHistogram32H[ucTmp];
2285                 }
2286                 else
2287                 {
2288                     uwHistogramAreaSum[ucTmp] = uwHistogramAreaSum[ucTmp-1] + g_wLumaHistogram32H[ucTmp];
2289                 }
2290             }
2291 
2292             ucTmp=0;
2293             while( uwHistogramAreaSum[ucTmp] < uwHistogramAreaSum[31]/8)
2294             {
2295                 ucTmp++;
2296             }
2297 
2298             if(ucTmp==0)
2299             {
2300                 //Mantis issue of 0264631 :[Konka J3] . Use autotest tool control dc on/off.  DLC divisor can't to be equal to zero .
2301                 if(g_wLumaHistogram32H[0]==0)
2302                 {
2303                     DLC_DEBUG(printk("\n Error in  [New DLC Algorithm][ %s  , %d ]  g_wLumaHistogram32H[0]=0 !!!\n", __FUNCTION__,__LINE__));
2304                     return;
2305                 }
2306 
2307                 BLE_EndPoint = ((g_uwTable[0]*uwHistogramAreaSum[31])/(16*g_wLumaHistogram32H[0]));
2308             }
2309             else
2310             {
2311                 uwHistogramForBleSum = (uwHistogramAreaSum[31]/8) - uwHistogramAreaSum[ucTmp-1];
2312 
2313                 for (Tmp=0; Tmp<30; Tmp++)
2314                 {
2315                     uwHistogramAreaSum[Tmp] = (g_uwTable[Tmp/2]+g_uwTable[(Tmp+1)/2]+1)/2;
2316                 }
2317 
2318                 for (Tmp=0; Tmp<(ucTmp-1); Tmp++)
2319                 {
2320                     if(Tmp==0)
2321                     {
2322                         BLE_sum = g_wLumaHistogram32H[0]*(g_uwTable[0]+1/2);
2323                     }
2324                     else
2325                     {
2326                         BLE_sum = BLE_sum + g_wLumaHistogram32H[Tmp] * ((uwHistogramAreaSum[Tmp]+uwHistogramAreaSum[Tmp-1]+1)/2);
2327                     }
2328                 }
2329 
2330                 //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
2331                 if(g_wLumaHistogram32H[ucTmp]==0)
2332                 {
2333                     DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  g_wLumaHistogram32H[%d]=0 !!!\n", __FUNCTION__,__LINE__,ucTmp));
2334                     return;
2335                 }
2336 
2337                 BLE_sum = BLE_sum + uwHistogramForBleSum *((uwHistogramAreaSum[ucTmp]+uwHistogramAreaSum[ucTmp-1]+1)/2);
2338 
2339                 BLE_EndPoint = (BLE_sum*8+(uwHistogramAreaSum[31]/2))/uwHistogramAreaSum[31];
2340             }
2341 
2342             BLE_EndPoint = (BLE_EndPoint*g_DlcParameters.uwDlcBleEndPointTH+512)/1024;
2343             BLE_EndPoint_Correction = BLE_EndPoint;
2344 
2345 
2346             //~~~~~~~~~output = BLE_EndPoint Correction ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2347 
2348             //~~~~~~~~~Used uwBLE_StartPoint_Blend & BLE_EndPoint_Correction Calculate BLE_Slop ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2349 
2350             if(uwBLE_StartPoint_Blend > 0x7F)
2351             {
2352                 uwBLE_StartPoint_Blend = 0x7F;
2353             }
2354 
2355             if( uwBLE_StartPoint_Blend*4 > BLE_EndPoint_Correction)
2356             {
2357                 BLE_Slop = (1024*uwBLE_StartPoint_Blend*4+((uwBLE_StartPoint_Blend*4 - BLE_EndPoint_Correction)/2))
2358                         /(uwBLE_StartPoint_Blend*4 - BLE_EndPoint_Correction);
2359             }
2360             else
2361             {
2362                 BLE_Slop = 0x7FF;
2363             }
2364 
2365             if(BLE_Slop > 0x7FF)
2366             {
2367                 BLE_Slop = 0x7FF;
2368             }
2369 
2370             if(BLE_Slop < 0x400)
2371             {
2372                 BLE_Slop = 0x400;
2373             }
2374 
2375             BLE_Slop_400 = g_DlcParameters.uwDlcBLESlopPoint_1;//(msReadByte(L_BK_DLC(0x38)))<<3;
2376             BLE_Slop_480 = g_DlcParameters.uwDlcBLESlopPoint_2;//(msReadByte(H_BK_DLC(0x38)))<<3;
2377             BLE_Slop_500 = g_DlcParameters.uwDlcBLESlopPoint_3;//(msReadByte(L_BK_DLC(0x39)))<<3;
2378             BLE_Slop_600 = g_DlcParameters.uwDlcBLESlopPoint_4;//(msReadByte(H_BK_DLC(0x39)))<<3;
2379             BLE_Slop_800 = g_DlcParameters.uwDlcBLESlopPoint_5;//(msReadByte(L_BK_DLC(0x3A)))<<3;
2380             Dark_BLE_Slop_Min = g_DlcParameters.uwDlcDark_BLE_Slop_Min;//(msReadByte(L_BK_DLC(0x3B)))<<3;
2381 
2382             //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2383             if(BLE_Slop <= 0x480)
2384             {
2385                 BLE_Slop =((BLE_Slop_480-BLE_Slop_400)*(BLE_Slop-0x400)+64)/128+BLE_Slop_400;
2386             }
2387             else if ((BLE_Slop > 0x480) && (BLE_Slop <= 0x500))
2388             {
2389                 BLE_Slop =((BLE_Slop_500-BLE_Slop_480)*(BLE_Slop-0x480)+64)/128+BLE_Slop_480;
2390             }
2391             else if ((BLE_Slop > 0x500) && (BLE_Slop <= 0x600))
2392             {
2393                 BLE_Slop =((BLE_Slop_600-BLE_Slop_500)*(BLE_Slop-0x500)+128)/256+BLE_Slop_500;
2394             }
2395             else
2396             {
2397                 BLE_Slop =((BLE_Slop_800-BLE_Slop_600)*(BLE_Slop-0x600)+256)/512+BLE_Slop_600;
2398             }
2399 
2400             if (ucYAvg < ((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2))
2401             {
2402                 BLE_Slop_tmp =(((Dark_BLE_Slop_Min - BLE_Slop_400)*(((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2)-ucYAvg)/
2403                               ((g_DlcParameters.ucDlcYAvgThresholdL+g_DlcParameters.ucDlcYAvgThresholdM)/2)) + BLE_Slop_400);
2404 
2405                 if(BLE_Slop > BLE_Slop_tmp)
2406                 {
2407                     BLE_Slop = BLE_Slop;
2408                 }
2409                 else
2410                 {
2411                     BLE_Slop = BLE_Slop_tmp;
2412                 }
2413             }
2414             else
2415             {
2416                 BLE_Slop = BLE_Slop;
2417             }
2418 
2419 
2420             // ~~~~~~~~~~~~~~~~~~~~~~~~ Pure Image Patch  ~~~~~~~~~~~~~~~~~~~~~~~~
2421             BLE_Slop = (BLE_Slop*(256-g_wPIAlpha)+ 1024*g_wPIAlpha + 128)/256;
2422 
2423             //~~~~~~~~~ output = uwBLE_Blend �]with slow approach�^ ~~~~~~~~~~~~~~~~~~~~~
2424 
2425             if(BLE_Slop >(Pre_BLE_Slop/16))
2426             {
2427                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2428                 {
2429                     uwBLE_Blend = (WORD)(((16*(DWORD)BLE_Slop)+(63*(DWORD)Pre_BLE_Slop)+63)/64);
2430                     Pre_BLE_Slop = uwBLE_Blend;
2431                     uwBLE_Blend = (uwBLE_Blend+8)/16;
2432                 }
2433                 else
2434                 {
2435                     uwBLE_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_Slop) +
2436                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_Slop/16))+127)/128);
2437                     Pre_BLE_Slop = uwBLE_Blend*16;          //14-bits
2438                 }
2439             }
2440             else
2441             {
2442                 if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2443                 {
2444                     uwBLE_Blend = (WORD)(((16*(DWORD)BLE_Slop)+(63*(DWORD)Pre_BLE_Slop))/64);
2445                     Pre_BLE_Slop = uwBLE_Blend;
2446                     uwBLE_Blend = (uwBLE_Blend+8)/16;
2447                 }
2448                 else
2449                 {
2450                     uwBLE_Blend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)BLE_Slop) +
2451                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(Pre_BLE_Slop/16)))/128);
2452                     Pre_BLE_Slop = uwBLE_Blend*16;
2453                 }
2454             }
2455 
2456             if(g_bSetDlcBleOn)
2457             {
2458                 msWriteByte(L_BK_DLC(0x10),uwBLE_StartPoint_Blend);
2459                 msWriteByte(H_BK_DLC(0x10),uwBLE_Blend>>3);
2460                 msWriteByte(L_BK_DLC(0x0D),(msReadByte(L_BK_DLC(0x0D))&0xF8)|(uwBLE_Blend & 0x07));
2461             }
2462             else
2463             {
2464                 msWriteByte(L_BK_DLC(0x10),0x00);
2465                 msWriteByte(H_BK_DLC(0x10),0x80);
2466                 msWriteByte(L_BK_DLC(0x0D),msReadByte(L_BK_DLC(0x0D))&0xF8);
2467             }
2468         }
2469     }
2470     Pre_TotalYSum = g_wLumiAverageTemp;
2471     msDlc_FunctionExit();
2472 }
2473 
2474 //////////////////////////////////////////////////////////////
2475 //
2476 //////////////////////////////////////////////////////////////
msDlcWriteCurveLSB(BOOL bWindow,BYTE ucIndex,BYTE ucValue)2477 void msDlcWriteCurveLSB(BOOL bWindow, BYTE ucIndex, BYTE ucValue)
2478 {
2479     DWORD wRegStart;
2480     BYTE ucData;
2481 
2482     msDlc_FunctionEnter();
2483 
2484 	if( MAIN_WINDOW == bWindow )
2485 	    wRegStart = REG_ADDR_DLC_DATA_LSB_START_MAIN;
2486     else
2487         wRegStart = REG_ADDR_DLC_DATA_LSB_START_SUB;
2488 
2489     switch(ucIndex)
2490     {
2491         case 0:
2492             wRegStart += 0;
2493             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2494             break;
2495         case 1:
2496             wRegStart += 0;
2497             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2498             break;
2499         case 2:
2500             wRegStart += 0;
2501             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2502             break;
2503         case 3:
2504             wRegStart += 1;
2505             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2506             break;
2507         case 4:
2508             wRegStart += 1;
2509             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2510             break;
2511         case 5:
2512             wRegStart += 1;
2513             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2514             break;
2515         case 6:
2516             wRegStart += 1;
2517             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2518             break;
2519         case 7:
2520             wRegStart += 2;
2521             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2522             break;
2523         case 8:
2524             wRegStart += 2;
2525             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2526             break;
2527         case 9:
2528             wRegStart += 2;
2529             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2530             break;
2531         case 10:
2532             wRegStart += 2;
2533             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2534             break;
2535         case 11:
2536             wRegStart += 3;
2537             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2538             break;
2539         case 12:
2540             wRegStart += 3;
2541             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2542             break;
2543         case 13:
2544             wRegStart += 3;
2545             ucData = (msReadByte(wRegStart) & 0xCF) | (ucValue << 4);
2546             break;
2547         case 14:
2548             wRegStart += 3;
2549             ucData = (msReadByte(wRegStart) & 0x3F) | (ucValue << 6);
2550             break;
2551         case 15:
2552             wRegStart += 4;
2553             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2554             break;
2555         case 16: // index 16
2556             wRegStart += 4;
2557             ucData = (msReadByte(wRegStart) & 0xF3) | (ucValue << 2);
2558             break;
2559         case 0xFF: // index n0
2560         default:
2561             wRegStart += 0;
2562             ucData = (msReadByte(wRegStart) & 0xFC) | (ucValue);
2563             break;
2564     }
2565 
2566     msWriteByte(wRegStart, ucData);
2567 
2568     msDlc_FunctionExit();
2569 }
2570 
msDlcWriteCurve(BOOL bWindow)2571 void msDlcWriteCurve(BOOL bWindow)
2572 {
2573     BYTE ucTmp;
2574     WORD uwBlend;
2575     WORD uwTmp;
2576 
2577     msDlc_FunctionEnter();
2578 
2579     // Write data to luma curve ...
2580 
2581     if(g_DlcParameters.ucDlcAlgorithmMode == XC_DLC_ALGORITHM_OLD)
2582     {
2583 
2584         if( MAIN_WINDOW == bWindow )
2585         {
2586             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2587             {
2588                 if(g_ucDlcFastLoop || (g_ucDlcFlickAlpha >= (DLC_DEFLICK_BLEND_FACTOR >> 1)))
2589                 {
2590                     if (g_ucDlcFastLoop)
2591                         g_ucDlcFastLoop--;
2592 
2593                     // target value with alpha blending
2594                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2595                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2596 
2597                     if(uwBlend > g_uwPreTable[ucTmp])
2598                     {
2599                         uwTmp = (uwBlend - g_uwPreTable[ucTmp]) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2600                         uwBlend = g_uwPreTable[ucTmp] + uwTmp;
2601                     }
2602                     else if(uwBlend < g_uwPreTable[ucTmp])
2603                     {
2604                         uwTmp = (g_uwPreTable[ucTmp] - uwBlend) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2605                         uwBlend = g_uwPreTable[ucTmp] - uwTmp;
2606                     }
2607 
2608                     g_uwPreTable[ucTmp] = uwBlend;
2609 
2610                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2611                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2612                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2613 
2614                     msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2615                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2616 
2617                     // set DLC curve index N0 & 16
2618                     if (ucTmp == 0)
2619                     {
2620                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2621                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2622                         msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2623                     }
2624 
2625                     if (ucTmp == 15)
2626                     {
2627                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2628                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2629                         msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2630                     }
2631                 }
2632                 else if (g_ucDlcFlickAlpha == 0)
2633                 {
2634                     g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2635                 }
2636                 else if ((ucTmp%4) == (g_ucUpdateCnt%4))
2637                 {
2638                     // target value with alpha blending
2639                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2640                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2641 
2642                     if(uwBlend > g_uwPreTable[ucTmp])
2643                     {
2644                         if((uwBlend - g_uwPreTable[ucTmp]) > DLC_DEFLICK_PRECISION_SHIFT)
2645                             uwBlend = g_uwPreTable[ucTmp] + DLC_DEFLICK_PRECISION_SHIFT;
2646                     }
2647                     else if(uwBlend < g_uwPreTable[ucTmp])
2648                     {
2649                         if((g_uwPreTable[ucTmp] - uwBlend) > DLC_DEFLICK_PRECISION_SHIFT)
2650                             uwBlend = g_uwPreTable[ucTmp] - DLC_DEFLICK_PRECISION_SHIFT;
2651                     }
2652 
2653                     g_uwPreTable[ucTmp] = uwBlend;
2654 
2655                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2656                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2657                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2658 
2659                     msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2660                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2661 
2662                     // set DLC curve index N0 & 16
2663                     if (ucTmp == 0)
2664                     {
2665                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2666                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2667                         msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2668                     }
2669 
2670                     if (ucTmp == 15)
2671                     {
2672                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2673                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2674                         msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2675                     }
2676                 }
2677             }
2678         }
2679         else
2680         {
2681 #if XC_DLC_SET_DLC_CURVE_BOTH_SAME
2682             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2683             {
2684                 if(g_ucDlcFastLoop || (g_ucDlcFlickAlpha >= (DLC_DEFLICK_BLEND_FACTOR >> 1)))
2685                 {
2686                     if (g_ucDlcFastLoop)
2687                         g_ucDlcFastLoop--;
2688 
2689                     // target value with alpha blending
2690                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2691                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2692 
2693                     if(uwBlend > g_uwPreTable[ucTmp])
2694                     {
2695                         uwTmp = (uwBlend - g_uwPreTable[ucTmp]) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2696                         uwBlend = g_uwPreTable[ucTmp] + uwTmp;
2697                     }
2698                     else if(uwBlend < g_uwPreTable[ucTmp])
2699                     {
2700                         uwTmp = (g_uwPreTable[ucTmp] - uwBlend) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2701                         uwBlend = g_uwPreTable[ucTmp] - uwTmp;
2702                     }
2703 
2704                     g_uwPreTable[ucTmp] = uwBlend;
2705 
2706                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2707                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2708                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2709 
2710                     msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2711                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2712 
2713                     // set DLC curve index N0 & 16
2714                     if (ucTmp == 0)
2715                     {
2716                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2717                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2718                         msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2719                     }
2720 
2721                     if (ucTmp == 15)
2722                     {
2723                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2724                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2725                         msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2726                     }
2727                 }
2728                 else if (g_ucDlcFlickAlpha == 0)
2729                 {
2730                     g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2731                 }
2732                 else if ((ucTmp%4) == (g_ucUpdateCnt%4))
2733                 {
2734                     // target value with alpha blending
2735                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * DLC_DEFLICK_PRECISION_SHIFT) +
2736                                  ((DLC_DEFLICK_BLEND_FACTOR - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp]))/DLC_DEFLICK_BLEND_FACTOR);
2737 
2738                     if(uwBlend > g_uwPreTable[ucTmp])
2739                     {
2740                         if((uwBlend - g_uwPreTable[ucTmp]) > DLC_DEFLICK_PRECISION_SHIFT)
2741                             uwBlend = g_uwPreTable[ucTmp] + DLC_DEFLICK_PRECISION_SHIFT;
2742                     }
2743                     else if(uwBlend < g_uwPreTable[ucTmp])
2744                     {
2745                         if((g_uwPreTable[ucTmp] - uwBlend) > DLC_DEFLICK_PRECISION_SHIFT)
2746                             uwBlend = g_uwPreTable[ucTmp] - DLC_DEFLICK_PRECISION_SHIFT;
2747                     }
2748 
2749                     g_uwPreTable[ucTmp] = uwBlend;
2750 
2751                     //g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / DLC_DEFLICK_PRECISION_SHIFT;
2752                     g_uwTable[ucTmp] = ((DWORD)g_uwPreTable[ucTmp]<<2) / DLC_DEFLICK_PRECISION_SHIFT;
2753                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2754 
2755                     msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2756                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2757 
2758                     // set DLC curve index N0 & 16
2759                     if (ucTmp == 0)
2760                     {
2761                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2762                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2763                         msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2764                     }
2765 
2766                     if (ucTmp == 15)
2767                     {
2768                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2769                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2770                         msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2771                     }
2772                 }
2773             }
2774 #else
2775             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2776             {
2777                 msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2778                 msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2779 
2780                 // set DLC curve index N0 & 16
2781                 if (ucTmp == 0)
2782                 {
2783                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2784                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2785                     msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2786                 }
2787 
2788                 if (ucTmp == 15)
2789                 {
2790                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2791                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2792                     msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2793                 }
2794             }
2795 #endif
2796         }
2797     }
2798     else
2799     {
2800 
2801         if( MAIN_WINDOW == bWindow )
2802         {
2803             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2804             {
2805                 g_uwTable[ucTmp] = (g_wPIAlpha*(ucTmp*64 + 32)+(256-g_wPIAlpha)*g_uwTable[ucTmp]+128)/256;
2806 
2807                 if(g_uwTable[ucTmp]>(g_uwPreTable[ucTmp]/16))
2808                 {
2809                     if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2810                     {
2811                         uwBlend = (WORD)(((16*(DWORD)g_uwTable[ucTmp])+(63*(DWORD)g_uwPreTable[ucTmp])+63)/64); //14-bits
2812                         g_uwPreTable[ucTmp] = uwBlend;                                                              //14-bits
2813                         uwBlend = (uwBlend+8)/16;                                                                     //10-bits
2814                     }
2815                     else
2816                     {
2817                         uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_uwTable[ucTmp]) +
2818                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(g_uwPreTable[ucTmp]/16))+127)/128); //10-bits
2819                         g_uwPreTable[ucTmp] = uwBlend*16;                                                            //14-bits
2820                         uwBlend = uwBlend;
2821                     }
2822                 }
2823                 else
2824                 {
2825                     if(g_ucDlcFlickAlpha==g_DlcParameters.ucDlcAlphaBlendingMin)
2826                     {
2827                         uwBlend = (WORD)(((16*(DWORD)g_uwTable[ucTmp])+(63*(DWORD)g_uwPreTable[ucTmp]))/64);  //14-bits
2828                         g_uwPreTable[ucTmp] = uwBlend;                                                            //14-bits
2829                         uwBlend = (uwBlend+8)/16;                                                                   //10-bits
2830                     }
2831                     else
2832                     {
2833                         uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_uwTable[ucTmp]) +
2834                                   ((128 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)(g_uwPreTable[ucTmp]/16)))/128); //10-bits
2835                         g_uwPreTable[ucTmp] = uwBlend*16;                                                        //14-bits
2836                         uwBlend = uwBlend;
2837                     }
2838                 }
2839 
2840                 g_uwTable[ucTmp] = uwBlend;
2841                 g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2842 
2843                 msWriteByte((REG_ADDR_DLC_DATA_START_MAIN+ucTmp), g_ucTable[ucTmp]);
2844                 msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2845 
2846                 // set DLC curve index N0 & 16
2847                 if (ucTmp == 0)
2848                 {
2849                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN, g_uwTable[0]>>2);
2850                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_MAIN + 1, 0x01);
2851                     msDlcWriteCurveLSB(MAIN_WINDOW, 0xFF, g_uwTable[0]&0x03);
2852                 }
2853 
2854                 if (ucTmp == 15)
2855                 {
2856                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN, 255-(g_uwTable[15]>>2));
2857                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_MAIN + 1, 0x01);
2858                     msDlcWriteCurveLSB(MAIN_WINDOW, 16, g_uwTable[15]&0x03);
2859                 }
2860             }
2861         }
2862         else
2863         {
2864 #if XC_DLC_SET_DLC_CURVE_BOTH_SAME
2865             //Mantis issue of 0232938:Hsense A3 DLC the divisor can't to be equal to zero .
2866             if((DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1)==0)
2867             {
2868                 DLC_DEBUG(printf("\n Error in  [New DLC Algorithm][ %s  , %d ]  (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1)=0 !!!\n", __FUNCTION__,__LINE__));
2869                 return;
2870             }
2871 
2872             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2873             {
2874                 if(g_ucDlcFastLoop || (g_ucDlcFlickAlpha >= (DLC_DEFLICK_BLEND_FACTOR >> 1)))
2875                 {
2876                     if (g_ucDlcFastLoop)
2877                     {
2878                         g_ucDlcFastLoop--;
2879                     }
2880 
2881                     // target value with alpha blending
2882 
2883                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * 4) +
2884                               ((32 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp])+16)/32);
2885 
2886                     if(uwBlend > g_uwPreTable[ucTmp])
2887                     {
2888                         uwTmp = (uwBlend - g_uwPreTable[ucTmp]) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2889                         uwBlend = g_uwPreTable[ucTmp] + uwTmp;
2890                     }
2891                     else if(uwBlend < g_uwPreTable[ucTmp])
2892                     {
2893                         uwTmp = (g_uwPreTable[ucTmp] - uwBlend) / (DLC_DEFLICK_BLEND_FACTOR - g_ucDlcFlickAlpha + 1);
2894                         uwBlend = g_uwPreTable[ucTmp] - uwTmp;
2895                     }
2896 
2897                     g_uwPreTable[ucTmp] = uwBlend;
2898 
2899                     g_uwTable[ucTmp] = g_uwPreTable[ucTmp];
2900                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2901 
2902                     msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2903                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2904 
2905                     // set DLC curve index N0 & 16
2906                     if (ucTmp == 0)
2907                     {
2908                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2909                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2910                         msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2911                     }
2912 
2913                     if (ucTmp == 15)
2914                     {
2915                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2916                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2917                         msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2918                     }
2919                 }
2920                 else if (g_ucDlcFlickAlpha == 0)
2921                 {
2922                     g_ucTable[ucTmp] = g_uwPreTable[ucTmp] / 4;
2923                 }
2924                 else if ((ucTmp%4) == (g_ucUpdateCnt%4))
2925                 {
2926                     // target value with alpha blending
2927 
2928                     uwBlend = (WORD)((((DWORD) g_ucDlcFlickAlpha * (DWORD)g_ucTable[ucTmp] * 4) +
2929                               ((32 - (DWORD)g_ucDlcFlickAlpha) * (DWORD)g_uwPreTable[ucTmp])+16)/32);
2930 
2931                     if(uwBlend > g_uwPreTable[ucTmp])
2932                     {
2933                         if((uwBlend - g_uwPreTable[ucTmp]) > 4)
2934                         {
2935                             uwBlend = g_uwPreTable[ucTmp] + 4;
2936                         }
2937                     }
2938                     else if(uwBlend < g_uwPreTable[ucTmp])
2939                     {
2940                         if((g_uwPreTable[ucTmp] - uwBlend) > 4)
2941                         {
2942                             uwBlend = g_uwPreTable[ucTmp] - 4;
2943                         }
2944                     }
2945 
2946                     g_uwPreTable[ucTmp] = uwBlend;
2947 
2948                     g_uwTable[ucTmp] = g_uwPreTable[ucTmp];
2949                     g_ucTable[ucTmp] = g_uwTable[ucTmp]>>2;
2950 
2951                     msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2952                     msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2953 
2954                     // set DLC curve index N0 & 16
2955                     if (ucTmp == 0)
2956                     {
2957                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2958                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2959                         msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2960                     }
2961 
2962                     if (ucTmp == 15)
2963                     {
2964                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2965                         msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2966                         msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2967                     }
2968                 }
2969             }
2970 #else
2971             for(ucTmp=0; ucTmp<=0x0f; ucTmp++)
2972             {
2973                 msWriteByte((REG_ADDR_DLC_DATA_START_SUB+ucTmp), g_ucTable[ucTmp]);
2974                 msDlcWriteCurveLSB(bWindow, ucTmp, g_uwTable[ucTmp]&0x03);
2975 
2976                 // set DLC curve index N0 & 16
2977                 if (ucTmp == 0)
2978                 {
2979                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB, g_uwTable[0]>>2);
2980                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_N0_SUB + 1, 0x01);
2981                     msDlcWriteCurveLSB(SUB_WINDOW, 0xFF, g_uwTable[0]&0x03);
2982                 }
2983 
2984                 if (ucTmp == 15)
2985                 {
2986                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB, 255-(g_uwTable[15]>>2));
2987                     msWriteByte(REG_ADDR_DLC_DATA_EXTEND_16_SUB + 1, 0x01);
2988                     msDlcWriteCurveLSB(SUB_WINDOW, 16, g_uwTable[15]&0x03);
2989                 }
2990             }
2991 #endif
2992         }
2993     }
2994 
2995     g_ucUpdateCnt++;
2996 
2997     msDlc_FunctionExit();
2998 }
2999 
3000 //////////////////////////////////////////////////////////////
3001 // Program register bytes to MST chip.
3002 //
3003 // Arguments: pTable - pointer of registers table
3004 //////////////////////////////////////////////////////////////
msWriteDLCRegsTbl(DlcRegUnitType code * pTable)3005 void msWriteDLCRegsTbl(DlcRegUnitType code *pTable)
3006 {
3007     WORD wReg; // register index
3008 
3009     while (1)
3010     {
3011          wReg = pTable->wReg; // get register index
3012          if (wReg == _END_OF_TBL_) // check end of table
3013              break;
3014 
3015          msWriteByte(wReg, pTable->ucValue); // write register
3016 
3017          pTable++; // next
3018     } // while
3019 }
3020 
msDlcSpeedupTrigger(BYTE ucLoop)3021 void msDlcSpeedupTrigger(BYTE ucLoop)
3022 {
3023     g_DlcParameters.ucDlcFlickAlphaStart = ucLoop;
3024 }
3025 
3026 /******************************************************************************/
3027 /*                           CGC                                         */
3028 /******************************************************************************/
3029 
msDlc_CGC_Init(void)3030 void msDlc_CGC_Init(void)
3031 {
3032     msDlc_FunctionEnter();
3033 
3034     if((msReadByte(REG_ADDR_DLC_HANDSHAKE) & _BIT7) )// Shall not Init CGC again while DLC is On.
3035     {
3036        msDlc_FunctionExit();
3037        return;
3038     }
3039 
3040     g_u8Chroma_OriGain = msReadByte(REG_ADDR_DLC_C_GAIN);
3041     msDlc_CGC_ReInit();
3042 
3043     msDlc_FunctionExit();
3044 }
3045 
msDlc_CGC_ReInit(void)3046 void msDlc_CGC_ReInit(void) // For CGC Init & Tool change parameter value dynamicly
3047 {
3048     if ((0xFF-g_u8Chroma_OriGain)>g_DlcParameters.ucCGCChroma_GainLimitH)
3049         g_u8CGC_UpperBond = (g_u8Chroma_OriGain+g_DlcParameters.ucCGCChroma_GainLimitH);
3050     else
3051         g_u8CGC_UpperBond = 0xFF;
3052 
3053     if (g_DlcParameters.ucCGCChroma_GainLimitL<g_u8Chroma_OriGain)
3054         g_u8CGC_LowerBond = (g_u8Chroma_OriGain-g_DlcParameters.ucCGCChroma_GainLimitL);
3055     else
3056         g_u8CGC_LowerBond = 0x00;
3057 
3058     g_u16Ydark = (Y01_weight*(g_ucTable[0]+g_ucTable[1])+Y23_weight*(g_ucTable[2]+g_ucTable[3]))/(2*(Y01_weight+Y23_weight));
3059     g_u16Ydark =  g_u16Ydark+((Y45_weight*(g_ucTable[4]+g_ucTable[5])+Y67_weight*(g_ucTable[6]+g_ucTable[7])))/(2*(Y45_weight+Y67_weight));
3060 
3061     if (g_DlcParameters.ucCGCCGain_offset & _BIT7)
3062         g_u16Ydark = (g_u16Ydark-(g_DlcParameters.ucCGCCGain_offset&0x7F));
3063     else
3064         g_u16Ydark = (g_u16Ydark+g_DlcParameters.ucCGCCGain_offset);
3065 
3066     g_u16Ydark_pre = g_u16Ydark;
3067 
3068     //printf("\r\n[1]. g_ucTable[0] = %d \n", g_ucTable[0]);
3069     //printf("\r\n[2]. g_ucTable[1] = %d \n", g_ucTable[1]);
3070     //printf("\r\n[3]. g_ucTable[2] = %d \n", g_ucTable[2]);
3071     //printf("\r\n[4]. g_ucTable[3] = %d \n", g_ucTable[3]);
3072     //printf("\r\n[5]. g_ucTable[4] = %d \n", g_ucTable[4]);
3073 
3074 
3075     //printf("\r\ng_u8CGC_UpperBond = %d \n", g_u8CGC_UpperBond);
3076     //printf("\r\ng_u8CGC_LowerBond = %d \n", g_u8CGC_LowerBond);
3077 
3078     //printf("\r\ng_u16Ydark(std) = %d \n", g_u16Ydark);
3079 }
3080 
msDlc_CGC_SetGain(BYTE u8CGain)3081 void msDlc_CGC_SetGain(BYTE u8CGain)
3082 {
3083     msDlc_FunctionEnter();
3084 
3085     if (g_DlcParameters.bCGCCGainCtrl && (REG_ADDR_DLC_C_GAIN != REG_NULL))
3086         msWriteByte(REG_ADDR_DLC_C_GAIN, u8CGain);
3087 
3088     msDlc_FunctionExit();
3089 }
3090 
msDlc_CGC_Handler(void)3091 void msDlc_CGC_Handler(void)
3092 {
3093     BYTE u8TmpVal;
3094 
3095     WORD u16Ytemp, u16Ydiff;
3096 
3097     msDlc_FunctionEnter();
3098 
3099     //printf("\r\n g_u8CGC_UpperBond = %d \n", g_u8CGC_UpperBond);
3100     //printf("\r\n g_u8CGC_LowerBond = %d \n", g_u8CGC_LowerBond);
3101 
3102     u16Ytemp = (DWORD) (Y01_weight*(g_ucTable[0]+g_ucTable[1])+
3103              Y23_weight*(g_ucTable[2]+g_ucTable[3]))/
3104             (Y01_weight+Y23_weight);
3105 
3106     u16Ytemp =  u16Ytemp/2+(DWORD)(Y45_weight*(g_ucTable[4]+g_ucTable[5])+
3107                        Y67_weight*(g_ucTable[6]+g_ucTable[7]))/
3108                       (2*(Y45_weight+Y67_weight));
3109 
3110     //printf("\r\nu16Ytemp       = %d", u16Ytemp);
3111     //printf("\r\ng_u16Ydark_pre = %d", g_u16Ydark_pre);
3112 
3113     if (abs(u16Ytemp-g_u16Ydark_pre)>=Yvar_Th)
3114     {
3115         g_u8TCount++;
3116 
3117      	 if ((u16Ytemp>g_u16Ydark)&&(g_u8TCount>=Timer_CountU))
3118      {
3119              //printf("\r\ng_u8TCountU       = %d", g_u8TCount);
3120          g_u8TCount = 0;
3121 
3122              u16Ydiff=(u16Ytemp-g_u16Ydark);
3123 
3124              if (u16Ydiff>g_DlcParameters.ucCGCYth)
3125              {
3126      	         g_u8Chroma_target = g_u8Chroma_OriGain+(WORD)((u16Ydiff-g_DlcParameters.ucCGCYth)*g_DlcParameters.ucCGCYCslope)/(0x20);
3127 
3128      	         if (g_u8Chroma_target > g_u8CGC_UpperBond)
3129      	             g_u8Chroma_target = g_u8CGC_UpperBond;
3130      	     }
3131      	     else
3132      	         g_u8Chroma_target = g_u8Chroma_OriGain;
3133          }
3134          else if ((u16Ytemp<=g_u16Ydark)&&(g_u8TCount>=Timer_CountD))
3135          {
3136              //printf("\r\ng_u8TCountD       = %d", g_u8TCount);
3137              g_u8TCount = 0;
3138 
3139              u16Ydiff=(g_u16Ydark-u16Ytemp);
3140 
3141              if (u16Ydiff>g_DlcParameters.ucCGCYth)
3142              {
3143      	         g_u8Chroma_target = g_u8Chroma_OriGain-(WORD)((u16Ydiff-g_DlcParameters.ucCGCYth)*g_DlcParameters.ucCGCYCslope)/(0x20);
3144 
3145      	         if (g_u8Chroma_target < g_u8CGC_LowerBond)
3146      	             g_u8Chroma_target = g_u8CGC_LowerBond;
3147      	     }
3148      	     else
3149      	         g_u8Chroma_target = g_u8Chroma_OriGain;
3150          }
3151 
3152          //printf("\r\ng_u8Chroma_target = %d", g_u8Chroma_target);
3153 
3154      	 u8TmpVal = msReadByte(REG_ADDR_DLC_C_GAIN);
3155 
3156      	 if (g_u8Chroma_target>u8TmpVal)
3157      	     msDlc_CGC_SetGain(u8TmpVal+CGain_StepU);
3158      	 else if (g_u8Chroma_target<u8TmpVal)
3159      	     msDlc_CGC_SetGain(u8TmpVal-CGain_StepD);
3160     }
3161 
3162     g_u16Ydark_pre = (DWORD)(31*g_u16Ydark_pre+u16Ytemp)/32;
3163 
3164     msDlc_FunctionExit();
3165 }
3166 
3167 //////////////////////////////////////////////////////////////
3168 // Debug Protocol
3169 //////////////////////////////////////////////////////////////
3170 
3171 // Debug Command Paser
3172 #define URCMD_MST_DLC_TEST_FORCE_PAUSE      0
3173 #define URCMD_MST_DLC_TEST_FORCE_RUN        1
3174 #define URCMD_MST_DLC_TEST_READ             2 // old mode
3175 #define URCMD_MST_DLC_TEST_WRITE_1          3
3176 #define URCMD_MST_DLC_TEST_WRITE_2          4
3177 #define URCMD_MST_DLC_TEST_REINIT           5
3178 #define URCMD_MST_DLC_TEST_READ_BOUNDARY	6
3179 #define URCMD_MST_DLC_TEST_WRITE_BOUNDARY	7
3180 #define URCMD_MST_DLC_TEST_READ_CV1         8
3181 #define URCMD_MST_DLC_TEST_READ_CV2A        9
3182 #define URCMD_MST_DLC_TEST_READ_CV2B        10
3183 #define URCMD_MST_DLC_TEST_WRITE_CV1        11
3184 #define URCMD_MST_DLC_TEST_WRITE_CV2A       12
3185 #define URCMD_MST_DLC_TEST_WRITE_CV2B       13
3186 #define URCMD_MST_DLC_TEST_READ_INFO        14
3187 #define URCMD_MST_DLC_TEST_WRITE_INFO       15
3188 #define URCMD_MST_DLC_TEST_CGC_REINIT       16
3189 #define URCMD_MST_DLC_TEST_READ_Sloplimit   17
3190 #define URCMD_MST_DLC_TEST_WRITE_Sloplimit  18
3191 
msDLC_ParametersTool(BYTE * pCmdBuff)3192 void msDLC_ParametersTool(BYTE *pCmdBuff)
3193 {
3194     BYTE ucTmpLen;
3195 
3196     ucTmpLen = *(pCmdBuff+1); //ucTmpLen
3197 
3198     switch(*(pCmdBuff+3)) //UART_EXT_CMD
3199     {
3200         case URCMD_MST_DLC_TEST_FORCE_PAUSE:
3201 
3202             break;
3203         case URCMD_MST_DLC_TEST_FORCE_RUN:
3204 
3205             break;
3206         case URCMD_MST_DLC_TEST_READ:
3207             {
3208                 BYTE i;
3209                 for (i=0; i<16; i+=2)
3210                 {
3211                     putchar(0xF3);
3212                     putchar(g_DlcParameters.ucLumaCurve[i]);
3213                     putchar(g_DlcParameters.ucLumaCurve[i+1]);
3214                     Delay1ms(150);
3215                 }
3216             }
3217             break;
3218         case URCMD_MST_DLC_TEST_WRITE_1:
3219             {
3220                 BYTE i;
3221               #if 1//( ENABLE_UART_CHECKSUM )
3222                 ucTmpLen--; // remove checksum byte
3223               #endif
3224                 for (i=0; i<(ucTmpLen-4); i++)
3225                     g_DlcParameters.ucLumaCurve[i] = *(pCmdBuff+4 + i);
3226             }
3227             break;
3228         case URCMD_MST_DLC_TEST_WRITE_2:
3229             {
3230                 BYTE i;
3231               #if 1//( ENABLE_UART_CHECKSUM )
3232                 ucTmpLen--; // remove checksum byte
3233               #endif
3234                 for (i=0; i<(ucTmpLen-4); i++)
3235                     g_DlcParameters.ucLumaCurve[8 + i] = *(pCmdBuff+4 + i);
3236             }
3237             break;
3238         case URCMD_MST_DLC_TEST_READ_CV1:
3239             {
3240                 putchar(0xF2);
3241                 putchar(g_DlcParameters.ucLumaCurve[*(pCmdBuff+4)]);
3242             }
3243             break;
3244         case URCMD_MST_DLC_TEST_READ_CV2A:
3245             {
3246                 putchar(0xF2);
3247                 putchar(g_DlcParameters.ucLumaCurve2_a[*(pCmdBuff+4)]);
3248             }
3249             break;
3250         case URCMD_MST_DLC_TEST_READ_CV2B:
3251             {
3252                 putchar(0xF2);
3253                 putchar(g_DlcParameters.ucLumaCurve2_b[*(pCmdBuff+4)]);
3254             }
3255             break;
3256         case URCMD_MST_DLC_TEST_WRITE_CV1:
3257             {
3258                 BYTE i;
3259               #if 1//( ENABLE_UART_CHECKSUM )
3260                 ucTmpLen--; // remove checksum byte
3261               #endif
3262                 for (i=0; i<(ucTmpLen-4-1); i++)
3263                     g_DlcParameters.ucLumaCurve[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
3264             }
3265             break;
3266         case URCMD_MST_DLC_TEST_WRITE_CV2A:
3267             {
3268                 BYTE i;
3269               #if 1//( ENABLE_UART_CHECKSUM )
3270                 ucTmpLen--; // remove checksum byte
3271               #endif
3272                 for (i=0; i<(ucTmpLen-4-1); i++)
3273                     g_DlcParameters.ucLumaCurve2_a[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
3274             }
3275             break;
3276         case URCMD_MST_DLC_TEST_WRITE_CV2B:
3277             {
3278                 BYTE i;
3279               #if 1//( ENABLE_UART_CHECKSUM )
3280                 ucTmpLen--; // remove checksum byte
3281               #endif
3282                 for (i=0; i<(ucTmpLen-4-1); i++)
3283                     g_DlcParameters.ucLumaCurve2_b[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
3284             }
3285             break;
3286         case URCMD_MST_DLC_TEST_READ_INFO:
3287             {
3288                 if (*(pCmdBuff+4) == 1) // DLC curve
3289                 {
3290                     putchar(0xF2);
3291                     putchar(g_ucTable[*(pCmdBuff+5)]);
3292                 }
3293                 else if (*(pCmdBuff+4) == 2) // DLC other
3294                 {
3295                     if (*(pCmdBuff+5) < 64)
3296                     {
3297                         putchar(0xF2);
3298                         if (*(pCmdBuff+5)%2)
3299                             putchar(g_wLumaHistogram32H[*(pCmdBuff+5)/2] & 0xFF);
3300                         else
3301                             putchar(g_wLumaHistogram32H[*(pCmdBuff+5)/2] >> 8);
3302                     }
3303                     else if (*(pCmdBuff+5) == 200)
3304                     {
3305                         WORD wVer;
3306                         wVer = msDlcLibVersionCheck();
3307                         putchar(0xF3);
3308                         putchar(wVer >> 8);
3309                         putchar(wVer & 0xFF);
3310                     }
3311                     else if (*(pCmdBuff+5) == 201)
3312                     {
3313                         putchar(0xF3);
3314                         putchar(g_DlcParameters.uwDlcBleStartPointTH >> 8);
3315                         putchar(g_DlcParameters.uwDlcBleStartPointTH & 0xFF);
3316                     }
3317                     else if (*(pCmdBuff+5) == 202)
3318                     {
3319                         putchar(0xF3);
3320                         putchar(g_DlcParameters.uwDlcBleEndPointTH >> 8);
3321                         putchar(g_DlcParameters.uwDlcBleEndPointTH & 0xFF);
3322                     }
3323                     else if (*(pCmdBuff+5) == 203)
3324                     {
3325                         putchar(0xF3);
3326                         putchar(g_DlcParameters.uwDlcBLESlopPoint_1 >> 8);
3327                         putchar(g_DlcParameters.uwDlcBLESlopPoint_1 & 0xFF);
3328                     }
3329                     else if (*(pCmdBuff+5) == 204)
3330                     {
3331                         putchar(0xF3);
3332                         putchar(g_DlcParameters.uwDlcBLESlopPoint_2 >> 8);
3333                         putchar(g_DlcParameters.uwDlcBLESlopPoint_2 & 0xFF);
3334                     }
3335                     else if (*(pCmdBuff+5) == 205)
3336                     {
3337                         putchar(0xF3);
3338                         putchar(g_DlcParameters.uwDlcBLESlopPoint_3 >> 8);
3339                         putchar(g_DlcParameters.uwDlcBLESlopPoint_3 & 0xFF);
3340                     }
3341                     else if (*(pCmdBuff+5) == 206)
3342                     {
3343                         putchar(0xF3);
3344                         putchar(g_DlcParameters.uwDlcBLESlopPoint_4 >> 8);
3345                         putchar(g_DlcParameters.uwDlcBLESlopPoint_4 & 0xFF);
3346                     }
3347                     else if (*(pCmdBuff+5) == 207)
3348                     {
3349                         putchar(0xF3);
3350                         putchar(g_DlcParameters.uwDlcBLESlopPoint_5 >> 8);
3351                         putchar(g_DlcParameters.uwDlcBLESlopPoint_5 & 0xFF);
3352                     }
3353                     else if (*(pCmdBuff+5) == 208)
3354                     {
3355                         putchar(0xF3);
3356                         putchar(g_DlcParameters.uwDlcDark_BLE_Slop_Min >> 8);
3357                         putchar(g_DlcParameters.uwDlcDark_BLE_Slop_Min & 0xFF);
3358                     }
3359                     else if (*(pCmdBuff+5) >= 64)
3360                     {
3361                         putchar(0xF2);
3362                         if (*(pCmdBuff+5) == 64)
3363                             putchar(g_wLumiAverageTemp & 0xFF);
3364                         else if (*(pCmdBuff+5) == 65)
3365                             putchar(g_wLumiAverageTemp >> 8);
3366                         else if (*(pCmdBuff+5) == 66)
3367                             putchar(g_wLumiTotalCount & 0xFF);
3368                         else if (*(pCmdBuff+5) == 67)
3369                             putchar(g_wLumiTotalCount >> 8);
3370                         else if (*(pCmdBuff+5) == 68)
3371                             putchar(g_DlcParameters.u8_L_L_U);
3372                         else if (*(pCmdBuff+5) == 69)
3373                             putchar(g_DlcParameters.u8_L_L_D);
3374                         else if (*(pCmdBuff+5) == 70)
3375                             putchar(g_DlcParameters.u8_L_H_U);
3376                         else if (*(pCmdBuff+5) == 71)
3377                             putchar(g_DlcParameters.u8_L_H_D);
3378                         else if (*(pCmdBuff+5) == 72)
3379                             putchar(g_DlcParameters.u8_S_L_U);
3380                         else if (*(pCmdBuff+5) == 73)
3381                             putchar(g_DlcParameters.u8_S_L_D);
3382                         else if (*(pCmdBuff+5) == 74)
3383                             putchar(g_DlcParameters.u8_S_H_U);
3384                         else if (*(pCmdBuff+5) == 75)
3385                             putchar(g_DlcParameters.u8_S_H_D);
3386                         else if (*(pCmdBuff+5) == 76)
3387                             putchar(g_ucHistogramMin);
3388                         else if (*(pCmdBuff+5) == 77)
3389                             putchar(g_ucHistogramMax);
3390                         else if (*(pCmdBuff+5) == 78)
3391                             putchar(g_DlcParameters.ucDlcPureImageMode);
3392                         else if (*(pCmdBuff+5) == 79)
3393                             putchar(g_DlcParameters.ucDlcLevelLimit);
3394                         else if (*(pCmdBuff+5) == 80)
3395                             putchar(g_DlcParameters.ucDlcAvgDelta);
3396                         else if (*(pCmdBuff+5) == 81)
3397                             putchar(g_DlcParameters.ucDlcFastAlphaBlending);
3398                         else if (*(pCmdBuff+5) == 82)
3399                             putchar(g_DlcParameters.bEnableBLE);
3400                         else if (*(pCmdBuff+5) == 83)
3401                             putchar(g_DlcParameters.bEnableWLE);
3402                         else if (*(pCmdBuff+5) == 84)
3403                             putchar(g_DlcParameters.ucDlcBLEPoint);
3404                         else if (*(pCmdBuff+5) == 85)
3405                             putchar(g_DlcParameters.ucDlcWLEPoint);
3406                         else if (*(pCmdBuff+5) == 86)
3407                             putchar(g_DlcParameters.ucDlcAvgDeltaStill);
3408                         else if (*(pCmdBuff+5) == 87)
3409                             putchar(g_DlcParameters.ucDlcYAvgThresholdL);
3410                         else if (*(pCmdBuff+5) == 88)
3411                             putchar(g_DlcParameters.ucDlcYAvgThresholdH);
3412                         else if (*(pCmdBuff+5) == 89)
3413                             putchar(g_DlcParameters.ucCGCCGain_offset);
3414                         else if (*(pCmdBuff+5) == 90)
3415                             putchar(g_DlcParameters.ucCGCChroma_GainLimitH);
3416                         else if (*(pCmdBuff+5) == 91)
3417                             putchar(g_DlcParameters.ucCGCChroma_GainLimitL);
3418                         else if (*(pCmdBuff+5) == 92)
3419                             putchar(g_DlcParameters.ucCGCYCslope);
3420                         else if (*(pCmdBuff+5) == 93)
3421                             putchar(g_DlcParameters.ucCGCYth);
3422                         else if (*(pCmdBuff+5) == 94)
3423                             putchar(g_DlcParameters.bCGCCGainCtrl);
3424                         else if (*(pCmdBuff+5) == 95)
3425                             putchar(g_DlcParameters.ucDlcYAvgThresholdM);
3426                         else if (*(pCmdBuff+5) == 96)
3427                             putchar(g_DlcParameters.ucDlcCurveMode);
3428                         else if (*(pCmdBuff+5) == 97)
3429                             putchar(g_DlcParameters.ucDlcCurveModeMixAlpha);
3430                         else if (*(pCmdBuff+5) == 98)
3431                             putchar(g_DlcParameters.ucDlcAlgorithmMode);
3432                         else if (*(pCmdBuff+5) == 99)
3433                             putchar(g_DlcParameters.ucDlcSepPointH);
3434                         else if (*(pCmdBuff+5) == 100)
3435                             putchar(g_DlcParameters.ucDlcSepPointL);
3436                         else if (*(pCmdBuff+5) == 101)
3437                             putchar(g_DlcParameters.ucDlcCurveDiff_L_TH);
3438                         else if (*(pCmdBuff+5) == 102)
3439                             putchar(g_DlcParameters.ucDlcCurveDiff_H_TH);
3440                         else if (*(pCmdBuff+5) == 103)
3441                             putchar(g_DlcParameters.ucDlcCurveDiffCoringTH);
3442                         else if (*(pCmdBuff+5) == 104)
3443                             putchar(g_DlcParameters.ucDlcAlphaBlendingMin);
3444                         else if (*(pCmdBuff+5) == 105)
3445                             putchar(g_DlcParameters.ucDlcAlphaBlendingMax);
3446                         else if (*(pCmdBuff+5) == 106)
3447                             putchar(g_DlcParameters.ucDlcFlicker_alpha);
3448                         else if (*(pCmdBuff+5) == 107)
3449                             putchar(g_DlcParameters.ucDlcYAVG_L_TH);
3450                         else if (*(pCmdBuff+5) == 108)
3451                             putchar(g_DlcParameters.ucDlcYAVG_H_TH);
3452                         else if (*(pCmdBuff+5) == 109)
3453                             putchar(g_DlcParameters.ucDlcDiffBase_L);
3454                         else if (*(pCmdBuff+5) == 110)
3455                             putchar(g_DlcParameters.ucDlcDiffBase_M);
3456                         else if (*(pCmdBuff+5) == 111)
3457                             putchar(g_DlcParameters.ucDlcDiffBase_H);
3458                         else
3459                             putchar(0);
3460                     }
3461                     else
3462                     {
3463                         putchar(0xF2);
3464                         putchar(0);
3465                     }
3466                 }
3467             }
3468             break;
3469         case URCMD_MST_DLC_TEST_WRITE_INFO:
3470             {
3471                 if (*(pCmdBuff+4) == 0)
3472                 {
3473                     g_DlcParameters.u8_L_L_U = *(pCmdBuff+5);
3474                 }
3475                 else if (*(pCmdBuff+4) == 1)
3476                 {
3477                     g_DlcParameters.u8_L_L_D = *(pCmdBuff+5);
3478                 }
3479                 else if (*(pCmdBuff+4) == 2)
3480                 {
3481                     g_DlcParameters.u8_L_H_U = *(pCmdBuff+5);
3482                 }
3483                 else if (*(pCmdBuff+4) == 3)
3484                 {
3485                     g_DlcParameters.u8_L_H_D = *(pCmdBuff+5);
3486                 }
3487                 else if (*(pCmdBuff+4) == 4)
3488                 {
3489                     g_DlcParameters.u8_S_L_U = *(pCmdBuff+5);
3490                 }
3491                 else if (*(pCmdBuff+4) == 5)
3492                 {
3493                     g_DlcParameters.u8_S_L_D = *(pCmdBuff+5);
3494                 }
3495                 else if (*(pCmdBuff+4) == 6)
3496                 {
3497                     g_DlcParameters.u8_S_H_U = *(pCmdBuff+5);
3498                 }
3499                 else if (*(pCmdBuff+4) == 7)
3500                 {
3501                     g_DlcParameters.u8_S_H_D = *(pCmdBuff+5);
3502                 }
3503                 else if (*(pCmdBuff+4) == 8)
3504                 {
3505                     g_DlcParameters.ucDlcPureImageMode = *(pCmdBuff+5);
3506                 }
3507                 else if (*(pCmdBuff+4) == 9)
3508                 {
3509                     g_DlcParameters.ucDlcLevelLimit = *(pCmdBuff+5);
3510                 }
3511                 else if (*(pCmdBuff+4) == 10)
3512                 {
3513                     g_DlcParameters.ucDlcAvgDelta = *(pCmdBuff+5);
3514                 }
3515                 else if (*(pCmdBuff+4) == 11)
3516                 {
3517                     g_DlcParameters.ucDlcFastAlphaBlending = *(pCmdBuff+5);
3518                 }
3519                 else if (*(pCmdBuff+4) == 12)
3520                 {
3521                     g_DlcParameters.bEnableBLE = *(pCmdBuff+5);
3522                 }
3523                 else if (*(pCmdBuff+4) == 13)
3524                 {
3525                     g_DlcParameters.bEnableWLE = *(pCmdBuff+5);
3526                 }
3527                 else if (*(pCmdBuff+4) == 14)
3528                 {
3529                     g_DlcParameters.ucDlcBLEPoint = *(pCmdBuff+5);
3530                 }
3531                 else if (*(pCmdBuff+4) == 15)
3532                 {
3533                     g_DlcParameters.ucDlcWLEPoint = *(pCmdBuff+5);
3534                 }
3535                 else if (*(pCmdBuff+4) == 16)
3536                 {
3537                     g_DlcParameters.ucDlcAvgDeltaStill= *(pCmdBuff+5);
3538                 }
3539                 else if (*(pCmdBuff+4) == 17)
3540                 {
3541                     g_DlcParameters.ucDlcYAvgThresholdL = *(pCmdBuff+5);
3542                 }
3543                 else if (*(pCmdBuff+4) == 18)
3544                 {
3545                     g_DlcParameters.ucDlcYAvgThresholdH = *(pCmdBuff+5);
3546                 }
3547                 else if (*(pCmdBuff+4) == 19)
3548                 {
3549                     g_DlcParameters.ucCGCCGain_offset = *(pCmdBuff+5);
3550                 }
3551                 else if (*(pCmdBuff+4) == 20)
3552                 {
3553                     g_DlcParameters.ucCGCChroma_GainLimitH = *(pCmdBuff+5);
3554                 }
3555                 else if (*(pCmdBuff+4) == 21)
3556                 {
3557                     g_DlcParameters.ucCGCChroma_GainLimitL = *(pCmdBuff+5);
3558                 }
3559                 else if (*(pCmdBuff+4) == 22)
3560                 {
3561                     g_DlcParameters.ucCGCYCslope = *(pCmdBuff+5);
3562                 }
3563                 else if (*(pCmdBuff+4) == 23)
3564                 {
3565                     g_DlcParameters.ucCGCYth = *(pCmdBuff+5);
3566                 }
3567                 else if (*(pCmdBuff+4) == 24)
3568                 {
3569                     g_DlcParameters.bCGCCGainCtrl = *(pCmdBuff+5);
3570                 }
3571                 else if (*(pCmdBuff+4) == 25)
3572                 {
3573                     g_DlcParameters.ucDlcYAvgThresholdM = *(pCmdBuff+5);
3574                 }
3575                 else if (*(pCmdBuff+4) == 26)
3576                 {
3577                     g_DlcParameters.ucDlcCurveMode = *(pCmdBuff+5);
3578                 }
3579                 else if (*(pCmdBuff+4) == 27)
3580                 {
3581                     g_DlcParameters.ucDlcCurveModeMixAlpha = *(pCmdBuff+5);
3582                 }
3583                 else if (*(pCmdBuff+4) == 28)
3584                 {
3585                     g_DlcParameters.ucDlcAlgorithmMode = *(pCmdBuff+5);
3586                 }
3587                 else if (*(pCmdBuff+4) == 29)
3588                 {
3589                     g_DlcParameters.ucDlcSepPointH = *(pCmdBuff+5);
3590                 }
3591                 else if (*(pCmdBuff+4) == 30)
3592                 {
3593                     g_DlcParameters.ucDlcSepPointL = *(pCmdBuff+5);
3594                 }
3595                 else if (*(pCmdBuff+4) == 31)
3596                 {
3597                     g_DlcParameters.uwDlcBleStartPointTH = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3598                 }
3599                 else if (*(pCmdBuff+4) == 32)
3600                 {
3601                     g_DlcParameters.uwDlcBleEndPointTH = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3602                 }
3603                 else if (*(pCmdBuff+4) == 33)
3604                 {
3605                     g_DlcParameters.ucDlcCurveDiff_L_TH = *(pCmdBuff+5);
3606                 }
3607                 else if (*(pCmdBuff+4) == 34)
3608                 {
3609                     g_DlcParameters.ucDlcCurveDiff_H_TH = *(pCmdBuff+5);
3610                 }
3611                 else if (*(pCmdBuff+4) == 35)
3612                 {
3613                     g_DlcParameters.uwDlcBLESlopPoint_1 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3614                 }
3615                 else if (*(pCmdBuff+4) == 36)
3616                 {
3617                     g_DlcParameters.uwDlcBLESlopPoint_2 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3618                 }
3619                 else if (*(pCmdBuff+4) == 37)
3620                 {
3621                     g_DlcParameters.uwDlcBLESlopPoint_3 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3622                 }
3623                 else if (*(pCmdBuff+4) == 38)
3624                 {
3625                     g_DlcParameters.uwDlcBLESlopPoint_4 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3626                 }
3627                 else if (*(pCmdBuff+4) == 39)
3628                 {
3629                     g_DlcParameters.uwDlcBLESlopPoint_5 = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3630                 }
3631                 else if (*(pCmdBuff+4) == 40)
3632                 {
3633                     g_DlcParameters.uwDlcDark_BLE_Slop_Min = (WORD)((*(pCmdBuff+6)<<8)+(*(pCmdBuff+5)));
3634                 }
3635                 else if (*(pCmdBuff+4) == 41)
3636                 {
3637                     g_DlcParameters.ucDlcCurveDiffCoringTH = *(pCmdBuff+5);
3638                 }
3639                 else if (*(pCmdBuff+4) == 42)
3640                 {
3641                     g_DlcParameters.ucDlcAlphaBlendingMin = *(pCmdBuff+5);
3642                 }
3643                 else if (*(pCmdBuff+4) == 43)
3644                 {
3645                     g_DlcParameters.ucDlcAlphaBlendingMax = *(pCmdBuff+5);
3646                 }
3647                 else if (*(pCmdBuff+4) == 44)
3648                 {
3649                     g_DlcParameters.ucDlcFlicker_alpha= *(pCmdBuff+5);
3650                 }
3651                 else if (*(pCmdBuff+4) == 45)
3652                 {
3653                     g_DlcParameters.ucDlcYAVG_L_TH = *(pCmdBuff+5);
3654                 }
3655                 else if (*(pCmdBuff+4) == 46)
3656                 {
3657                     g_DlcParameters.ucDlcYAVG_H_TH = *(pCmdBuff+5);
3658                 }
3659                 else if (*(pCmdBuff+4) == 47)
3660                 {
3661                     g_DlcParameters.ucDlcDiffBase_L= *(pCmdBuff+5);
3662                 }
3663                 else if (*(pCmdBuff+4) == 48)
3664                 {
3665                     g_DlcParameters.ucDlcDiffBase_M = *(pCmdBuff+5);
3666                 }
3667                 else if (*(pCmdBuff+4) == 49)
3668                 {
3669                     g_DlcParameters.ucDlcDiffBase_H = *(pCmdBuff+5);
3670                 }
3671                 msDlcSpeedupTrigger(16);
3672             }
3673             break;
3674         case URCMD_MST_DLC_TEST_REINIT:
3675             msDlc_AP_ReinitWithoutPara();
3676             msDlcOnOff( _ENABLE, MAIN_WINDOW);
3677             break;
3678         case URCMD_MST_DLC_TEST_CGC_REINIT:
3679             msDlc_CGC_ReInit();
3680             break;
3681         case URCMD_MST_DLC_TEST_READ_Sloplimit:
3682             {
3683                 putchar(0xF2);
3684                 putchar(g_DlcParameters.ucDlcHistogramLimitCurve[*(pCmdBuff+4)]);
3685             }
3686             break;
3687         case URCMD_MST_DLC_TEST_WRITE_Sloplimit:
3688             {
3689                 BYTE i;
3690               #if 1//( ENABLE_UART_CHECKSUM )
3691                 ucTmpLen--; // remove checksum byte
3692               #endif
3693                 for (i=0; i<(ucTmpLen-4-1); i++)
3694                     g_DlcParameters.ucDlcHistogramLimitCurve[*(pCmdBuff+4) + i] = *(pCmdBuff+5 + i);
3695             }
3696             break;
3697     }
3698 }
3699 
3700 //////////////////////////////////////////////////////////////
3701 //
3702 //////////////////////////////////////////////////////////////
msDlcLibVersionCheck(void)3703 WORD msDlcLibVersionCheck(void)
3704 {
3705     return DLC_LIB_VERSION;
3706 }
3707 
msDlcCGCLibVersionCheck(void)3708 WORD msDlcCGCLibVersionCheck(void)
3709 {
3710     return DLC_CGC_LIB_VERSION;
3711 }
3712 
3713 #undef _MSDLC_C_
3714