Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Image support in python (.obm) #289

Draft
wants to merge 15 commits into
base: upsilon-dev
Choose a base branch
from
Draft
2 changes: 2 additions & 0 deletions apps/code/catalog.de.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Zählt die Vorkommen von x"
PythonDegrees = "x von Bogenmaß in Grad umrechnen"
PythonDivMod = "Quotient und Rest"
PythonDrawCircle = "Zeichne einen Kreis"
PythonDrawImage = "Zeichne ein Bild"
PythonImageSize = "Bildgröße zurückgeben [b,h]"
PythonDrawLine = "Eine Linie zeichnen"
PythonDrawString = "Text bei Pixel (x,y) darstellen"
PythonErf = "Fehlerfunktion"
Expand Down
3 changes: 3 additions & 0 deletions apps/code/catalog.en.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ PythonCount = "Count the occurrences of x"
PythonDegrees = "Convert x from radians to degrees"
PythonDivMod = "Quotient and remainder"
PythonDrawCircle = "Draw a circle"
PythonDrawImage = "Draw an image"
PythonImageSize = "Return image size [w,h]"
PythonDrawImage = "Draw an image"
Oreig403 marked this conversation as resolved.
Show resolved Hide resolved
PythonDrawLine = "Draw a line"
PythonDrawString = "Display a text from pixel (x,y)"
PythonErf = "Error function"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.es.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Count the occurrences of x"
PythonDegrees = "Convert x from radians to degrees"
PythonDivMod = "Quotient and remainder"
PythonDrawCircle = "Draw a circle"
PythonDrawImage = "Draw an image"
PythonImageSize = "Return image size [w,h]"
PythonDrawLine = "Draw a line"
PythonDrawString = "Display a text from pixel (x,y)"
PythonErf = "Error function"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.fr.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Compte les occurrences de x"
PythonDegrees = "Conversion de radians en degrés"
PythonDivMod = "Quotient et reste"
PythonDrawCircle = "Trace un cercle"
PythonDrawImage = "Dessine une image"
PythonImageSize = "Retourne la taille de l'image [w,h]"
PythonDrawLine = "Trace une ligne"
PythonDrawString = "Affiche un texte au pixel (x,y)"
PythonErf = "Fonction d'erreur"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.hu.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Számolja az x elöfordulását"
PythonDegrees = "x konvertálása radiánokrol fokokra"
PythonDivMod = "Hányados és maradék"
PythonDrawCircle = "Rajzolj egy kört"
PythonDrawImage = "Rajzolj egy képet"
PythonImageSize = "Visszatérés képmérete [w,h]"
PythonDrawLine = "Húzzon egy vonalat "
PythonDrawString = "Szöveg megjelenítése (x, y)-en"
PythonErf = "Hiba funkció"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.it.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Conta le ricorrenze di x"
PythonDegrees = "Conversione di radianti in gradi"
PythonDivMod = "Quoziente e resto"
PythonDrawCircle = "Disegnare un cerchio"
PythonDrawImage = "Disegna un'immagine"
PythonImageSize = "Restituisci la dimensione dell'immagine [l,h]"
PythonDrawLine = "Disegna una linea"
PythonDrawString = "Visualizza il testo dal pixel x,y"
PythonErf = "Funzione d'errore"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.nl.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Tel voorkomen van x"
PythonDegrees = "Zet x om van radialen naar graden"
PythonDivMod = "Quotiënt en rest"
PythonDrawCircle = "Teken een cirkel"
PythonDrawImage = "Teken een afbeelding"
PythonImageSize = "Retour afbeeldingsgrootte [b,h]"
PythonDrawLine = "Teken een lijn"
PythonDrawString = "Geef een tekst weer van pixel (x,y)"
PythonErf = "Error functie"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.pt.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ PythonCount = "Contar as ocorrências de x"
PythonDegrees = "Converter x de radianos para graus"
PythonDivMod = "Quociente e resto"
PythonDrawCircle = "Desenha um círculo"
PythonDrawImage = "Desenhe uma imagem"
PythonImageSize = "Tamanho da imagem de retorno [w,h]"
PythonDrawLine = "Desenhe uma linha"
PythonDrawString = "Mostrar o texto do pixel (x,y)"
PythonErf = "Função erro"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/catalog.universal.i18n
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ PythonCommandFabs = "fabs(x)"
PythonCommandFillCircle = "fill_circle(x,y,r,color)"
PythonCommandFillPolygon = "fill_polygon([(x1,y1),...],color)"
PythonCommandFillRect = "fill_rect(x,y,width,height,color)"
PythonCommandDrawImage = "draw_image(image,x,y,width,height)"
PythonCommandImageSize = "image_size(image)"
PythonCommandFloat = "float(x)"
PythonCommandFloor = "floor(x)"
PythonCommandFmod = "fmod(a,b)"
Expand Down
2 changes: 2 additions & 0 deletions apps/code/python_toolbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ const ToolboxMessageTree KandinskyModuleChildren[] = {
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDrawString, I18n::Message::PythonDrawString),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDrawLine, I18n::Message::PythonDrawLine),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDrawCircle, I18n::Message::PythonDrawCircle),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandDrawImage, I18n::Message::PythonDrawImage),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandImageSize, I18n::Message::PythonImageSize),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandFillRect, I18n::Message::PythonFillRect),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandFillCircle, I18n::Message::PythonFillCircle),
ToolboxMessageTree::Leaf(I18n::Message::PythonCommandFillPolygon, I18n::Message::PythonFillPolygon),
Expand Down
2 changes: 2 additions & 0 deletions python/port/genhdr/qstrdefs.in.h
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,8 @@ Q(color)
Q(draw_line)
Q(draw_string)
Q(draw_circle)
Q(draw_image)
Q(image_size)
Q(fill_rect)
Q(fill_circle)
Q(fill_polygon)
Expand Down
58 changes: 56 additions & 2 deletions python/port/mod/kandinsky/modkandinsky.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
extern "C" {
#include "modkandinsky.h"
#include <py/runtime.h>
#include <assert.h>
}
#include <escher/palette.h>

