freemyipod r490 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r489‎ | r490 | r491 >
Date:03:04, 23 January 2011
Author:theseven
Status:new
Tags:
Comment:
New project: emCORE User Interface Library. For now only supports 24bit to lcd-native dithering.
Modified paths:
  • /libs/ui (added) (history)
  • /libs/ui/Makefile (added) (history)
  • /libs/ui/SOURCES (added) (history)
  • /libs/ui/dither.c (added) (history)
  • /libs/ui/dither.h (added) (history)
  • /libs/ui/export (added) (history)
  • /libs/ui/export/libui.h (added) (history)
  • /libs/ui/ls.x (added) (history)
  • /libs/ui/main.c (added) (history)
  • /libs/ui/version.h (added) (history)

Diff [purge]

Index: libs/ui/dither.c
@@ -0,0 +1,164 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "emcorelib.h"
 26+#include "dither.h"
 27+
 28+
 29+static void dither_slow(int width, int height, void* inbuf, int inx, int iny, int instride,
 30+ void* outbuf, int outx, int outy, int outstride)
 31+{
 32+ int bpp = lcd_get_bytes_per_pixel();
 33+ int bits = lcd_get_format();
 34+ int swap = bits & BIT(30);
 35+ int rwidth = MAX(7, bits & BITRANGE(0, 3));
 36+ int gwidth = MAX(7, bits & BITRANGE(10, 13));
 37+ int bwidth = MAX(7, bits & BITRANGE(20, 23));
 38+ int rshift = bits & BITRANGE(4, 9);
 39+ int gshift = bits & BITRANGE(14, 19);
 40+ int bshift = bits & BITRANGE(24, 29);
 41+ int roffs = 1 << (6 - rwidth);
 42+ int goffs = 1 << (6 - gwidth);
 43+ int boffs = 1 << (6 - bwidth);
 44+ int rmask = BITRANGE(7 - rwidth, 7);
 45+ int gmask = BITRANGE(7 - gwidth, 7);
 46+ int bmask = BITRANGE(7 - bwidth, 7);
 47+ int rclip = 7 - rshift;
 48+ int gclip = 7 - gshift;
 49+ int bclip = 7 - bshift;
 50+ char* in = ((char*)inbuf) + (instride * iny + inx) * 3;
 51+ char* out = ((char*)outbuf) + (instride * iny + inx) * bpp;
 52+ int x, y;
 53+ for (y = 0; y < height; y++)
 54+ {
 55+ for (x = 0; x < width; x++)
 56+ {
 57+ int origb = *in++;
 58+ int origg = *in++;
 59+ int origr = *in++;
 60+ int realr = origr >> rclip;
 61+ int realg = origg >> gclip;
 62+ int realb = origb >> bclip;
 63+ int errr = origr - (realr << rclip) - roffs;
 64+ int errg = origg - (realg << gclip) - goffs;
 65+ int errb = origb - (realb << bclip) - boffs;
 66+ if (x + 1 < width)
 67+ {
 68+ *(in + 0) = MAX(0, MIN(255, *(in + 0) + errb / 2));
 69+ *(in + 1) = MAX(0, MIN(255, *(in + 1) + errg / 2));
 70+ *(in + 2) = MAX(0, MIN(255, *(in + 2) + errr / 2));
 71+ }
 72+ if (y + 1 < height)
 73+ {
 74+ *(in + 3 * instride - 1) = MAX(0, MIN(255, *(in + 3 * instride - 1) + errr / 4));
 75+ *(in + 3 * instride - 2) = MAX(0, MIN(255, *(in + 3 * instride - 2) + errg / 4));
 76+ *(in + 3 * instride - 3) = MAX(0, MIN(255, *(in + 3 * instride - 3) + errb / 4));
 77+ *(in + 3 * instride - 4) = MAX(0, MIN(255, *(in + 3 * instride - 4) + errr / 4));
 78+ *(in + 3 * instride - 5) = MAX(0, MIN(255, *(in + 3 * instride - 5) + errg / 4));
 79+ *(in + 3 * instride - 6) = MAX(0, MIN(255, *(in + 3 * instride - 6) + errb / 4));
 80+ }
 81+ int pixel = (realr << rshift) | (realg << gshift) | (realb << bshift);
 82+ if (bpp == 1) *out = pixel;
 83+ else if (bpp == 2)
 84+ {
 85+ if (swap) *((short*)out) = (pixel >> 8) | ((pixel << 8) & 0xff00);
 86+ else *((short*)out) = pixel;
 87+ }
 88+ else if (bpp == 3)
 89+ {
 90+ if (swap)
 91+ {
 92+ *(out + 0) = pixel & 0xff;
 93+ *(out + 1) = (pixel >> 8) & 0xff;
 94+ *(out + 2) = (pixel >> 16) & 0xff;
 95+ }
 96+ else
 97+ {
 98+ *(out + 0) = (pixel >> 16) & 0xff;
 99+ *(out + 1) = (pixel >> 8) & 0xff;
 100+ *(out + 2) = pixel & 0xff;
 101+ }
 102+ }
 103+ else if (bpp == 4)
 104+ {
 105+ if (swap) *((int*)out) = (pixel >> 24) | ((pixel >> 8) & 0xff00)
 106+ | ((pixel << 8) & 0xff0000) | ((pixel << 24) & 0xff000000);
 107+ else *((int*)out) = pixel;
 108+ }
 109+ out += bpp;
 110+ }
 111+ in += (instride - width) * 3;
 112+ out += (outstride - width) * bpp;
 113+ }
 114+}
 115+
 116+static void dither_rgb565(int width, int height, void* inbuf, int inx, int iny, int instride,
 117+ void* outbuf, int outx, int outy, int outstride)
 118+{
 119+ char* in = ((char*)inbuf) + (instride * iny + inx) * 3;
 120+ short* out = ((short*)outbuf) + instride * iny + inx;
 121+ int x, y;
 122+ for (y = 0; y < height; y++)
 123+ {
 124+ for (x = 0; x < width; x++)
 125+ {
 126+ int origb = *in++;
 127+ int origg = *in++;
 128+ int origr = *in++;
 129+ int realr = origr >> 3;
 130+ int realg = origg >> 2;
 131+ int realb = origb >> 3;
 132+ int errr = origr - (realr << 3) - 4;
 133+ int errg = origg - (realg << 2) - 2;
 134+ int errb = origb - (realb << 3) - 4;
 135+ if (x + 1 < width)
 136+ {
 137+ *(in + 0) = MAX(0, MIN(255, *(in + 0) + errb / 2));
 138+ *(in + 1) = MAX(0, MIN(255, *(in + 1) + errg / 2));
 139+ *(in + 2) = MAX(0, MIN(255, *(in + 2) + errr / 2));
 140+ }
 141+ if (y + 1 < height)
 142+ {
 143+ *(in + 3 * instride - 1) = MAX(0, MIN(255, *(in + 3 * instride - 1) + errr / 4));
 144+ *(in + 3 * instride - 2) = MAX(0, MIN(255, *(in + 3 * instride - 2) + errg / 4));
 145+ *(in + 3 * instride - 3) = MAX(0, MIN(255, *(in + 3 * instride - 3) + errb / 4));
 146+ *(in + 3 * instride - 4) = MAX(0, MIN(255, *(in + 3 * instride - 4) + errr / 4));
 147+ *(in + 3 * instride - 5) = MAX(0, MIN(255, *(in + 3 * instride - 5) + errg / 4));
 148+ *(in + 3 * instride - 6) = MAX(0, MIN(255, *(in + 3 * instride - 6) + errb / 4));
 149+ }
 150+ *out++ = (realr << 11) | (realg << 5) | realb;
 151+ }
 152+ in += (instride - width) * 3;
 153+ out += outstride - width;
 154+ }
 155+}
 156+
 157+
 158+void dither(int width, int height, void* inbuf, int inx, int iny, int instride,
 159+ void* outbuf, int outx, int outy, int outstride)
 160+{
 161+ int bits = lcd_get_format();
 162+ if (bits == 0x004154b1)
 163+ dither_rgb565(width, height, inbuf, inx, iny, instride, outbuf, outx, outy, outstride);
 164+ else dither_slow(width, height, inbuf, inx, iny, instride, outbuf, outx, outy, outstride);
 165+}
