summaryrefslogtreecommitdiff
path: root/src/AlienImage/AlienImage_AidaAlienData.cxx
blob: 830cf2a5ba9027d7262c1fa5c91a91bc1ce42dfe (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
#define TEST    //GG_140699
//              Check file extension, must be ".aida".

#include <TCollection_AsciiString.hxx>
#include <Image_Convertor.hxx>
#include <Aspect_GenericColorMap.hxx>
#include <Aspect_ColorCubeColorMap.hxx>
#include <AlienImage_AidaAlienData.ixx>
#include <Aspect_ColorMapEntry.hxx>

# include <stdio.h>

#ifdef TRACE
static int Verbose = 0 ;
#endif


AlienImage_AidaAlienData::AlienImage_AidaAlienData()

{
  myDataIsDef		= Standard_False ;
  myColorsIsDef		= Standard_False ;
  myDitheringColorMap	= new Aspect_ColorCubeColorMap( 40, 5,1, 8,6, 3,54 ) ;
  myDitheringMethod	= Image_DM_NearestColor ;
}

void AlienImage_AidaAlienData::Clear()

{
  myDataIsDef	= Standard_False ;
  myColorsIsDef	= Standard_False ;
}

Standard_Boolean AlienImage_AidaAlienData::Write( OSD_File& file ) const

{ Standard_Integer  r,c ;
  TCollection_AsciiString out ;
  char hexa[3] ;
  unsigned char p ;
  unsigned long int rc,gc,bc ;
  TCollection_AsciiString Space = " " ;

  if ( ! myDataIsDef )   	return( Standard_False ) ;
  if ( ! myColorsIsDef ) 	return( Standard_False ) ;

  out = TCollection_AsciiString( "#BC(" ) +
	TCollection_AsciiString( myData->RowLength() ) +
	Space +
	TCollection_AsciiString( myData->ColLength() ) +
	TCollection_AsciiString( " #[\n" ) ;

  file.Write( out, out.Length() ) ;

  if ( file.Failed() ) {
	// ERROR
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ;
  }

  for ( r = myData->LowerRow() ; r <= myData->UpperRow() ; r++ ) {
    out = TCollection_AsciiString( "#*" ) ;
    for ( c = myData->LowerCol() ; c <= myData->UpperCol() ; c++ ) {
	p = ( unsigned char ) myData->Value( r, c ) ;
	sprintf( hexa , "%.2x", p ) ;
	out += TCollection_AsciiString( hexa ) ;
    }
    out += TCollection_AsciiString( "\n" ) ;

    file.Write( out, out.Length() ) ;
  
    if ( file.Failed() ) {
	// ERROR
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ;
    }
  }

  out = TCollection_AsciiString( "](\n" ) ;

  file.Write( out, out.Length() ) ;

  if ( file.Failed() ) {
	// ERROR
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ;
  }

  // write out the color map buffer
  for ( c = 1 ; c <= myColors->Size() ; c++ ) {
	rc = (long unsigned int )( myColors->Entry(c).Color().Red()   * 32767 + 0.5 );
	gc = (long unsigned int )( myColors->Entry(c).Color().Green() * 32767 + 0.5 );
	bc = (long unsigned int )( myColors->Entry(c).Color().Blue()  * 32767 + 0.5 );

	out = TCollection_AsciiString( "#[" ) + 
	      TCollection_AsciiString( myColors->Entry(c).Index() ) + Space +
	      TCollection_AsciiString( Standard_Integer(rc) ) + Space +
	      TCollection_AsciiString( Standard_Integer(gc) ) + Space +
	      TCollection_AsciiString( Standard_Integer(bc) ) + Space +
	      TCollection_AsciiString( "()]\n" ) ;

	file.Write( out, out.Length() ) ;

	if ( file.Failed() ) {
		// ERROR
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ;
        }

  }

  out = TCollection_AsciiString( "))" ) ;

  file.Write( out, out.Length() ) ;

  if ( file.Failed() ) {
	// ERROR
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ;
  }

  return( Standard_True ) ;
  
}


Standard_Boolean AlienImage_AidaAlienData::Read( OSD_File& file )

{ TCollection_AsciiString tmp, in ;
  Standard_Integer loc1, loc2, width, height, i, bblcount, l ,value ;
  TCollection_AsciiString HexaSet = "0123456789ABCDEFabcdef" ;
  int r,g,b,pixel,status ;
  Handle(Aspect_GenericColorMap) GenCmap = NULL ;

#ifdef TEST
  OSD_Path path; file.Path(path);
  TCollection_AsciiString ext = path.Extension(); ext.LowerCase();
  if( ext != ".aida" ) {
    TCollection_AsciiString sysname; path.SystemName(sysname);
#ifdef TRACE
    cout << " *** AlienImage_AidaAlienData::Read('" << sysname << "'). must have an '.aida' extension" << endl;
#endif
    return Standard_False;
  }
#endif

  Clear() ;

  // Extract AidaBYTEMAPS type "#BC"

  // Get "#"
  do { 
	  file.Read( tmp, 1) ;
  } while( !file.Failed() && tmp != TCollection_AsciiString( "#" ) ) ;

  if ( file.Failed() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }

  // Get "BC"
  file.Read( tmp, 2 ) ;

  if ( file.Failed() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  if ( tmp != TCollection_AsciiString( "BC" ) ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }

  // Get "(" Start of AidaBYTEMAPS size definition
  do { 
	  file.Read( tmp, 1) ;
  } while( !file.Failed() && tmp != TCollection_AsciiString( "(" ) ) ;

  if ( file.Failed() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  // Get "#" Start of AidaBYTEMAPS Pixel definition
  in.Clear() ;

  do { 
	file.Read( tmp, 1) ;

	if ( tmp.IsAscii() ) {
		in += tmp ;
	}
  } while( !file.Failed() && tmp != TCollection_AsciiString( "#" ) ) ;

  if ( file.Failed() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  // Extract Image size in "Width Height #"

  loc1 = 1 ;

  if ( ( loc2 = in.Location( 1, ' ', loc1, in.Length() ) ) == 0 ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }

  if ( ( loc1 ) > ( loc2-1 ) ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  tmp = in.SubString( loc1, loc2-1 ) ;

  if ( !tmp.IsIntegerValue() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }

  if ( ( width = tmp.IntegerValue() ) <= 0 ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }

  tmp = in.SubString( loc2, in.Length()-1 ) ;
  
  if ( !tmp.IsIntegerValue() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  if ( ( height = tmp.IntegerValue() ) <= 0 ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


#ifdef TRACE
  if ( Verbose )
    cout << "Width,Height :" << width << "," << height << endl << flush ;
#endif

  AllocData( width, height ) ;

  // Get "[" start of AidaBYTEMAPS Pixel definition

  do {
	file.Read( tmp, 1) ;
  } while( !file.Failed() && tmp != TCollection_AsciiString( "[" ) ) ;

  if ( file.Failed() ) {
	file.Seek( 0, OSD_FromBeginning ) ;
	return( Standard_False ) ; // ERROR
  }


  // Extract Pixel lines "#*................."
  in.Clear() ;

  for ( i = 0 ; i < height ; i++ ) {

 	// Get "#"
 	do { 
	  file.Read( tmp, 1) ;
  	} while( !file.Failed() && tmp != TCollection_AsciiString( "#" ) ) ;

  	if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  	}

	// Get "*"
	file.Read( tmp, 1) ;

  	if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  	}

	if ( tmp != TCollection_AsciiString( "*" ) ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  	}

        // Get Pixel line
	bblcount = 0 ;
        in.Clear() ;

	while ( bblcount < (2*width) ) {// Two byte per PIXEL
	  file.Read( tmp, 1) ; 

  	  if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  	  }

	  if ( tmp.IsAscii() ) {
		if ( tmp.FirstLocationInSet( HexaSet, 1, 1 ) ) {
			in += tmp ; bblcount++ ;
		}
		else {
			// Get Next line character before end of current line
			file.Seek( 0, OSD_FromBeginning ) ;
			return( Standard_False ) ; // ERROR
		}
	  }
	}

#ifdef TRACE
  if ( Verbose )
	cout << "Get one line :\"" << in << "\"\n" << flush ;
#endif

	for ( l = 0 ; l < in.Length() ; l += 2 ) {
		tmp = in.SubString( l+1, l+2 ) ;

		value = ( Standard_Integer )strtol( tmp.ToCString(), NULL, 16 );

		SetPixel( l/2 , i, value ) ;
#ifdef TRACE
  if ( Verbose > 1 )
	cout << "SetPixel(" << l/2 << "," << i << "," << value << ")\n" <<flush;
#endif
	}
  }

  // Get "]" end of Aida image data
  do { 
	  file.Read( tmp, 1) ;
  } while( !file.Failed() && tmp != TCollection_AsciiString( "]" ) ) ;

  if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  }

  // Get "(" start of Aida colors definition or ")" end of Aida ByteMaps
  do { 
	  file.Read( tmp, 1) ;
  } while( !file.Failed() && 
	   tmp != TCollection_AsciiString( "(" ) &&
	   tmp != TCollection_AsciiString( ")" ) ) ;

  if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  }

  if ( tmp == TCollection_AsciiString( ")" ) ) {
	return( Standard_True ) ; // No Color definition ERROR ??  
  }

  // Find Color definition "#[ Red Green Blue ... ]" color is from 0->32767

  myColors = GenCmap = new Aspect_GenericColorMap() ;
  myColorsIsDef	= Standard_True ;

  do {
    // Get "#" start of a Aida color definition or 
    // Get ")" end of Aida colors definition
    do { 
	  file.Read( tmp, 1) ;
    } while( !file.Failed() && 
	     tmp != TCollection_AsciiString( "#" ) &&
	     tmp != TCollection_AsciiString( ")" ) ) ;

    if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
    }

    if ( tmp == TCollection_AsciiString( "#" ) ) {

      // Get "[" start of a Aida color definition
      do { 
	  file.Read( tmp, 1) ;
      } while( !file.Failed() && tmp != TCollection_AsciiString( "[" ) ) ;

      if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
      }

      // Get "]" end of a Aida color definition
      in.Clear() ;

      do { 
	  file.Read( tmp, 1) ;
	  if ( tmp.IsAscii() && tmp != TCollection_AsciiString( "]" ) ) {
		in += tmp ;
	  }
      } while( !file.Failed() && tmp != TCollection_AsciiString( "]" ) ) ;

      if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
      }

      status = sscanf( in.ToCString(), "%d %d %d %d", &pixel,&r,&g,&b ) ;

      if ( status == 4 ) {
	GenCmap->AddEntry( 
		Aspect_ColorMapEntry( pixel,
			  Quantity_Color( r/32767.,g/32767.,b/32767.,
					   Quantity_TOC_RGB ) ) ) ;			

      }	

#ifdef TRACE
  if ( Verbose )
      cout << in << endl << flush ;
#endif
    }
    
  } while ( !file.Failed() && tmp != TCollection_AsciiString( ")" ) )  ;


  if ( file.Failed() ) {
		file.Seek( 0, OSD_FromBeginning ) ;
		return( Standard_False ) ; // ERROR
  }

  
  return( Standard_True ) ;
  
}

