[PSP] sceJpeg Sample

Create a single thread for each patch to be added to the repository. Please try to stay on topic.
Post Reply
ardatan
Posts: 44
Joined: Sat Jan 12, 2008 8:47 am

[PSP] sceJpeg Sample

Post by ardatan »

I have prepared a sample for usage of sceJpeg instead of Jpeglib.
Thanks to J.K. for his help....
And thanks to DXLibraryPortable Creators for their useful library...

If you add this to PSPSDK, it may help someone for his/her new app. :)

main.c

Code: Select all

/*
 * PSP Software Development Kit - http://www.pspdev.org
 * -----------------------------------------------------------------------
 * Licensed under the BSD license, see LICENSE in PSPSDK root for details.
 *
 * main.c - sceJpeg Sample
 *
 * Copyright (c) 2009 Ardatan
 *
 */

#include <pspkernel.h>
#include <pspdebug.h>
#include <pspsdk.h>
#include <stdlib.h>
#include <pspdisplay.h>
#include <pspjpeg.h>
#include <kubridge.h>

PSP_MODULE_INFO&#40;"JpegSample", PSP_MODULE_USER, 1, 1&#41;;
PSP_HEAP_SIZE_MAX&#40;&#41;;

#define BUF_WIDTH &#40;512&#41;
#define SCR_WIDTH &#40;480&#41;
#define SCR_HEIGHT &#40;272&#41;
#define PIXEL_SIZE &#40;4&#41;
#define FRAME_SIZE &#40;BUF_WIDTH * SCR_HEIGHT * PIXEL_SIZE&#41;
#define ZBUF_SIZE &#40;BUF_WIDTH SCR_HEIGHT * 2&#41;
#define printf pspDebugScreenPrintf
#define PTR2HND&#40;PTR&#41;	&#40;&#40;PTR&#41; - 1&#41;
#define HND2PTR&#40;HND&#41;	&#40;HND + 1&#41;

u32 *vram = &#40;u32 *&#41;0x04000000;

/* Exit callback */
int exit_callback&#40;int arg1, int arg2, void *common&#41;
&#123;

	sceKernelExitGame&#40;&#41;;

	return 0;
&#125;

/* Callback thread */
int CallbackThread&#40;SceSize args, void *argp&#41;
&#123;
	int cbid;

	cbid = sceKernelCreateCallback&#40;"Exit Callback", exit_callback, NULL&#41;;
	sceKernelRegisterExitCallback&#40;cbid&#41;;

	sceKernelSleepThreadCB&#40;&#41;;

	return 0;
&#125;

/* Sets up the callback thread and returns its thread id */
int SetupCallbacks&#40;void&#41;
&#123;
	int thid = 0;

	thid = sceKernelCreateThread&#40;"update_thread", CallbackThread, 0x11, 0xFA0, 0, 0&#41;;
	if&#40;thid >= 0&#41;
	&#123;
		sceKernelStartThread&#40;thid, 0, 0&#41;;
	&#125;

	return thid;
&#125;


int FileRead_open&#40;const char* FileName&#41;
&#123;
	return PTR2HND&#40;sceIoOpen&#40;FileName,PSP_O_RDWR,0777&#41;&#41;;
&#125;
int FileRead_close&#40;int FileHandle&#41;
&#123;
	if&#40;FileHandle == -1&#41;return -1;
	sceIoClose&#40;HND2PTR&#40;FileHandle&#41;&#41;;
	return 0;
&#125;
int FileRead_size&#40;const char *FileName&#41;
&#123;
	SceIoStat stat;
	if&#40;sceIoGetstat&#40;FileName,&stat&#41; < 0&#41; return -1;
	if&#40;stat.st_size > 0x00000000ffffffff&#41;return 0xffffffff;
	return stat.st_size;
&#125;

int FileRead_read&#40;void * Buffer,int Size,int FileHandle&#41;
&#123;
	if&#40;FileHandle == -1&#41;return -1;
	return sceIoRead&#40;HND2PTR&#40;FileHandle&#41;,Buffer,Size&#41;;
&#125;

