2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 /* ---- includes ----------------------------------------------------------- */
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"
25 /* ---- typedefs ----------------------------------------------------------- */
27 /* ---- constants ---------------------------------------------------------- */
29 /* ------------------------------------------------------------------------- */
31 /* ========================================================================= */
33 /* ---- \ghd{ auxiliary functions } ---------------------------------------- */
35 /* ========================================================================= */
37 /** Computes grey level histogram of given image. */
38 void bim_createHisto16( uint16* histoPtrA,
39 const struct bim_UInt16ByteImage* imagePtrA )
43 const uint16* srcPtrL;
45 /* init histogram array with 0 */
47 for( iL = 256; iL > 0; iL-- )
52 srcPtrL = imagePtrA->arrE.arrPtrE;
54 /* calculate histogram (assuming even image width) */
55 for( iL = imagePtrA->arrE.sizeE; iL > 0; iL-- )
57 dstPtrL[ ( *srcPtrL & 0x0FF ) ]++;
58 dstPtrL[ ( *srcPtrL >> 8 ) ]++;
63 /* ------------------------------------------------------------------------- */
65 /** Computes grey level histogram of given image. */
66 void bim_createHistoOfSection16( uint16* histoPtrA,
67 const struct bts_Int16Rect* sectionPtrA,
68 const struct bim_UInt16ByteImage* imagePtrA )
71 const uint16* srcPtrL;
73 struct bts_Int16Rect sectionL = *sectionPtrA;
76 int32 imgWidthL = imagePtrA->widthE;
77 int32 imgHeightL = imagePtrA->heightE;
79 bbs_ERROR0( "bim_createHistoOfSection16(...): not implemented" );
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 );
91 sectWidthL = sectionL.x2E - sectionL.x1E;
92 sectHeightL = sectionL.y2E - sectionL.y1E;
94 /* init histogram with 0 */
96 for( xL = 256; xL > 0; xL-- )
101 /* calculate histogram */
102 srcPtrL = imagePtrA->arrE.arrPtrE + sectionL.y1E * imgWidthL + sectionL.x1E;
104 for( yL = 0; yL < sectHeightL; yL++ )
106 for( xL = 0; xL < sectWidthL; xL++ )
108 dstPtrL[ ( *srcPtrL & 0x0FF ) ]++;
109 dstPtrL[ ( *srcPtrL >> 8 ) ]++;
111 /* dstPtrL[ *srcPtrL++ ]++; */
113 srcPtrL += imgWidthL - sectWidthL;
117 /* ------------------------------------------------------------------------- */
119 /** equalize image using given histogram */
120 void bim_equalize16( struct bim_UInt16ByteImage* imagePtrA,
121 const uint16* histoPtrA )
125 uint32 totalSumL = 0;
126 const uint16* histoArrPtrL;
128 uint16 mappingL[ 256 ];
130 /* determine number of counts in histogram */
131 histoArrPtrL = histoPtrA;
132 for( kL = 256; kL > 0; kL-- )
134 totalSumL += *histoArrPtrL++;
137 if( totalSumL == 0 ) totalSumL = 1;
139 /* compute transfer function (cumulative histogram) */
140 histoArrPtrL = histoPtrA;
141 for( kL = 0; kL < 256; kL++ )
143 sumL += *histoArrPtrL++;
144 mappingL[ kL ] = ( sumL * 255 ) / totalSumL;
147 /* remap pixel values */
148 dstPtrL = imagePtrA->arrE.arrPtrE;
149 for( kL = imagePtrA->arrE.sizeE; kL > 0; kL-- )
151 *dstPtrL = mappingL[ *dstPtrL & 0x00FF ] | ( mappingL[ *dstPtrL >> 8 ] << 8 );
156 /* ------------------------------------------------------------------------- */
158 /* ========================================================================= */
160 /* ---- \ghd{ external functions } ----------------------------------------- */
162 /* ========================================================================= */
164 /* ------------------------------------------------------------------------- */
166 void bim_UInt16ByteImage_equalize( struct bim_UInt16ByteImage* imagePtrA )
168 uint16 histogramL[ 256 ];
169 bim_createHisto16( histogramL, imagePtrA );
170 bim_equalize16( imagePtrA, histogramL );
173 /* ------------------------------------------------------------------------- */
175 void bim_UInt16ByteImage_equalizeSection( struct bim_UInt16ByteImage* imagePtrA,
176 const struct bts_Int16Rect* sectionPtrA )
178 uint16 histogramL[ 256 ];
179 bim_createHistoOfSection16( histogramL, sectionPtrA, imagePtrA );
180 bim_equalize16( imagePtrA, histogramL );
183 /* ========================================================================= */