Читаем снизу вверх с помощью смещения указателя f_seek' ом, слева направо и особое внимание обратите на это:
padding = 0;
scanlinebytes = Size.width * byte_pixel;
while ( ( scanlinebytes + padding ) % 4 != 0 )
padding++;
psw = scanlinebytes + padding;
------------------------------------------------------------------------------------------------------------| ImgSize LoadBitmapFromSD(uint8_t *NameOfFile, uint32_t AddressOfImage) | |
| { | |
| uint32_t index = 0, byte_pixel = 0; | |
| uint32_t psw, scanlinebytes, padding; | |
| ImgSize Size; | |
| // uint32_t address; | |
| uint32_t input_color_mode = 0; | |
| uint8_t pbmp[DisplayWIDTH*4]; | |
| uint8_t res; //variable for return values | |
| FATFS fs; //fat object FATFS | |
| FIL OurFile; // this is our file here | |
| UINT br; //just counter | |
| DWORD PositionOfFile, LineBytesSize; | |
| res = f_mount(&fs,"0:",1); | |
| if (res == FR_OK){ | |
| //open the file | |
| res = f_open(&OurFile,(char const*)NameOfFile,FA_READ); | |
| if (res == FR_OK)f_read(&OurFile, pbmp, 30, &br); | |
| } | |
| else{ | |
| //íå óäàëîñü ñìîíòèðîâàòü äèñê | |
| Size.height = Size.width =0; | |
| return Size; | |
| } | |
| /* Get bitmap data address offset */ | |
| index = *(__IO uint16_t *) (pbmp + 10); | |
| index |= (*(__IO uint16_t *) (pbmp + 12)) << 16; | |
| /* Read bitmap width */ | |
| Size.width = *(uint16_t *) (pbmp + 18); | |
| Size.width |= (*(uint16_t *) (pbmp + 20)) << 16; | |
| /* Read bitmap height */ | |
| Size.height = *(uint16_t *) (pbmp + 22); | |
| Size.height |= (*(uint16_t *) (pbmp + 24)) << 16; | |
| /* Read bit/pixel */ | |
| byte_pixel = (*(uint16_t *) (pbmp + 28))/8; | |
| /* Set the address */ | |
| /* Get the layer pixel format */ | |
| if (byte_pixel == 4) | |
| { | |
| input_color_mode = CM_ARGB8888; | |
| } | |
| else if (byte_pixel == 2) | |
| { | |
| input_color_mode = CM_RGB565; | |
| } | |
| else | |
| { | |
| input_color_mode = CM_RGB888; | |
| } | |
| /* Bypass the bitmap header */ | |
| padding = 0; | |
| scanlinebytes = Size.width * byte_pixel; | |
| while ( ( scanlinebytes + padding ) % 4 != 0 ) | |
| padding++; | |
| psw = scanlinebytes + padding; | |
| PositionOfFile = index + psw * (Size.height - 1); | |
| LineBytesSize = byte_pixel * Size.width; | |
| res = f_lseek(&OurFile, PositionOfFile); //pointer to the last line of bitmap | |
| res = f_read(&OurFile, &pbmp[0], LineBytesSize, &br); | |
| /* Convert picture to ARGB8888 pixel format */ | |
| for(index=0; index < Size.height; index++) | |
| { | |
| /* Pixel format conversion */ | |
| LL_ConvertLineToARGB8888(pbmp, (void *)AddressOfImage, (uint32_t) Size.width, (uint32_t) input_color_mode); | |
| /* Increment the source and destination buffers */ | |
| AddressOfImage += (Size.width * 4); | |
| PositionOfFile -= psw; | |
| f_lseek(&OurFile, PositionOfFile); //pointer to the last line of bitmap | |
| f_read(&OurFile, pbmp, LineBytesSize + (Size.width) % 4, &br); | |
| } | |
| f_close(&OurFile);//close the file | |
| f_mount(NULL, "0:", 0);//unmount the drive | |
| return Size; | |
| } |
// Сама функция LL_ConvertLineToARGB8888 ниже:
void LL_ConvertLineToARGB8888(void *pSrc, void *pDst, uint32_t xSize, uint32_t ColorMode)
{
/* Configure the DMA2D Mode, Color Mode and output offset */
hdma2d.Init.Mode = DMA2D_M2M_PFC;
hdma2d.Init.ColorMode = DMA2D_ARGB8888;
hdma2d.Init.OutputOffset = 0;
/* Foreground Configuration */
hdma2d.LayerCfg[1].AlphaMode = DMA2D_NO_MODIF_ALPHA;
hdma2d.LayerCfg[1].InputAlpha = 0xFF;
hdma2d.LayerCfg[1].InputColorMode = ColorMode;
hdma2d.LayerCfg[1].InputOffset = 0;
hdma2d.Instance = DMA2D;
/* DMA2D Initialization */
if(HAL_DMA2D_Init(&hdma2d) == HAL_OK)
{
if(HAL_DMA2D_ConfigLayer(&hdma2d, 1) == HAL_OK)
{
if (HAL_DMA2D_Start(&hdma2d, (uint32_t)pSrc, (uint32_t)pDst, xSize, 1) == HAL_OK)
{
/* Polling For DMA transfer */
HAL_DMA2D_PollForTransfer(&hdma2d, 10);
}
}
}
}
