Exemple #1
0
struct texture* texture_load_from_png(const char* file_path)
{
    if(!file_path) {
        fprintf(stderr, "texure, texture_load_from_png, file_path is nil?");
        return NULL;
    }

    // load png data into memory
    size_t file_size = 0;
    unsigned char* origin_data = fs_read(file_path, &file_size, 0);

    unsigned char* pixel_data = NULL;
    unsigned int width = 0;
    unsigned int height = 0;
    unsigned int error = 0;

    error = lodepng_decode32(&pixel_data,
                             &width, &height,
                             origin_data, file_size);
    s_free(origin_data);
    if(error) {
        fprintf(stderr, "error %u: %s\n", error, lodepng_error_text(error));
        return NULL;
    }

    struct texture* tex = texture_load_from_mem(pixel_data, width, height, GL_RGBA);

    // TODO: third party use the standard malloc, we may replace that with s_malloc some day.
    free(pixel_data);
    return tex;
}
Exemple #2
0
void load_texture(C3D_Tex *tex, const u8 *img, const u32 img_size) {
	unsigned int width, height;
	u8* image;

	lodepng_decode32(&image, &width, &height, img, img_size);

	u8 *gpusrc = (u8 *) linearAlloc(width * height * 4);

	// lodepng outputs big endian rgba so we need to convert
	convert_endianess(gpusrc, image, width * height);

	// ensure data is in physical ram
	GSPGPU_FlushDataCache(gpusrc, width * height * 4);

	// Load the texture and bind it to the first texture unit
	C3D_TexInit(tex, width, height, GPU_RGBA8);

	// Convert image to 3DS tiled texture format
	C3D_SyncDisplayTransfer((u32*)gpusrc, GX_BUFFER_DIM(width, height), (u32*) tex->data, GX_BUFFER_DIM(width, height), TEXTURE_TRANSFER_FLAGS);

	C3D_TexSetFilter(tex, GPU_LINEAR, GPU_NEAREST);

	free(image);
	linearFree(gpusrc);
}
Exemple #3
0
void load_png_texture_from_buffer(const char *in, int size) {
    unsigned int error;
    unsigned char *data;
    unsigned int width, height;
    error = lodepng_decode32(&data, &width, &height, (unsigned char *)in, size);
    if (error) {
        fprintf(stderr, "error %u: %s\n", error, lodepng_error_text(error));
    }
    flip_image_vertical(data, width, height);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
                 GL_UNSIGNED_BYTE, data);
    free(data);
}
/*
Example 2
Load PNG file from disk to memory first, then decode to raw pixels in memory.
*/
void decodeTwoSteps(const char *filename)
{
    unsigned error;
    unsigned char *image;
    unsigned width, height;
    unsigned char *png;
    size_t pngsize;

    lodepng_load_file(&png, &pngsize, filename);
    error = lodepng_decode32(&image, &width, &height, png, pngsize);
    if (error) printf("error %u: %s\n", error, lodepng_error_text(error));

    free(png);

    /*use image here*/

    free(image);
}
Exemple #5
0
bool Image::loadPNGMem( unsigned char *ptr, size_t sz, bool multiply_color_by_alpha ) {
    unsigned error;
    unsigned char* image_data = (unsigned char*) MALLOC( 2048 * 2048 * 4 );
    unsigned w, h;    
    error = lodepng_decode32( &image_data, &w, &h,  ptr,sz );
    if(error) {
        fprintf(stderr, "decoder error %u: %s\n", error, lodepng_error_text(error) );
        FREE(image_data);
        return false;
    }
    width = w;
    height = h;
    if( multiply_color_by_alpha ) multiplyAlphaRGBA(image_data,width,height);    
    ensureBuffer();
    IMAGE_BUFFER_COPY;
    FREE(image_data);
    return true;    
}
Exemple #6
0
int winpr_image_png_read_buffer(wImage* image, BYTE* buffer, int size)
{
	UINT32 width;
	UINT32 height;
	int lodepng_status;

	lodepng_status = lodepng_decode32(&(image->data), &width, &height, buffer, size);

	if (lodepng_status)
		return -1;

	image->width = width;
	image->height = height;

	image->bitsPerPixel = 32;
	image->bytesPerPixel = 4;
	image->scanline = image->bytesPerPixel * image->width;

	return 1;
}
Exemple #7
0
int LodePngCoder::Decode(const unsigned char *inBuffer,
                         unsigned int bufferLen, unsigned char **outputBuffer,
                         unsigned int *output_width,
                         unsigned int *output_height)
{
    unsigned int uw = 0, uh = 0;
    unsigned char *buffer = NULL;
    int error = lodepng_decode32(&buffer, &uw, &uh, inBuffer, bufferLen);

    if (output_width) *output_width = uw;
    if (output_height) *output_height = uh;
    if (outputBuffer) *outputBuffer = buffer;

    if (error)
    {
        LOG_I("[LodePngCoder] fail to decode, error:%d, %s", error,
              lodepng_error_text(error));
    }

    return error;
}
Exemple #8
0
static bool icvDecodePng24(std::string* dst, const char* data, int size, int* width, int* height) {
	if(dst == NULL || data == NULL || size <= 0) {
		return false;
	}
	if(width == NULL || height == NULL) {
		return false;
	}

	unsigned char* img;
	unsigned w, h;

	unsigned err = lodepng_decode32(&img, &w, &h, (const unsigned char*)data, size);
	if(err != 0) return false;

	dst->assign((const char*)img, w*h*3);
	free(img);

	*width = w;
	*height = h;
	return true;
}
void so_img_loader_lodepng_load(const char* file, so_img_loader_data_t* info)
{
    unsigned error;
    unsigned char* image;
    unsigned width, height;
    unsigned char* png;
    size_t pngsize;
    char   abs_file_path[FILENAME_MAX];

    ks_helper_path_join_relative_app(abs_file_path, sizeof(abs_file_path), file);

    lodepng_load_file(&png, &pngsize, abs_file_path);
    error = lodepng_decode32(&image, &width, &height, png, pngsize);
    if(error) printf("error %u: %s\n", error, lodepng_error_text(error));

    free(png);

    info->pixels = (char*)image;
    info->width = width;
    info->height = height;
}
Exemple #10
0
int winpr_image_png_read_fp(wImage* image, FILE* fp)
{
	INT64 size;
	BYTE* data;
	UINT32 width;
	UINT32 height;
	int lodepng_status;

	_fseeki64(fp, 0, SEEK_END);
	size = _ftelli64(fp);
	_fseeki64(fp, 0, SEEK_SET);

	data = (BYTE*) malloc(size);

	if (!data)
		return -1;

	if (fread((void*) data, size, 1, fp) != 1)
	{
		free(data);
		return -1;
	}

	lodepng_status = lodepng_decode32(&(image->data), &width, &height, data, size);

	free(data);

	if (lodepng_status)
		return -1;

	image->width = width;
	image->height = height;

	image->bitsPerPixel = 32;
	image->bytesPerPixel = 4;
	image->scanline = image->bytesPerPixel * image->width;

	return 1;
}
Exemple #11
0
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;
}
    void sprite_sheet::incremental_load(const application_folder & in_application_folder)
    {
        switch(internal_status)
        {
            case sprite_sheet_status::ready:
            case sprite_sheet_status::failed:
                break;
            case sprite_sheet_status::waiting_to_load:
            {
                if(internal_loaded_file_data.status() == file_data_status::waiting_to_load)
                {
                    internal_status = sprite_sheet_status::loading;
                    internal_loaded_file_data.load(in_application_folder, internal_sheet_file_path.c_str());
                }
                break;
            }
            case sprite_sheet_status::loading:
            {
                if(internal_loaded_file_data.status() == file_data_status::loading)
                {
                }
                else if(internal_loaded_file_data.status() == file_data_status::ready)
                {
                    auto file_data = internal_loaded_file_data.data();
                    atl::input_bit_string_type bit_string(file_data.data(), file_data.data() + file_data.size());
                    
                    // Load frames:
                    glActiveTexture(GL_TEXTURE0);

                    uint32_t numSheets;
                    atl::bit_string_read_chunked_integer<uint32_t>(bit_string, numSheets, 3);
                    uint32_t totalSprites;
                    atl::bit_string_read_chunked_integer<uint32_t>(bit_string, totalSprites, 7);

                    sprites.resize(totalSprites);
                    textures.resize(numSheets);

                    for(auto & sheetId : textures)
                    {
                        // give the sheet a valid opengl texture ID
                        glGenTextures(1, &sheetId);
                        check_gl_errors();

                        uint32_t fontSheetWidthPower, fontSheetHeightPower;
                        atl::bit_string_read_ranged_integer<uint32_t>(bit_string, fontSheetWidthPower, 0, 12);
                        atl::bit_string_read_ranged_integer<uint32_t>(bit_string, fontSheetHeightPower, 0, 12);
                        uint32_t sheetWidth = 1 << fontSheetWidthPower;
                        uint32_t sheetHeight = 1 << fontSheetHeightPower;

                        atl::size2f sheetSize(sheetWidth, sheetHeight);

                        uint32_t sheetSprites;
                        atl::bit_string_read_ranged_integer<uint32_t>(bit_string, sheetSprites, 0, totalSprites);
                        while(sheetSprites-- > 0)
                        {
                            // Deserialize the frame index:
                            uint32_t spriteIndex;
                            atl::bit_string_read_ranged_integer<uint32_t>(bit_string, spriteIndex, 0, totalSprites - 1);

                            // Deserialize the texture coordinates:
                            atl::box2f l_texCoords;
                            {
                                uint32_t l_texT, l_texR, l_texB, l_texL;
                                atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texT, 0, sheetHeight);
                                atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texR, 0, sheetWidth);
                                atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texB, 0, sheetHeight);
                                atl::bit_string_read_ranged_integer<uint32_t>(bit_string, l_texL, 0, sheetWidth);

                                if(l_texR - l_texL == 1)
                                {
                                    l_texCoords.l = l_texCoords.r = (float(l_texL) + 0.5f) / float(sheetWidth);
                                }
                                else
                                {
                                    l_texCoords.l = (float(l_texL)) / float(sheetWidth);
                                    l_texCoords.r = (float(l_texR)) / float(sheetWidth);
                                }

                                if(l_texB - l_texT == 1)
                                {
                                    l_texCoords.t = l_texCoords.b = (float(l_texT) + 0.5f) / float(sheetHeight);
                                }
                                else
                                {
                                    l_texCoords.t = (float(l_texT)) / float(sheetHeight);
                                    l_texCoords.b = (float(l_texB)) / float(sheetHeight);
                                }
                            }

                            // Deserialize the drawn area:
                            atl::box2f l_area;
                            atl::bit_string_read_values(bit_string, l_area.t, l_area.r, l_area.b, l_area.l);

                            sprites[spriteIndex].set(sheetId, l_texCoords, l_area);
                        }

                        // Decode PNG data and upload to card:
                        glBindTexture(GL_TEXTURE_2D, sheetId);

                        int32_t numPNGBytesInt32;
                        atl::bit_string_read_value(bit_string, numPNGBytesInt32);
                        size_t numPNGBytes = numPNGBytesInt32;
                        atl::bit_string_skip_to_next_byte(bit_string);
                        const unsigned char * l_pngBytes = bit_string.ptr;
                        bit_string.ptr += numPNGBytes;

                        unsigned char * imageDataOut = nullptr;
                        unsigned int imageWidthOut;
                        unsigned int imageHeightOut;
                        unsigned int decodeError = lodepng_decode32(&imageDataOut, &imageWidthOut, &imageHeightOut, l_pngBytes, numPNGBytes);
                        atl_fatal_if(decodeError > 0, "Couldn't decode PNG in sprite sheet");
                        atl_fatal_if(imageWidthOut != sheetWidth || imageHeightOut != sheetHeight, "PNG in sprite sheet of unexpected size");

                        // Allocate GL resource:
                        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
                        check_gl_errors();
                        glTexImage2D(GL_TEXTURE_2D,
                                        0,
                                        GL_RGBA,
                                        atl::integer_cast<GLsizei>(sheetWidth, [](){}),
                                        atl::integer_cast<GLsizei>(sheetHeight, [](){}),
                                        0,
                                        GL_RGBA,
                                        GL_UNSIGNED_BYTE,
                                        imageDataOut);
                        check_gl_errors();
                        GLint lFilteringMode = internal_texture_filtering_mode == texture_filtering_mode::linear ? GL_LINEAR : GL_NEAREST;
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, lFilteringMode);
                        check_gl_errors();
                        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, lFilteringMode);
                        check_gl_errors();

                        delete[] imageDataOut;
                    }
                    internal_loaded_file_data.free();
                    internal_status = sprite_sheet_status::ready;
                }
                else
                {
                    internal_status = sprite_sheet_status::failed;
                }
                break;
            }
        }
    }