u8* convertjpeg&#40;const char *FileName&#41;
&#123;
		printf&#40;"Convert Starts\n"&#41;;
	int size = FileRead_size&#40;FileName&#41;;
	static u32 rgbabuf&#91;480*272&#93; __attribute__&#40;&#40;aligned&#40;64&#41;&#41;&#41;;
	u8* databuf = &#40;u8*&#41;malloc&#40;size&#41;;
	if&#40;databuf == NULL || rgbabuf == NULL&#41;
	&#123;
		printf&#40;"Malloc Error!\n"&#41;;
		free&#40;databuf&#41;;
		free&#40;rgbabuf&#41;;
		sceJpegDeleteMJpeg&#40;&#41;;
		sceJpegFinishMJpeg&#40;&#41;;
		return NULL;
	&#125;
	int fh = FileRead_open&#40;FileName&#41;;
	if&#40;fh == -1&#41;
	&#123;
		printf&#40;"File couldn't open!\n"&#41;;
		free&#40;databuf&#41;;
		free&#40;rgbabuf&#41;;
		sceJpegDeleteMJpeg&#40;&#41;;
		sceJpegFinishMJpeg&#40;&#41;;
		return NULL;
	&#125;
	FileRead_read&#40;databuf,size,fh&#41;;
	FileRead_close&#40;fh&#41;;
	int res = sceJpegDecodeMJpeg&#40;databuf,size,rgbabuf,0&#41;;
	free&#40;databuf&#41;;
	sceJpegDeleteMJpeg&#40;&#41;;
	sceJpegFinishMJpeg&#40;&#41;;
	if&#40;res < 0&#41;
	&#123;
		printf&#40;"Decode Error!\n"&#41;;
		free&#40;rgbabuf&#41;;
		return NULL;
	&#125;
	printf&#40;"Convert is completed!\n"&#41;;
	sceDisplaySetMode&#40;0, 480, 272&#41;;
	sceDisplaySetFrameBuf&#40;&#40;void *&#41;0x04000000, 512, PSP_DISPLAY_PIXEL_FORMAT_8888, PSP_DISPLAY_SETBUF_NEXTFRAME&#41;;
	int i, j, m, n;
		for &#40;i = 0; i < 272; i++&#41;
		&#123;
			m = i*480;
			n = i*512;

			for &#40;j = 0; j < 480; j++&#41;
			&#123;
				vram&#91;n+j&#93; = rgbabuf&#91;m+j&#93;;
			&#125;
		&#125;
	return 0;
&#125;

int LoadStartModule&#40;char *path&#41; 
&#123; 
  u32 loadResult; 
  u32 startResult; 
  int status; 

  loadResult = kuKernelLoadModule&#40;path, 0, NULL&#41;; 
  if &#40;loadResult & 0x80000000&#41; &#123;
    return -1; 
  &#125; 
  else &#123; 
    startResult = sceKernelStartModule&#40;loadResult, 0, NULL, &status, NULL&#41;; 
  &#125; 

  if &#40;loadResult != startResult&#41; &#123;
    return -2; 
  &#125; 
  return 0; 
&#125;

int StartJpegPsp&#40;&#41; 
&#123;
  printf&#40;"Loading 'flash0&#58;/kd/avcodec.prx'..."&#41;;
  if &#40;LoadStartModule&#40;"flash0&#58;/kd/avcodec.prx"&#41; == 0&#41; &#123;
    printf&#40;"ok\n"&#41;;
  &#125;
  else &#123;
    printf&#40;"failed\n"&#41;;
    return -1;
  &#125;

  printf&#40;"init Jpeg decompression..."&#41;;
  if &#40;sceJpegInitMJpeg&#40;&#41; == 0&#41; &#123;
    if &#40;sceJpegCreateMJpeg&#40;480, 272&#41; == 0&#41; &#123;
      printf&#40;"ok\n"&#41;;
      return 0;
    &#125;
  &#125;

  printf&#40;"failed\n"&#41;;
  return -1;
&#125;


int main&#40;void&#41;
&#123;
SetupCallbacks&#40;&#41;;
pspDebugScreenInit&#40;&#41;;
StartJpegPsp&#40;&#41;;
convertjpeg&#40;"background.jpg"&#41;;
sceKernelSleepThread&#40;&#41;;
return 0;
&#125;
Makefile

Code: Select all

TARGET = jpegsample
OBJS = main.o
LIBS =  -lpspkubridge -lpspjpeg
INCDIR =
CFLAGS = -O0 -G0 -Wall -g
CXXFLAGS = $&#40;CFLAGS&#41; -fno-exceptions -fno-rtti
ASFLAGS = $&#40;CFLAGS&#41;

BUILD_PRX = 1
PSP_LARGE_MEMORY = 1
LIBDIR = 
PSPBIN = $&#40;PSPSDK&#41;/../bin

EXTRA_TARGETS = EBOOT.PBP
PSP_EBOOT_TITLE = JPEG
PSPSDK=$&#40;shell psp-config --pspsdk-path&#41;
include $&#40;PSPSDK&#41;/lib/build.mak
I'm sorry for my bad English.
jimparis
Posts: 1145
Joined: Fri Jun 10, 2005 4:21 am
Location: Boston

Post by jimparis »

kubridge isn't in the sdk, so this can't be added as-is.
Can you repost as a full patch (with Makefile.am changes, etc) with that fixed up?
Post Reply