Commit 16001977 authored by Bruno Coudoin's avatar Bruno Coudoin

this too is no more needed. replaced by the gimp script fu instead.

svn path=/branches/gcomprixogoo/; revision=3344
parent 36d74c51
#!/usr/bin/python
#
# Copyright (C) 2006 Miguel DE IZARRA
# Copyright (C) 2006, 2008 Miguel DE IZARRA
#
# 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
......@@ -19,9 +19,9 @@
#######################################################################
#
# a Python-Fu Gimp script for GCompris Shapegame activities
#
#
# Require: gimp-python
#
#
# To install, copy this script in ~/.gimp-2.2/plug-ins/ and set it executable
# cp gcompris_shapegame.py ~/.gimp-2.2/plug-ins/
# chmod +x ~/.gimp-2.2/plug-ins/gcompris_shapegame.py
......@@ -47,7 +47,7 @@
# a. Choice output directory and title (default based on image name)
# b. Set the border size between regions (depend on the original map)
# c. Choice if borders between region should be show
#
#
# We can change color of a region by opening region file in gimp,
# and fill it by a new color.
#
......@@ -78,24 +78,24 @@ def gcompris_puzzle(img, sdrawable, x, y, dossier, title, puzzle_type):
ptype = 0 -> normal, 1 -> rectanglular (grid)
"""
width, height = img.width, img.height
layer = gimp.Layer(img, "puzzle", width, height, RGB_IMAGE,
layer = gimp.Layer(img, "puzzle", width, height, RGB_IMAGE,
100, NORMAL_MODE)
layer.fill(WHITE_FILL)
img.add_layer(layer)
if ptype:
vspace = math.ceil(width / float(x))
hspace = math.ceil(height / float(y))
pdb.plug_in_grid(img, layer, 1, hspace, 0, (0, 0, 0), 255,
1, vspace, 0, (0, 0, 0), 255,
pdb.plug_in_grid(img, layer, 1, hspace, 0, (0, 0, 0), 255,
1, vspace, 0, (0, 0, 0), 255,
0, 0, 0, (0, 0, 0), 255)
else:
pdb.plug_in_jigsaw(img, layer, x, y, 1, 0, 0)
return layer
if not gcompris_init(img):
return
return
# Init
# Init
bg_layer = img.active_layer
pdb.gimp_selection_none(img)
width, height = img.width, img.height
......@@ -113,10 +113,10 @@ def gcompris_puzzle(img, sdrawable, x, y, dossier, title, puzzle_type):
pdb.gimp_floating_sel_to_layer(pdb.gimp_edit_paste(bg_layer, 1))
img.active_layer.name = chr(ord('A')+ idx) + str(idy+1)
layerlist.append(img.active_layer)
pdb.gimp_selection_none(img)
img.remove_layer(puzzle)
d_title = dict(x=405, y=495, justification ="GTK_JUSTIFY_CENTER",
color_skin="gcompris/text button")
d_title["<name>"] = title
......@@ -153,7 +153,7 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
save_bg = gimp.get_background()
save_fg = gimp.get_foreground()
layer_map = img.layers[0]
if not layer_map.has_alpha:
layer_map.add_alpha()
......@@ -179,7 +179,7 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
pdb.gimp_edit_fill(layer, BACKGROUND_FILL)
layer.name = chan.name
layerlist.append(layer)
pdb.gimp_context_set_default_colors()
# Fill country white
pdb.gimp_selection_load(all_channel)
......@@ -188,12 +188,12 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
pdb.gimp_selection_grow(img, bordersize)
earth = pdb.gimp_selection_save(img)
earth.name = "earth"
# Fill limit black
pdb.gimp_selection_combine(all_channel, CHANNEL_OP_SUBTRACT)
pdb.gimp_edit_fill(layer_map, FOREGROUND_FILL) # Fill Black
# Clear other
# Clear other
pdb.gimp_selection_load(earth)
pdb.gimp_selection_invert(img)
pdb.gimp_edit_clear(layer_map)
......@@ -206,7 +206,7 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
gimp.set_background(94, 146, 229)
pdb.gimp_edit_blend(layer_map, 0, 0, 0, 50, 0, 0, False, False, 0, 0,
True, 0, 0, img.width, img.height)
# Water 2
pdb.gimp_selection_load(earth)
pdb.gimp_selection_grow(img, 20)
......@@ -215,7 +215,7 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
gimp.set_background(137, 173, 225)
pdb.gimp_edit_blend(layer_map, 0, 0, 0, 25, 0, 0, False, False, 0, 0,
True, 0, 0, img.width, img.height)
# Remove tmp channel
img.remove_channel(earth)
img.remove_channel(all_channel)
......@@ -223,7 +223,7 @@ def gcompris_geography(img, sdrawable, dossier, title, bordersize, keepLimit):
gimp.set_background(save_bg)
gimp.set_foreground(save_fg)
pdb.gimp_selection_none(img)
shape = dict()
shape["sound"] = os.path.join("sounds", "$LOCALE", "geography", dossier, "%n.ogg")
shape["<tooltip>"] = "%n"
......@@ -264,7 +264,7 @@ def gcompris_layer_to_board(layerlist, activity, subdir, title, background, shap
def realbasename(f):
"""Return the basename without extension"""
return os.path.splitext(os.path.basename(f))[0]
def dict_to_str(name, dico):
""" Convert a dictionnary to string of a xml element
dictionnary'keys are convert to a property of the xml element with associate value
......@@ -283,7 +283,7 @@ def gcompris_layer_to_board(layerlist, activity, subdir, title, background, shap
else:
tmp += "/>\n"
return tmp
# Init
img = layerlist[0].image
width, height = img.width, img.height
......@@ -296,7 +296,7 @@ def gcompris_layer_to_board(layerlist, activity, subdir, title, background, shap
if os.path.exists(out_dir):
os.remove(out_dir)
os.mkdir(out_dir)
out_xml = os.path.join(out_dir, "board.xml")
xml = file(out_xml, "w")
xml.write("""<?xml version="1.0" encoding="UTF-8"?>
......@@ -304,14 +304,14 @@ def gcompris_layer_to_board(layerlist, activity, subdir, title, background, shap
if title:
xml.write(dict_to_str("title", title))
if isinstance(background, dict):
xml.write(dict_to_str("Shape", background))
elif isinstance(background, gimp.Layer):
layerlist.append(background)
deltax, deltay = (800-width)/2, (520-height)/2
for tile_layer in layerlist:
tile_filename = tile_layer.name+".png"
tile_filename_long = os.path.join(out_dir, tile_filename)
......@@ -334,6 +334,6 @@ def gcompris_layer_to_board(layerlist, activity, subdir, title, background, shap
xml.write(dict_to_str("shape", shape_dict))
xml.write("</ShapeGame>\n")
xml.close()
main()
#
# creating region puzzle for germany
#
#
# 1. got map from wikipedia ->
# source/Deutschland_Bundeslaender_1990_thresshold.tif
#
# 2. used gimp to extract individual regions by filling all others with
# white and save them as "bayern.png" etc. The size of each
# indivudual image must be the same as the full image
#
# 3. create the background image by filling the separation lines and
# store it as Deutschland_komplett.png
#
# 4. Now crop away borders and compute the coordiantes for the puzzle
# pieces, using pnmcrop from "netpbm" package, and the compute the
# target spots with the distance transform, based on the great
# algorithm and source code from Pedro F. Felzenszwalb and Daniel
# P. Huttenlocher, see http://people.cs.uchicago.edu/~pff/dt/
#
cd ~/gcompris_dev/gcompris/boards/geography/germany_region/source2
rm -f coordinates.txt
for i in baden-wuerttemberg.png bayern.png berlin.png brandenburg.png bremen.png hamburg.png hessen.png mecklenburg-vorpommern.png niedersachsen.png nordrhein-westfalen.png rheinland-pfalz.png saarland.png sachsen-anhalt.png sachsen.png schleswig_holstein.png thueringen.png; do
echo filename: $i >> coordinates.txt
pngtopnm $i |pnmscale 0.8| pnmcrop --white --verbose 2>> coordinates.txt | pnmtopng -transparent white > ../$i
echo shape: `pngtopnm ../$i | head -2 | tail -1` >> coordinates.txt
pngtopnm ../$i | ppmtopgm | pgmtopbm -threshold -value 0.9 | pnminvert > binmask.pbm
~/gcompris_dev/gcompris/tool/shapegame/nice_target_spot binmask.pbm target.ppm
pnmtopng -transparent white target.ppm > ../target_$i
done
#
# simulate output from pnmcrop for background-image
#
pngtopnm Deutschland_komplett.png |pnmscale 0.8| pnmtopng -transparent white > ../back.png
echo filename: back.png >> coordinates.txt
echo pnmcrop: cropping 0 rows off the top >> coordinates.txt
echo pnmcrop: cropping 0 cols off the left >> coordinates.txt
echo shape: `pngtopnm ../back.png | head -2 | tail -1` >> coordinates.txt
#
# 5. Now create the xml-file board6_0.xml.in from created
# source2/coordinates.txt
#
cd ~/gcompris_dev/gcompris/boards/geography/germany_region/source2
cat > realnames.txt <<EOF
Baden-Württemberg
Bayern
Berlin
Brandenburg
Bremen
Hamburg
Hessen
Mecklenburg-Vorpommern
Niedersachsen
Nordrhein-Westfalen
Rheinland-Pfalz
Saarland
Sachsen-Anhalt
Sachsen
Schleswig-Holstein
Thüringen
EOF
grep "filename:" coordinates.txt | awk '{print $2}' > filenames.txt
grep "off the left" coordinates.txt | awk '{print $3}' > coords_x.txt
grep "off the top" coordinates.txt | awk '{print $3}' > coords_y.txt
grep "shape:" coordinates.txt | awk '{print $2, $3}' > shape.txt
paste filenames.txt coords_x.txt coords_y.txt shape.txt realnames.txt> hurz.txt
echo '<?xml version="1.0" encoding="UTF-8"?>
<ShapeGame>
<Title x="600" y="495" justification="GTK_JUSTIFY_CENTER">
<_name>Deutschland Bundesländer</_name>
</Title>' > board6_0.xml.in
awk '{
if($1 != "back.png")
{
print " <Shape name=\"A"NR"\" pixmapfile=\"geography/germany_region/"$1"\" targetfile=\"geography/germany_region/target_"$1"\" x=\""$2+100+$4/2"\" y=\""$3+$5/2"\" zoomx=\"1\" zoomy=\"1\" position=\"0\"><_tooltip>"$6"</_tooltip></Shape>";
}
else
{
print " <Shape name=\"1\" pixmapfile=\"geography/germany_region/"$1"\" type=\"SHAPE_BACKGROUND\" x=\""$2+100+$4/2"\" y=\""$3+$5/2"\" zoomx=\"1\" zoomy=\"1\" position=\"0\"/>";
}
}' < hurz.txt >> board6_0.xml.in
echo '</ShapeGame>' >> board6_0.xml.in
cp -f board6_0.xml.in ../../board6_0.xml.in
#
# edit gcompris/boards/geography/Makefile.am to include board6_0.xml.in
#
cd ~/gcompris_dev/gcompris
intltoolize && automake && autoconf && ./configure --prefix $PWD
cd ~/gcompris_dev/gcompris
make install
~/gcompris_dev/gcompris/bin/gcompris
#
# Create the distribution for Bruno
#
cd ~/gcompris_dev/
tar -zcvf germany_regions_2006_01_29.tar.gz gcompris/tool/ gcompris/boards/geography/germany_region/ gcompris/boards/geography/Makefile.am
INCDIR = -I.
DBG = -g
OPT = -O3
CPP = g++
CFLAGS = $(DBG) $(OPT) $(INCDIR)
LINK = -lm
.cpp.o:
$(CPP) $(CFLAGS) -c $< -o $@
all: dt
dt: dt.cpp
$(CPP) $(CFLAGS) -o dt dt.cpp $(LINK)
clean:
/bin/rm -f dt *.o
clean-all: clean
/bin/rm -f *~
Implementation of the distance transform algorithm described in:
Distance Transforms of Sampled Functions
Pedro F. Felzenszwalb and Daniel P. Huttenlocher
Cornell Computing and Information Science TR2004-1963
http://people.cs.uchicago.edu/~pff/dt/
The main program "dt.cpp" computes the standard distance transform of
a binary image. The algorithm for computing the transform of a
sampled function is in "dt.h".
1) Type make to compile "dt".
2) Run "./dt input.pbm output.pgm" to compute the distance transform
of a binary image. The input is a binary PBM image and the output is
a grayscale PGM image. Note that the actual distance values are
scaled to fit in the [0-255] grayscale range.
The code in this directory is Copyrighted by Pedro Felzenszwalb and is
released under GPL:
Betreff: Re: Gnu public license for your distance transform code?
Datum: Sun, 29 Jan 2006 19:20:05 -0600
Von: Pedro Felzenszwalb <pff@cs.uchicago.edu>
An: Olaf Ronneberger <Olaf@Ronneberger.net>
Referenzen: <43DD45AB.1060706@Ronneberger.net>
Dear Olaf,
I am glad to hear the code is being used in your software and I am
happy to release it under the GPL.
Do you need me to send you anything in particular?
Pedro
/* dt - dt.cpp
*
* Copyright (C) 2006 Pedro Felzenszwalb
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include "pnmfile.h"
#include "imconv.h"
#include "dt.h"
int main(int argc, char **argv) {
if (argc != 3) {
fprintf(stderr, "usage: %s input(pbm) output(pgm)\n", argv[0]);
return 1;
}
char *input_name = argv[1];
char *output_name = argv[2];
// load input
image<uchar> *input = loadPBM(input_name);
// compute dt
image<float> *out = dt(input);
// take square roots
for (int y = 0; y < out->height(); y++) {
for (int x = 0; x < out->width(); x++) {
imRef(out, x, y) = sqrt(imRef(out, x, y));
}
}
// convert to grayscale
image<uchar> *gray = imageFLOATtoUCHAR(out);
// save output
savePGM(gray, output_name);
delete input;
delete out;
delete gray;
}
/* dt - dt.h
*
* Copyright (C) 2006 Pedro Felzenszwalb
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* distance transform */
#ifndef DT_H
#define DT_H
#include <algorithm>
#include "image.h"
#define INF 1E20
/* dt of 1d function using squared distance */
static float *dt(float *f, int n) {
float *d = new float[n];
int *v = new int[n];
float *z = new float[n+1];
int k = 0;
v[0] = 0;
z[0] = -INF;
z[1] = +INF;
for (int q = 1; q <= n-1; q++) {
float s = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
while (s <= z[k]) {
k--;
s = ((f[q]+square(q))-(f[v[k]]+square(v[k])))/(2*q-2*v[k]);
}
k++;
v[k] = q;
z[k] = s;
z[k+1] = +INF;
}
k = 0;
for (int q = 0; q <= n-1; q++) {
while (z[k+1] < q)
k++;
d[q] = square(q-v[k]) + f[v[k]];
}
delete [] v;
delete [] z;
return d;
}
/* dt of 2d function using squared distance */
static void dt(image<float> *im) {
int width = im->width();
int height = im->height();
float *f = new float[std::max(width,height)];
// transform along columns
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
f[y] = imRef(im, x, y);
}
float *d = dt(f, height);
for (int y = 0; y < height; y++) {
imRef(im, x, y) = d[y];
}
delete [] d;
}
// transform along rows
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
f[x] = imRef(im, x, y);
}
float *d = dt(f, width);
for (int x = 0; x < width; x++) {
imRef(im, x, y) = d[x];
}
delete [] d;
}
delete f;
}
/* dt of binary image using squared distance */
static image<float> *dt(image<uchar> *im, uchar on = 1) {
int width = im->width();
int height = im->height();
image<float> *out = new image<float>(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (imRef(im, x, y) == on)
imRef(out, x, y) = 0;
else
imRef(out, x, y) = INF;
}
}
dt(out);
return out;
}
#endif
/* dt - image.h
*
* Copyright (C) 2006 Pedro Felzenszwalb
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* a simple image class */
#ifndef IMAGE_H
#define IMAGE_H
#include <cstring>
template <class T>
class image {
public:
/* create an image */
image(const int width, const int height, const bool init = true);
/* delete an image */
~image();
/* init an image */
void init(const T &val);
/* copy an image */
image<T> *copy() const;
/* get the width of an image. */
int width() const { return w; }
/* get the height of an image. */
int height() const { return h; }
/* image data. */
T *data;
/* row pointers. */
T **access;
private:
int w, h;
};
/* use imRef to access image data. */
#define imRef(im, x, y) (im->access[y][x])
/* use imPtr to get pointer to image data. */
#define imPtr(im, x, y) &(im->access[y][x])
template <class T>
image<T>::image(const int width, const int height, const bool init) {
w = width;
h = height;
data = new T[w * h]; // allocate space for image data
access = new T*[h]; // allocate space for row pointers
// initialize row pointers
for (int i = 0; i < h; i++)
access[i] = data + (i * w);
if (init)
memset(data, 0, w * h * sizeof(T));
}
template <class T>
image<T>::~image() {
delete [] data;
delete [] access;
}
template <class T>
void image<T>::init(const T &val) {
T *ptr = imPtr(this, 0, 0);
T *end = imPtr(this, w-1, h-1);
while (ptr <= end)
*ptr++ = val;
}
template <class T>
image<T> *image<T>::copy() const {
image<T> *im = new image<T>(w, h, false);
memcpy(im->data, data, w * h * sizeof(T));
return im;
}
#endif
/* dt - imconv.h
*
* Copyright (C) 2006 Pedro Felzenszwalb
*
* 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 program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* image conversion */
#ifndef CONV_H
#define CONV_H
#include <climits>
#include "image.h"
#include "imutil.h"
#include "misc.h"
#define RED_WEIGHT 0.299
#define GREEN_WEIGHT 0.587
#define BLUE_WEIGHT 0.114
static image<uchar> *imageRGBtoGRAY(image<rgb> *input) {
int width = input->width();
int height = input->height();
image<uchar> *output = new image<uchar>(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imRef(output, x, y) = (uchar)
(imRef(input, x, y).r * RED_WEIGHT +
imRef(input, x, y).g * GREEN_WEIGHT +
imRef(input, x, y).b * BLUE_WEIGHT);
}
}
return output;
}
static image<rgb> *imageGRAYtoRGB(image<uchar> *input) {
int width = input->width();
int height = input->height();
image<rgb> *output = new image<rgb>(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imRef(output, x, y).r = imRef(input, x, y);
imRef(output, x, y).g = imRef(input, x, y);
imRef(output, x, y).b = imRef(input, x, y);
}
}
return output;
}
static image<float> *imageUCHARtoFLOAT(image<uchar> *input) {
int width = input->width();
int height = input->height();
image<float> *output = new image<float>(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imRef(output, x, y) = imRef(input, x, y);
}
}
return output;
}
static image<float> *imageINTtoFLOAT(image<int> *input) {
int width = input->width();
int height = input->height();
image<float> *output = new image<float>(width, height, false);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
imRef(output, x, y) = imRef(input, x, y);
}
}
return output;
}
static image<uchar> *imageFLOATtoUCHAR(image<float> *input,
float min, float max) {
int width = input->width();
int height = input->height();
image<uchar> *output = new image<uchar>(width, height, false);
if (max == min)
return output;
float scale = UCHAR_MAX / (max - min);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
uchar val = (uchar)((imRef(input, x, y) - min) * scale);
imRef(output, x, y) = bound(val, (uchar)0, (uchar)UCHAR_MAX);
}
}
return output;
}
static image<uchar> *imageFLOATtoUCHAR(image<float> *input) {