summaryrefslogtreecommitdiff
path: root/EDK/MiniSetup/TseAdvanced/jpeg6.c
blob: 4ba65346bf1a8854d191df3e4adca2d584f0d868 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
//**                                                             **//
//**         (C)Copyright 2010, American Megatrends, Inc.        **//
//**                                                             **//
//**                     All Rights Reserved.                    **//
//**                                                             **//
//**   5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093  **//
//**                                                             **//
//**                     Phone (770)-246-8600                    **//
//**                                                             **//
//*****************************************************************//
//*****************************************************************//
//*****************************************************************//
// $Archive: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/TseAdvanced/jpeg6.c $
//
// $Author: Premkumara $
//
// $Revision: 11 $
//
// $Date: 5/21/13 1:29a $
//
//*****************************************************************//
//*****************************************************************//
// Revision History
// ----------------
// $Log: /Alaska/SOURCE/Modules/AMITSE2_0/AMITSE/TseAdvanced/jpeg6.c $
// 
// 11    5/21/13 1:29a Premkumara
// [TAG]  		EIP114841 
// [Category]  	Bug Fix
// [Severity]  	Important
// [Symptom]  	When use the broken JPG file as user logo, system hangs up
// CP:0xb4
// [RootCause]  	When corrupted Jpeg image has no proper AC co-efficent
// value in Huffman table then dc_value_length is incremented beyond its
// range value.
// [Solution]  	Checked dc_value_length boundary value.
// [Files]  		Jpeg6.c
// 
// 10    10/18/12 5:53a Arunsb
// Updated for 2.16.1235 QA submission
// 
// 14    10/10/12 12:35p Arunsb
// Synched the source for v2.16.1232, backup with Aptio
// 
// 9     4/26/12 3:25a Arunsb
// [TAG]  		EIP78033
// [Category]  	Bug Fix
// [Severity]  	Normal
// [Symptom]  	YUV122 sampling JPEG image not displayed
// [RootCause]  	No support for  YUV122 sampling
// [Solution]  	Support added for YUV122 sampling
// [Files]  		jpeg6.c
// 
// 8     5/03/11 1:42p Madhans
// [TAG]  		EIP59177 
// [Category]  	Improvement
// [Description]  	Support for JPEG with RSI markers. Fix to support logo
// size that bigger then Screen resolution.
// [Files]  		Logo.c
// Jpeg6.c
// commonHelper.c
// 
// 7     4/29/11 4:44p Madhans
// [TAG]  		EIP59177 
// [Category]  	Improvement
// [Description]  	Support for JPEG with RSI markers. Fix to support logo
// size that bigger then Screen resolution.
// [Files]  		Logo.c
// Jpeg6.c
// commonHelper.c
// 
// 6     9/27/10 7:47a Mallikarjunanv
// EIP 40555 : To avoid Compilation issues with Fareast Windows.
// 
// 5     2/19/10 1:02p Madhans
// Updated for TSE 2.01. Refer Changelog.log for File change history.
// 
// 8     2/19/10 8:11a Mallikarjunanv
// updated year in copyright message
// 
// 7     1/09/10 4:43a Mallikarjunanv
// Updated TSE2.01 Release sources with coding standards
// 
// 6     8/13/09 12:16p Blaines
// Move Image support to binary module
// 
// 5     7/08/09 3:34p Madhans
// Coding Standards.
// 
// 4     6/29/09 12:46p Blaines
// EIP #22305 Fix for improper JPEG Image size calculation logic 
// 
// 3     6/23/09 7:09p Blaines
// Coding standard update, 
// Remove spaces from file header to allow proper chm function list
// creation.
// 
// 2     6/12/09 7:43p Presannar
// Initial implementation of coding standards for AMITSE2.0
// 
// 1     6/04/09 8:05p Madhans
// 
// 1     4/28/09 11:16p Madhans
// Tse 2.0 Code complete Checkin.
// 
// 1     4/28/09 10:27p Madhans
// Tse 2.0 Code complete Checkin.
// 
// 1     9/26/06 4:03p Madhans
// JpegC Implimentation
// 
// 5     9/22/06 3:23a Shirinmd
// Added Support for JPEG Images having 1 Huffman Table and 1 Quantization
// table
// 
// 4     9/15/06 9:54a Shirinmd
// Added the check to find the type of Huffman table
// 
// 3     8/03/06 7:16a Shirinmd
// Removed initialization of global buffers in InitGlobals function to
// reduce code size
// 
// 2     8/02/06 8:55a Shirinmd
// Made changes to reduce the code size
// 
// 1     8/01/06 8:03a Shirinmd
// Files for JPEG decoding (C version)
//
//*****************************************************************//
//*****************************************************************//
//<AMI_FHDR_START>
//
// Name:		jpeg6.c
//
// Description:	Support for JPEG decoding
//
//<AMI_FHDR_END>
//*****************************************************************//

#include "Mydefs.h"
#include "Jpeg6.h"


//Globals_START

UINTN unBitStreamOFFSET;
BYTE bNumBitsRead;
BYTE btempBitStreamByte;

UINTN unSOF0;
WORD wDCLumtable[13*3];
WORD wACLumtable[170*3];
WORD wDCChrtable[13*3];
WORD wACChrtable[170*3];

WORD space_image[256*4];
AMI_TRUECOLOR_PIXEL_JPEG * ScreenBufferJPEG = (AMI_TRUECOLOR_PIXEL_JPEG *)NULL;