Index: libs/ui/export/libui.h
@@ -0,0 +1,51 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __LIBUI_H__
 26+#define __LIBUI_H__
 27+
 28+#include "emcorelib.h"
 29+
 30+
 31+#include "../dither.h"
 32+
 33+
 34+/* increase this every time the api struct changes */
 35+#define LIBUI_API_VERSION 1
 36+
 37+/* update this to latest version if a change to the api struct breaks
 38+ backwards compatibility (and please take the opportunity to sort in any
 39+ new function which are "waiting" at the end of the function table) */
 40+#define LIBUI_MIN_API_VERSION 1
 41+
 42+/* NOTE: To support backwards compatibility, only add new functions at
 43+ the end of the structure. Every time you add a new function,
 44+ remember to increase LIBUI_API_VERSION. If you make changes to the
 45+ existing APIs, also update LIBUI_MIN_API_VERSION to current version */
 46+
 47+struct libui_api
 48+{
 49+ typeof(dither)* dither;
 50+};
 51+
 52+#endif
Property changes on: libs/ui/export/libui.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 53+*
\ No newline at end of property
Index: libs/ui/SOURCES
@@ -0,0 +1,2 @@
 2+main.c
 3+dither.c
Property changes on: libs/ui/SOURCES
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 4+*
\ No newline at end of property
Index: libs/ui/ls.x
@@ -0,0 +1,42 @@
 2+ENTRY(__emcore_lib_header)
 3+OUTPUT_FORMAT(elf32-littlearm)
 4+OUTPUT_ARCH(arm)
 5+
 6+MEMORY
 7+{
 8+ VIRTUAL : ORIGIN = 0x00000000, LENGTH = 0x10000000
 9+}
 10+
 11+SECTIONS
 12+{
 13+ .text :
 14+ {
 15+ __emcore_lib_base = .;
 16+ KEEP(.emcoreentrypoint*)
 17+ *(.emcoreentrypoint*)
 18+ *(.text*)
 19+ *(.glue_7)
 20+ *(.glue_7t)
 21+ . = ALIGN(0x4);
 22+ } > VIRTUAL
 23+
 24+ .data :
 25+ {
 26+ *(.rodata*)
 27+ . = ALIGN(0x4);
 28+ *(.data*)
 29+ . = ALIGN(0x4);
 30+ } > VIRTUAL
 31+
 32+ .bss (NOLOAD) :
 33+ {
 34+ *(.bss*)
 35+ *(COMMON)
 36+ } > VIRTUAL
 37+
 38+ /DISCARD/ :
 39+ {
 40+ *(.eh_frame)
 41+ }
 42+
 43+}