#include <apps/external/archive.h>
#include <escher.h>
#include <kandinsky.h>
#include <ion.h>
#include "port.h"
#include <py/obj.h>


struct OBMHeader
{
uint32_t signature; //Normally it is 32145
int32_t width;
int32_t height;
const KDColor image_data;
};
static mp_obj_t TupleForKDColor(KDColor c) {
mp_obj_tuple_t * t = static_cast<mp_obj_tuple_t *>(MP_OBJ_TO_PTR(mp_obj_new_tuple(3, NULL)));
t->items[0] = MP_OBJ_NEW_SMALL_INT(c.red());
Expand Down Expand Up @@ -95,6 +104,51 @@ mp_obj_t modkandinsky_draw_circle(size_t n_args, const mp_obj_t * args) {
KDIonContext::sharedContext()->drawCircle(center, r, color);
return mp_const_none;
}
mp_obj_t modkandinsky_draw_image(size_t n_args, const mp_obj_t * args) {
const char * image = mp_obj_str_get_str(args[0]);
mp_int_t x = mp_obj_get_int(args[1]);
mp_int_t y = mp_obj_get_int(args[2]);
mp_int_t width = mp_obj_get_int(args[3]);
mp_int_t height = mp_obj_get_int(args[4]);


if (width < 0) {
width = -width;
x = x - width;
}
if (height < 0) {
height = -height;
y = y - height;
}
int index = External::Archive::indexFromName(image);
if (index > -1) {
External::Archive::File image;
External::Archive::fileAtIndex(index, image);
OBMHeader* h = (OBMHeader*)image.data;


MicroPython::ExecutionEnvironment::currentExecutionEnvironment()->displaySandbox();
KDIonContext::sharedContext()->fillRectWithPixels(KDRect(x, y, width, height), &(h->image_data), nullptr);
}
return mp_const_none;
}

mp_obj_t modkandinsky_image_size(size_t n_args, const mp_obj_t * args) {
const char * image = mp_obj_str_get_str(args[0]);
int index = External::Archive::indexFromName(image);
if (index > -1) {
External::Archive::File image;
External::Archive::fileAtIndex(index, image);
OBMHeader* h = (OBMHeader*)image.data;
mp_obj_t size[2];
size[0] = mp_obj_new_int((h->width));
size[1] = mp_obj_new_int((h->height));
return mp_obj_new_tuple(2, size);
}
return mp_const_none;

}


mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t * args) {
mp_int_t x = mp_obj_get_int(args[0]);
Expand Down
2 changes: 2 additions & 0 deletions python/port/mod/kandinsky/modkandinsky.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ mp_obj_t modkandinsky_set_pixel(mp_obj_t x, mp_obj_t y, mp_obj_t color);
mp_obj_t modkandinsky_draw_string(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_draw_line(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_draw_circle(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_draw_image(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_image_size(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_fill_rect(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_fill_circle(size_t n_args, const mp_obj_t *args);
mp_obj_t modkandinsky_fill_polygon(size_t n_args, const mp_obj_t *args);
Expand Down
6 changes: 5 additions & 1 deletion python/port/mod/kandinsky/modkandinsky_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_3(modkandinsky_set_pixel_obj, modkandinsky_set_pi
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_string_obj, 3, 6, modkandinsky_draw_string);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_line_obj, 5, 5, modkandinsky_draw_line);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_circle_obj, 4, 4, modkandinsky_draw_circle);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_rect_obj, 5, 5, modkandinsky_fill_rect);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_circle_obj, 4, 4, modkandinsky_fill_circle);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_rect_obj, 5, 5, modkandinsky_fill_rect);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_draw_image_obj, 5, 5, modkandinsky_draw_image);
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_image_size_obj, 1, 1, modkandinsky_image_size);
Oreig403 marked this conversation as resolved.
Show resolved Hide resolved
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(modkandinsky_fill_polygon_obj, 2, 2, modkandinsky_fill_polygon);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modkandinsky_wait_vblank_obj, modkandinsky_wait_vblank);
STATIC MP_DEFINE_CONST_FUN_OBJ_0(modkandinsky_get_palette_obj, modkandinsky_get_palette);
Expand All @@ -20,6 +22,8 @@ STATIC const mp_rom_map_elem_t modkandinsky_module_globals_table[] = {
{ MP_ROM_QSTR(MP_QSTR_draw_string), (mp_obj_t)&modkandinsky_draw_string_obj },
{ MP_ROM_QSTR(MP_QSTR_draw_line), (mp_obj_t)&modkandinsky_draw_line_obj },
{ MP_ROM_QSTR(MP_QSTR_draw_circle), (mp_obj_t)&modkandinsky_draw_circle_obj },
{ MP_ROM_QSTR(MP_QSTR_draw_image), (mp_obj_t)&modkandinsky_draw_image_obj },
{ MP_ROM_QSTR(MP_QSTR_image_size), (mp_obj_t)&modkandinsky_image_size_obj },
{ MP_ROM_QSTR(MP_QSTR_fill_rect), (mp_obj_t)&modkandinsky_fill_rect_obj },
{ MP_ROM_QSTR(MP_QSTR_fill_circle), (mp_obj_t)&modkandinsky_fill_circle_obj },
{ MP_ROM_QSTR(MP_QSTR_fill_polygon), (mp_obj_t)&modkandinsky_fill_polygon_obj },
Expand Down