WORD image_Y[64];
WORD image_Y1[64];
WORD image_Y2[64];
WORD image_Y3[64];
WORD image_Cr[64];
WORD image_Cb[64];
UINTN QT[2];
UINTN HT[4];
BYTE bQTLumTable[64];
BYTE bQTChrTable[64];
WORD jpeg_zigzag_order[] = {								\
							0,  1,  5,  6, 14, 15, 27, 28,	\
							2,  4,  7, 13, 16, 26, 29, 42,	\
							3,  8, 12, 17, 25, 30, 41, 43,	\
							9, 11, 18, 24, 31, 40, 44, 53,	\
							10, 19, 23, 32, 39, 45, 52, 54,	\
							20, 22, 33, 38, 46, 51, 55, 60,	\
							21, 34, 37, 47, 50, 56, 59, 61,	\
							35, 36, 48, 49, 57, 58, 62, 63	\
							};

WORD jpeg_zigzag_order_temp[64];


WORD diff_Y	;
WORD diff_Cr;
WORD diff_Cb;
WORD gRsi = 0;
UINT32 g_un32BufferWidth ;
UINT32 g_un32BufferHeight;

//Globals_End


//Function Declarations
void BuildHuffmanCodeTable(WORD * pwHuffCodeTable ,BYTE * pbySrcHT);
void HuffmanDecoding(WORD * pwDCCoeffTable, WORD * pwACCoeffTable, WORD * pwImage);
void GetDCValue(WORD * pwHuffmanTable, WORD * pwValue, WORD * pwLengthDC_RunSizeAC);
BOOL SearchHuffmanCode(WORD * pwSearchTablePos, WORD * pwHuffmanCodeValue, WORD wDc_value_length);
BOOL GetEncodedBit();
void ReadJPEGImage(WORD * pwDCCoeffTable, WORD * pwACCoeffTable, WORD * pwImage, WORD * diff, BYTE * pbySrcQT, int nLumFlag);
void ReadJPEGImage_YUV(DWORD * pdwBuffer, WORD wSampling);
void OutputJPEGImage_YUV(WORD wXPos, WORD wYPos, WORD wXLength, WORD wYLength, WORD	wSampling);
void Output8x8Image(WORD wLeft, WORD wTop, BYTE * pspace_image);
BOOL InitJpegParams(BYTE * pbyImage);
DWORD ConvertYCbCrToRGB(short Y, short Cb, short Cr);
void InvertDCT(WORD * pwSource);
void InvertDCT_xy(WORD * pwSource, WORD wVal);
void GetTables(BYTE bNumQT, BYTE bNumHT);


//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InitGlobals
//
// Description:	Initializes required global variables
//
// Input:		None
//
// Output:		None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void InitGlobals(){
	diff_Y		= 0;
	diff_Cr	= 0;
	diff_Cb	= 0;
	bNumBitsRead = 8;
	btempBitStreamByte = 0;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	DecodeJPEG
