summaryrefslogtreecommitdiff
path: root/src/Image/Image_Convertor.cxx
blob: 843b97d492887009bc49b93c6539eddefefd289d (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
#include <Image_Convertor.ixx>

#include <Image_PseudoColorImage.hxx>
#include <Image_ColorImage.hxx>
#include <Image_LookupTable.hxx>
#include <Aspect_ColorMapEntry.hxx>

#ifdef TRACE
static int Verbose = 0 ;
#endif

Image_Convertor::Image_Convertor() { myDitheringMethod = Image_DM_NearestColor;}

void Image_Convertor::SetDitheringMethod( const Image_DitheringMethod Method )

{ myDitheringMethod = Method ; }

Handle(Image_PseudoColorImage) Image_Convertor::Convert( 
	const Handle(Image_ColorImage)& aImage,
	const Handle(Aspect_ColorMap)& aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;

  if ( myDitheringMethod == Image_DM_NearestColor ) {
	ret_image = NearestDithering( aImage, aColorMap ) ;
  }
  else if ( myDitheringMethod == Image_DM_ErrorDiffusion ) {
	ret_image = ErrorDiffusionDithering( aImage, aColorMap ) ;
  }

  return ret_image ;
}


Handle(Image_PseudoColorImage) Image_Convertor::Convert( 
	const Handle(Image_PseudoColorImage)& aImage,
	const Handle(Aspect_ColorMap)& aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;

  if ( myDitheringMethod == Image_DM_NearestColor ) {
	ret_image = NearestDithering( aImage, aColorMap ) ;
  }
  else if ( myDitheringMethod == Image_DM_ErrorDiffusion ) {
	ret_image = ErrorDiffusionDithering( aImage, aColorMap ) ;
  }

  return ret_image ;
}


Handle(Image_ColorImage) Image_Convertor::Convert( 
	const Handle(Image_PseudoColorImage)& aImage ) const

{ Handle(Image_ColorImage) ret_image = NULL ;
  Standard_Integer x, y, val, lastval ;
  Quantity_Color aColor ;
  Standard_Integer UpX = aImage->UpperX() ;
  Standard_Integer UpY = aImage->UpperY() ;

  ret_image = new Image_ColorImage( aImage->LowerX(), aImage->LowerY(),
				    aImage->Width() , aImage->Height() ) ;

  lastval = aImage->Pixel(aImage->LowerX(), aImage->LowerY() ).Value() ;

  aColor  = aImage->ColorMap()->FindEntry(lastval).Color() ;

  for ( y = aImage->LowerY() ; y <= UpY ; y++ ) {
    for ( x = aImage->LowerX() ; x <= UpX ; x++ ) {

	val = aImage->Pixel(x,y).Value() ;

	if ( lastval != val ) {
	  lastval = val ;
	  aColor  = aImage->ColorMap()->FindEntry(lastval).Color() ;		
	}

        ret_image->SetPixel( x, y, aColor );
    }
  }

  return( ret_image ) ;

}

Handle(Image_PseudoColorImage) Image_Convertor::NearestDithering( 
	const Handle(Image_ColorImage)& aImage,
	const Handle(Aspect_ColorMap)&  aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;
  Standard_Integer x, y, index ;
  Quantity_Color lastval, val ;
  Standard_Integer UpX = aImage->UpperX() ;
  Standard_Integer UpY = aImage->UpperY() ;

  ret_image = new Image_PseudoColorImage( aImage->LowerX(), aImage->LowerY(),
				          aImage->Width() , aImage->Height(),
					  aColorMap );

  lastval = aImage->PixelColor(aImage->LowerX(),aImage->LowerY()) ;
  index   = aColorMap->NearestEntry( lastval ).Index() ;

  for ( y = aImage->LowerY() ; y <= UpY ; y++ ) {
    for ( x = aImage->LowerX() ; x <= UpX ; x++ ) {
       val = aImage->PixelColor(x,y) ;
       if ( !( val == lastval ) ) {
		lastval = val ;
		index   = aColorMap->NearestEntry( lastval ).Index() ;
       }
       ret_image->SetPixel( x, y, index ) ;
    }
  }

  return( ret_image ) ;

}


Handle(Image_PseudoColorImage) Image_Convertor::NearestDithering( 
	const Handle(Image_PseudoColorImage)& aImage,
	const Handle(Aspect_ColorMap)&  aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;
  Standard_Integer x,y, val, lastval, index  ;
  Standard_Integer UpX = aImage->UpperX() ;
  Standard_Integer UpY = aImage->UpperY() ;
  Image_LookupTable ColorMapLookup ;
  Aspect_ColorMapEntry aEntry ;
  Standard_Integer i, size ;

  for ( i = 1 , size = aImage->ColorMap()->Size() ; i <= size ; i++ ) {
	aEntry = aImage->ColorMap()->Entry(i) ;
	index  = aColorMap->NearestEntry( aEntry.Color() ).Index() ;

	ColorMapLookup.Bind( aEntry.Index(), index ) ;
  }

  ret_image = new Image_PseudoColorImage( aImage->LowerX(), aImage->LowerY(),
				          aImage->Width() , aImage->Height(),
					  aColorMap );

  lastval = aImage->Pixel( aImage->LowerX(), aImage->LowerY() ).Value() ;
  index   = ColorMapLookup.Find( lastval ).Value() ;


  for ( y = aImage->LowerY() ; y <= UpY ; y++ ) {
    for ( x = aImage->LowerX() ; x <= UpX ; x++ ) {

       val = aImage->Pixel(x,y).Value() ;

       if ( val != lastval ) {
		lastval = val ;
		index   = ColorMapLookup.Find( lastval ).Value() ;
       }

       ret_image->SetPixel( x, y, index ) ;
    }
  }

  return( ret_image ) ;

}

static Standard_Boolean ManageLastKernelError = 1 ;

static struct {
	Standard_Real v ;
	Standard_Integer dx, dy ;
} Kernel[4] = {		{ 7./16.,  1, 0 },
			{ 3./16., -1, 1 },
			{ 5./16.,  0, 1 },
			{ 1./16.,  1, 1 } } ;

Handle(Image_PseudoColorImage) Image_Convertor::ErrorDiffusionDithering( 
	const Handle(Image_PseudoColorImage)& RefImage,
	const Handle(Aspect_ColorMap)&  aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;
  Standard_Integer UpX  =  RefImage->UpperX() ;
  Standard_Integer UpY  =  RefImage->UpperY() ;
  Standard_Integer LowX =  RefImage->LowerX() ;
  Standard_Integer LowY =  RefImage->LowerY() ;
  Standard_Integer x,y, index, i, nx,ny  ;
  Standard_Integer val, lastval ;
  Standard_Integer Error, ErrorDiff, pix, npix, MinIndex, MaxIndex ;
  Aspect_IndexPixel aPixel ;
  Image_LookupTable ColorMapLookup ;
  Aspect_ColorMapEntry aEntry ;
  Standard_Integer size ;

  MaxIndex = MinIndex = RefImage->ColorMap()->Entry(1).Index() ;

  for ( i = 1 , size = RefImage->ColorMap()->Size() ; i <= size ; i++ ) {
	aEntry   = RefImage->ColorMap()->Entry(i) ;
	MinIndex = Min( aEntry.Index(), MinIndex ) ;
	MaxIndex = Max( aEntry.Index(), MaxIndex ) ;
	index    = aColorMap->NearestEntry( aEntry.Color() ).Index() ;

	ColorMapLookup.Bind( aEntry.Index(), index ) ;
  }


  Handle(Image_PseudoColorImage) aImage = 
	Handle(Image_PseudoColorImage)::DownCast( RefImage->Dup() ) ;

  ret_image = new Image_PseudoColorImage( aImage->LowerX(), aImage->LowerY(),
				          aImage->Width() , aImage->Height(),
					  aColorMap );

  lastval = aImage->Pixel( aImage->LowerX(), aImage->LowerY() ).Value() ;
  index   = ColorMapLookup.Find( lastval ).Value() ;

  for ( y = aImage->LowerY() ; y <= UpY ; y++ ) {
    for ( x = aImage->LowerX() ; x <= UpX ; x++ ) {

       	val = aImage->Pixel(x,y).Value() ;

       	if ( ! ( val == lastval ) ) {
		lastval = val ;
  		index   = ColorMapLookup.Find( lastval ).Value() ;
       	}

	ErrorDiff = Error = val - index ;

#ifdef TRACE
	if ( Verbose > 3 ) 
	  cout << "ErrorDiffusion : " << Error << endl << flush ;
#endif

	if ( Error != 0 ) {

	  for ( i = 0 ; i < 4 ; i++ ) {
		nx = x+Kernel[i].dx ;
		ny = y+Kernel[i].dy ;

		if ( nx >= LowX && nx <= UpX &&
		     ny >= LowY && ny <= UpY ) {
			aImage->Pixel( nx, ny, aPixel ) ;
			pix = aPixel.Value() ;

			if ( ManageLastKernelError && i == 3 ) {
			  // Last one
			  npix = pix + ErrorDiff ;
			}
			else {
			  npix = pix + Standard_Integer( Kernel[i].v * Error ) ;
			}

			// ErrorDiffusion may produce Pixel out of ColorMap
			// Clamp value to min max of ColorMap
			npix = Max( npix, MinIndex ) ;
			npix = Min( npix, MaxIndex ) ;

			ErrorDiff -= ( npix - pix ) ; 

#ifdef TRACE
			if ( Verbose > 2 && i == 3 ) {
	 		   cout << "ErrorDiffusion Last Error: " << 
				(ErrorDiff-( Kernel[i].v*Error)) <<endl<<flush ;
			}
#endif

			aPixel.SetValue( npix ) ;

			aImage->SetPixel( nx, ny, aPixel ) ;
		}

	  }
	}

       	ret_image->SetPixel( x, y, index ) ;
    }
  }

  return( ret_image ) ;

}

Handle(Image_PseudoColorImage) Image_Convertor::ErrorDiffusionDithering( 
	const Handle(Image_ColorImage)& RefImage,
	const Handle(Aspect_ColorMap)&  aColorMap ) const

{ Handle(Image_PseudoColorImage) ret_image = NULL ;
  Standard_Integer UpX  =  RefImage->UpperX() ;
  Standard_Integer UpY  =  RefImage->UpperY() ;
  Standard_Integer LowX =  RefImage->LowerX() ;
  Standard_Integer LowY =  RefImage->LowerY() ;
  Standard_Integer x,y, index, i, nx,ny  ;
  Quantity_Color val, lastval, NewCol ;
  Aspect_ColorMapEntry aEntry ;
  Aspect_ColorPixel aPixel ;
  Standard_Real RedError, GreenError, BlueError, r,g,b,cr,cg,cb ;
  Standard_Real RedDiffError, GreenDiffError, BlueDiffError ;
  Handle(Image_ColorImage) aImage = 
	Handle(Image_ColorImage)::DownCast( RefImage->Dup() ) ;
  ret_image = new Image_PseudoColorImage( aImage->LowerX(), aImage->LowerY(),
				          aImage->Width() , aImage->Height(),
					  aColorMap );

  lastval = aImage->PixelColor( aImage->LowerX(), aImage->LowerY() ) ;

  aEntry = aColorMap->NearestEntry( lastval ) ;
  index  = aEntry.Index() ;

  for ( y = aImage->LowerY() ; y <= UpY ; y++ ) {
    for ( x = aImage->LowerX() ; x <= UpX ; x++ ) {

       	val = aImage->PixelColor(x,y) ;

       	if ( ! ( val == lastval ) ) {
		lastval = val ;
		aEntry  = aColorMap->NearestEntry( lastval ) ;
  		index   = aEntry.Index() ;
       	}

	RedDiffError   = RedError   = val.Red()   - aEntry.Color().Red() ;
	GreenDiffError = GreenError = val.Green() - aEntry.Color().Green() ;
	BlueDiffError  = BlueError  = val.Blue()  - aEntry.Color().Blue() ;

#ifdef TRACE
	if ( Verbose > 3 ) 
	  cout << "ErrorDiffusion : " << RedError << "," <<
					  GreenError << "," <<
					  BlueError << "," << endl << flush ;
#endif
	if ( RedError != 0. && GreenError != 0. && BlueError != 0. ) {

	  for ( i = 0 ; i < 4 ; i++ ) {
		nx = x+Kernel[i].dx ;
		ny = y+Kernel[i].dy ;

		if ( nx >= LowX && nx <= UpX &&
		     ny >= LowY && ny <= UpY ) {
			aImage->Pixel( nx, ny, aPixel ) ;
			aPixel.Value().Values( r,g,b, Quantity_TOC_RGB ) ;

			if ( ManageLastKernelError && i == 3 ) {
			  // Last one
			  cr = r + RedDiffError ;
			  cg = g + GreenDiffError ;
			  cb = b + BlueDiffError ;
				
			}
			else {
			  cr = r + ( Kernel[i].v * RedError ) ;
			  cg = g + ( Kernel[i].v * GreenError ) ;
			  cb = b + ( Kernel[i].v * BlueError ) ;

			}

			// ErrorDiffusion may produce Pixel out of ColorMap
			// Clamp Color
			cr = Max( cr, 0. ) ; cr = Min( cr, 1. ) ;
			cg = Max( cg, 0. ) ; cg = Min( cg, 1. ) ;
			cb = Max( cb, 0. ) ; cb = Min( cb, 1. ) ;

			RedDiffError   -= ( cr - r ) ;
			GreenDiffError -= ( cg - g ) ;
			BlueDiffError  -= ( cb - b ) ;

#ifdef TRACE
			if ( Verbose > 2 && i == 3 ) {

	 		   cout << "ErrorDiffusion Last Error: " << 
					RedDiffError << "," <<
					GreenDiffError << "," <<
					BlueDiffError << "," << endl << flush ;
			}
#endif
			NewCol.SetValues( cr,cg,cb, Quantity_TOC_RGB ) ;
			aPixel.SetValue( NewCol ) ;
			aImage->SetPixel( nx, ny, aPixel ) ;
		}

	  }
	}

       	ret_image->SetPixel( x, y, index ) ;
    }
  }

  return( ret_image ) ;

}