import PixelImage from '../model/PixelImage' ; export default class C64Layout { public static concat ( arrayBuffers ) { if ( arrayBuffers . length === 1 ) { return arrayBuffers [ 0 ] ; } return arrayBuffers . reduce ( ( total , current ) => { const result = new Uint8Array ( total . length + current . length ) ; result . set ( total , 0 ) ; result . set ( current , total . length ) ; return result ; } ) ; } public static pad ( buffer , numberOfBytes ) { return this . concat ( [ buffer , new Uint8Array ( numberOfBytes ) ] ) ; } public static convertBitmap ( pixelImage ) { const bitmapSize = ( pixelImage . mode . width * pixelImage . mode . height ) / pixelImage . mode . pixelsPerByte ( ) ; const bitmap = new Uint8Array ( bitmapSize ) ; let bitmapIndex = 0 ; pixelImage . mode . forEachCell ( 0 , ( x , y ) => { pixelImage . mode . forEachCellRow ( y , rowY => { pixelImage . mode . forEachByte ( x , byteX => { let packedByte = 0 ; if ( byteX >= pixelImage . mode . fliBugSize ) { pixelImage . mode . forEachPixel ( byteX , ( pixelX , shiftTimes ) => { packedByte |= pixelImage . mapPixelIndex ( pixelX , rowY ) << shiftTimes ; } ) ; } bitmap [ bitmapIndex ] = packedByte ; bitmapIndex += 1 ; } ) ; } ) ; } ) ; return bitmap ; } public static convertScreenram ( pixelImage , lowerColorIndex , upperColorIndex , yOffset = 0 ) { return pixelImage . extractAttributeData ( yOffset , ( x , y ) => { return ( ( ( pixelImage . colorMaps [ upperColorIndex ] . get ( x , y ) << 4 ) & 0xf0 ) | ( pixelImage . colorMaps [ lowerColorIndex ] . get ( x , y ) & 0x0f ) ) ; } ) ; } public static convertColorram ( pixelImage , colorMapIndex ) { return pixelImage . extractAttributeData ( 0 , ( x , y ) => { return pixelImage . colorMaps [ colorMapIndex ] . get ( x , y ) & 0x0f ; } ) ; } }