static rt_bool_t rtgui_image_hdc_check(struct rtgui_filerw *file) { int start; rt_bool_t is_HDC; rt_uint8_t magic[4]; if (!file) return 0; start = rtgui_filerw_tell(file); /* move to the beginning of file */ rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET); is_HDC = RT_FALSE; if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic)) { if (magic[0] == 'H' && magic[1] == 'D' && magic[2] == 'C' && magic[3] == '\0') { is_HDC = RT_TRUE; } } rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET); return(is_HDC); }
static rt_bool_t rtgui_image_png_check(struct rtgui_filerw *file) { int start; rt_bool_t is_PNG; rt_uint8_t magic[4]; if (!file) return 0; start = rtgui_filerw_tell(file); /* move to the begining of file */ rtgui_filerw_seek(file, 0, SEEK_SET); is_PNG = RT_FALSE; if (rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic)) { if (magic[0] == 0x89 && magic[1] == 'P' && magic[2] == 'N' && magic[3] == 'G') { is_PNG = RT_TRUE; } } rtgui_filerw_seek(file, start, SEEK_SET); return(is_PNG); }
static rt_bool_t rtgui_image_bmp_check(struct rtgui_filerw* file) { char magic[2]; rt_bool_t is_bmp; rt_off_t start; if ( !file ) return 0; start = rtgui_filerw_tell(file); /* move to the beginning of file */ rtgui_filerw_seek(file, 0, RTGUI_FILE_SEEK_SET); is_bmp = RT_FALSE; if ( rtgui_filerw_read(file, magic, 1, sizeof(magic)) == sizeof(magic) ) { if (magic[0] == 'B' && magic[1] == 'M') { is_bmp = RT_TRUE; } } rtgui_filerw_seek(file, start, RTGUI_FILE_SEEK_SET); return(is_bmp); }
static rt_bool_t rtgui_image_hdc_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load) { rt_uint32_t header[5]; struct rtgui_image_hdc *hdc; hdc = (struct rtgui_image_hdc *) rtgui_malloc(sizeof(struct rtgui_image_hdc)); if (hdc == RT_NULL) return RT_FALSE; hdc->hw_driver = rtgui_graphic_driver_get_default(); if (hdc->hw_driver == RT_NULL) { rtgui_free(hdc); return RT_FALSE; } rtgui_filerw_read(file, (char *)&header, 1, sizeof(header)); /* set image information */ image->w = (rt_uint16_t)header[1]; image->h = (rt_uint16_t)header[2]; image->engine = &rtgui_image_hdc_engine; image->data = hdc; hdc->filerw = file; hdc->byte_per_pixel = hdc->hw_driver->bits_per_pixel / 8; hdc->pitch = image->w * hdc->byte_per_pixel; hdc->pixel_offset = rtgui_filerw_tell(file); if (load == RT_TRUE) { /* load all pixels */ hdc->pixels = rtgui_malloc(image->h * hdc->pitch); if (hdc->pixels == RT_NULL) { /* release data */ rtgui_free(hdc); return RT_FALSE; } rtgui_filerw_read(hdc->filerw, hdc->pixels, 1, image->h * hdc->pitch); rtgui_filerw_close(hdc->filerw); hdc->filerw = RT_NULL; hdc->pixel_offset = 0; } else { hdc->pixels = RT_NULL; } return RT_TRUE; }
static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load) { unsigned int width; unsigned int height; unsigned int error; rt_uint8_t* pixel; rt_uint8_t* in; rt_uint32_t in_size; RT_ASSERT(image != RT_NULL); RT_ASSERT(file != RT_NULL); rtgui_filerw_seek(file, 0, SEEK_END); in_size = rtgui_filerw_tell(file); in = rtgui_malloc(in_size); if (in == RT_NULL) return RT_FALSE; /* out of memory */ rtgui_filerw_seek(file, 0, SEEK_SET); rtgui_filerw_read(file, in, in_size, 1); error = lodepng_decode32(&pixel, &width, &height, in, in_size); if(error) { rt_kprintf("error %u: %s\n", error, lodepng_error_text(error)); rtgui_free(in); return RT_FALSE; } rtgui_free(in); /* set image information */ image->w = width; image->h = height; image->engine = &rtgui_image_png_engine; image->data = pixel; /* NOTE: the pixel format of PNG is ABGR888, bit0 R,G,B,A bit31 */ /* convert pixel to ARGB888, swap B/G */ { rt_uint8_t* pixel_ptr; rt_uint8_t* pixel_end; pixel_ptr = (rt_uint8_t*) pixel; pixel_end = pixel_ptr + width * height * 4; while (pixel_ptr < pixel_end) { pixel_ptr[0] = pixel_ptr[0] ^ pixel_ptr[2]; pixel_ptr[2] = pixel_ptr[0] ^ pixel_ptr[2]; pixel_ptr[0] = pixel_ptr[0] ^ pixel_ptr[2]; pixel_ptr += 4; } } /* close file handler */ rtgui_filerw_close(file); return RT_TRUE; }