• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* ---- includes ----------------------------------------------------------- */
18 
19 #include "b_BasicEm/Functions.h"
20 #include "b_BasicEm/Int16Arr.h"
21 #include "b_BasicEm/Math.h"
22 #include "b_ImageEm/HistoEq16.h"
23 #include "b_ImageEm/UInt16ByteImage.h"
24 
25 /* ---- typedefs ----------------------------------------------------------- */
26 
27 /* ---- constants ---------------------------------------------------------- */
28 
29 /* ------------------------------------------------------------------------- */
30 
31 /* ========================================================================= */
32 /*                                                                           */
33 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
34 /*                                                                           */
35 /* ========================================================================= */
36 
37 /** Computes grey level histogram of given image. */
bim_createHisto16(uint16 * histoPtrA,const struct bim_UInt16ByteImage * imagePtrA)38 void bim_createHisto16( uint16* histoPtrA,
39 						const struct bim_UInt16ByteImage* imagePtrA )
40 {
41 	uint32 iL;
42 	uint16* dstPtrL;
43 	const uint16* srcPtrL;
44 
45 	/* init histogram array with 0 */
46 	dstPtrL = histoPtrA;
47 	for( iL = 256; iL > 0; iL-- )
48 	{
49 		*dstPtrL++ = 0;
50 	}
51 
52 	srcPtrL = imagePtrA->arrE.arrPtrE;
53 	dstPtrL = histoPtrA;
54 	/* calculate histogram (assuming even image width) */
55 	for( iL = imagePtrA->arrE.sizeE; iL > 0; iL-- )
56 	{
57 		dstPtrL[ ( *srcPtrL & 0x0FF ) ]++;
58 		dstPtrL[ ( *srcPtrL >> 8 ) ]++;
59 		srcPtrL++;
60 	}
61 }
62 
63 /* ------------------------------------------------------------------------- */
64 
65 /** Computes grey level histogram of given image. */
bim_createHistoOfSection16(uint16 * histoPtrA,const struct bts_Int16Rect * sectionPtrA,const struct bim_UInt16ByteImage * imagePtrA)66 void bim_createHistoOfSection16( uint16* histoPtrA,
67 								 const struct bts_Int16Rect* sectionPtrA,
68 								 const struct bim_UInt16ByteImage* imagePtrA )
69 {
70 	uint32 xL, yL;
71 	const uint16* srcPtrL;
72 	uint16* dstPtrL;
73 	struct bts_Int16Rect sectionL = *sectionPtrA;
74 	uint32 sectWidthL;
75 	uint32 sectHeightL;
76 	int32 imgWidthL = imagePtrA->widthE;
77 	int32 imgHeightL = imagePtrA->heightE;
78 
79 	bbs_ERROR0( "bim_createHistoOfSection16(...): not implemented" );
80 
81 	/* adjustments */
82 	sectionL.x1E = bbs_max( 0, sectionL.x1E );
83 	sectionL.x1E = bbs_min( imgWidthL, sectionL.x1E );
84 	sectionL.x2E = bbs_max( 0, sectionL.x2E );
85 	sectionL.x2E = bbs_min( imgWidthL, sectionL.x2E );
86 	sectionL.y1E = bbs_max( 0, sectionL.y1E );
87 	sectionL.y1E = bbs_min( imgHeightL, sectionL.y1E );
88 	sectionL.y2E = bbs_max( 0, sectionL.y2E );
89 	sectionL.y2E = bbs_min( imgHeightL, sectionL.y2E );
90 
91 	sectWidthL = sectionL.x2E - sectionL.x1E;
92 	sectHeightL = sectionL.y2E - sectionL.y1E;
93 
94 	/* init histogram with 0 */
95 	dstPtrL = histoPtrA;
96 	for( xL = 256; xL > 0; xL-- )
97 	{
98 		*dstPtrL++ = 0;
99 	}
100 
101 	/* calculate histogram */
102 	srcPtrL = imagePtrA->arrE.arrPtrE + sectionL.y1E * imgWidthL + sectionL.x1E;
103 	dstPtrL = histoPtrA;
104 	for( yL = 0; yL < sectHeightL; yL++ )
105 	{
106 		for( xL = 0; xL < sectWidthL; xL++ )
107 		{
108 			dstPtrL[ ( *srcPtrL & 0x0FF ) ]++;
109 			dstPtrL[ ( *srcPtrL >> 8 ) ]++;
110 			srcPtrL++;
111 			/* dstPtrL[ *srcPtrL++ ]++;	 */
112 		}
113 		srcPtrL += imgWidthL - sectWidthL;
114 	}
115 }
116 
117 /* ------------------------------------------------------------------------- */
118 
119 /** equalize image using given histogram */
bim_equalize16(struct bim_UInt16ByteImage * imagePtrA,const uint16 * histoPtrA)120 void bim_equalize16( struct bim_UInt16ByteImage* imagePtrA,
121 					 const uint16* histoPtrA )
122 {
123 	uint32 kL;
124 	uint32 sumL = 0;
125 	uint32 totalSumL = 0;
126 	const uint16* histoArrPtrL;
127 	uint16* dstPtrL;
128 	uint16 mappingL[ 256 ];
129 
130 	/* determine number of counts in histogram */
131 	histoArrPtrL = histoPtrA;
132 	for( kL = 256; kL > 0; kL-- )
133 	{
134 		totalSumL += *histoArrPtrL++;
135 	}
136 
137 	if( totalSumL == 0 ) totalSumL = 1;
138 
139 	/* compute transfer function (cumulative histogram) */
140 	histoArrPtrL = histoPtrA;
141 	for( kL = 0; kL < 256; kL++ )
142 	{
143 		sumL += *histoArrPtrL++;
144 		mappingL[ kL ] = ( sumL * 255 ) / totalSumL;
145 	}
146 
147 	/* remap pixel values */
148 	dstPtrL = imagePtrA->arrE.arrPtrE;
149 	for( kL = imagePtrA->arrE.sizeE; kL > 0; kL-- )
150 	{
151 		*dstPtrL = mappingL[ *dstPtrL & 0x00FF ] | ( mappingL[ *dstPtrL >> 8 ] << 8 );
152 		dstPtrL++;
153 	}
154 }
155 
156 /* ------------------------------------------------------------------------- */
157 
158 /* ========================================================================= */
159 /*                                                                           */
160 /* ---- \ghd{ external functions } ----------------------------------------- */
161 /*                                                                           */
162 /* ========================================================================= */
163 
164 /* ------------------------------------------------------------------------- */
165 
bim_UInt16ByteImage_equalize(struct bim_UInt16ByteImage * imagePtrA)166 void bim_UInt16ByteImage_equalize( struct bim_UInt16ByteImage* imagePtrA )
167 {
168 	uint16 histogramL[ 256 ];
169 	bim_createHisto16( histogramL, imagePtrA );
170 	bim_equalize16( imagePtrA, histogramL );
171 }
172 
173 /* ------------------------------------------------------------------------- */
174 
bim_UInt16ByteImage_equalizeSection(struct bim_UInt16ByteImage * imagePtrA,const struct bts_Int16Rect * sectionPtrA)175 void bim_UInt16ByteImage_equalizeSection( struct bim_UInt16ByteImage* imagePtrA,
176 										  const struct bts_Int16Rect* sectionPtrA )
177 {
178 	uint16 histogramL[ 256 ];
179 	bim_createHistoOfSection16( histogramL, sectionPtrA, imagePtrA );
180 	bim_equalize16( imagePtrA, histogramL );
181 }
182 
183 /* ========================================================================= */
184