From 0b5ad60c26f30dc5363c21e436b73292c09ac567 Mon Sep 17 00:00:00 2001 From: Simon Rothen Date: Sat, 30 Aug 2014 13:15:53 +0200 Subject: Introducing Targets iBasso DX50 & iBasso DX90 The port to for this two targets has been entirely developped by Ilia Sergachev (alias Il or xzcc). His source can be found at https://bitbucket.org/isergachev/rockbox . The few necesary modifications for the DX90 port was done by headwhacker form head-fi.org. Unfortunately i could not try out the final state of the DX90 port. The port is hosted on android (without java) as standalone app. The official Firmware is required to run this port. Ilia did modify the source files for the "android" target in the rockbox source to make the DX port work. The work I did was to separate the code for DX50 (&DX90) from the android target. On this Target Ilia used source from tinyalsa from AOSP. I did not touch that part of the code because I do not understand it. What else I changed from Ilias sources besides the separation from the target "android": * removed a dirty hack to keep backlight off * changed value battery meter to voltage battery meter * made all plugins compile (named target as "standalone") and added keymaps * i added the graphics for the manual but did not do anything else for the manual yet * minor optimizations known bugs: * timers are slowed donw when playback is active (tinyalsa related?) * some minor bugs Things to do: * The main prolem will be how to install the app correctly. A guy called DOC2008 added a CWM (by androtab.info) to the official firmware and Ilia made a CWM installation script and a dualboot selector (rbutils/ibassoboot, build with ndk-build). We will have to find a way to install rockbox in a proper way without breaking any copyrights. Maybe ADB is an option but it is not enable with OF by default. Patching the OF is probably the way to go. * All the wiki and manual to build: needed: android ndk installed, android sdk installed with additional build-tools 19.1.0 installed ./tools/configure select iBasso DX50 or iBasso DX90 make -j apk the content of rockbox.zip/.rockbox needs to be copied to /system/rockbox/app_rockbox/rockbox/ (rockbox app not needed) the content of libs/armeabi to /system/rockbox/lib/ (rockbox app needed) The boot selector is needed as /system/bin/MangoPlayer and the iBasso app as /system/bin/MangoPlayer_original. There is also the "vold" file. The one from OF does not work with DX50 rockbox (DX90 works!?), the one from Ilia is necessary. Until we have found a proper way to install it, it can only be installed following the instructions of Ilia on his bitbucket page, using the CWM-OF and his installation script package. Change-Id: Ic4faaf84824c162aabcc08e492cee6e0068719d0 Reviewed-on: http://gerrit.rockbox.org/941 Tested: Chiwen Chang Reviewed-by: Michael Giacomelli --- rbutil/ibassoboot/jni/Android.mk | 6 + rbutil/ibassoboot/jni/chooser.bmp | Bin 0 -> 230454 bytes rbutil/ibassoboot/jni/ibassodualboot.c | 439 ++++++++++++++++++ rbutil/ibassoboot/jni/qdbmp.c | 798 +++++++++++++++++++++++++++++++++ rbutil/ibassoboot/jni/qdbmp.h | 133 ++++++ 5 files changed, 1376 insertions(+) create mode 100644 rbutil/ibassoboot/jni/Android.mk create mode 100644 rbutil/ibassoboot/jni/chooser.bmp create mode 100644 rbutil/ibassoboot/jni/ibassodualboot.c create mode 100644 rbutil/ibassoboot/jni/qdbmp.c create mode 100644 rbutil/ibassoboot/jni/qdbmp.h (limited to 'rbutil/ibassoboot') diff --git a/rbutil/ibassoboot/jni/Android.mk b/rbutil/ibassoboot/jni/Android.mk new file mode 100644 index 0000000000..1d1566d8c0 --- /dev/null +++ b/rbutil/ibassoboot/jni/Android.mk @@ -0,0 +1,6 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_MODULE := MangoPlayer +LOCAL_SRC_FILES := ibassodualboot.c qdbmp.c +include $(BUILD_EXECUTABLE) diff --git a/rbutil/ibassoboot/jni/chooser.bmp b/rbutil/ibassoboot/jni/chooser.bmp new file mode 100644 index 0000000000..3e6742d600 Binary files /dev/null and b/rbutil/ibassoboot/jni/chooser.bmp differ diff --git a/rbutil/ibassoboot/jni/ibassodualboot.c b/rbutil/ibassoboot/jni/ibassodualboot.c new file mode 100644 index 0000000000..3f20bbeecf --- /dev/null +++ b/rbutil/ibassoboot/jni/ibassodualboot.c @@ -0,0 +1,439 @@ +/*************************************************************************** + * __________ __ ___. + * Open \______ \ ____ ____ | | _\_ |__ _______ ___ + * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / + * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < + * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ + * \/ \/ \/ \/ \/ + * $Id$ + * + * Copyright (C) 2014 by Ilia Sergachev + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + ****************************************************************************/ + +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +#include "qdbmp.h" + + +#define MIN_TIME 1395606821 +#define TIME_FILE "/data/time_store" +#define TIME_CHECK_PERIOD 60 /* seconds */ +#define VOLD_LINK "/data/vold" +#define PLAYER_FILE "/data/chosen_player" +#define NOASK_FLAG "/data/no_ask_once" +#define POLL_MS 10 + + +#define KEYCODE_HEADPHONES 114 +#define KEYCODE_HOLD 115 +#define KEYCODE_PWR 116 +#define KEYCODE_PWR_LONG 117 +#define KEYCODE_SD 143 +#define KEYCODE_VOLPLUS 158 +#define KEYCODE_VOLMINUS 159 +#define KEYCODE_PREV 160 +#define KEYCODE_NEXT 162 +#define KEYCODE_PLAY 161 + +#define KEY_HOLD_OFF 16 + +void checktime() +{ + time_t t_stored=0, t_current=time(NULL); + + FILE *f = fopen(TIME_FILE, "r"); + if(f!=NULL) + { + fscanf(f, "%ld", &t_stored); + fclose(f); + } + + printf("stored time: %ld, current time: %ld\n", t_stored, t_current); + + if(t_storedd_name[0] == '.' && + (de->d_name[1] == '\0' || + (de->d_name[1] == '.' && de->d_name[2] == '\0'))) + continue; + strcpy(filename, de->d_name); + open_device(devname, print_flags); + } + closedir(dir); + return 0; +} + + + +void button_init_device(void) +{ + int res; + int print_flags = 0; + const char *device = NULL; + const char *device_path = "/dev/input"; + + nfds = 1; + ufds = calloc(1, sizeof(ufds[0])); + ufds[0].fd = inotify_init(); + ufds[0].events = POLLIN; + if(device) + { + res = open_device(device, print_flags); + if(res < 0) { + fprintf(stderr, "open device failed\n"); + } + } + else + { + res = inotify_add_watch(ufds[0].fd, device_path, IN_DELETE | IN_CREATE); + if(res < 0) + { + fprintf(stderr, "could not add watch for %s, %s\n", device_path, strerror(errno)); + } + res = scan_dir(device_path, print_flags); + if(res < 0) + { + fprintf(stderr, "scan dir failed for %s\n", device_path); + } + } +} + + +int draw() +{ + int fbfd = 0; + struct fb_var_screeninfo vinfo; + struct fb_fix_screeninfo finfo; + long int screensize = 0; + char *fbp = 0; + int x = 0, y = 0; + long int location = 0; + + /* Open the file for reading and writing */ + fbfd = open("/dev/graphics/fb0", O_RDWR); + if (fbfd == -1) + { + perror("Error: cannot open framebuffer device"); + exit(1); + } + /* Get fixed screen information */ + if (ioctl(fbfd, FBIOGET_FSCREENINFO, &finfo) == -1) + { + perror("Error reading fixed information"); + exit(2); + } + + /* Get variable screen information */ + if (ioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo) == -1) + { + perror("Error reading variable information"); + exit(3); + } + + /* Figure out the size of the screen in bytes */ + screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8; + + vinfo.xres = vinfo.xres_virtual = vinfo.width = 320; + vinfo.yres = vinfo.yres_virtual = vinfo.height = 240; + vinfo.xoffset = vinfo.yoffset = vinfo.sync = vinfo.vmode = 0; + vinfo.pixclock = 104377; + vinfo.left_margin = 20; + vinfo.right_margin = 50; + vinfo.upper_margin = 2; + vinfo.lower_margin = 4; + vinfo.hsync_len = 10; + vinfo.vsync_len = 2; + vinfo.red.offset = 11; + vinfo.red.length = 5; + vinfo.red.msb_right = 0; + vinfo.green.offset = 5; + vinfo.green.length = 6; + vinfo.green.msb_right = 0; + vinfo.blue.offset = 0; + vinfo.blue.length = 5; + vinfo.blue.msb_right = 0; + vinfo.transp.offset = vinfo.transp.length = vinfo.transp.msb_right = 0; + vinfo.nonstd = 4; + + if (ioctl(fbfd, FBIOPUT_VSCREENINFO, &vinfo)) + { + perror("fbset(ioctl)"); + exit(4); + } + + + /* Map the device to memory */ + fbp = (char *)mmap(0, screensize, PROT_READ | PROT_WRITE, MAP_SHARED, + fbfd, 0); + if ((int)fbp == -1) + { + perror("Error: failed to map framebuffer device to memory"); + exit(4); + } + + BMP* bmp = BMP_ReadFile("/system/rockbox/chooser.bmp"); + BMP_CHECK_ERROR( stderr, -1 ); + + UCHAR r, g, b; + unsigned short int t; + + for (y = 0; y < 240; y++) + for (x = 0; x < 320; x++) + { + location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) + + (y+vinfo.yoffset) * finfo.line_length; + + BMP_GetPixelRGB(bmp, x, y, &r, &g, &b); + t = (r>>3)<<11 | (g>>2) << 5 | (b>>3); + *((unsigned short int*)(fbp + location)) = t; + } + + BMP_Free( bmp ); + + munmap(fbp, screensize); + close(fbfd); + return 0; +} + + +int choose_player() +{ + int i; + int res; + struct input_event event; + + while(true) + { + poll(ufds, nfds, POLL_MS); + for(i = 1; i < nfds; i++) + { + if(ufds[i].revents & POLLIN) + { + res = read(ufds[i].fd, &event, sizeof(event)); + if(res < (int)sizeof(event)) + { + fprintf(stderr, "could not get event\n"); + } + if(event.type==1) + { + if(event.code==KEYCODE_NEXT) + { + puts("rockbox!"); + return 1; + } + else if(event.code==KEYCODE_PREV) + { + puts("mango!"); + return 0; + } + else if(event.code==KEYCODE_PWR || event.code==KEYCODE_PWR_LONG) + { + reboot(LINUX_REBOOT_CMD_POWER_OFF); + } + } + else if(event.type==3) + { + if(event.code==53) //x coord + { + if(event.value<160) + { + puts("mango!"); + return 0; + } + else + { + puts("rockbox!"); + return 1; + } + } + } + } + } + } + return true; +} + +bool check_for_hold() +{ + FILE *f = fopen("/sys/class/axppower/holdkey", "r"); + char x; + fscanf(f, "%c", &x); + fclose(f); + + if(x & KEY_HOLD_OFF) + return false; + else + return true; +} + +int main(int argc, char **argv) +{ + FILE *f; + int last_chosen_player = -1; + + f = fopen(PLAYER_FILE, "r"); + if(f!=NULL) + { + fscanf(f, "%d", &last_chosen_player); + fclose(f); + } + bool ask = (access(VOLD_LINK, F_OK) == -1) || ((access(NOASK_FLAG, F_OK) == -1) && check_for_hold()) || (last_chosen_player==-1); + + if(ask) + { + draw(); + button_init_device(); + int player_chosen_now = choose_player(); + + if(last_chosen_player!=player_chosen_now) + { + f = fopen(PLAYER_FILE, "w"); + fprintf(f, "%d", player_chosen_now); + fclose(f); + } + + if(last_chosen_player!=player_chosen_now || (access(VOLD_LINK, F_OK) == -1)) + { + system("rm "VOLD_LINK); + + if(player_chosen_now) + system("ln -s /system/bin/vold_rockbox "VOLD_LINK); + else + system("ln -s /system/bin/vold_original "VOLD_LINK); + + system("touch "NOASK_FLAG); + system("reboot"); + } + last_chosen_player = player_chosen_now; + } + + system("rm "NOASK_FLAG); + + while(1) + { + if(last_chosen_player) + { +// system("/system/bin/openadb"); + system("/system/rockbox/lib/rockbox"); + } + else +// system("/system/bin/closeadb"); + system("/system/bin/MangoPlayer_original"); + + sleep(1); + } + + return 0; +} + + + diff --git a/rbutil/ibassoboot/jni/qdbmp.c b/rbutil/ibassoboot/jni/qdbmp.c new file mode 100644 index 0000000000..fd1337277d --- /dev/null +++ b/rbutil/ibassoboot/jni/qdbmp.c @@ -0,0 +1,798 @@ +#include "qdbmp.h" +#include +#include + + +/* Bitmap header */ +typedef struct _BMP_Header +{ + USHORT Magic; /* Magic identifier: "BM" */ + UINT FileSize; /* Size of the BMP file in bytes */ + USHORT Reserved1; /* Reserved */ + USHORT Reserved2; /* Reserved */ + UINT DataOffset; /* Offset of image data relative to the file's start */ + UINT HeaderSize; /* Size of the header in bytes */ + UINT Width; /* Bitmap's width */ + UINT Height; /* Bitmap's height */ + USHORT Planes; /* Number of color planes in the bitmap */ + USHORT BitsPerPixel; /* Number of bits per pixel */ + UINT CompressionType; /* Compression type */ + UINT ImageDataSize; /* Size of uncompressed image's data */ + UINT HPixelsPerMeter; /* Horizontal resolution (pixels per meter) */ + UINT VPixelsPerMeter; /* Vertical resolution (pixels per meter) */ + UINT ColorsUsed; /* Number of color indexes in the color table that are actually used by the bitmap */ + UINT ColorsRequired; /* Number of color indexes that are required for displaying the bitmap */ +} BMP_Header; + + +/* Private data structure */ +struct _BMP +{ + BMP_Header Header; + UCHAR* Palette; + UCHAR* Data; +}; + + +/* Holds the last error code */ +static BMP_STATUS BMP_LAST_ERROR_CODE = 0; + + +/* Error description strings */ +static const char* BMP_ERROR_STRING[] = +{ + "", + "General error", + "Could not allocate enough memory to complete the operation", + "File input/output error", + "File not found", + "File is not a supported BMP variant (must be uncompressed 8, 24 or 32 BPP)", + "File is not a valid BMP image", + "An argument is invalid or out of range", + "The requested action is not compatible with the BMP's type" +}; + + +/* Size of the palette data for 8 BPP bitmaps */ +#define BMP_PALETTE_SIZE ( 256 * 4 ) + + + +/*********************************** Forward declarations **********************************/ +int ReadHeader ( BMP* bmp, FILE* f ); +int WriteHeader ( BMP* bmp, FILE* f ); + +int ReadUINT ( UINT* x, FILE* f ); +int ReadUSHORT ( USHORT *x, FILE* f ); + +int WriteUINT ( UINT x, FILE* f ); +int WriteUSHORT ( USHORT x, FILE* f ); + + + + + + +/*********************************** Public methods **********************************/ + + +/************************************************************** + Creates a blank BMP image with the specified dimensions + and bit depth. +**************************************************************/ +BMP* BMP_Create( UINT width, UINT height, USHORT depth ) +{ + BMP* bmp; + int bytes_per_pixel = depth >> 3; + UINT bytes_per_row; + + if ( height <= 0 || width <= 0 ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return NULL; + } + + if ( depth != 8 && depth != 24 && depth != 32 ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_NOT_SUPPORTED; + return NULL; + } + + + /* Allocate the bitmap data structure */ + bmp = calloc( 1, sizeof( BMP ) ); + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + return NULL; + } + + + /* Set header' default values */ + bmp->Header.Magic = 0x4D42; + bmp->Header.Reserved1 = 0; + bmp->Header.Reserved2 = 0; + bmp->Header.HeaderSize = 40; + bmp->Header.Planes = 1; + bmp->Header.CompressionType = 0; + bmp->Header.HPixelsPerMeter = 0; + bmp->Header.VPixelsPerMeter = 0; + bmp->Header.ColorsUsed = 0; + bmp->Header.ColorsRequired = 0; + + + /* Calculate the number of bytes used to store a single image row. This is always + rounded up to the next multiple of 4. */ + bytes_per_row = width * bytes_per_pixel; + bytes_per_row += ( bytes_per_row % 4 ? 4 - bytes_per_row % 4 : 0 ); + + + /* Set header's image specific values */ + bmp->Header.Width = width; + bmp->Header.Height = height; + bmp->Header.BitsPerPixel = depth; + bmp->Header.ImageDataSize = bytes_per_row * height; + bmp->Header.FileSize = bmp->Header.ImageDataSize + 54 + ( depth == 8 ? BMP_PALETTE_SIZE : 0 ); + bmp->Header.DataOffset = 54 + ( depth == 8 ? BMP_PALETTE_SIZE : 0 ); + + + /* Allocate palette */ + if ( bmp->Header.BitsPerPixel == 8 ) + { + bmp->Palette = (UCHAR*) calloc( BMP_PALETTE_SIZE, sizeof( UCHAR ) ); + if ( bmp->Palette == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + free( bmp ); + return NULL; + } + } + else + { + bmp->Palette = NULL; + } + + + /* Allocate pixels */ + bmp->Data = (UCHAR*) calloc( bmp->Header.ImageDataSize, sizeof( UCHAR ) ); + if ( bmp->Data == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + free( bmp->Palette ); + free( bmp ); + return NULL; + } + + + BMP_LAST_ERROR_CODE = BMP_OK; + + return bmp; +} + + +/************************************************************** + Frees all the memory used by the specified BMP image. +**************************************************************/ +void BMP_Free( BMP* bmp ) +{ + if ( bmp == NULL ) + { + return; + } + + if ( bmp->Palette != NULL ) + { + free( bmp->Palette ); + } + + if ( bmp->Data != NULL ) + { + free( bmp->Data ); + } + + free( bmp ); + + BMP_LAST_ERROR_CODE = BMP_OK; +} + + +/************************************************************** + Reads the specified BMP image file. +**************************************************************/ +BMP* BMP_ReadFile( const char* filename ) +{ + BMP* bmp; + FILE* f; + + if ( filename == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return NULL; + } + + + /* Allocate */ + bmp = calloc( 1, sizeof( BMP ) ); + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + return NULL; + } + + + /* Open file */ + f = fopen( filename, "rb" ); + if ( f == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_NOT_FOUND; + free( bmp ); + return NULL; + } + + + /* Read header */ + if ( ReadHeader( bmp, f ) != BMP_OK || bmp->Header.Magic != 0x4D42 ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_INVALID; + fclose( f ); + free( bmp ); + return NULL; + } + + + /* Verify that the bitmap variant is supported */ + if ( ( bmp->Header.BitsPerPixel != 32 && bmp->Header.BitsPerPixel != 24 && bmp->Header.BitsPerPixel != 8 ) + || bmp->Header.CompressionType != 0 || bmp->Header.HeaderSize != 40 ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_NOT_SUPPORTED; + fclose( f ); + free( bmp ); + return NULL; + } + + + /* Allocate and read palette */ + if ( bmp->Header.BitsPerPixel == 8 ) + { + bmp->Palette = (UCHAR*) malloc( BMP_PALETTE_SIZE * sizeof( UCHAR ) ); + if ( bmp->Palette == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + fclose( f ); + free( bmp ); + return NULL; + } + + if ( fread( bmp->Palette, sizeof( UCHAR ), BMP_PALETTE_SIZE, f ) != BMP_PALETTE_SIZE ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_INVALID; + fclose( f ); + free( bmp->Palette ); + free( bmp ); + return NULL; + } + } + else /* Not an indexed image */ + { + bmp->Palette = NULL; + } + + + /* Allocate memory for image data */ + bmp->Data = (UCHAR*) malloc( bmp->Header.ImageDataSize ); + if ( bmp->Data == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_OUT_OF_MEMORY; + fclose( f ); + free( bmp->Palette ); + free( bmp ); + return NULL; + } + + + /* Read image data */ + if ( fread( bmp->Data, sizeof( UCHAR ), bmp->Header.ImageDataSize, f ) != bmp->Header.ImageDataSize ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_INVALID; + fclose( f ); + free( bmp->Data ); + free( bmp->Palette ); + free( bmp ); + return NULL; + } + + + fclose( f ); + + BMP_LAST_ERROR_CODE = BMP_OK; + + return bmp; +} + + +/************************************************************** + Writes the BMP image to the specified file. +**************************************************************/ +void BMP_WriteFile( BMP* bmp, const char* filename ) +{ + FILE* f; + + if ( filename == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return; + } + + + /* Open file */ + f = fopen( filename, "wb" ); + if ( f == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_FILE_NOT_FOUND; + return; + } + + + /* Write header */ + if ( WriteHeader( bmp, f ) != BMP_OK ) + { + BMP_LAST_ERROR_CODE = BMP_IO_ERROR; + fclose( f ); + return; + } + + + /* Write palette */ + if ( bmp->Palette ) + { + if ( fwrite( bmp->Palette, sizeof( UCHAR ), BMP_PALETTE_SIZE, f ) != BMP_PALETTE_SIZE ) + { + BMP_LAST_ERROR_CODE = BMP_IO_ERROR; + fclose( f ); + return; + } + } + + + /* Write data */ + if ( fwrite( bmp->Data, sizeof( UCHAR ), bmp->Header.ImageDataSize, f ) != bmp->Header.ImageDataSize ) + { + BMP_LAST_ERROR_CODE = BMP_IO_ERROR; + fclose( f ); + return; + } + + + BMP_LAST_ERROR_CODE = BMP_OK; + fclose( f ); +} + + +/************************************************************** + Returns the image's width. +**************************************************************/ +UINT BMP_GetWidth( BMP* bmp ) +{ + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return -1; + } + + BMP_LAST_ERROR_CODE = BMP_OK; + + return ( bmp->Header.Width ); +} + + +/************************************************************** + Returns the image's height. +**************************************************************/ +UINT BMP_GetHeight( BMP* bmp ) +{ + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return -1; + } + + BMP_LAST_ERROR_CODE = BMP_OK; + + return ( bmp->Header.Height ); +} + + +/************************************************************** + Returns the image's color depth (bits per pixel). +**************************************************************/ +USHORT BMP_GetDepth( BMP* bmp ) +{ + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + return -1; + } + + BMP_LAST_ERROR_CODE = BMP_OK; + + return ( bmp->Header.BitsPerPixel ); +} + + +/************************************************************** + Populates the arguments with the specified pixel's RGB + values. +**************************************************************/ +void BMP_GetPixelRGB( BMP* bmp, UINT x, UINT y, UCHAR* r, UCHAR* g, UCHAR* b ) +{ + UCHAR* pixel; + UINT bytes_per_row; + UCHAR bytes_per_pixel; + + if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + else + { + BMP_LAST_ERROR_CODE = BMP_OK; + + bytes_per_pixel = bmp->Header.BitsPerPixel >> 3; + + /* Row's size is rounded up to the next multiple of 4 bytes */ + bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height; + + /* Calculate the location of the relevant pixel (rows are flipped) */ + pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x * bytes_per_pixel ); + + + /* In indexed color mode the pixel's value is an index within the palette */ + if ( bmp->Header.BitsPerPixel == 8 ) + { + pixel = bmp->Palette + *pixel * 4; + } + + /* Note: colors are stored in BGR order */ + if ( r ) *r = *( pixel + 2 ); + if ( g ) *g = *( pixel + 1 ); + if ( b ) *b = *( pixel + 0 ); + } +} + + +/************************************************************** + Sets the specified pixel's RGB values. +**************************************************************/ +void BMP_SetPixelRGB( BMP* bmp, UINT x, UINT y, UCHAR r, UCHAR g, UCHAR b ) +{ + UCHAR* pixel; + UINT bytes_per_row; + UCHAR bytes_per_pixel; + + if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + + else if ( bmp->Header.BitsPerPixel != 24 && bmp->Header.BitsPerPixel != 32 ) + { + BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH; + } + + else + { + BMP_LAST_ERROR_CODE = BMP_OK; + + bytes_per_pixel = bmp->Header.BitsPerPixel >> 3; + + /* Row's size is rounded up to the next multiple of 4 bytes */ + bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height; + + /* Calculate the location of the relevant pixel (rows are flipped) */ + pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x * bytes_per_pixel ); + + /* Note: colors are stored in BGR order */ + *( pixel + 2 ) = r; + *( pixel + 1 ) = g; + *( pixel + 0 ) = b; + } +} + + +/************************************************************** + Gets the specified pixel's color index. +**************************************************************/ +void BMP_GetPixelIndex( BMP* bmp, UINT x, UINT y, UCHAR* val ) +{ + UCHAR* pixel; + UINT bytes_per_row; + + if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + + else if ( bmp->Header.BitsPerPixel != 8 ) + { + BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH; + } + + else + { + BMP_LAST_ERROR_CODE = BMP_OK; + + /* Row's size is rounded up to the next multiple of 4 bytes */ + bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height; + + /* Calculate the location of the relevant pixel */ + pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x ); + + + if ( val ) *val = *pixel; + } +} + + +/************************************************************** + Sets the specified pixel's color index. +**************************************************************/ +void BMP_SetPixelIndex( BMP* bmp, UINT x, UINT y, UCHAR val ) +{ + UCHAR* pixel; + UINT bytes_per_row; + + if ( bmp == NULL || x < 0 || x >= bmp->Header.Width || y < 0 || y >= bmp->Header.Height ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + + else if ( bmp->Header.BitsPerPixel != 8 ) + { + BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH; + } + + else + { + BMP_LAST_ERROR_CODE = BMP_OK; + + /* Row's size is rounded up to the next multiple of 4 bytes */ + bytes_per_row = bmp->Header.ImageDataSize / bmp->Header.Height; + + /* Calculate the location of the relevant pixel */ + pixel = bmp->Data + ( ( bmp->Header.Height - y - 1 ) * bytes_per_row + x ); + + *pixel = val; + } +} + + +/************************************************************** + Gets the color value for the specified palette index. +**************************************************************/ +void BMP_GetPaletteColor( BMP* bmp, UCHAR index, UCHAR* r, UCHAR* g, UCHAR* b ) +{ + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + + else if ( bmp->Header.BitsPerPixel != 8 ) + { + BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH; + } + + else + { + if ( r ) *r = *( bmp->Palette + index * 4 + 2 ); + if ( g ) *g = *( bmp->Palette + index * 4 + 1 ); + if ( b ) *b = *( bmp->Palette + index * 4 + 0 ); + + BMP_LAST_ERROR_CODE = BMP_OK; + } +} + + +/************************************************************** + Sets the color value for the specified palette index. +**************************************************************/ +void BMP_SetPaletteColor( BMP* bmp, UCHAR index, UCHAR r, UCHAR g, UCHAR b ) +{ + if ( bmp == NULL ) + { + BMP_LAST_ERROR_CODE = BMP_INVALID_ARGUMENT; + } + + else if ( bmp->Header.BitsPerPixel != 8 ) + { + BMP_LAST_ERROR_CODE = BMP_TYPE_MISMATCH; + } + + else + { + *( bmp->Palette + index * 4 + 2 ) = r; + *( bmp->Palette + index * 4 + 1 ) = g; + *( bmp->Palette + index * 4 + 0 ) = b; + + BMP_LAST_ERROR_CODE = BMP_OK; + } +} + + +/************************************************************** + Returns the last error code. +**************************************************************/ +BMP_STATUS BMP_GetError() +{ + return BMP_LAST_ERROR_CODE; +} + + +/************************************************************** + Returns a description of the last error code. +**************************************************************/ +const char* BMP_GetErrorDescription() +{ + if ( BMP_LAST_ERROR_CODE > 0 && BMP_LAST_ERROR_CODE < BMP_ERROR_NUM ) + { + return BMP_ERROR_STRING[ BMP_LAST_ERROR_CODE ]; + } + else + { + return NULL; + } +} + + + + + +/*********************************** Private methods **********************************/ + + +/************************************************************** + Reads the BMP file's header into the data structure. + Returns BMP_OK on success. +**************************************************************/ +int ReadHeader( BMP* bmp, FILE* f ) +{ + if ( bmp == NULL || f == NULL ) + { + return BMP_INVALID_ARGUMENT; + } + + /* The header's fields are read one by one, and converted from the format's + little endian to the system's native representation. */ + if ( !ReadUSHORT( &( bmp->Header.Magic ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.FileSize ), f ) ) return BMP_IO_ERROR; + if ( !ReadUSHORT( &( bmp->Header.Reserved1 ), f ) ) return BMP_IO_ERROR; + if ( !ReadUSHORT( &( bmp->Header.Reserved2 ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.DataOffset ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.HeaderSize ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.Width ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.Height ), f ) ) return BMP_IO_ERROR; + if ( !ReadUSHORT( &( bmp->Header.Planes ), f ) ) return BMP_IO_ERROR; + if ( !ReadUSHORT( &( bmp->Header.BitsPerPixel ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.CompressionType ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.ImageDataSize ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.HPixelsPerMeter ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.VPixelsPerMeter ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.ColorsUsed ), f ) ) return BMP_IO_ERROR; + if ( !ReadUINT( &( bmp->Header.ColorsRequired ), f ) ) return BMP_IO_ERROR; + + return BMP_OK; +} + + +/************************************************************** + Writes the BMP file's header into the data structure. + Returns BMP_OK on success. +**************************************************************/ +int WriteHeader( BMP* bmp, FILE* f ) +{ + if ( bmp == NULL || f == NULL ) + { + return BMP_INVALID_ARGUMENT; + } + + /* The header's fields are written one by one, and converted to the format's + little endian representation. */ + if ( !WriteUSHORT( bmp->Header.Magic, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.FileSize, f ) ) return BMP_IO_ERROR; + if ( !WriteUSHORT( bmp->Header.Reserved1, f ) ) return BMP_IO_ERROR; + if ( !WriteUSHORT( bmp->Header.Reserved2, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.DataOffset, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.HeaderSize, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.Width, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.Height, f ) ) return BMP_IO_ERROR; + if ( !WriteUSHORT( bmp->Header.Planes, f ) ) return BMP_IO_ERROR; + if ( !WriteUSHORT( bmp->Header.BitsPerPixel, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.CompressionType, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.ImageDataSize, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.HPixelsPerMeter, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.VPixelsPerMeter, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.ColorsUsed, f ) ) return BMP_IO_ERROR; + if ( !WriteUINT( bmp->Header.ColorsRequired, f ) ) return BMP_IO_ERROR; + + return BMP_OK; +} + + +/************************************************************** + Reads a little-endian unsigned int from the file. + Returns non-zero on success. +**************************************************************/ +int ReadUINT( UINT* x, FILE* f ) +{ + UCHAR little[ 4 ]; /* BMPs use 32 bit ints */ + + if ( x == NULL || f == NULL ) + { + return 0; + } + + if ( fread( little, 4, 1, f ) != 1 ) + { + return 0; + } + + *x = ( little[ 3 ] << 24 | little[ 2 ] << 16 | little[ 1 ] << 8 | little[ 0 ] ); + + return 1; +} + + +/************************************************************** + Reads a little-endian unsigned short int from the file. + Returns non-zero on success. +**************************************************************/ +int ReadUSHORT( USHORT *x, FILE* f ) +{ + UCHAR little[ 2 ]; /* BMPs use 16 bit shorts */ + + if ( x == NULL || f == NULL ) + { + return 0; + } + + if ( fread( little, 2, 1, f ) != 1 ) + { + return 0; + } + + *x = ( little[ 1 ] << 8 | little[ 0 ] ); + + return 1; +} + + +/************************************************************** + Writes a little-endian unsigned int to the file. + Returns non-zero on success. +**************************************************************/ +int WriteUINT( UINT x, FILE* f ) +{ + UCHAR little[ 4 ]; /* BMPs use 32 bit ints */ + + little[ 3 ] = (UCHAR)( ( x & 0xff000000 ) >> 24 ); + little[ 2 ] = (UCHAR)( ( x & 0x00ff0000 ) >> 16 ); + little[ 1 ] = (UCHAR)( ( x & 0x0000ff00 ) >> 8 ); + little[ 0 ] = (UCHAR)( ( x & 0x000000ff ) >> 0 ); + + return ( f && fwrite( little, 4, 1, f ) == 1 ); +} + + +/************************************************************** + Writes a little-endian unsigned short int to the file. + Returns non-zero on success. +**************************************************************/ +int WriteUSHORT( USHORT x, FILE* f ) +{ + UCHAR little[ 2 ]; /* BMPs use 16 bit shorts */ + + little[ 1 ] = (UCHAR)( ( x & 0xff00 ) >> 8 ); + little[ 0 ] = (UCHAR)( ( x & 0x00ff ) >> 0 ); + + return ( f && fwrite( little, 2, 1, f ) == 1 ); +} + diff --git a/rbutil/ibassoboot/jni/qdbmp.h b/rbutil/ibassoboot/jni/qdbmp.h new file mode 100644 index 0000000000..d6c0e6c452 --- /dev/null +++ b/rbutil/ibassoboot/jni/qdbmp.h @@ -0,0 +1,133 @@ +#ifndef _BMP_H_ +#define _BMP_H_ + + +/************************************************************** + + QDBMP - Quick n' Dirty BMP + + v1.0.0 - 2007-04-07 + http://qdbmp.sourceforge.net + + + The library supports the following BMP variants: + 1. Uncompressed 32 BPP (alpha values are ignored) + 2. Uncompressed 24 BPP + 3. Uncompressed 8 BPP (indexed color) + + QDBMP is free and open source software, distributed + under the MIT licence. + + Copyright (c) 2007 Chai Braudo (braudo@users.sourceforge.net) + + Permission is hereby granted, free of charge, to any person obtaining a copy + of this software and associated documentation files (the "Software"), to deal + in the Software without restriction, including without limitation the rights + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + copies of the Software, and to permit persons to whom the Software is + furnished to do so, subject to the following conditions: + + The above copyright notice and this permission notice shall be included in + all copies or substantial portions of the Software. + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + THE SOFTWARE. + +**************************************************************/ + +#include + + + +/* Type definitions */ +#ifndef UINT + #define UINT unsigned long int +#endif + +#ifndef USHORT + #define USHORT unsigned short +#endif + +#ifndef UCHAR + #define UCHAR unsigned char +#endif + + +/* Version */ +#define QDBMP_VERSION_MAJOR 1 +#define QDBMP_VERSION_MINOR 0 +#define QDBMP_VERSION_PATCH 1 + + +/* Error codes */ +typedef enum +{ + BMP_OK = 0, /* No error */ + BMP_ERROR, /* General error */ + BMP_OUT_OF_MEMORY, /* Could not allocate enough memory to complete the operation */ + BMP_IO_ERROR, /* General input/output error */ + BMP_FILE_NOT_FOUND, /* File not found */ + BMP_FILE_NOT_SUPPORTED, /* File is not a supported BMP variant */ + BMP_FILE_INVALID, /* File is not a BMP image or is an invalid BMP */ + BMP_INVALID_ARGUMENT, /* An argument is invalid or out of range */ + BMP_TYPE_MISMATCH, /* The requested action is not compatible with the BMP's type */ + BMP_ERROR_NUM +} BMP_STATUS; + + +/* Bitmap image */ +typedef struct _BMP BMP; + + + + +/*********************************** Public methods **********************************/ + + +/* Construction/destruction */ +BMP* BMP_Create ( UINT width, UINT height, USHORT depth ); +void BMP_Free ( BMP* bmp ); + + +/* I/O */ +BMP* BMP_ReadFile ( const char* filename ); +void BMP_WriteFile ( BMP* bmp, const char* filename ); + + +/* Meta info */ +UINT BMP_GetWidth ( BMP* bmp ); +UINT BMP_GetHeight ( BMP* bmp ); +USHORT BMP_GetDepth ( BMP* bmp ); + + +/* Pixel access */ +void BMP_GetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR* r, UCHAR* g, UCHAR* b ); +void BMP_SetPixelRGB ( BMP* bmp, UINT x, UINT y, UCHAR r, UCHAR g, UCHAR b ); +void BMP_GetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR* val ); +void BMP_SetPixelIndex ( BMP* bmp, UINT x, UINT y, UCHAR val ); + + +/* Palette handling */ +void BMP_GetPaletteColor ( BMP* bmp, UCHAR index, UCHAR* r, UCHAR* g, UCHAR* b ); +void BMP_SetPaletteColor ( BMP* bmp, UCHAR index, UCHAR r, UCHAR g, UCHAR b ); + + +/* Error handling */ +BMP_STATUS BMP_GetError (); +const char* BMP_GetErrorDescription (); + + +/* Useful macro that may be used after each BMP operation to check for an error */ +#define BMP_CHECK_ERROR( output_file, return_value ) \ + if ( BMP_GetError() != BMP_OK ) \ + { \ + fprintf( ( output_file ), "BMP error: %s\n", BMP_GetErrorDescription() ); \ + return( return_value ); \ + } \ + +#endif -- cgit