Exemple #13
0
//---------------------------------------------------------------------------------
static void sceneInit(void) {
//---------------------------------------------------------------------------------
	int i;

	// Load the vertex shader, create a shader program and bind it
	vshader_dvlb = DVLB_ParseFile((u32*)vshader_shbin, vshader_shbin_size);
	shaderProgramInit(&program);
	shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]);
	C3D_BindProgram(&program);

	// Get the location of the uniforms
	uLoc_projection = shaderInstanceGetUniformLocation(program.vertexShader, "projection");

	// Configure attributes for use with the vertex shader
	// Attribute format and element count are ignored in immediate mode
	C3D_AttrInfo* attrInfo = C3D_GetAttrInfo();
	AttrInfo_Init(attrInfo);
	AttrInfo_AddLoader(attrInfo, 0, GPU_FLOAT, 3); // v0=position
	AttrInfo_AddLoader(attrInfo, 1, GPU_FLOAT, 2); // v2=texcoord

	// Compute the projection matrix
	// Note: we're setting top to 240 here so origin is at top left.
	Mtx_OrthoTilt(&projection, 0.0, 400.0, 240.0, 0.0, 0.0, 1.0);

	// Configure buffers
	C3D_BufInfo* bufInfo = C3D_GetBufInfo();
	BufInfo_Init(bufInfo);

	unsigned char* image;
	unsigned width, height;

	lodepng_decode32(&image, &width, &height, ballsprites_png, ballsprites_png_size);

	u8 *gpusrc = linearAlloc(width*height*4);

	// GX_DisplayTransfer needs input buffer in linear RAM
	u8* src=image; u8 *dst=gpusrc;

	// lodepng outputs big endian rgba so we need to convert
	for(int i = 0; i<width*height; i++) {
		int r = *src++;
		int g = *src++;
		int b = *src++;
		int a = *src++;

		*dst++ = a;
		*dst++ = b;
		*dst++ = g;
		*dst++ = r;
	}

	// ensure data is in physical ram
	GSPGPU_FlushDataCache(gpusrc, width*height*4);

	// Load the texture and bind it to the first texture unit
	C3D_TexInit(&spritesheet_tex, width, height, GPU_RGBA8);

	// Convert image to 3DS tiled texture format
	C3D_SafeDisplayTransfer ((u32*)gpusrc, GX_BUFFER_DIM(width,height), (u32*)spritesheet_tex.data, GX_BUFFER_DIM(width,height), TEXTURE_TRANSFER_FLAGS);
	gspWaitForPPF();

	C3D_TexSetFilter(&spritesheet_tex, GPU_LINEAR, GPU_NEAREST);
	C3D_TexBind(0, &spritesheet_tex);

	free(image);
	linearFree(gpusrc);

	// Configure the first fragment shading substage to just pass through the texture color
	// See https://www.opengl.org/sdk/docs/man2/xhtml/glTexEnv.xml for more insight
	C3D_TexEnv* env = C3D_GetTexEnv(0);
	C3D_TexEnvSrc(env, C3D_Both, GPU_TEXTURE0, 0, 0);
	C3D_TexEnvOp(env, C3D_Both, 0, 0, 0);
	C3D_TexEnvFunc(env, C3D_Both, GPU_REPLACE);

	srand(time(NULL));

	for(i = 0; i < NUM_SPRITES; i++) {
		//random place and speed
		sprites[i].x = (rand() % (400 - 32 )) << 8;
		sprites[i].y = (rand() % (240 - 32 )) << 8 ;
		sprites[i].dx = (rand() & 0xFF) + 0x100;
		sprites[i].dy = (rand() & 0xFF) + 0x100;
		sprites[i].image = rand() & 3;

		if(rand() & 1)
			sprites[i].dx = -sprites[i].dx;
		if(rand() & 1)
			sprites[i].dy = -sprites[i].dy;
	}

	// Configure depth test to overwrite pixels with the same depth (needed to draw overlapping sprites)
	C3D_DepthTest(true, GPU_GEQUAL, GPU_WRITE_ALL);
}
Exemple #14
0
Bool png_load(u4 buffer_size, u1 *buffer, struct image *image) {
	return lodepng_decode32((unsigned char **)&image->data, &image->width, &image->height, buffer, buffer_size) ? False : True;
}