Handle_Image_Image AlienImage_AidaAlienData::ToImage() const

{ Standard_Integer x,y ;
  Handle(Image_PseudoColorImage) aPImage = 
	new Image_PseudoColorImage( 0, 0,
				    myData->RowLength(), myData->ColLength(),
				    myColors );

  for( y = 0 ; y < aPImage->Height() ; y++ ) {
    for( x = 0 ; x < aPImage->Width() ; x++ ) {
	aPImage->SetPixel( aPImage->LowerX()+x, 
			   aPImage->LowerY()+y, Pixel( x, y ) ) ;
    }
  }

  return aPImage ;
}

void AlienImage_AidaAlienData::FromImage( const Handle_Image_Image& anImage )

{ if ( anImage->IsInstance(STANDARD_TYPE(Image_PseudoColorImage)) ) {
	Handle(Image_PseudoColorImage) aPImage =
		Handle(Image_PseudoColorImage)::DownCast(anImage) ;

	FromPseudoColorImage( aPImage ) ;
  }
  else if ( anImage->IsInstance(STANDARD_TYPE(Image_ColorImage)) )  {
	Handle(Image_ColorImage) aCImage =
		Handle(Image_ColorImage)::DownCast(anImage) ;

	FromColorImage( aCImage ) ;
  }
  else {
 	 Standard_TypeMismatch_Raise_if( Standard_True,
	 "Attempt to convert a unknown Image_Image type to a AidaAlienData");
  }
}

