Skip to content

Commit

Permalink
Add libpngdec example. It include one on build libpng library to load…
Browse files Browse the repository at this point in the history
… png from memory or files
  • Loading branch information
Estwald committed Nov 9, 2010
1 parent f53bfb4 commit dfca631
Show file tree
Hide file tree
Showing 5 changed files with 534 additions and 0 deletions.
76 changes: 76 additions & 0 deletions samples/pngtest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
.SUFFIXES:
ifeq ($(strip $(PSL1GHT)),)
$(error "PSL1GHT must be set in the environment.")
endif

include $(PSL1GHT)/Makefile.base

TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCE := source
INCLUDE := include
DATA := data
LIBS := -lgcm_sys -lreality -lsysutil -lio -lpngdec -lsysmodule

TITLE := pngtest - PSL1GHT
APPID := TEST00003
CONTENTID := UP0001-$(APPID)_00-0000000000000000

CFLAGS += -g -O2 -Wall --std=gnu99
CXXFLAGS += -g -O2 -Wall

ifneq ($(BUILD),$(notdir $(CURDIR)))

export OUTPUT := $(CURDIR)/$(TARGET)
export VPATH := $(foreach dir,$(SOURCE),$(CURDIR)/$(dir)) \
$(foreach dir,$(DATA),$(CURDIR)/$(dir))
export BUILDDIR := $(CURDIR)/$(BUILD)
export DEPSDIR := $(BUILDDIR)