Property changes on: libs/ui/ls.x
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 44+*
\ No newline at end of property
Index: libs/ui/dither.h
@@ -0,0 +1,34 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __DITHER_H__
 26+#define __DITHER_H__
 27+
 28+#include "emcorelib.h"
 29+
 30+
 31+void dither(int width, int height, void* inbuf, int inx, int iny, int instride,
 32+ void* outbuf, int outx, int outy, int outstride);
 33+
 34+
 35+#endif
Index: libs/ui/main.c
@@ -0,0 +1,33 @@
 2+//
 3+//
 4+// Copyright 2011 TheSeven
 5+//
 6+//
 7+// This file is part of emCORE.
 8+//
 9+// emCORE is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emCORE is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emCORE. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#include "emcorelib.h"
 26+#include "export/libui.h"
 27+
 28+
 29+struct libui_api apitable =
 30+{
 31+ .dither = dither
 32+};
 33+
 34+EMCORE_LIB_HEADER(0x49554365, LIBUI_API_VERSION, LIBUI_MIN_API_VERSION, NULL, NULL, apitable)
Property changes on: libs/ui/main.c
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 35+*
\ No newline at end of property
Index: libs/ui/version.h
@@ -0,0 +1,36 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __VERSION_H__
 26+#define __VERSION_H__
 27+
 28+
 29+#define VERSION "0.0.1pre"
 30+#define VERSION_MAJOR 0
 31+#define VERSION_MINOR 0
 32+#define VERSION_PATCH 1
 33+#define VERSION_SVN "$REVISION$"
 34+#define VERSION_SVN_INT $REVISIONINT$
 35+
 36+
 37+#endif
