350 loadBMPWidth:w height:h bytesPerPixel:bpp from:aStream into:data |
348 loadBMPWidth:w height:h bytesPerPixel:bpp from:aStream into:data |
351 |buff idx fileBytesPerRow imgBytesPerRow align| |
349 |buff idx fileBytesPerRow imgBytesPerRow align| |
352 |
350 |
353 align := 4. |
351 align := 4. |
354 |
352 |
355 compression == 0 ifTrue:[ |
353 ((compression == 0) or:[compression == 3]) ifTrue:[ |
356 imgBytesPerRow := w * bpp. |
354 imgBytesPerRow := w * bpp. |
357 fileBytesPerRow := imgBytesPerRow. |
355 fileBytesPerRow := imgBytesPerRow. |
358 (fileBytesPerRow bitAnd:(align-1)) ~~ 0 ifTrue:[ |
356 (fileBytesPerRow bitAnd:(align-1)) ~~ 0 ifTrue:[ |
359 fileBytesPerRow := (fileBytesPerRow bitAnd:((align-1) bitInvert)) + align. |
357 fileBytesPerRow := (fileBytesPerRow bitAnd:((align-1) bitInvert)) + align. |
360 ]. |
358 ]. |
361 "/ |
359 "/ |
362 "/ stupid - last row comes first |
360 "/ stupid - last row comes first |
363 "/ |
361 "/ |
364 idx := imgBytesPerRow * (h - 1) + 1. |
362 idx := imgBytesPerRow * (h - 1) + 1. |
365 buff := ByteArray uninitializedNew:fileBytesPerRow. |
363 buff := ByteArray uninitializedNew:fileBytesPerRow. |
366 |
364 |
367 1 to:h do:[:row | |
365 1 to:h do:[:row | |
368 (aStream nextBytes:fileBytesPerRow into:buff) ~~ fileBytesPerRow ifTrue:[ |
366 (aStream nextBytes:fileBytesPerRow into:buff) == fileBytesPerRow ifFalse:[ |
369 ^ false |
367 ^ false |
370 ]. |
368 ]. |
371 data replaceFrom:idx to:idx+imgBytesPerRow-1 with:buff. |
369 data replaceFrom:idx to:idx+imgBytesPerRow-1 with:buff. |
372 idx := idx - imgBytesPerRow. |
370 idx := idx - imgBytesPerRow. |
373 ]. |
371 ]. |
374 ^ true |
372 compression == 3 ifTrue:[ |
375 ]. |
373 self breakPoint:#cg. "/ TODO: check what we have to do here... |
|
374 ]. |
|
375 ^ true |
|
376 ]. |
|
377 "/ 'BMPReader: unsupported compression: ' infoPrint. compression infoPrintCR. |
|
378 self fileFormatError:('unsupported compression:', compression printString). |
376 ^ false. |
379 ^ false. |
377 ! |
380 ! |
378 |
381 |
379 loadBMPWidth:w height:h depth:d from:aStream into:data |
382 loadBMPWidth:w height:h depth:d from:aStream into:data |
380 "helper: load a BMP image" |
383 "helper: load a BMP image" |
384 ^ self loadUncompressedFrom:aStream into:data. |
387 ^ self loadUncompressedFrom:aStream into:data. |
385 ]. |
388 ]. |
386 compression == 1 ifTrue:[ |
389 compression == 1 ifTrue:[ |
387 ^ self loadRLECompressedBMP8From:aStream into:data. |
390 ^ self loadRLECompressedBMP8From:aStream into:data. |
388 ]. |
391 ]. |
389 self breakPoint:#cg info:'unhandled compression'. |
392 "/ self breakPoint:#cg info:'unhandled compression'. |
|
393 self fileFormatError:('unsupported compression:', compression printString). |
390 ^ false |
394 ^ false |
391 ]. |
395 ]. |
392 d == 4 ifTrue:[ |
396 d == 4 ifTrue:[ |
393 ^ self loadBMP4From:aStream into:data |
397 ^ self loadBMP4From:aStream into:data |
394 ]. |
398 ]. |
413 self swapBytesFromRGB_to_BGR. |
417 self swapBytesFromRGB_to_BGR. |
414 ]. |
418 ]. |
415 |
419 |
416 ^ true |
420 ^ true |
417 ]. |
421 ]. |
|
422 self fileFormatError:('unsupported depth:', d printString). |
418 ^ false |
423 ^ false |
419 |
424 |
420 "Created: / 17.9.1995 / 18:48:11 / claus" |
425 "Created: / 17.9.1995 / 18:48:11 / claus" |
421 "Modified: / 3.2.1998 / 20:21:16 / cg" |
426 "Modified: / 3.2.1998 / 20:21:16 / cg" |
|
427 ! |
|
428 |
|
429 loadRLECompressedBMP4From:aStream into:aByteArray |
|
430 "load bmp-rle-4 pixel imagedata" |
|
431 |
|
432 |bytesPerRowInData x y dstIndex lineStartIndex cnt pair clr1 clr2 code n nbyte| |
|
433 |
|
434 bytesPerRowInData := self bytesPerRow. |
|
435 x := 0. |
|
436 y := height - 1. |
|
437 lineStartIndex := (y * bytesPerRowInData) + 1. |
|
438 dstIndex := lineStartIndex. |
|
439 |
|
440 [ y < height ] whileTrue:[ |
|
441 cnt := aStream nextByte. |
|
442 pair := aStream nextByte. |
|
443 cnt ~~ 0 ifTrue:[ |
|
444 clr1 := pair bitShift:-4. |
|
445 clr2 := pair bitAnd:16rF. |
|
446 [cnt > 1] whileTrue:[ |
|
447 aByteArray at:dstIndex put:((clr2 << 4) bitOr:clr1). |
|
448 dstIndex := dstIndex + 1. |
|
449 cnt := cnt - 2. |
|
450 x := x + 2. |
|
451 ]. |
|
452 (cnt > 0) ifTrue:[ |
|
453 "/ got odd count |
|
454 aByteArray at:dstIndex put:clr1. |
|
455 x := x + 1. |
|
456 self halt. |
|
457 ]. |
|
458 ] ifFalse:[ |
|
459 "/ cnt == 0: escape codes */ |
|
460 code := pair. |
|
461 code == 0 ifTrue:[ |
|
462 "/ end of line |
|
463 x := 0. |
|
464 y := y - 1. |
|
465 lineStartIndex := lineStartIndex - bytesPerRowInData. |
|
466 dstIndex := lineStartIndex. |
|
467 ] ifFalse:[ |
|
468 code == 1 ifTrue:[ |
|
469 "/ end of pic |
|
470 ^ true |
|
471 ]. |
|
472 code == 2 ifTrue:[ |
|
473 "/ delta |
|
474 x := x + aStream nextSignedByte. |
|
475 y := y - aStream nextSignedByte. |
|
476 lineStartIndex := (y * bytesPerRowInData) + 1. |
|
477 dstIndex := lineStartIndex + x. |
|
478 ] ifFalse:[ |
|
479 "/ absolute; cnt pixels coming |
|
480 cnt := code. |
|
481 nbyte := cnt // 2. |
|
482 n := aStream nextBytes:nbyte into:aByteArray startingAt:dstIndex. |
|
483 n ~~ nbyte ifTrue:[^ false]. |
|
484 dstIndex := dstIndex + nbyte. |
|
485 x := x + cnt. |
|
486 |
|
487 cnt odd ifTrue:[ |
|
488 clr1 := aStream nextByte. |
|
489 aByteArray at:dstIndex put:clr1. |
|
490 x := x + 1. |
|
491 self halt. |
|
492 ]. |
|
493 |
|
494 "/ odd count - padd |
|
495 nbyte odd ifTrue:[ |
|
496 aStream skip:1. |
|
497 ]. |
|
498 ]. |
|
499 ]. |
|
500 ]. |
|
501 ]. |
|
502 ^ true. |
422 ! |
503 ! |
423 |
504 |
424 loadRLECompressedBMP8From:aStream into:aByteArray |
505 loadRLECompressedBMP8From:aStream into:aByteArray |
425 "load bmp-8 bit per pixel imagedata" |
506 "load bmp-8 bit per pixel imagedata" |
426 |
507 |