//
// Description:	Decode the JPEG image into the given buffer at given position, 
//					subject to the limit of the Buffer Width and Height
//
// Input:		WORD wXPos - X Start Postion with in the Buffer 
//									where the image need to be decoded
//					WORD wYPos - Y Start Postion with in the Buffer 
//									where the image need to be decoded
//					BYTE * pbyJPEGInputData - Pointer that gives the memory location 
//											  where the Input data is available
//					BYTE * pbyJPEGOutputData - Pointer that gives the pointer 
//											   for the output buffer
//					UINT32 un32BufferWidth - Width of the output Buffer
//					UINT32 un32BufferHeight	- Height of the output Buffer
//
// Output:		BOOL - TRUE/FALSE Indicate whether the Decoding was successful
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOL DecodeJPEG(WORD wXPos, WORD wYPos, BYTE * pbyJPEGInputData, BYTE * pbyJPEGOutputData, UINT32 un32BufferWidth, UINT32 un32BufferHeight){
	WORD wX_Length=0, wY_Length=0; 
	ScreenBufferJPEG = (AMI_TRUECOLOR_PIXEL_JPEG *)pbyJPEGOutputData;
	g_un32BufferWidth	= un32BufferWidth ; 
	g_un32BufferHeight	= un32BufferHeight;

    gRsi = 0;
	InitGlobals();

	if(InitJpegParams(pbyJPEGInputData)){
		wX_Length = (*(WORD*)((BYTE *)unSOF0+5)<<8)|*((BYTE *)unSOF0+6);
		wY_Length = (*(WORD*)((BYTE *)unSOF0+3)<<8)|*((BYTE *)unSOF0+4);
		 
if((0x11==(*(BYTE *)(unSOF0+3+4+1+1+3)))&&(0x11==((*(BYTE *)(unSOF0+3+4+1+1+3+3)))))
		{
#if MKF_JPEG_YUV111_SUPPORT
			if(0x11==(*(BYTE *)(unSOF0+3+4+1+1)))
				OutputJPEGImage_YUV(wXPos, wYPos, wX_Length, wY_Length, 0x111);
			else
#endif
				if(0x22==(*(BYTE *)(unSOF0+3+4+1+1)))
					OutputJPEGImage_YUV(wXPos, wYPos, wX_Length, wY_Length, 0x122);
		}
		return TRUE;
	}
	else
		return FALSE;
	
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetJPEGDimensions
//
// Description:	Get the dimensions (Width and Height) for the JPEG image
//
// Input:	BYTE * pbyJPEGInputData - Pointer that gives the memory 
//									location where the Input data is available
//					UINT32 * pun32Width - Pointer to the UINT32 variable that 
//									will recieve the Width of the Image
//					UINT32 * pun32Height - Pointer to the UINT32 variable that 
//									will recieve the Height of the Image
//
// Output:	BOOL - TRUE/FALSE.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOL GetJPEGDimensions(BYTE * pbyJPEGInputData, UINT32 * pun32Width, UINT32 * pun32Height){
	if(InitJpegParams(pbyJPEGInputData)){
		*pun32Width =  (WORD)((*(WORD*)((BYTE *)unSOF0+5)<<8)|*((BYTE *)unSOF0+6));
		*pun32Height=  (WORD)((*(WORD*)((BYTE *)unSOF0+3)<<8)|*((BYTE *)unSOF0+4));
		return TRUE;
	}
	else return FALSE;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	BuildHuffmanCodeTable
//
// Description:	Builds the Huffman codes for the particular JPEG Huffman table.
//					By incrementing the offset of the Huffman table by 3, 
//					the offset of the actual data is obtained. The first 
//					16 bytes represent the no: of code words for a paricular 
//					bit-size. Having the bit-size and the no: of code words 
//					for each bit-size, the Huffman code table is built.
//
// Input:	WORD * pwHuffCodeTable - Pointer to the memory location 
//							that contains the Huffman codes for the particular JPEG Huffman table.
//					BYTE * pbySrcHT - Pointer to the memory location that contains 
//							the offset of the JPEG Huffman Table.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void BuildHuffmanCodeTable(WORD * pwHuffCodeTable ,BYTE * pbySrcHT){

	BYTE * pbySrcHTCopy = NULL;
	int nCounter=0;
	int nOffSet=0;			
	BYTE wCodeSize = 1;		
	WORD wCodeWord = 0;		

	pbySrcHT			+= 1;	
	pbySrcHTCopy = pbySrcHT;
	do{
		for(nCounter=*pbySrcHT;nCounter>0; nCounter--){
			pwHuffCodeTable[nOffSet++]	= wCodeSize;
			pwHuffCodeTable[nOffSet++]	= wCodeWord++;
			pwHuffCodeTable[nOffSet++] = *(pbySrcHTCopy+16);
			pbySrcHTCopy++;
		}
		wCodeWord<<=1;
		pbySrcHT++; 
		wCodeSize++;
	}while(wCodeSize<=16);
	pwHuffCodeTable[nOffSet]	= 17;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ReadJPEGImage
//
// Description:	Reads the 8x8 matrix data for each of Y_0,Y_1,Y_2,Y_3,Cb and Cr. 
//					The final output will be image data after inverse DCT is applied.
//
// Input:	WORD * pwDCCoeffTable - DC Luminance/ Chrominance Huffman Table.
//					WORD * pwACCoeffTable - AC Luminance/ Chrominance Huffman Table.
//					WORD * pwImage - Pointer to the 8x8 matrix data for each of Y_0,Y_1,Y_2,Y_3,Cb and Cr.
//					WORD * pwDiff  - Pointer to the variable that hold the differences of the DC coefficients. 
//						Adds the difference to the Prev Dc coefficient value to get the present DC coefficient value.
//					BYTE * pbySrcQT - Pointer to the Quantization table.
//					int nLumFlag - Flag if it is set, indicates that the image component is luminance.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void ReadJPEGImage(WORD * pwDCCoeffTable, WORD * pwACCoeffTable, WORD * pwImage, WORD * diff, BYTE * pbySrcQT, int nLumFlag)
{
	int nCounter;
	HuffmanDecoding(pwDCCoeffTable, pwACCoeffTable, pwImage);
	*diff = *diff + pwImage[0]; 
	pwImage[0] = *diff;
	
	//DeQuantization
	for(nCounter=0; nCounter<64; nCounter++)
	{
		pwImage[nCounter] = pwImage[nCounter] * (WORD)pbySrcQT[nCounter]; 
	}
	
	//InvertZigZagOrder
	for(nCounter=0; nCounter<64; nCounter++)
	{
		jpeg_zigzag_order_temp[nCounter] = *((WORD *)((BYTE *)pwImage+(jpeg_zigzag_order[nCounter]<<1)));
	}
	memcpy(pwImage,jpeg_zigzag_order_temp, 128);
	InvertDCT(pwImage);

	if(nLumFlag)
	{
	//Add128
	for(nCounter=0; nCounter<64; nCounter++)
		pwImage[nCounter] +=128; 
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ReadJPEGImage_YUV
//
// Description:	Reads the 8x8 matrix data for each of Y_0,Y_1,Y_2,Y_3,Cb and Cr. 
//					The final output will be RGB pixel data.
//
// Input:	DWORD * pdwBuffer - RGB format of image data.
//					WORD wSampling - Value tells whether image is YUV111(luminance 
//						as well as chrominance is taken for every pixel) or 
//						YUV122(luminance is taken for every pixel while chrominance 
//						is taken as medium value for 2x2 block of pixels).
//						Since the sampling factor for Luminance is 2, all the pixel 
//						values are stored, where as for Cb and Cr, only the average 
//						of 2x2 pixels is stored.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void ReadJPEGImage_YUV(DWORD * pdwBuffer, WORD wSampling){
	int nCounter = 0;
	int nCbCrCounter = 0;					
	ReadJPEGImage(wDCLumtable, wACLumtable, image_Y, &diff_Y, bQTLumTable, 1);
	
	if(0x122 ==wSampling)
	{
		ReadJPEGImage(wDCLumtable, wACLumtable, image_Y1,&diff_Y, bQTLumTable, 1);
		ReadJPEGImage(wDCLumtable, wACLumtable, image_Y2,&diff_Y, bQTLumTable, 1);
		ReadJPEGImage(wDCLumtable, wACLumtable, image_Y3,&diff_Y, bQTLumTable, 1);
	}

	ReadJPEGImage(wDCChrtable, wACChrtable, image_Cr,&diff_Cr, bQTChrTable, 0);
	ReadJPEGImage(wDCChrtable, wACChrtable, image_Cb,&diff_Cb, bQTChrTable, 0);

	for(nCounter=0; nCounter< 64; nCounter++){
#if MKF_JPEG_YUV111_SUPPORT
		if(0x111 ==wSampling)
		pdwBuffer[nCounter] = ConvertYCbCrToRGB(image_Y[nCounter], image_Cr[nCounter], image_Cb[nCounter]);
		else
#endif
		if(0x122 ==wSampling)
		{
			nCbCrCounter = (nCounter&0xfff7)/2;
			pdwBuffer[nCounter]		= ConvertYCbCrToRGB(image_Y[nCounter], image_Cr[nCbCrCounter], image_Cb[nCbCrCounter]);
			pdwBuffer[64+nCounter]	= ConvertYCbCrToRGB(image_Y1[nCounter], image_Cr[nCbCrCounter + 4], image_Cb[nCbCrCounter + 4]);
			pdwBuffer[128+nCounter] = ConvertYCbCrToRGB(image_Y2[nCounter], image_Cr[nCbCrCounter + 32], image_Cb[nCbCrCounter + 32]);
			pdwBuffer[192+nCounter] = ConvertYCbCrToRGB(image_Y3[nCounter], image_Cr[nCbCrCounter + 32 + 4], image_Cb[nCbCrCounter + 32 + 4]);
		}
	}

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	HuffmanDecoding
//
// Description:	This routine decodes the AC and DC coefficients using the 
//					Huffman tables stored in the JPEG image.
//
// Input:	WORD * pwDCCoeffTable - DC Luminance/ Chrominance Huffman Table.
//					WORD * pwACCoeffTable - AC Luminance/ Chrominance Huffman Table.
//					WORD * pwImage -Pointer to the memory location that contains the decoded data.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void HuffmanDecoding(WORD * pwDCCoeffTable, WORD * pwACCoeffTable, WORD * pwImage)
{
	int nBlockCount=0;

	WORD wLengthDC_RunSizeAC	= 0;
	WORD wCoeffVal		= 0;

	memset(pwImage,(64*sizeof(WORD)),0);

	GetDCValue(pwDCCoeffTable, &wCoeffVal, &wLengthDC_RunSizeAC);
	pwImage[nBlockCount++] = wCoeffVal;	// DC coefficient Value
	
	for(;nBlockCount<64;nBlockCount++)
	{
			GetDCValue(pwACCoeffTable, &wCoeffVal, &wLengthDC_RunSizeAC);
			if(0==wLengthDC_RunSizeAC){
				break;
			}
		wLengthDC_RunSizeAC&=0x0F0;
		wLengthDC_RunSizeAC>>=4;

		for(;wLengthDC_RunSizeAC>0;wLengthDC_RunSizeAC--){
			pwImage[nBlockCount++] = 0;
		}

		pwImage[nBlockCount] = wCoeffVal;	// AC coefficient Value
	}

	return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetDCValue
//
// Description:	This routine returns the differences(DCCoefficientpresent - 
//					DCCoefficientprev) for the DC coefficient. For AC coefficients, 
//					it returns the actual AC coefficient value and Run/Size.
//
// Input:	WORD * pwHuffmanTable - Pointer to the huffman table ?DC Luminance, 
//						DC Chrominance, AC Luminance, AC Chrominance.
//					WORD * pwValue - Code Word (Actual Bit representation).
//					WORD * pwLengthDC_RunSizeAC - Indicates the no: of zeroes.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void GetDCValue(WORD * pwHuffmanTable, WORD * pwValue, WORD * pwLengthDC_RunSizeAC)
{
	WORD dc_value_length=0;
	WORD wCoeffLen=0; 
	DWORD dwCoeffVal=0;
	int i=0;

	while(1)
	{
		wCoeffLen<<=1;
		if(GetEncodedBit())
			wCoeffLen|=0x1;

		dc_value_length++;
		
		if( 0xffff == dc_value_length ) //EIP-114841 Corrupted Jpeg
			return;

		if(SearchHuffmanCode(pwHuffmanTable, (WORD*)&wCoeffLen,dc_value_length)){
			break;
		}
	};


	*pwLengthDC_RunSizeAC	= wCoeffLen;
		
	wCoeffLen&=0x0f;
	
	for(i=0;i<wCoeffLen;i++)
	{
		dwCoeffVal<<=1;
		if(GetEncodedBit())
			dwCoeffVal|=0x1;
	}


	if(0!=wCoeffLen--)
	{
		if(!( dwCoeffVal & (0x1<<(wCoeffLen%16)) )   )
		{
			wCoeffLen = (14+16) - wCoeffLen;
	
			dwCoeffVal=(dwCoeffVal<<(wCoeffLen&0xff)) | 0x80000000;
			
			*((signed int *)(&dwCoeffVal))	>>= (wCoeffLen&0xff);
			dwCoeffVal++;
		}
	}
		
	*pwValue				= (WORD)dwCoeffVal;
	return;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	SearchHuffmanCode
//
// Description:	Searches the Huffman code of the DC/AC Luminance/Chrominace 
//					values in Huffman code tables and find the appropriate HuffmanCodeValue.
//
// Input:	WORD * pwSearchTablePos - Pointer to the huffman table ?DC Luminance, 
//						DC Chrominance, AC Luminance, AC Chrominance.
//					WORD * pwHuffmanCodeValue - Pointer to the variable that contains 
//						the Huffman code value.
//					WORD wDc_value_length - No: of bits in the Huffman code.
//
// Output:	BOOL - TRUE/FALSE.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOL SearchHuffmanCode(WORD * pwSearchTablePos, WORD * pwHuffmanCodeValue, WORD wDc_value_length)
{
	BOOL bReturn = FALSE;

	while(wDc_value_length>=(*pwSearchTablePos))
	{
		if(wDc_value_length==(*pwSearchTablePos))
		{
			if(*pwHuffmanCodeValue==*(pwSearchTablePos+1))
			{ 
				*pwHuffmanCodeValue=*(pwSearchTablePos+2);
				bReturn = TRUE;
				break;
			}
		}
		pwSearchTablePos+=3;
	}
	return bReturn;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetEncodedBit
//
// Description:	Reads bits from the stream and returns whether the carry 
//					flag is set or not.
//
// Input:	None
//
// Output:	BOOL - TRUE/FALSE depending on whether the Carry flag is set or not.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOL GetEncodedBit()
{	
	
	UINTN unBitStreamOFFSETCopy;
	BYTE bStreamData;

	unBitStreamOFFSETCopy = unBitStreamOFFSET;
	if(8==bNumBitsRead){
		bStreamData=*(BYTE *)(unBitStreamOFFSETCopy++);
		if(0xff==bStreamData){
			if(0!=(*(BYTE *)(unBitStreamOFFSETCopy++))){
					bStreamData=*(BYTE *)(unBitStreamOFFSETCopy++);
				}
		}
		unBitStreamOFFSET = unBitStreamOFFSETCopy;
		btempBitStreamByte = bStreamData;
		bNumBitsRead=0;
	}
	bNumBitsRead++;
	bStreamData					= btempBitStreamByte;

	if( (bStreamData&(0x1<<(8-bNumBitsRead))) ){
		return TRUE;
	}
	else{
		return FALSE;
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	OutputJPEGImage_YUV
//
// Description:	This functions writes to the Global Screen Buffer by calling 
//					Output8x8Image function.
//
// Input:	WORD wX_position - X Start Position within the output buffer 
//						from where the image needs to be decoded.
//					WORD wY_position - Y Start Position within the output buffer 
//						from where the image needs to be decoded.
//					WORD wX_length - Width of the image.
//					WORD wY_length - Height of the image.
//					WORD wSampling - Value tells whether image is YUV111(luminance 
//						as well as chrominance is taken for every pixel) or YUV122
//						(luminance is taken for every pixel while chrominance is 
//						taken as medium value for 2x2 block of pixels).
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void OutputJPEGImage_YUV(WORD wX_position, WORD wY_position, WORD wX_length, WORD wY_length, WORD	wSampling)
{
	WORD Temp_X_Pos= wX_position;
	WORD Temp_X_Len= wX_length;
	UINTN BlkCount=0;

	if(wSampling==0x122)
	{
		wY_length+= 0x0f;
		wY_length >>=4;
	}
#if MKF_JPEG_YUV111_SUPPORT
	else if(wSampling==0x111)
	{
		wY_length+= 0x07;
		wY_length >>=3;
	}
#endif	
		for(;wY_length>0;wY_length--)
		{
			wX_position			= Temp_X_Pos;
			wX_length			= Temp_X_Len; 

			if(wSampling==0x122)
			{
				wX_length+= 0x0f;
				wX_length >>=4;
			
			for(;wX_length>0;wX_length--)
			{
//				memset(space_image,sizeof(space_image),0);
				{
					ReadJPEGImage_YUV((DWORD *)space_image,wSampling);
					Output8x8Image(wX_position, wY_position, (BYTE *)space_image);
					Output8x8Image(wX_position+8, wY_position, (BYTE *)space_image+256);
					Output8x8Image(wX_position, wY_position+8, (BYTE *)space_image+(256*2));
					Output8x8Image(wX_position+8, wY_position+8, (BYTE *)space_image+(256*3));
					wX_position+=16;
					BlkCount++;
                    // if Restart Interval valid Support for YUV122
					if(gRsi && ((BlkCount%gRsi) == 0))
					{   
                        UINT8 *jpgScan = (UINT8*)unBitStreamOFFSET;
                        // It is going to be 0xFFD0 .. 0xFFD7
                        if((jpgScan[0] == 0xFF) && ((jpgScan[1]& 0xF8 ) == 0xD0))
                        {
                            // Restart the scan params
 							InitGlobals();
                            // Skip the Rsi maraker
							unBitStreamOFFSET+=2;
                        }
					}
				}
			}
			wY_position+=16;
			}
#if MKF_JPEG_YUV111_SUPPORT
			else if(wSampling==0x111)
			{
				wX_length+= 0x07;
				wX_length >>=3;
				for(;wX_length>0;wX_length--)
				{
						//memset(space_image,sizeof(space_image),wSampling);
						ReadJPEGImage_YUV((DWORD *)space_image,wSampling);
						Output8x8Image(wX_position, wY_position, (BYTE *)space_image);
						wX_position+=8;
						BlkCount++;
                        // if Restart Interval valid
						if(gRsi && ((BlkCount%gRsi) == 0))
						{   
                            UINT8 *jpgScan = (UINT8*)unBitStreamOFFSET;
                            // It is going to be 0xFFD0 .. 0xFFD7
                            if((jpgScan[0] == 0xFF) && ((jpgScan[1]& 0xF8 ) == 0xD0))
                            {
                                // Restart the scan params
 							    InitGlobals();
                                // Skip the Rsi maraker
							    unBitStreamOFFSET+=2;
                            }
						}
				}
				wY_position+=8;
			}
#endif
		}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Output8x8Image
//
// Description:	This functions writes to the Global Screen Buffer.
//
// Input:	WORD wLeft - Left coordinate from where the Screen buffer 
//						is to be filled.
//					WORD wTop - Top coordinate from where the Screen buffer is to be filled.
//					BYTE * pspace_image - Pointer to 8x8 block of image.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>  
void Output8x8Image(WORD wLeft, WORD wTop, BYTE * pspace_image){
	int nCounterY=0;
	int nCounterX=0;
	AMI_TRUECOLOR_PIXEL_JPEG * pStartofLineInScreenBuffer = (AMI_TRUECOLOR_PIXEL_JPEG *)ScreenBufferJPEG + wTop * g_un32BufferWidth + wLeft;

	for(;nCounterY<8;nCounterY++){
		if((UINT32)(wTop + nCounterY) >= g_un32BufferHeight){
			break;
		}
		for(nCounterX=0;nCounterX<8; nCounterX++){
			if( (UINT32)(wLeft + nCounterX) < g_un32BufferWidth){
				(pStartofLineInScreenBuffer + nCounterX)->Red	= *pspace_image;
				(pStartofLineInScreenBuffer + nCounterX)->Green	= *(pspace_image+1);
				(pStartofLineInScreenBuffer + nCounterX)->Blue	= *(pspace_image+2);
				}
			pspace_image+=4;
		}
		pStartofLineInScreenBuffer+=g_un32BufferWidth;
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	GetTables
//
// Description:	Gets the 2 Quantization Tables and 4 Huffman Tables.
//					Checks the type of table and then builds the corresponding table.
//
// Input:	BYTE bNumQT - Number of Quantization Tables.
//					BYTE bNumHT - Number of Huffman Tables.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void GetTables(BYTE bNumQT, BYTE bNumHT)
{
	int i,j;
	
	for(j=0;j<bNumQT;j++)
	{

		switch(*((BYTE*)QT[j]))
		{
		case 0x00:
				for(i=0;i<64;i++)
				{
					bQTLumTable[i] = *((BYTE*)QT[j]+i+1);
				}
			break;
		case 0x01:
				for(i=0;i<64;i++)
				{
					bQTChrTable[i] = *((BYTE*)QT[j]+i+1);
				}
			break;
		}
	}
	for(i=0;i<bNumHT;i++)
	{
		switch((*(BYTE*)(HT[i])))
		{
			case 0x00:
				BuildHuffmanCodeTable(wDCLumtable,(BYTE *)(HT[i]));
				break;
			case 0x10:
				BuildHuffmanCodeTable(wACLumtable,(BYTE *)(HT[i]));
				break;
			case 0x01:
				BuildHuffmanCodeTable(wDCChrtable,(BYTE *)(HT[i]));
				break;
			case 0x11:
				BuildHuffmanCodeTable(wACChrtable,(BYTE *)(HT[i]));
				break;
		}
	}

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InitJpegParams
//
// Description:	Reads the image parameters and checks whether the image 
//				is supported or not.
//              TSE Jpeg Algorithm Supports following Markers
//              ==================================================
//              0C0h - Start Of Frame (Baseline DCT) (REQUIRED)
//              0C4h - Define Huffman Table (REQUIRED)
//              0D0h - RSI0 Marker (OPTIONAL)
//              0D1h - RSI1 Marker (OPTIONAL)
//              0D2h - RSI2 Marker (OPTIONAL)
//              0D3h - RSI3 Marker (OPTIONAL)
//              0D4h - RSI4 Marker (OPTIONAL)
//              0D5h - RSI5 Marker (OPTIONAL)
//              0D6h - RSI6 Marker (OPTIONAL)
//              0D7h - RSI7 Marker (OPTIONAL)
//              0D8h - Start of Image (REQUIRED)
//              0D9h - End of Image (REQUIRED)
//              0DAh - Start Of Scan (REQUIRED)
//              0DBh - Define Quantization Table (REQUIRED)
//              0DDh - Define Restart Interval (OPTIONAL)
//              0E0h - 0xEF - APPx Markers (Does Depend on and just Skips marker) (DON'T CARE)
//              0F0h - 0xFD - Reserved for JPEG extensions (Does Depend on and just Skips marker) (DON'T CARE)
//              0FEh - Jpeg Comment (Does Depend on and just Skips marker) (DON'T CARE)
//
//              TSE Jpeg Algorithom Does not Support following Markers
//              ======================================================
//              000h-0BFh - RESERVED
//              0C1h - Start Of Frame (Extended sequential DCT)
//              0C2h - Start Of Frame (Progressive DCT)
//              0C3h - Start Of Frame (Lossless (sequential))
//              0C5h - Start Of Frame (Differential sequential DCT)
//              0C6h - Start Of Frame (Differential progressive DCT)
//              0C7h - Start Of Frame (Differential lossless (sequential))
//              0C8h - Start Of Frame (Reserved for JPEG extensions)
//              0C9h - Start Of Frame (Extended sequential DCT)
//              0CAh - Start Of Frame (Progressive DCT)
//              0CBh - Start Of Frame (Lossless (sequential))
//              0CDh - Start Of Frame (Differential sequential DCT)
//              0CEh - Start Of Frame (Differential progressive DCT)
//              0CFh - Start Of Frame (Differential lossless (sequential))
//              0CCh - Arithmetic coding conditioning
//              0DCh - Define number of lines
//              0DEh - Define hierarchical progression
//              0DFh - Expand reference component(s)
//              0FFh - RESERVED
//
// Input:	BYTE * pbyImage - Pointer to the JPEG image.
//
// Output:	BOOL TRUE/FALSE.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
BOOL InitJpegParams(BYTE * pbyImage)
{
	
	BYTE bJpegImageFlag	 = 0;
	BYTE bCount = 0;
	int i = 0;
	BYTE bNumHT=0;
	BYTE bNumQT=0;
	WORD wLen = 0;

	if ( ( *pbyImage != 0xFF ) || ( *(pbyImage+1) != 0xD8 ) )	// if marker is not Start Of Image
		return FALSE;
	do{
		if(0x0FF== *pbyImage){
			pbyImage++;
			//; start of image
			if ( 0xD8 == *pbyImage )
			{
				pbyImage++;
				continue;
			}
			//; end of image
			if(0x0D9== *pbyImage){
				break;
			}
			if(*pbyImage){
                // APPx Marks No information needed for us
                if((0x0F0 & *pbyImage) == 0x0E0){
				}
				//	; if maker is Start Of Frame
				else if(0x0C0== *pbyImage){
					bJpegImageFlag|=2;
					unSOF0 = (UINTN)(pbyImage+1);
				}
				//; if maker is Define Huffman Table
				else if(0x0C4== *pbyImage){
					HT[bNumHT] = (UINTN)(pbyImage+3);
					bNumHT++;
				}
				else if((0x0F0 & *pbyImage) == 0xC0){
				//	; if Frame markers
                    switch(*pbyImage) 
                    {
                    //Unsupported Frame Markers
                    case 0x0C1: //Start Of Frame (Extended sequential DCT)
                    case 0x0C2: //Start Of Frame (Progressive DCT)
                    case 0x0C3: //Start Of Frame (Lossless (sequential))
                    case 0x0C5: //Start Of Frame (Differential sequential DCT)
                    case 0x0C6: //Start Of Frame (Differential progressive DCT)
                    case 0x0C7: //Start Of Frame (Differential lossless (sequential))
                    case 0x0C8: //Start Of Frame (Reserved for JPEG extensions)
                    case 0x0C9: //Start Of Frame (Extended sequential DCT)
                    case 0x0CA: //Start Of Frame (Progressive DCT)
                    case 0x0CB: //Start Of Frame (Lossless (sequential))
                    case 0x0CD: //Start Of Frame (Differential sequential DCT)
                    case 0x0CE: //Start Of Frame (Differential progressive DCT)
                    case 0x0CF: //Start Of Frame (Differential lossless (sequential))
                    //Arithmetic coding conditioning
                    case 0x0CC:
    					return FALSE;
                    default:
                        // Never comes here as C4 and C0 is handled above
                        break;
                    }
				}
				//; if maker is Start Of Scan
				else if(0x0DA== *pbyImage){
					//SOS = (UINTN)(pbyImage+1);
					unBitStreamOFFSET = (UINTN)(pbyImage+13);
					break;
				}
				//; if maker is Define Quantization Table
				else if(0x0DB== *pbyImage){
						QT[bNumQT] = (UINTN)(pbyImage+3);
						bNumQT++;
				}
				//; if marker is Define Restart Interval Start
				else if(0x0DD== *pbyImage){
					*((BYTE *)&gRsi) = *(pbyImage+4);
					*((BYTE *)&gRsi+1) = *(pbyImage+3);
				}
                // if Restart RSIx Marker
				else if((*pbyImage & 0xF8 ) == 0xD0){
                }
				//; if marker is Define number of lines
				else if(0x0DC== *pbyImage){
   					return FALSE;
				}
				//; if marker is Define hierarchical progression
				else if(0x0DE== *pbyImage){
   					return FALSE;
				}

				//; if marker is Expand reference component(s)
				else if(0x0DF== *pbyImage){
   					return FALSE;
				}
                // Reserved for JPEG extensions and Comment. Skip it
                else if((*pbyImage >= 0xF0) && (*pbyImage <= 0xFE)){
                }
                // if the Marker is Less then 0xC0 Or 0xFF then they are reserved marker. No Valid
                // JPEG file may use it. We don't handle such files
                // that is ((*pbyImage < 0xC0) || (*pbyImage == 0xFF))
                else 
                {
   					return FALSE;
                }
    			pbyImage++;
				*((BYTE *)&wLen) = *(pbyImage+1);
				*((BYTE *)&wLen+1) = *pbyImage;
				pbyImage += wLen;
			}
			else
			{
				return FALSE;
			}
		}
		else
		{
			return FALSE;
		}
	}while(1);
	
	//Support for 1 QT
	if(1 == bNumQT)
	{
			QT[bNumQT++] = (UINTN)(QT[0]+65);
	}

	//Support for 1 HT
	if(1 == bNumHT)
	{
		while(bNumHT<4)
		{
			bCount = 0;
		for(i=0;i<16;i++)
		{
			bCount = bCount + *(BYTE*)(HT[bNumHT-1]+i+1);
		}
		HT[bNumHT++] = (UINTN)(HT[bNumHT-1]+bCount+17);
		}
	}
// Check for 4 HT and 2 QT and SOF0
	if((4==bNumHT)&&(2==bNumQT)&&(0x02 & bJpegImageFlag)) 
	{
		GetTables(bNumQT, bNumHT);
		return TRUE;
	}
else
return FALSE;

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	Check
//
// Description:	Internal function for doing a check for each color component
//
// Input:	INT32 n32ColorComp - Color Component (R, G or B).
//
// Output:	The color component value after the check.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
INT32 Check(INT32 n32ColorComp)
{
	n32ColorComp>>=14;
	if(0x8000==(n32ColorComp&0x8000))
		n32ColorComp=0;

	if((n32ColorComp&0xffff)>255)
		n32ColorComp=0x00ff;//mov CX,255

	return n32ColorComp;
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	ConvertYCbCrToRGB
//
// Description:	Converts the luminance(Y) and chrominance(Cb and Cr) values
//					of the image to R, G, B values.	The R,G, B values are stored 
//					in a DWORD variable.
//
// Input:	short Y - Y is the luminance value.
//					short Cb - Cb is the chrominance Hue value.
//					short Cr - Cr is the chrominance Saturation value.
//
// Output:	Variable having the RGB value, which is a DWORD.
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
DWORD ConvertYCbCrToRGB(short Y, short Cb, short Cr)
{
	DWORD dwRGB    =0;
	INT32 n32ColorComp	=0;
	INT32 YComp = (((INT32)(Y))<<14);
	INT32 CrComp = (((INT32)(Cr))*c_g_cr);
	INT32 CbComp =	((INT32)(Cb) * c_g_cb);
	//;;;cal r
	n32ColorComp = Check((YComp + (CrComp<<1)));
	dwRGB = ((n32ColorComp<<16)|((unsigned int)n32ColorComp>>16));

	//;;;cal g
	n32ColorComp = Check((YComp-CrComp-CbComp));
	*(((char *)(&dwRGB))+1) = *(char *)(&n32ColorComp);

	//;;;cal b
	n32ColorComp = Check((YComp + CbComp + (CbComp<<2) + (CbComp>>3)));
	*(char *)(&dwRGB) = *(char *)(&n32ColorComp);

	return dwRGB;

}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InvertDCT
//
// Description:	Performs inverse discrete cosine transform on dequantized data.
//
// Input:	WORD * pwSource - Input is dequantized array of 8x8 pixels and 
//						output is data after  applying inverse discrete cosine transform.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void InvertDCT(WORD * pwSource){
	WORD * pwSourceCopy=pwSource;
	int nCounter=0;

	for(nCounter=0; nCounter<8; nCounter++){
		InvertDCT_xy(pwSource,1);
		pwSource+=8;		
	}
	pwSource = pwSourceCopy;
	for(nCounter=0; nCounter<8; nCounter++){
		InvertDCT_xy(pwSource,8);
		pwSource++;		
	}
}

//<AMI_PHDR_START>
//----------------------------------------------------------------------------
// Procedure:	InvertDCT_xy
//
// Description:	Performs inverse discrete cosine transform on dequantized 
//					data in x and y direction.
//
// Input:	WORD * pwSource - Input is dequantized array of 8x8 pixels 
//						and output is data after applying inverse discrete cosine transform.
//					WORD wVal - 
//						wVal = 1, then the routine performs inverse discrete 
//							cosine transform on dequantized data in x direction.
//						wVal = 8, then the routine performs inverse discrete cosine 
//							transform on dequantized data in y direction.
//
// Output:	None
//
//----------------------------------------------------------------------------
//<AMI_PHDR_END>
void InvertDCT_xy(WORD * pwSource, WORD wVal)
{
INT32 buf1[8];
INT32 buf2[8];
int Index[8];
int i=0;
for(;i<8;i++)
Index[i] = i*wVal;

//;;;stage 1, 2 ,3

//	;;o0=x0 and normalize
	buf2[0] = (((INT32)(INT16)(pwSource[0])*c1_sqrt2))>>14;


//	;;o1=x4*c1_4
	buf2[1] = (((INT32)(INT16)(pwSource[Index[4]]))*c1_4)>>14;

//	;;o2=x2

	buf2[2]	= ((INT32)(INT16)(pwSource[Index[2]]));
//	;;;o3=(x2+x6)*c1_4

	buf2[3] = ((((buf2[2] + (INT32)(INT16)(pwSource[Index[6]]))*c1_4))>>14);

//	;;o4=x1

	buf2[4]	= (INT32)(INT16)(pwSource[Index[1]]);

//	;;;o6=x1+x3

	buf2[6]	= buf2[4]	+ (INT32)(INT16)(pwSource[Index[3]]);

//	;;;o7=(x1+x3+x5+x7)*c1_4

	buf2[7] = (((buf2[6]+((INT32)(INT16)(pwSource[Index[5]]))+((INT32)(INT16)(pwSource[Index[7]])))*c1_4)>>14);

//	;;;o5=(x3+x5)*c1_4

	buf2[5] = (((((INT32)(INT16)(pwSource[Index[3]]))+((INT32)(INT16)(pwSource[Index[5]])))*c1_4)>>14);

//;;;stage 4, 5

//	;;b0=o0+o1
	buf1[0] = buf2[0]+buf2[1];

//	;;b1=o0-o1
	buf1[1] = buf2[0]-buf2[1];

//	;;b2=(o2+o3)*c1_8

	buf1[2]=(((buf2[2]+buf2[3])*c1_8)>>14);

//	;;b3=(o2-o3)*c3_8

	buf1[3]=(((buf2[2]-buf2[3])*c3_8)>>14);

//	;;b4=o4+o5
	buf1[4] = buf2[4]+buf2[5];

//	;;b5=o4-o5
	buf1[5] = buf2[4]-buf2[5];

//	;;b6=(o6+o7)*c1_8

	buf1[6]=(((buf2[6]+buf2[7])*c1_8)>>14);

//	;;b7=(o6-o7)*c3_8

	buf1[7]=(((buf2[6]-buf2[7])*c3_8)>>14);

//;;;stage 6, 7

//	;;o0=b0+b2
	buf2[0] = buf1[0]+buf1[2];

//	;;o2=b0-b2
	buf2[2] = buf1[0]-buf1[2];

//	;;o1=b1+b3
	buf2[1] = buf1[1]+buf1[3];

//	;;o3=b1-b3
	buf2[3] = buf1[1]-buf1[3];

//	;;o4=(b4+b6)*c1_16

	buf2[4] = (((buf1[4]+buf1[6])*c1_16)>>14);

//	;;o6=(b4-b6)*c7_16

	buf2[6] = (((buf1[4]-buf1[6])*c7_16)>>14);

//	;;o5=(b5+b7)*c3_16

	buf2[5] = (((buf1[5]+buf1[7])*c3_16)>>14);

//	;;o7=(b5-b7)*c5_16

	buf2[7] = (((buf1[5]-buf1[7])*c5_16)>>14);

//;;;stage 8

//	;;b0=o0+o4

	pwSource[0] = (WORD)((buf2[0]+buf2[4])>>1); 

//	;;b7=o0-o4

	pwSource[Index[7]] = (WORD)((buf2[0]-buf2[4])>>1);

//	;;b1=o1+o5

	pwSource[Index[1]] = (WORD)((buf2[1]+buf2[5])>>1);

//	;;b6=o1-o5

	pwSource[Index[6]] = (WORD)((buf2[1]-buf2[5])>>1);

//	;;b2=o3+o7

	pwSource[Index[2]] = (WORD)((buf2[3]+buf2[7])>>1);

//	;;b5=o3-o7

	pwSource[Index[5]] = (WORD)((buf2[3]-buf2[7])>>1);

//	;;b3=o2+o6

	pwSource[Index[3]] = (WORD)((buf2[2]+buf2[6])>>1);

//	;;b4=o2-o6

	pwSource[Index[4]] = (WORD)((buf2[2]-buf2[6])>>1);
	}
//**********************************************************************
//**********************************************************************
//**                                                                  **
//**        (C)Copyright 1985-2010, American Megatrends, Inc.         **
//**                                                                  **
//**                       All Rights Reserved.                       **
//**                                                                  **
//**     5555 Oakbrook Pkwy, Building 200,Norcross, Georgia 30093     **
//**                                                                  **
//**                       Phone: (770)-246-8600                      **
//**                                                                  **
//**********************************************************************
//**********************************************************************