\ No newline at end of file
Property changes on: libs/ui/version.h
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 38+*
\ No newline at end of property
Index: libs/ui/Makefile
@@ -0,0 +1,109 @@
 2+NAME := ui
 3+COMPRESS := true
 4+
 5+EMCOREDIR ?= ../../emcore/trunk/
 6+
 7+ifeq ($(shell uname),WindowsNT)
 8+CCACHE :=
 9+else
 10+CCACHE := $(shell which ccache)
 11+endif
 12+
 13+CROSS ?= arm-elf-eabi-
 14+CC := $(CCACHE) $(CROSS)gcc
 15+AS := $(CROSS)as
 16+LD := $(CROSS)ld
 17+OBJCOPY := $(CROSS)objcopy
 18+ELF2ECA := $(CROSS)elf2emcoreapp
 19+
 20+CFLAGS += -Os -fno-pie -fno-stack-protector -fomit-frame-pointer -I. -I$(EMCOREDIR)/export -ffunction-sections -fdata-sections -mcpu=arm940t
 21+LDFLAGS += "$(shell $(CC) -print-libgcc-file-name)" -d -r --gc-sections
 22+
 23+preprocess = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#")
 24+preprocesspaths = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#" | sed -e "s:^..*:$(dir $(1))&:")
 25+
 26+REVISION := $(shell svnversion .)
 27+REVISIONINT := $(shell echo $(REVISION) | sed -e "s/[^0-9].*$$//")
 28+
 29+HELPERS := build/__emcore_armhelpers.o
 30+
 31+SRC := $(call preprocesspaths,SOURCES,-I. -I..)
 32+OBJ := $(SRC:%.c=build/%.o)
 33+OBJ := $(OBJ:%.S=build/%.o) $(HELPERS)
 34+
 35+all: $(NAME)
 36+
 37+-include $(OBJ:%=%.dep)
 38+
 39+$(NAME): build/$(NAME).emcorelib
 40+
 41+build/$(NAME).emcorelib: build/$(NAME).elf
 42+ @echo [EMCLIB] $<
 43+ifeq ($(COMPRESS),true)
 44+ @$(ELF2ECA) -l -z -s 0 -o $@ $^
 45+else
 46+ @$(ELF2ECA) -l -s 0 -o $@ $^
 47+endif
 48+
 49+build/$(NAME).elf: ls.x $(OBJ)
 50+ @echo [LD] $@
 51+ @$(LD) $(LDFLAGS) -o $@ -T ls.x $(OBJ)
 52+
 53+build/%.o: %.c build/version.h
 54+ @echo [CC] $<
 55+ifeq ($(shell uname),WindowsNT)
 56+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 57+else
 58+ @-mkdir -p $(dir $@)
 59+endif
 60+ @$(CC) -c $(CFLAGS) -o $@ $<
 61+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 62+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 63+ifeq ($(shell uname),WindowsNT)
 64+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 65+else
 66+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 67+endif
 68+ @rm -f $@.dep.tmp
 69+
 70+build/%.o: %.S build/version.h
 71+ @echo [CC] $<
 72+ifeq ($(shell uname),WindowsNT)
 73+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 74+else
 75+ @-mkdir -p $(dir $@)
 76+endif
 77+ @$(CC) -c $(CFLAGS) -o $@ $<
 78+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 79+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 80+ifeq ($(shell uname),WindowsNT)
 81+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 82+else
 83+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 84+endif
 85+ @rm -f $@.dep.tmp
 86+
 87+build/__emcore_%.o: $(EMCOREDIR)/export/%.S
 88+ @echo [CC] $<
 89+ifeq ($(shell uname),WindowsNT)
 90+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 91+else
 92+ @-mkdir -p $(dir $@)
 93+endif
 94+ @$(CC) -c $(CFLAGS) -o $@ $<
 95+
 96+build/version.h: version.h .svn/entries build
 97+ @echo [PP] $<
 98+ifeq ($(shell uname),WindowsNT)
 99+ @sed -e "s/\$$REVISION\$$/$(REVISION)/" -e "s/\$$REVISIONINT\$$/$(REVISIONINT)/" < $< > $@
 100+else
 101+ @sed -e 's/\$$REVISION\$$/$(REVISION)/' -e 's/\$$REVISIONINT\$$/$(REVISIONINT)/' < $< > $@
 102+endif
 103+
 104+build:
 105+ @mkdir $@
 106+
 107+clean:
 108+ rm -rf build
 109+
 110+.PHONY: all clean $(NAME)
Property changes on: libs/ui/Makefile
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
 111+*
\ No newline at end of property
Index: libs/ui
Property changes on: libs/ui
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
 112+build