void AlienImage_AidaAlienData::SetColorImageDitheringMethod( 
	const Image_DitheringMethod aMethod ,
	const Handle(Aspect_ColorMap)& aColorMap )

{ myDitheringMethod = aMethod ; myDitheringColorMap = aColorMap ; }

//------------------------------------------------------------------------------
//		Private Method
//------------------------------------------------------------------------------


void AlienImage_AidaAlienData::FromPseudoColorImage(
			const Handle_Image_PseudoColorImage& anImage)

{ Standard_Integer x,y ;

  AllocData( anImage->Width(), anImage->Height() ) ;

  myColors = anImage->ColorMap() ;
  myColorsIsDef	= Standard_True ;

  for ( y = 0 ; y < anImage->Height() ; y++ ) {
    for ( x = 0 ; x < anImage->Width() ; x++ ) {
	SetPixel( x, 
		  y, 
		  anImage->Pixel( anImage->LowerX()+x,
				  anImage->LowerY()+y ).Value() ) ;
    }
  }
}

void AlienImage_AidaAlienData::FromColorImage(
			const Handle_Image_ColorImage& anImage)

{ Image_Convertor Convertor ;

  Handle(Image_PseudoColorImage) aPImage = 
	new Image_PseudoColorImage( anImage->LowerX(), anImage->LowerY(),
				    anImage->Width(),  anImage->Height(),
				    myDitheringColorMap ) ;

  Convertor.SetDitheringMethod( myDitheringMethod ) ;

  aPImage = Convertor.Convert( anImage, myDitheringColorMap ) ;

  FromPseudoColorImage( aPImage ) ;
}

void AlienImage_AidaAlienData::AllocData( 
	const Standard_Integer dx, 
	const Standard_Integer dy )
{
  myData = new TColStd_HArray2OfInteger( 0, dy-1 , 0, dx-1 ) ;
  myDataIsDef = Standard_True ;
}

void AlienImage_AidaAlienData::SetPixel( 
	const Standard_Integer x, 
	const Standard_Integer y, 
	const Standard_Integer value )
{
  myData->SetValue( y, x, value ) ;
}

Standard_Integer AlienImage_AidaAlienData::Pixel( 
	const Standard_Integer x, const Standard_Integer y ) const

{ return myData->Value( y, x ) ; }