CFILES := $(foreach dir,$(SOURCE),$(notdir $(wildcard $(dir)/*.c)))
CXXFILES := $(foreach dir,$(SOURCE),$(notdir $(wildcard $(dir)/*.cpp)))
SFILES := $(foreach dir,$(SOURCE),$(notdir $(wildcard $(dir)/*.S)))
BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.bin)))

export OFILES := $(CFILES:.c=.o) \
$(CXXFILES:.cpp=.o) \
$(SFILES:.S=.o)

export BINFILES := $(BINFILES:.bin=.bin.h)

export INCLUDES := $(foreach dir,$(INCLUDE),-I$(CURDIR)/$(dir)) \
-I$(CURDIR)/$(BUILD)

.PHONY: $(BUILD) clean

$(BUILD):
@[ -d $@ ] || mkdir -p $@
@make --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile

clean:
@echo Clean...
@rm -rf $(BUILD) $(OUTPUT).elf $(OUTPUT).self $(OUTPUT).a $(OUTPUT).pkg

pkg: $(BUILD)
@echo Creating PKG...
@mkdir -p $(BUILD)/pkg
@mkdir -p $(BUILD)/pkg/USRDIR
@cp $(ICON0) $(BUILD)/pkg/
@$(FSELF) -n $(BUILD)/$(TARGET).elf $(BUILD)/pkg/USRDIR/EBOOT.BIN
@$(SFO) --title "$(TITLE)" --appid "$(APPID)" -f $(SFOXML) $(BUILD)/pkg/PARAM.SFO
@$(PKG) --contentid $(CONTENTID) $(BUILD)/pkg/ $(OUTPUT).pkg

run: $(BUILD)
@$(PS3LOADAPP) $(OUTPUT).self

else

DEPENDS := $(OFILES:.o=.d)

$(OUTPUT).self: $(OUTPUT).elf
$(OUTPUT).elf: $(OFILES)
$(OFILES): $(BINFILES)

-include $(DEPENDS)

endif
Binary file added samples/pngtest/data/psl1ght_png.bin
Binary file not shown.
165 changes: 165 additions & 0 deletions samples/pngtest/source/libpng.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* libpng.c
Copyright (c) 2010 Hermes <www.elotrolado.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
- The names of the contributors may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <malloc.h>
#include <string.h>

#include "libpng.h"


static void *png_malloc(u32 size, void * a) {

return malloc(size);
}


static int png_free(void *ptr, void * a) {

free(ptr);

return 0;
}

int LoadPNG(PngDatas *png, char *filename)
{
int ret=-1;

int mHandle;
int sHandle;

PngDecThreadInParam InThdParam;
PngDecThreadOutParam OutThdParam;

uint64_t build_malloc, build_free; // used to create the fake 32 bits addrs .opd for functions png_malloc() and png_free()

PngDecInParam inParam;
PngDecOutParam outParam;

PngDecSrc src;
uint32_t space_allocated;

PngDecInfo DecInfo;

uint64_t bytes_per_line;
PngDecDataInfo DecDataInfo;

InThdParam.enable = 0;
InThdParam.ppu_prio = 512;
InThdParam.spu_prio = 200;
InThdParam.addr_malloc_func = build32_func_addr(png_malloc, &build_malloc); // (see sysmodule.h)
InThdParam.addr_malloc_arg = 0; // no args: if you want one uses get32_addr() to get the 32 bit address (see sysmodule.h)
InThdParam.addr_free_func = build32_func_addr(png_free, &build_free); // (see sysmodule.h)
InThdParam.addr_free_arg = 0; // no args if you want one uses get32_addr() to get the 32 bit address (see sysmodule.h)


ret= PngDecCreate(&mHandle, &InThdParam, &OutThdParam);

png->bmp_out= NULL;

if(ret == 0) {

memset(&src, 0, sizeof(PngDecSrc));

if(filename) {
src.stream_select = PNGDEC_FILE;
src.addr_file_name = get32_addr(filename);
} else {
src.stream_select = PNGDEC_BUFFER;
src.addr_stream_ptr = get32_addr((void *) png->png_in);
src.stream_size = png->png_size;
}

src.enable = PNGDEC_DISABLE;

ret= PngDecOpen(mHandle, &sHandle, &src, &space_allocated);

if(ret == 0) {

ret = PngDecReadHeader(mHandle, sHandle, &DecInfo);

if(ret == 0) {

inParam.addr_cmd_ptr = 0;
inParam.mode = PNGDEC_TOP_TO_BOTTOM;
inParam.color_space = PNGDEC_ARGB;
inParam.bit_depth = 8;
inParam.pack_flag = 1;

if((DecInfo.color_space == PNGDEC_GRAYSCALE_ALPHA) || (DecInfo.color_space == PNGDEC_RGBA) || (DecInfo.chunk_info & 0x10))
inParam.alpha_select = 0;
else
inParam.alpha_select = 1;

inParam.color_alpha = 0xff;

ret = PngDecSetParameter(mHandle, sHandle, &inParam, &outParam);
}

if(ret == 0) {
png->wpitch= outParam.width* 4;
bytes_per_line = (uint64_t) png->wpitch;

png->bmp_out= malloc(png->wpitch * outParam.height);

if(!png->bmp_out) {

ret=-1; // out of memory

} else {

memset(png->bmp_out, 0, png->wpitch * outParam.height);

ret = PngDecDecodeData(mHandle, sHandle, png->bmp_out, &bytes_per_line, &DecDataInfo);

if((ret == 0) && (DecDataInfo.status == 0)){

png->width = outParam.width;
png->height = outParam.height;

ret=0; // ok :)

}
}
}

PngDecClose(mHandle, sHandle);
}

if(ret && png->bmp_out) {

free(png->bmp_out); png->bmp_out= NULL;

}

PngDecDestroy(mHandle);

}

return ret;
}

60 changes: 60 additions & 0 deletions samples/pngtest/source/libpng.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* libpng.h
Copyright (c) 2010 Hermes <www.elotrolado.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
- Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
- The names of the contributors may not be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef _LIBPNG_H_
#define _LIBPNG_H_

#ifdef __cplusplus
extern "C" {
#endif


#include <sysmodule/sysmodule.h>
#include <pngdec/pngdec.h>

typedef struct PngDatas {

void * png_in; // ignored except if char *filename == NULL in LoadPNG()
uint32_t png_size; // ignored except if char *filename == NULL in LoadPNG()

void * bmp_out; // internally allocated (bmp 32 bits color ARGB format)

int wpitch; // output width pitch in bytes
int width; // output
int height; // output

} PngDatas;

int LoadPNG(PngDatas *png, char *filename);

#ifdef __cplusplus
}
#endif

#endif

Loading

0 comments on commit dfca631

Please sign in to comment.