7fd59977 |
1 | #include <Aspect_GenericColorMap.hxx> |
2 | #include <Image_PseudoColorImage.hxx> |
3 | |
4 | #include <AlienImage_MemoryOperations.hxx> |
5 | #include <AlienImage_SUNRFFormat.hxx> |
6 | #include <Image_Convertor.hxx> |
7 | #include <AlienImage_SunRFAlienData.ixx> |
8 | #include <Aspect_ColorMapEntry.hxx> |
9 | |
10 | #include <Standard_Byte.hxx> |
11 | #include <Standard.hxx> |
12 | |
13 | #ifdef TRACE |
14 | static int Verbose = 1 ; |
15 | #endif |
16 | |
17 | #define RUN_FLAG 0x80 |
18 | |
19 | // Each line of the image is rounded out to a multiple of 16 bits |
20 | #define ROWBYTES() (((myHeader.ras_width*myHeader.ras_depth + 7 )/8 + 1 ) & ~1 ) |
21 | |
22 | |
23 | |
24 | AlienImage_SunRFAlienData::AlienImage_SunRFAlienData() |
25 | |
26 | { Clear() ; } |
27 | |
28 | void AlienImage_SunRFAlienData::SetFormat( |
29 | const AlienImage_SUNRFFormat aFormat ) |
30 | |
31 | { switch ( aFormat ) { |
32 | case AlienImage_SUNRF_Old : |
33 | myHeader.ras_type = RT_OLD ; break ; |
34 | case AlienImage_SUNRF_Standard : |
35 | myHeader.ras_type = RT_STANDARD ; break ; |
36 | case AlienImage_SUNRF_ByteEncoded : |
37 | myHeader.ras_type = RT_BYTE_ENCODED ; break ; |
38 | case AlienImage_SUNRF_RGB : |
39 | myHeader.ras_type = RT_FORMAT_RGB ; break ; |
40 | default : |
41 | cout << "SunRFAlienData : Unknown or Unsuported Format\n" ; |
42 | break ; |
43 | } |
44 | } |
45 | |
46 | AlienImage_SUNRFFormat AlienImage_SunRFAlienData::Format() const |
47 | |
48 | { AlienImage_SUNRFFormat ret = AlienImage_SUNRF_Unknown ; |
49 | |
50 | switch ( myHeader.ras_type ) { |
51 | case RT_OLD : |
52 | ret = AlienImage_SUNRF_Old ; break ; |
53 | case RT_STANDARD : |
54 | ret = AlienImage_SUNRF_Standard ; break ; |
55 | case RT_BYTE_ENCODED : |
56 | ret = AlienImage_SUNRF_ByteEncoded ; break ; |
57 | case RT_FORMAT_RGB : |
58 | ret = AlienImage_SUNRF_RGB ; break ; |
59 | } |
60 | |
61 | return ret ; |
62 | |
63 | } |
64 | |
65 | void AlienImage_SunRFAlienData::FreeData() |
66 | |
67 | { |
68 | if ( myData && myDataSize ) { |
69 | //Free all allocated memory |
70 | Standard::Free(myData) ; |
71 | myData = NULL ; |
72 | myDataSize = 0 ; |
73 | } |
74 | |
75 | if ( myRedData && myHeader.ras_maplength ) { |
76 | //Free all allocated memory |
77 | Standard::Free( myRedData) ; |
78 | myRedData = NULL ; |
79 | } |
80 | |
81 | if ( myGreenData && myHeader.ras_maplength ) { |
82 | //Free all allocated memory |
83 | Standard::Free(myGreenData) ; |
84 | myRedData = NULL ; |
85 | } |
86 | |
87 | if ( myBlueData && myHeader.ras_maplength ) { |
88 | //Free all allocated memory |
89 | Standard::Free(myBlueData) ; |
90 | myRedData = NULL ; |
91 | } |
92 | |
93 | } |
94 | |
95 | void AlienImage_SunRFAlienData::Clear() |
96 | |
97 | { FreeData() ; |
98 | |
99 | |
100 | myHeader.ras_magic = RAS_MAGIC ; |
101 | myHeader.ras_width = 0 ; |
102 | myHeader.ras_height = 0 ; |
103 | myHeader.ras_length = 0 ; |
104 | myHeader.ras_type = RT_STANDARD ; |
105 | myHeader.ras_maptype = RMT_NONE ; |
106 | myHeader.ras_maplength = 0 ; |
107 | |
108 | } |
109 | |
110 | Standard_Boolean AlienImage_SunRFAlienData::Write( OSD_File& file ) const |
111 | |
112 | { Standard_Integer size; |
113 | AlienImage_SUNRFFileHeader TheHeader = myHeader ; |
114 | |
115 | // Write out TheHeader information |
116 | |
117 | if ( myData && myDataSize && |
118 | myHeader.ras_type == RT_FORMAT_RGB && |
119 | myHeader.ras_depth == 8 ) { |
120 | // Convert PseudoColorImage to TrueColor |
121 | |
122 | Handle(Image_Image) aImage = ToImage() ; |
123 | |
124 | if ( aImage->IsKind( STANDARD_TYPE(Image_PseudoColorImage) ) ) { |
125 | Image_Convertor Convertor; |
126 | |
127 | Handle(Image_ColorImage) aCImage = |
128 | Convertor.Convert(Handle(Image_PseudoColorImage)::DownCast(aImage)); |
129 | |
130 | Handle(AlienImage_SunRFAlienData) newThis = |
131 | new AlienImage_SunRFAlienData() ; |
132 | |
133 | newThis->FromImage( aCImage ) ; |
134 | newThis->SetFormat( AlienImage_SUNRF_RGB ) ; |
135 | return newThis->Write( file ) ; |
136 | } |
137 | } |
138 | |
139 | size = ( Standard_Integer ) sizeof( TheHeader ) ; |
140 | |
141 | const Standard_Address pHeader = ( Standard_Address ) &TheHeader ; |
142 | |
143 | file.Write( pHeader, sizeof( TheHeader ) ) ; |
144 | |
145 | if ( file.Failed() ) { |
146 | // ERROR |
147 | file.Seek( 0, OSD_FromBeginning ) ; |
148 | return( Standard_False ) ; |
149 | } |
150 | |
151 | // write out the color map buffer |
152 | |
153 | if ( TheHeader.ras_maplength ) { |
154 | file.Write( myRedData, myHeader.ras_maplength/3 ) ; |
155 | file.Write( myGreenData, myHeader.ras_maplength/3 ) ; |
156 | file.Write( myBlueData, myHeader.ras_maplength/3 ) ; |
157 | |
158 | if ( file.Failed() ) { |
159 | // ERROR |
160 | file.Seek( 0, OSD_FromBeginning ) ; |
161 | return( Standard_False ) ; |
162 | } |
163 | } |
164 | |
165 | Standard_Integer rwbytes ; |
166 | |
167 | rwbytes = ROWBYTES() ; |
168 | |
169 | if ( myData && myDataSize ) { |
170 | if ( myHeader.ras_type == RT_OLD || |
171 | myHeader.ras_type == RT_STANDARD || |
172 | myHeader.ras_type == RT_FORMAT_RGB ) { |
173 | if ( myHeader.ras_type == RT_FORMAT_RGB ) { |
174 | // Swap Sun Default BGR Format to RGB |
175 | Standard_Byte *p = ( Standard_Byte * )myData ; |
176 | Standard_Byte tmp, *pix ; |
177 | Standard_Integer j, i ; |
178 | |
179 | if ( myHeader.ras_depth == 24 || myHeader.ras_depth == 32 ) { |
180 | for ( i = 0 ;i < myHeader.ras_height ; i++, p += rwbytes ) { |
181 | for ( j = 0, pix=p; j < myHeader.ras_width ; j++,pix+=3) { |
182 | if ( myHeader.ras_depth == 32 ) pix++ ; |
183 | tmp = *pix ; |
184 | *pix = *(pix+2) ; |
185 | *(pix+2) = tmp ; |
186 | } |
187 | } |
188 | } |
189 | else if ( myHeader.ras_depth == 8 ) { |
190 | // ERROR |
191 | file.Seek( 0, OSD_FromBeginning ) ; |
192 | return( Standard_False ) ; |
193 | } |
194 | } |
195 | |
196 | file.Write( myData, myDataSize ) ; |
197 | |
198 | if ( file.Failed() ) { |
199 | // ERROR |
200 | file.Seek( 0, OSD_FromBeginning ) ; |
201 | return( Standard_False ) ; |
202 | } |
203 | |
204 | if ( myHeader.ras_type == RT_FORMAT_RGB && |
205 | ( myHeader.ras_depth == 24 || myHeader.ras_depth == 32 ) ) { |
206 | // Swap RGB Format to Sun Default |
207 | Standard_Byte *p = ( Standard_Byte * )myData ; |
208 | Standard_Byte tmp, *pix ; |
209 | Standard_Integer j, i ; |
210 | |
211 | for ( i = 0 ;i < myHeader.ras_height ; i++, p += rwbytes ) { |
212 | for ( j = 0, pix=p; j < myHeader.ras_width ; j++,pix+=3) { |
213 | if ( myHeader.ras_depth == 32 ) pix++ ; |
214 | tmp = *pix ; |
215 | *pix = *(pix+2) ; |
216 | *(pix+2) = tmp ; |
217 | } |
218 | } |
219 | } |
220 | } |
221 | else if ( myHeader.ras_type == RT_BYTE_ENCODED ) { |
222 | Standard_Byte *p = ( Standard_Byte * )myData ; |
223 | Standard_Integer i ; |
224 | |
225 | for ( i = 0 ; i < myHeader.ras_height ; i++, p += rwbytes ) { |
226 | if ( WritePixelRow( file, ( Standard_Address) p, rwbytes ) == |
227 | Standard_False ) { |
228 | file.Seek( 0, OSD_FromBeginning ) ; |
229 | return( Standard_False ) ; |
230 | } |
231 | |
232 | } |
233 | } |
234 | |
235 | } |
236 | |
237 | return( Standard_True ) ; |
238 | |
239 | } |
240 | |
241 | |
242 | Standard_Boolean AlienImage_SunRFAlienData::Read( OSD_File& file ) |
243 | |
244 | { Standard_Integer bblcount, size ; |
245 | Standard_Address pheader = ( Standard_Address ) &myHeader ; |
246 | |
247 | // Read in myHeader information |
248 | |
249 | file.Read( pheader, sizeof( myHeader ), bblcount ) ; |
250 | |
251 | if ( file.Failed() || ( bblcount != sizeof( myHeader ) ) ) { |
252 | // ERROR |
253 | file.Seek( 0, OSD_FromBeginning ) ; |
254 | return( Standard_False ) ; |
255 | } |
256 | |
257 | // check to see if the dump file is in the proper format */ |
258 | if (myHeader.ras_magic != RAS_MAGIC) { |
259 | // ERROR "XWD file format version mismatch." |
260 | |
261 | file.Seek( 0, OSD_FromBeginning ) ; |
262 | return( Standard_False ) ; |
263 | } |
264 | |
265 | #ifdef TRACE |
266 | if ( Verbose ) cout << myHeader << endl << flush ; |
267 | #endif |
268 | |
269 | // read in the color map buffer |
270 | |
271 | if ( myHeader.ras_maplength ) { |
272 | size = myHeader.ras_maplength / 3 ; |
273 | |
274 | myRedData = Standard::Allocate( size ) ; |
275 | myGreenData = Standard::Allocate( size ) ; |
276 | myBlueData = Standard::Allocate( size ) ; |
277 | |
278 | file.Read( myRedData, size, bblcount ) ; |
279 | file.Read( myGreenData, size, bblcount ) ; |
280 | file.Read( myBlueData, size, bblcount ) ; |
281 | |
282 | if ( file.Failed() || ( bblcount != size ) ) { |
283 | // ERROR |
284 | file.Seek( 0, OSD_FromBeginning ) ; |
285 | return( Standard_False ) ; |
286 | } |
287 | |
288 | #ifdef TRACE |
289 | if ( Verbose ) { |
290 | Standard_Byte *r = ( Standard_Byte * )myRedData ; |
291 | Standard_Byte *g = ( Standard_Byte * )myGreenData ; |
292 | Standard_Byte *b = ( Standard_Byte * )myBlueData ; |
293 | |
294 | for (i = 0 ; i < myHeader.ncolors; i++,p++) { |
295 | cout << "(" << r << "," << g << "," << b << ")\n" << flush ; |
296 | } |
297 | |
298 | } |
299 | #endif |
300 | } |
301 | |
302 | if ( myHeader.ras_width && myHeader.ras_height && myHeader.ras_depth ) { |
303 | Standard_Integer rwbytes ; |
304 | |
305 | rwbytes = ROWBYTES() ; |
306 | |
307 | myDataSize = rwbytes * myHeader.ras_height ; |
308 | |
309 | myData = Standard::Allocate( myDataSize ) ; |
310 | |
311 | if ( myHeader.ras_type == RT_OLD || |
312 | myHeader.ras_type == RT_STANDARD || |
313 | myHeader.ras_type == RT_FORMAT_RGB ) { |
314 | file.Read( myData, myDataSize, bblcount ) ; |
315 | |
316 | if ( file.Failed() || ( bblcount != myDataSize ) ) { |
317 | // ERROR |
318 | file.Seek( 0, OSD_FromBeginning ) ; |
319 | return( Standard_False ) ; |
320 | } |
321 | |
322 | if ( myHeader.ras_type == RT_FORMAT_RGB && |
323 | ( myHeader.ras_depth == 24 || myHeader.ras_depth == 32 )) { |
324 | // Swap RGB to Sun Default BGR Format |
325 | Standard_Byte *p = ( Standard_Byte * )myData ; |
326 | Standard_Byte tmp, *pix ; |
327 | Standard_Integer i, j ; |
328 | |
329 | for ( i = 0 ; i < myHeader.ras_height ; i++, p += rwbytes ) { |
330 | for ( j = 0, pix = p; j < myHeader.ras_width ; j++,pix+=3) { |
331 | if ( myHeader.ras_depth == 32 ) pix++ ; |
332 | tmp = *pix ; |
333 | *pix = *(pix+2) ; |
334 | *(pix+2) = tmp ; |
335 | } |
336 | } |
337 | } |
338 | } |
339 | else if ( myHeader.ras_type == RT_BYTE_ENCODED ) { |
340 | Standard_Byte *p = ( Standard_Byte * )myData ; |
341 | Standard_Integer i ; |
342 | |
343 | for ( i = 0 ; i < myHeader.ras_height ; i++, p += rwbytes ) { |
344 | if ( ReadPixelRow( file, ( Standard_Address) p, rwbytes ) == |
345 | Standard_False ) { |
346 | file.Seek( 0, OSD_FromBeginning ) ; |
347 | return( Standard_False ) ; |
348 | } |
349 | } |
350 | } |
351 | |
352 | } |
353 | |
354 | return( Standard_True ) ; |
355 | |
356 | } |
357 | |
358 | Handle_Image_Image AlienImage_SunRFAlienData::ToImage() const |
359 | |
360 | { if ( myHeader.ras_depth <= 8 && |
361 | myHeader.ras_maplength ) { |
362 | return( ToPseudoColorImage() ) ; |
363 | } |
364 | else if ( myHeader.ras_depth == 24 || myHeader.ras_depth == 32 ) { |
365 | return( ToColorImage() ) ; |
366 | } |
367 | else { |
368 | Standard_TypeMismatch_Raise_if( Standard_True, |
369 | "Attempt to convert a SunRFAlienData to a unknown Image_Image type"); |
370 | |
371 | return( NULL ) ; |
372 | } |
373 | } |
374 | |
375 | void AlienImage_SunRFAlienData::FromImage( const Handle_Image_Image& anImage ) |
376 | |
377 | { if ( anImage->Type() == Image_TOI_PseudoColorImage ) { |
378 | Handle(Image_PseudoColorImage) aPImage = |
379 | Handle(Image_PseudoColorImage)::DownCast(anImage) ; |
380 | |
381 | FromPseudoColorImage( aPImage ) ; |
382 | } |
383 | else if ( anImage->Type() == Image_TOI_ColorImage ) { |
384 | Handle(Image_ColorImage) aCImage = |
385 | Handle(Image_ColorImage)::DownCast(anImage) ; |
386 | |
387 | FromColorImage( aCImage ) ; |
388 | } |
389 | else { |
390 | Standard_TypeMismatch_Raise_if( Standard_True, |
391 | "Attempt to convert a unknown Image_Image type to a SunRFAlienData"); |
392 | } |
393 | } |
394 | |
395 | //------------------------------------------------------------------------------ |
396 | // Private Method |
397 | //------------------------------------------------------------------------------ |
398 | |
399 | Standard_Boolean AlienImage_SunRFAlienData::ReadPixelRow( |
400 | OSD_File& file, |
401 | const Standard_Address pdata, |
402 | const Standard_Integer rwbytes) |
403 | |
404 | { Standard_Byte *p = ( Standard_Byte * )pdata ; |
405 | Standard_Byte byte, val ; |
406 | Standard_Integer RLEcnt, PixelCount, i, bblcount ; |
407 | Standard_Address pb = ( Standard_Address ) &byte ; |
408 | |
409 | PixelCount = 0 ; |
410 | |
411 | while ( PixelCount < myHeader.ras_width ) { |
412 | file.Read( pb, 1, bblcount ) ; |
413 | |
414 | if ( file.Failed() || ( bblcount != 1 ) ) { |
415 | // ERROR |
416 | file.Seek( 0, OSD_FromBeginning ) ; |
417 | return( Standard_False ) ; |
418 | } |
419 | |
420 | if ( byte != RUN_FLAG ) { |
421 | // Get a single pixel byte |
422 | RLEcnt = 1 , val = byte ; |
423 | } |
424 | else { // RLE Flag |
425 | file.Read( pb, 1, bblcount ) ; |
426 | |
427 | if ( file.Failed() || ( bblcount != 1 ) ) { |
428 | // ERROR |
429 | file.Seek( 0, OSD_FromBeginning ) ; |
430 | return( Standard_False ) ; |
431 | } |
432 | |
433 | if ( byte == 0 ) { |
434 | RLEcnt = 1 , val = RUN_FLAG ; |
435 | } |
436 | else { |
437 | RLEcnt = byte ; |
438 | |
439 | file.Read( pb, 1, bblcount ) ; |
440 | |
441 | if ( file.Failed() || ( bblcount != 1 ) ) { |
442 | // ERROR |
443 | file.Seek( 0, OSD_FromBeginning ) ; |
444 | return( Standard_False ) ; |
445 | } |
446 | |
447 | val = byte ; |
448 | } |
449 | |
450 | for ( i = 0 ; i < RLEcnt ; i++, PixelCount++, p++ ) *p = val ; |
451 | } |
452 | } |
453 | |
454 | return( Standard_True ) ; |
455 | |
456 | } |
457 | |
458 | Standard_Boolean AlienImage_SunRFAlienData::WritePixelRow( |
459 | OSD_File& file, |
460 | const Standard_Address pdata, |
461 | const Standard_Integer rwbytes ) const |
462 | { Standard_Integer n, n1, n2 = 0; |
463 | Standard_Byte *scanln = ( Standard_Byte * ) pdata ; |
464 | Standard_Byte b ; |
465 | |
466 | while ( n2 < rwbytes ) { |
467 | n1 = n2 ; |
468 | n2 = n1 + 1 ; |
469 | |
470 | while( ( n2 < rwbytes ) && ( scanln[n1] == scanln[n2] ) ) n2++ ; |
471 | |
472 | n = n2 - n1 ; |
473 | |
474 | if ( n == 1 ) { |
475 | b = scanln[n1]; file.Write( ( Standard_Address ) &b, 1 ) ; |
476 | |
477 | if ( scanln[n1] == RUN_FLAG ) { |
478 | b = 0 ; file.Write( ( Standard_Address ) &b, 1 ) ; |
479 | } |
480 | |
481 | if ( file.Failed() ) { |
482 | // ERROR |
483 | file.Seek( 0, OSD_FromBeginning ) ; |
484 | return( Standard_False ) ; |
485 | } |
486 | |
487 | } |
488 | else { |
489 | while ( n > 256 ) { |
490 | b = RUN_FLAG ; file.Write( ( Standard_Address ) &b, 1) ; |
491 | b = 255 ; file.Write( ( Standard_Address ) &b, 1) ; |
492 | b = scanln[n1];file.Write( ( Standard_Address ) &b, 1) ; |
493 | n -= 256 ; |
494 | |
495 | if ( file.Failed() ) { |
496 | // ERROR |
497 | file.Seek( 0, OSD_FromBeginning ) ; |
498 | return( Standard_False ) ; |
499 | } |
500 | } |
501 | |
502 | b = RUN_FLAG ; file.Write( ( Standard_Address ) &b, 1 ) ; |
503 | b = n-1 ; file.Write( ( Standard_Address ) &b, 1 ) ; |
504 | b = scanln[n1];file.Write( ( Standard_Address ) &b, 1 ) ; |
505 | |
506 | } |
507 | } |
508 | |
509 | return( Standard_True ) ; |
510 | |
511 | } |
512 | |
513 | void AlienImage_SunRFAlienData::FromPseudoColorImage( |
514 | const Handle(Image_PseudoColorImage)& TheImage) |
515 | |
516 | { Standard_Integer rowbytes,i ; |
517 | Standard_Integer x, y, pix; |
518 | Handle(Image_PseudoColorImage)anImage = |
519 | TheImage->Squeeze(Aspect_IndexPixel( 0 )) ; |
520 | Handle(Aspect_ColorMap) Cmap = anImage->ColorMap() ; |
521 | Aspect_ColorMapEntry aEntry ; |
522 | |
523 | FreeData() ; |
524 | myHeader.ras_magic = RAS_MAGIC ; |
525 | myHeader.ras_width = anImage->Width() ; |
526 | myHeader.ras_height = anImage->Height() ; |
527 | myHeader.ras_depth = 8 ; |
528 | |
529 | rowbytes = ROWBYTES() ; |
530 | |
531 | myDataSize = myHeader.ras_height * rowbytes ; |
532 | myData = Standard::Allocate( myDataSize ) ; |
533 | myHeader.ras_length = myDataSize ; |
534 | |
535 | myHeader.ras_maptype = RMT_EQUAL_RGB ; |
536 | myHeader.ras_maplength = Cmap->Size() ; |
537 | |
538 | myRedData = Standard::Allocate( myHeader.ras_maplength ) ; |
539 | myGreenData = Standard::Allocate( myHeader.ras_maplength ) ; |
540 | myBlueData = Standard::Allocate( myHeader.ras_maplength ) ; |
541 | |
542 | Standard_Byte *pr = ( Standard_Byte * ) myRedData ; |
543 | Standard_Byte *pg = ( Standard_Byte * ) myGreenData ; |
544 | Standard_Byte *pb = ( Standard_Byte * ) myBlueData ; |
545 | |
546 | for ( i = 0 ; i < myHeader.ras_maplength ; i++, pr++, pg++, pb++ ) { |
547 | aEntry = Cmap->FindEntry( i ) ; |
548 | *pr = ( Standard_Byte ) ( aEntry.Color().Red() * 255. + 0.5 ) ; |
549 | *pg = ( Standard_Byte ) ( aEntry.Color().Green() * 255. + 0.5 ) ; |
550 | *pb = ( Standard_Byte ) ( aEntry.Color().Blue() * 255. + 0.5 ) ; |
551 | } |
552 | |
553 | myHeader.ras_maplength *= 3 ; |
554 | |
555 | if ( myData != NULL ) { |
556 | Standard_Byte *pr = ( Standard_Byte * ) myData ; |
557 | Standard_Byte *p ; |
558 | |
559 | for ( y = 0 ; y < myHeader.ras_height ; y++, pr += rowbytes ) { |
560 | for ( x = 0, p = pr ; x < myHeader.ras_width ; x++ ) { |
561 | |
562 | pix = anImage->Pixel( anImage->LowerX()+x , |
563 | anImage->LowerY()+y ).Value() ; |
564 | |
565 | *p = ( Standard_Byte ) pix ; p++ ; |
566 | } |
567 | } |
568 | } |
569 | } |
570 | |
571 | void AlienImage_SunRFAlienData::FromColorImage( |
572 | const Handle_Image_ColorImage& anImage) |
573 | |
574 | { Standard_Integer rowbytes ; |
575 | Standard_Integer x, y; |
576 | Quantity_Color col ; |
577 | Standard_Real r,g,b ; |
578 | |
579 | FreeData() ; |
580 | |
581 | myHeader.ras_magic = RAS_MAGIC ; |
582 | myHeader.ras_width = anImage->Width() ; |
583 | myHeader.ras_height = anImage->Height() ; |
584 | myHeader.ras_depth = 24 ; |
585 | |
586 | rowbytes = ROWBYTES() ; |
587 | |
588 | myDataSize = myHeader.ras_height * rowbytes ; |
589 | myData = Standard::Allocate( myDataSize ) ; |
590 | myHeader.ras_length = myDataSize ; |
591 | |
592 | myHeader.ras_maptype = RMT_NONE ; |
593 | myHeader.ras_maplength = 0 ; |
594 | |
595 | if ( myData != NULL ) { |
596 | Standard_Byte *pr = ( Standard_Byte * ) myData ; |
597 | Standard_Byte *p ; |
598 | |
599 | for ( y = 0 ; y < myHeader.ras_height ; y++, pr += rowbytes ) { |
600 | for ( x = 0, p = pr ; x < myHeader.ras_width ; x++ ) { |
601 | |
602 | col = anImage->Pixel( anImage->LowerX()+x , |
603 | anImage->LowerY()+y ).Value() ; |
604 | |
605 | r = ( Standard_Integer ) ( col.Red() * 255. + 0.5 ); |
606 | g = ( Standard_Integer ) ( col.Green() * 255. + 0.5 ); |
607 | b = ( Standard_Integer ) ( col.Blue() * 255. + 0.5 ); |
608 | |
609 | *p = ( Standard_Byte ) b ; p++ ; |
610 | *p = ( Standard_Byte ) g ; p++ ; |
611 | *p = ( Standard_Byte ) r ; p++ ; |
612 | } |
613 | } |
614 | } |
615 | } |
616 | |
617 | Handle_Image_ColorImage AlienImage_SunRFAlienData::ToColorImage() const |
618 | |
619 | { Aspect_ColorPixel CPixel ; |
620 | Quantity_Color acolor ; |
621 | Handle(Image_ColorImage) ret_image = NULL ; |
622 | Standard_Integer x,y, rowbytes ; |
623 | Standard_Real r,g,b ; |
624 | Standard_Byte *pr = ( Standard_Byte * ) myData ; |
625 | Standard_Byte *p ; |
626 | |
627 | if ( myHeader.ras_depth == 24 || myHeader.ras_depth == 32 ) { |
628 | ret_image = new Image_ColorImage( 0,0, |
629 | (Standard_Integer)myHeader.ras_width, |
630 | (Standard_Integer)myHeader.ras_height ) ; |
631 | |
632 | rowbytes = ROWBYTES() ; |
633 | |
634 | for ( y = 0 ; y < myHeader.ras_height ; y++, pr += rowbytes ) { |
635 | for ( x = 0, p = pr ; x < myHeader.ras_width ; x++ ) { |
636 | if ( myHeader.ras_depth == 32 ) p++ ; // Skeep Alpha |
637 | b = ( Standard_Real ) *p / 255. ; p++ ; |
638 | g = ( Standard_Real ) *p / 255. ; p++ ; |
639 | r = ( Standard_Real ) *p / 255. ; p++ ; |
640 | |
641 | acolor.SetValues( r,g,b, Quantity_TOC_RGB ) ; |
642 | |
643 | CPixel.SetValue ( acolor ) ; |
644 | |
645 | ret_image->SetPixel( ret_image->LowerX()+x , |
646 | ret_image->LowerY()+y, CPixel ) ; |
647 | } |
648 | } |
649 | |
650 | } |
651 | |
652 | return( ret_image ) ; |
653 | } |
654 | |
655 | Handle_Image_PseudoColorImage AlienImage_SunRFAlienData::ToPseudoColorImage() |
656 | const |
657 | |
658 | { Standard_Real r,g,b ; |
659 | Standard_Integer x, y ; |
660 | Handle(Image_PseudoColorImage) ret_image = NULL ; |
661 | |
662 | if ( myHeader.ras_depth <= 8 && |
663 | myHeader.ras_maplength ) { |
664 | Standard_Integer i,rowbytes ; |
665 | Aspect_ColorMapEntry Centry ; |
666 | Quantity_Color color ; |
667 | Aspect_IndexPixel IPixel ; |
668 | Standard_Byte *red = ( Standard_Byte * ) myRedData ; |
669 | Standard_Byte *green = ( Standard_Byte * ) myGreenData ; |
670 | Standard_Byte *blue = ( Standard_Byte * ) myBlueData ; |
671 | Standard_Byte *p ; |
672 | Standard_Byte *pr = ( Standard_Byte * ) myData ; |
673 | Handle(Aspect_GenericColorMap) colormap = |
674 | new Aspect_GenericColorMap(); |
675 | |
676 | for ( i = 0 ; i < myHeader.ras_maplength/3 ; i++, red++, green++, blue++ ) { |
677 | r = ( Standard_Real ) *red / 255. ; |
678 | g = ( Standard_Real ) *green / 255. ; |
679 | b = ( Standard_Real ) *blue / 255. ; |
680 | color.SetValues( r,g,b, Quantity_TOC_RGB ); |
681 | Centry.SetValue( i, color ) ; |
682 | colormap->AddEntry( Centry ) ; |
683 | } |
684 | |
685 | ret_image = new Image_PseudoColorImage( 0,0, |
686 | Standard_Integer(myHeader.ras_width), |
687 | Standard_Integer(myHeader.ras_height), |
688 | colormap ) ; |
689 | |
690 | rowbytes = ROWBYTES() ; |
691 | |
692 | for ( y = 0 ; y < myHeader.ras_height ; y++, pr += rowbytes ) { |
693 | for ( x = 0, p = pr ; x < myHeader.ras_width ; x++, p++ ) { |
694 | |
695 | IPixel.SetValue( Standard_Integer( *p ) ) ; |
696 | ret_image->SetPixel( ret_image->LowerX()+x , |
697 | ret_image->LowerY()+y, IPixel ) ; |
698 | } |
699 | } |
700 | } |
701 | |
702 | return ret_image ; |
703 | } |
704 | |