Code: Select all
#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pspdisplay.h>
#include <pspnet_inet.h>
#include <pspnet_apctl.h>
#include <pspnet_resolver.h>
#include <psputility.h>
#include <netinet/in.h>
#include <kubridge.h>
#include <sys/select.h>
#include <pspctrl.h>
#include <errno.h>
#include <math.h>
#include <psputility.h>
#include <pspgu.h>
#include <pspgum.h>
#include <pspjpeg.h>
PSP_MODULE_INFO("JpegSample", PSP_MODULE_USER, 1, 1);
PSP_HEAP_SIZE_MAX();
#define BUF_WIDTH (512)
#define SCR_WIDTH (480)
#define SCR_HEIGHT (272)
#define PIXEL_SIZE (4)
#define FRAME_SIZE (BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE)
#define ZBUF_SIZE (BUF_WIDTH SCR_HEIGHT * 2)
#define printf pspDebugScreenPrintf
#define PTR2HND(PTR) ((PTR) - 1)
#define HND2PTR(HND) (HND + 1)
static unsigned int __attribute__((aligned(16))) list[262144];
/* Exit callback */
int exit_callback(int arg1, int arg2, void *common)
{
sceKernelExitGame();
return 0;
}
/* Callback thread */
int CallbackThread(SceSize args, void *argp)
{
int cbid;
cbid = sceKernelCreateCallback("Exit Callback", exit_callback, NULL);
sceKernelRegisterExitCallback(cbid);
sceKernelSleepThreadCB();
return 0;
}
/* Sets up the callback thread and returns its thread id */
int SetupCallbacks(void)
{
int thid = 0;
thid = sceKernelCreateThread("update_thread", CallbackThread, 0x11, 0xFA0, 0, 0);
if(thid >= 0)
{
sceKernelStartThread(thid, 0, 0);
}
return thid;
}
int FileRead_open(const char* FileName)
{
return PTR2HND(sceIoOpen(FileName,PSP_O_RDWR,0777));
// return PTR2HND(fopen(FileName,"rb"));
}
int FileRead_close(int FileHandle)
{
if(FileHandle == -1)return -1;
sceIoClose(HND2PTR(FileHandle));
// fclose(HND2PTR(FileHandle));
return 0;
}
int FileRead_size(const char *FileName)
{
SceIoStat stat;
if(sceIoGetstat(FileName,&stat) < 0) return -1;
if(stat.st_size > 0x00000000ffffffff)return 0xffffffff;
return stat.st_size;
}
int FileRead_read(void * Buffer,int Size,int FileHandle)
{
if(FileHandle == -1)return -1;
return sceIoRead(HND2PTR(FileHandle),Buffer,Size);
}
u8* convertjpeg(const char *FileName)
{
printf("ConvertStarts\n");
int size = FileRead_size(FileName);
u8* rgbabuf = (u8*)malloc(4*480*272);
u8* databuf = (u8*)malloc(size);
if(databuf == NULL || rgbabuf == NULL)
{
printf("Malloc Error!\n");
free(databuf);
free(rgbabuf);
sceJpegDeleteMJpeg();
sceJpegFinishMJpeg();
return NULL;
}
int fh = FileRead_open(FileName);
if(fh == -1)
{
printf("File couldn't open!\n");
free(databuf);
free(rgbabuf);
sceJpegDeleteMJpeg();
sceJpegFinishMJpeg();
return NULL;
}
FileRead_read(databuf,size,fh);
FileRead_close(fh);
int res = sceJpegDecodeMJpeg(databuf,size,rgbabuf,0);
free(databuf);
sceJpegDeleteMJpeg();
sceJpegFinishMJpeg();
if(res < 0)
{
printf("Decode Error!\n");
free(rgbabuf);
return NULL;
}
printf("Convert is completed!\n");
return rgbabuf;
}
void initGraphics()
{
sceGuInit();
sceDisplayWaitVblankStart();
sceGuDisplay(1);
sceGuStart(GU_DIRECT, list);
}
void drawFrame(unsigned char* textureData)
{
sceGuStart(GU_DIRECT, list);
//sceGuDispBuffer(SCR_WIDTH,SCR_HEIGHT, 0,BUF_WIDTH);
void* framebuffer = 0;
sceGuCopyImage(GU_PSM_8888, 0, 0, 480, 272, 480, textureData, 0, 0, 512, (void*)(((unsigned int)framebuffer) + 0x4000000));
sceGuFinish();
sceGuSync(0,0);
sceDisplayWaitVblankStart();
sceGuSwapBuffers();
}
int LoadStartModule(char *path)
{
u32 loadResult;
u32 startResult;
int status;
loadResult = kuKernelLoadModule(path, 0, NULL);
if (loadResult & 0x80000000) {
return -1;
}
else {
startResult = sceKernelStartModule(loadResult, 0, NULL, &status, NULL);
}
if (loadResult != startResult) {
return -2;
}
return 0;
}
int StartJpegPsp()
{
printf("Loading 'flash0:/kd/avcodec.prx'...");
if (LoadStartModule("flash0:/kd/avcodec.prx") == 0) {
printf("ok\n");
}
else {
printf("failed\n");
return -1;
}
printf("init Jpeg decompression...");
if (sceJpegInitMJpeg() == 0) {
if (sceJpegCreateMJpeg(480, 272) == 0) {
printf("ok\n");
return 0;
}
}
printf("failed\n");
return -1;
}
int main(void)
{
SetupCallbacks();
pspDebugScreenInit();
StartJpegPsp();
u8* bitmap = convertjpeg("background.jpg");
initGraphics();
drawFrame(bitmap);
sceKernelSleepThread();
return 0;
}
When I try to run this. It gaves Decode error....