static int xml_load_items(const char* filename) { struct rtgui_filerw* filerw; char buffer[512]; rtgui_xml_t *xml; int length; /* create filerw context */ filerw = rtgui_filerw_create_file(filename, "rb"); if (filerw == RT_NULL) { rt_kprintf("read file fail %s\n", filename); return 0; } length = rtgui_filerw_read(filerw, buffer, 512, 1); if(length <= 0) { rt_kprintf("read fail\n"); rtgui_filerw_close(filerw); return 0; } xml = rtgui_xml_create(512, xml_event_handler, RT_NULL); if (xml != RT_NULL) { rtgui_xml_parse(xml, buffer, length); rtgui_xml_destroy(xml); } rtgui_filerw_close(filerw); return 0; }
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 void rtgui_image_hdc_unload(struct rtgui_image *image) { struct rtgui_image_hdc *hdc; if (image != RT_NULL) { hdc = (struct rtgui_image_hdc *) image->data; if (hdc->pixels != RT_NULL) rtgui_free(hdc->pixels); if (hdc->filerw != RT_NULL) { rtgui_filerw_close(hdc->filerw); hdc->filerw = RT_NULL; } /* release data */ rtgui_free(hdc); } }
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; }
static rt_bool_t rtgui_image_png_load(struct rtgui_image *image, struct rtgui_filerw *file, rt_bool_t load) { png_uint_32 width; png_uint_32 height; int bit_depth; int color_type; double gamma; struct rtgui_image_png *png; png = (struct rtgui_image_png *) rtgui_malloc(sizeof(struct rtgui_image_png)); png->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); if (png->png_ptr == RT_NULL) { rtgui_free(png); return RT_FALSE; } png_set_error_fn(png->png_ptr, RT_NULL, _image_png_error_fn, _image_png_error_fn); png->info_ptr = png_create_info_struct(png->png_ptr); if (png->info_ptr == RT_NULL) { png_destroy_read_struct(&png->png_ptr, NULL, NULL); rtgui_free(png); return RT_FALSE; } png->filerw = file; png->is_loaded = RT_FALSE; png_set_read_fn(png->png_ptr, png->filerw, rtgui_image_png_read_data); png_read_info(png->png_ptr, png->info_ptr); png_get_IHDR(png->png_ptr, png->info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); /* set image information */ image->w = width; image->h = height; image->engine = &rtgui_image_png_engine; image->data = png; if (bit_depth == 16) png_set_strip_16(png->png_ptr); if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png->png_ptr); if (bit_depth < 8) png_set_expand(png->png_ptr); if (png_get_valid(png->png_ptr, png->info_ptr, PNG_INFO_tRNS)) png_set_expand(png->png_ptr); if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) png_set_gray_to_rgb(png->png_ptr); /* Ignore background color */ /* set gamma conversion */ if (png_get_gAMA(png->png_ptr, png->info_ptr, &gamma)) png_set_gamma(png->png_ptr, (double)2.2, gamma); png_read_update_info(png->png_ptr, png->info_ptr); if (load == RT_TRUE) { /* load all pixels */ png->pixels = rtgui_malloc(image->w * image->h * sizeof(rtgui_color_t)); if (png->pixels == RT_NULL) { png_read_end(png->png_ptr, RT_NULL); /* destroy png struct */ png_destroy_info_struct(png->png_ptr, &png->info_ptr); png_destroy_read_struct(&png->png_ptr, RT_NULL, RT_NULL); /* release data */ rtgui_free(png); return RT_FALSE; } rtgui_image_png_process(png->png_ptr, png->info_ptr, png); png_read_end(png->png_ptr, RT_NULL); png->is_loaded = RT_TRUE; /* close file handler */ rtgui_filerw_close(png->filerw); png->filerw = RT_NULL; } else { png->pixels = RT_NULL; } return RT_TRUE; }
static rt_bool_t rtgui_image_bmp_load(struct rtgui_image* image, struct rtgui_filerw* src, rt_bool_t load) { rt_uint32_t Rmask; rt_uint32_t Gmask; rt_uint32_t Bmask; int ExpandBMP, bmpPitch; struct rtgui_image_bmp* bmp; struct rtgui_image_bmp_header* header; struct rtgui_image_palette* palette; bmp = RT_NULL; header = RT_NULL; palette = RT_NULL; header = (struct rtgui_image_bmp_header*) rtgui_malloc(sizeof(struct rtgui_image_bmp_header)); if (header == RT_NULL) return RT_FALSE; if ( rtgui_filerw_read(src, header->magic, 1, sizeof(header->magic)) == sizeof(sizeof(header->magic)) ) { if (header->magic[0] != 'B' || header->magic[1] != 'M') { goto __exit; } } header->bfSize = rtgui_filerw_read32(src); header->bfReserved1 = rtgui_filerw_read16(src); header->bfReserved2 = rtgui_filerw_read16(src); header->bfOffBits = rtgui_filerw_read32(src); /* Read the Win32 BITMAPINFOHEADER */ header->biSize = rtgui_filerw_read32(src); if ( header->biSize == 12 ) { header->biWidth = (rt_uint32_t)rtgui_filerw_read16(src); header->biHeight = (rt_uint32_t)rtgui_filerw_read16(src); header->biPlanes = rtgui_filerw_read16(src); header->biBitCount = rtgui_filerw_read16(src); header->biCompression = BI_RGB; header->biSizeImage = 0; header->biXPelsPerMeter = 0; header->biYPelsPerMeter = 0; header->biClrUsed = 0; header->biClrImportant = 0; } else { header->biWidth = rtgui_filerw_read32(src); header->biHeight = rtgui_filerw_read32(src); header->biPlanes = rtgui_filerw_read16(src); header->biBitCount = rtgui_filerw_read16(src); header->biCompression = rtgui_filerw_read32(src); header->biSizeImage = rtgui_filerw_read32(src); header->biXPelsPerMeter = rtgui_filerw_read32(src); header->biYPelsPerMeter = rtgui_filerw_read32(src); header->biClrUsed = rtgui_filerw_read32(src); header->biClrImportant = rtgui_filerw_read32(src); } /* allocate palette and expand 1 and 4 bit bitmaps to 8 bits per pixel */ switch (header->biBitCount) { case 1: ExpandBMP = header->biBitCount; palette = rtgui_image_bmp_load_palette(header, src); header->biBitCount = 8; break; case 4: ExpandBMP = header->biBitCount; palette = rtgui_image_bmp_load_palette(header, src); header->biBitCount = 8; break; case 8: palette = rtgui_image_bmp_load_palette(header, src); ExpandBMP = 0; break; default: ExpandBMP = 0; break; } /* We don't support any BMP compression right now */ Rmask = Gmask = Bmask = 0; switch (header->biCompression) { case BI_RGB: /* If there are no masks, use the defaults */ if ( header->bfOffBits == (14 + header->biSize) ) { /* Default values for the BMP format */ switch (header->biBitCount) { case 15: case 16: Rmask = 0x7C00; Gmask = 0x03E0; Bmask = 0x001F; break; case 24: case 32: Rmask = 0x00FF0000; Gmask = 0x0000FF00; Bmask = 0x000000FF; break; default: break; } break; } /* Fall through -- read the RGB masks */ case BI_BITFIELDS: switch (header->biBitCount) { case 15: case 16: case 32: Rmask = rtgui_filerw_read32(src); Gmask = rtgui_filerw_read32(src); Bmask = rtgui_filerw_read32(src); break; default: break; } break; default: rt_kprintf("Compressed BMP files not supported\n"); goto __exit; } bmp = (struct rtgui_image_bmp*) rtgui_malloc(sizeof(struct rtgui_image_bmp)); if (bmp == RT_NULL) goto __exit; /* set image information */ image->w = header->biWidth; image->h = header->biHeight; image->engine = &rtgui_image_bmp_engine; image->data = bmp; bmp->filerw = src; bmp->byte_per_pixel = header->biBitCount/8; bmp->pitch = image->w * bmp->byte_per_pixel; bmp->pixel_offset = header->bfOffBits; bmp->Rmask = Rmask; bmp->Gmask = Gmask; bmp->Bmask = Bmask; bmp->ExpandBMP = ExpandBMP; if (palette != RT_NULL) image->palette = palette; /* get padding */ switch (ExpandBMP) { case 1: bmpPitch = (header->biWidth + 7) >> 3; bmp->pad = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0); break; case 4: bmpPitch = (header->biWidth + 1) >> 1; bmp->pad = (((bmpPitch)%4) ? (4-((bmpPitch)%4)) : 0); break; default: bmp->pad = ((bmp->pitch%4) ? (4-(bmp->pitch%4)) : 0); break; } if (load == RT_TRUE) { rt_uint8_t *bits; rt_uint32_t i; /* load all pixels */ bmp->pixels = rtgui_malloc(image->h * bmp->pitch); if (bmp->pixels == RT_NULL) goto __exit; /* Read the pixels. Note that the bmp image is upside down */ if ( rtgui_filerw_seek(src, bmp->pixel_offset, RTGUI_FILE_SEEK_SET) < 0) goto __exit; bits = bmp->pixels + image->h * bmp->pitch; while ( bits > bmp->pixels ) { bits -= bmp->pitch; switch (ExpandBMP) { case 1: case 4: { rt_uint8_t pixel = 0; int shift = (8 - ExpandBMP); for ( i=0; i < image->w; ++i ) { if ( i % (8/ExpandBMP) == 0 ) { if ( !rtgui_filerw_read(src, &pixel, 1, 1) ) goto __exit; } *(bits+i) = (pixel>>shift); pixel <<= ExpandBMP; } } break; default: if ( rtgui_filerw_read(src, bits, 1, bmp->pitch) != bmp->pitch ) goto __exit; break; } /* Skip padding bytes */ if ( bmp->pad ) { rt_uint8_t padbyte; for ( i=0; i < bmp->pad; ++i ) { rtgui_filerw_read(src, &padbyte, 1, 1); } } } rtgui_filerw_close(bmp->filerw); bmp->line_pixels = RT_NULL; bmp->filerw = RT_NULL; bmp->pixel_offset = 0; }