Add support of drawing images

Issue #248 new
Patryk Kubiak created an issue

Implementation reason

The program cgraphic needs to draw a logo of the system on loading and login screens. Actually it is not possible to do it in the WidgetScreen module.

Generic Description

The goal of the task is to implement function oC_ColorMap_DrawImage responsible for drawing images in the color map. The function should use upng library to do it. It is available in the core/gui directory

Example

The example how to draw an image using uPNG:

#!c

//==========================================================================================================================================
/**
 * @brief shows PNG file
 */
//==========================================================================================================================================
static void ShowImage( upng_t * upng )
{
    oC_Screen_t     screen          = oC_ScreenMan_GetDefaultScreen();
    oC_ColorMap_t * colorMap        = NULL;

    oC_Screen_ReadColorMap(screen,&colorMap);

    if(colorMap != NULL)
    {
        const uint8_t * buffer = upng_get_buffer(upng);

        if(buffer != NULL)
        {
            unsigned width, height, depth;
            unsigned x, y, d;
            oC_Pixel_Position_t pos;

            width = upng_get_width(upng);
            height = upng_get_height(upng);
            depth = upng_get_bpp(upng) / 8;

            printf("");

            for( y = 0; y < height ; y++)
            {
                for( x = 0; x < width ; x++)
                {
                    oC_Color_t color = 0;

                    for (d = 0; d != depth; ++d) {
                        color |= buffer[(height - y - 1) * width * depth + x * depth + (depth - d - 1)] << (d*8);
                    }

                    pos.X = x;
                    pos.Y = height - y;

                    oC_ColorMap_SetColor(colorMap,pos,color,colorMap->ColorFormat);
                }
            }
        }
    }
}

//==========================================================================================================================================
/**
 * @brief shows the image from the path
 */
//==========================================================================================================================================
static void ShowImageFromPath( const char * Path )
{
    upng_t * upng = NULL;

    upng = upng_new_from_file(Path);
    if(upng_get_error(upng) == UPNG_EOK)
    {
        upng_decode(upng);

        if (upng_get_error(upng) == UPNG_EOK)
        {
            ShowImage(upng);
        }
        else
        {
            printf("Cannot decode upng!\n");
        }
        upng_free(upng);
    }
}

Required Interface

#!c

// The function draws an image
//     ColorMap         - color map where image should be drawed
//     StartPosition   - top left corner of the image
//     Width - width of the image to draw
//     Height - height of the image to draw
//     FilePath - path to the file with image (for now *.png only). It will be resized if needed
oC_ErrorCode_t oC_ColorMap_DrawImage( oC_ColorMap_t * ColorMap , oC_Pixel_Position_t StartPosition, oC_Pixel_ResolutionUInt_t Width, oC_Pixel_ResolutionUInt_t Height , const char * FilePath );

// The function is inside the `oc_colormap.c` file and it is responsible for
// drawing only PNG files at the given position
// Parameters are exactly like in the oC_ColorMap_DrawImage
// If the function cannot open file, because it is not `*png`, it should return 
// `oC_ErrorCode_WrongFileFormat` error code
static oC_ErrorCode_t DrawImage_PNG( oC_ColorMap_t * ColorMap , oC_Pixel_Position_t StartPosition, oC_Pixel_ResolutionUInt_t Width, oC_Pixel_ResolutionUInt_t Height , const char * FilePath ); 

Bitbucket

https://bitbucket.org/chocos/chocos/issues/248

Trello

Link to trello board:

https://trello.com/c/x95ohvVf

Story

  • [ #240 ] - Preparation of the sample graphic program

List of requirements

  • [ #248 ] - The function allows for drawing .png images*
  • [ #248 ] - The function uses UPNG library for drawing images

Comments (9)

  1. Log in to comment