Gfxprim
Threads by month
- ----- 2026 -----
- March
- February
- January
- ----- 2025 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2024 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2023 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2022 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2021 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2020 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2019 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2018 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2017 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2016 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2015 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2014 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2013 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2012 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
- March
- February
- January
- ----- 2011 -----
- December
- November
- October
- September
- August
- July
- June
- May
- April
September 2011
- 1 participants
- 19 discussions
[repo.or.cz] gfxprim.git branch generate updated: 78f907f5c2ec5e07373ddfc04774c9a78639a44d
by metan 28 Sep '11
by metan 28 Sep '11
28 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 78f907f5c2ec5e07373ddfc04774c9a78639a44d (commit)
from 2fae7f19796fbac3a1e8a05891838595669c9407 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/78f907f5c2ec5e07373ddfc04774c9a78639…
commit 78f907f5c2ec5e07373ddfc04774c9a78639a44d
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 28 12:52:27 2011 +0200
Unrolled inner loops and used gcc vector aritmetics.
diff --git a/libs/filters/GP_ScaleDown.c b/libs/filters/GP_ScaleDown.c
index da5d9d3..90ba812 100644
--- a/libs/filters/GP_ScaleDown.c
+++ b/libs/filters/GP_ScaleDown.c
@@ -69,11 +69,21 @@ float cubic(float x)
return 0;
}
+typedef float v4sf __attribute__ ((vector_size (sizeof(float) * 4)));
+
+typedef union v4f {
+ v4sf v;
+ float f[4];
+} v4f;
+
+#define MUL_V4SF(a, b) ((union v4f)((a).v * (b).v))
+#define SUM_V4SF(a) ((a).f[0] + (a).f[1] + (a).f[2] + (a).f[3])
+
GP_Context *GP_Scale(GP_Context *src, GP_Size w, GP_Size h)
{
GP_Context *dst;
float col_r[src->h], col_g[src->h], col_b[src->h];
- uint32_t i, j, k;
+ uint32_t i, j;
int idx;
if (src->pixel_type != GP_PIXEL_RGB888)
@@ -89,55 +99,93 @@ GP_Context *GP_Scale(GP_Context *src, GP_Size w, GP_Size h)
/* Generate interpolated column */
for (j = 0; j < src->h; j++) {
- uint8_t r[4], g[4], b[4];
- float c[4];
-
- for (k = 0; k < 4; k++) {
- GP_Pixel pix;
- idx = x + k - 1;
+ v4f cv, rv, gv, bv;
+ GP_Pixel pix[4];
+
+ idx = x - 1;
- if (idx < 0)
- idx = 0;
+ if (idx < 0)
+ idx = 0;
- if (idx > (int)src->w - 1)
- idx = src->w - 1;
+ if (idx > (int)src->w - 4)
+ idx = src->w - 4;
- pix = GP_GetPixel_Raw_24BPP(src, idx, j);
+ pix[0] = GP_GetPixel_Raw_24BPP(src, idx, j);
+ pix[1] = GP_GetPixel_Raw_24BPP(src, idx + 1, j);
+ pix[2] = GP_GetPixel_Raw_24BPP(src, idx + 2, j);
+ pix[3] = GP_GetPixel_Raw_24BPP(src, idx + 3, j);
- r[k] = GP_Pixel_GET_R_RGB888(pix);
- g[k] = GP_Pixel_GET_G_RGB888(pix);
- b[k] = GP_Pixel_GET_B_RGB888(pix);
+ rv.f[0] = GP_Pixel_GET_R_RGB888(pix[0]);
+ rv.f[1] = GP_Pixel_GET_R_RGB888(pix[1]);
+ rv.f[2] = GP_Pixel_GET_R_RGB888(pix[2]);
+ rv.f[3] = GP_Pixel_GET_R_RGB888(pix[3]);
- c[k] = cubic(x - idx);
- }
-
- col_r[j] = r[0] * c[0] + r[1] * c[1] + r[2] * c[2] + r[3] * c[3];
- col_g[j] = g[0] * c[0] + g[1] * c[1] + g[2] * c[2] + g[3] * c[3];
- col_b[j] = b[0] * c[0] + b[1] * c[1] + b[2] * c[2] + b[3] * c[3];
-
+ gv.f[0] = GP_Pixel_GET_G_RGB888(pix[0]);
+ gv.f[1] = GP_Pixel_GET_G_RGB888(pix[1]);
+ gv.f[2] = GP_Pixel_GET_G_RGB888(pix[2]);
+ gv.f[3] = GP_Pixel_GET_G_RGB888(pix[3]);
+
+ bv.f[0] = GP_Pixel_GET_B_RGB888(pix[0]);
+ bv.f[1] = GP_Pixel_GET_B_RGB888(pix[1]);
+ bv.f[2] = GP_Pixel_GET_B_RGB888(pix[2]);
+ bv.f[3] = GP_Pixel_GET_B_RGB888(pix[3]);
+
+ cv.f[0] = cubic(x - idx);
+ cv.f[1] = cubic(x - idx - 1);
+ cv.f[2] = cubic(x - idx - 2);
+ cv.f[3] = cubic(x - idx - 3);
+
+ rv = MUL_V4SF(rv, cv);
+ gv = MUL_V4SF(gv, cv);
+ bv = MUL_V4SF(bv, cv);
+
+ col_r[j] = SUM_V4SF(rv);
+ col_g[j] = SUM_V4SF(gv);
+ col_b[j] = SUM_V4SF(bv);
}
/* now interpolate column for new image */
for (j = 0; j < h; j++) {
float y = (1.00 * j / h) * src->h + 0.5;
- float r = 0, g = 0, b = 0, c;
-
- for (k = 0; k < 4; k++) {
- idx = y + k - 1;
+ v4f cv, rv, gv, bv;
+ float r, g, b;
+
+ idx = y - 1;
- if (idx < 0)
- idx = 0;
-
- if (idx > (int)src->h - 1)
- idx = src->h - 1;
-
- c = cubic(y - idx);
-
- r += col_r[idx] * c;
- g += col_g[idx] * c;
- b += col_b[idx] * c;
- }
+ if (idx < 0)
+ idx = 0;
+
+ if (idx > (int)src->h - 4)
+ idx = src->h - 4;
+
+ rv.f[0] = col_r[idx];
+ rv.f[1] = col_r[idx + 1];
+ rv.f[2] = col_r[idx + 2];
+ rv.f[3] = col_r[idx + 3];
+
+ gv.f[0] = col_g[idx];
+ gv.f[1] = col_g[idx + 1];
+ gv.f[2] = col_g[idx + 2];
+ gv.f[3] = col_g[idx + 3];
+ bv.f[0] = col_b[idx];
+ bv.f[1] = col_b[idx + 1];
+ bv.f[2] = col_b[idx + 2];
+ bv.f[3] = col_b[idx + 3];
+
+ cv.f[0] = cubic(y - idx);
+ cv.f[1] = cubic(y - idx - 1);
+ cv.f[2] = cubic(y - idx - 2);
+ cv.f[3] = cubic(y - idx - 3);
+
+ rv = MUL_V4SF(rv, cv);
+ gv = MUL_V4SF(gv, cv);
+ bv = MUL_V4SF(bv, cv);
+
+ r = SUM_V4SF(rv);
+ g = SUM_V4SF(gv);
+ b = SUM_V4SF(bv);
+
if (r > 255)
r = 255;
-----------------------------------------------------------------------
Summary of changes:
libs/filters/GP_ScaleDown.c | 124 ++++++++++++++++++++++++++++++-------------
1 files changed, 86 insertions(+), 38 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 2fae7f19796fbac3a1e8a05891838595669c9407
by metan 27 Sep '11
by metan 27 Sep '11
27 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 2fae7f19796fbac3a1e8a05891838595669c9407 (commit)
from 6e824825b32a4d3ed3fc9b7e41dee600d010b093 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/2fae7f19796fbac3a1e8a05891838595669c…
commit 2fae7f19796fbac3a1e8a05891838595669c9407
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Tue Sep 27 20:23:39 2011 +0200
First version of bicubic interpolation, only for rgb888 now.
diff --git a/libs/filters/GP_ScaleDown.c b/libs/filters/GP_ScaleDown.c
index 2cc379b..da5d9d3 100644
--- a/libs/filters/GP_ScaleDown.c
+++ b/libs/filters/GP_ScaleDown.c
@@ -52,3 +52,114 @@ GP_Context *GP_ScaleDown(GP_Context *src)
return dst;
}
+
+#define A 0.5
+
+float cubic(float x)
+{
+ if (x < 0)
+ x = -x;
+
+ if (x < 1)
+ return (2 - A)*x*x*x + (A - 3)*x*x + 1;
+
+ if (x < 2)
+ return -A*x*x*x + 5*A*x*x - 8*A*x + 4*A;
+
+ return 0;
+}
+
+GP_Context *GP_Scale(GP_Context *src, GP_Size w, GP_Size h)
+{
+ GP_Context *dst;
+ float col_r[src->h], col_g[src->h], col_b[src->h];
+ uint32_t i, j, k;
+ int idx;
+
+ if (src->pixel_type != GP_PIXEL_RGB888)
+ return NULL;
+
+ dst = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
+
+ if (dst == NULL)
+ return NULL;
+
+ for (i = 0; i < w; i++) {
+ float x = (1.00 * i / w) * src->w + 0.5;
+
+ /* Generate interpolated column */
+ for (j = 0; j < src->h; j++) {
+ uint8_t r[4], g[4], b[4];
+ float c[4];
+
+ for (k = 0; k < 4; k++) {
+ GP_Pixel pix;
+ idx = x + k - 1;
+
+ if (idx < 0)
+ idx = 0;
+
+ if (idx > (int)src->w - 1)
+ idx = src->w - 1;
+
+ pix = GP_GetPixel_Raw_24BPP(src, idx, j);
+
+ r[k] = GP_Pixel_GET_R_RGB888(pix);
+ g[k] = GP_Pixel_GET_G_RGB888(pix);
+ b[k] = GP_Pixel_GET_B_RGB888(pix);
+
+ c[k] = cubic(x - idx);
+ }
+
+ col_r[j] = r[0] * c[0] + r[1] * c[1] + r[2] * c[2] + r[3] * c[3];
+ col_g[j] = g[0] * c[0] + g[1] * c[1] + g[2] * c[2] + g[3] * c[3];
+ col_b[j] = b[0] * c[0] + b[1] * c[1] + b[2] * c[2] + b[3] * c[3];
+
+ }
+
+ /* now interpolate column for new image */
+ for (j = 0; j < h; j++) {
+ float y = (1.00 * j / h) * src->h + 0.5;
+ float r = 0, g = 0, b = 0, c;
+
+ for (k = 0; k < 4; k++) {
+ idx = y + k - 1;
+
+ if (idx < 0)
+ idx = 0;
+
+ if (idx > (int)src->h - 1)
+ idx = src->h - 1;
+
+ c = cubic(y - idx);
+
+ r += col_r[idx] * c;
+ g += col_g[idx] * c;
+ b += col_b[idx] * c;
+ }
+
+ if (r > 255)
+ r = 255;
+
+ if (r < 0)
+ r = 0;
+
+ if (g > 255)
+ g = 255;
+
+ if (g < 0)
+ g = 0;
+
+ if (b > 255)
+ b = 255;
+
+ if (b < 0)
+ b = 0;
+
+ GP_Pixel pix = GP_Pixel_CREATE_RGB888((uint8_t)r, (uint8_t)g, (uint8_t)b);
+ GP_PutPixel_Raw_24BPP(dst, i, j, pix);
+ }
+ }
+
+ return dst;
+}
diff --git a/tests/SDL/Makefile b/tests/SDL/Makefile
index e5d9500..6d93313 100644
--- a/tests/SDL/Makefile
+++ b/tests/SDL/Makefile
@@ -6,7 +6,7 @@ INCLUDE=core gfx SDL backends
LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL -lpng
APPS=pixeltest fileview fonttest linetest randomshapetest shapetest sierpinsky- symbolstest textaligntest trianglefps input blittest subcontext ppm_test+ symbolstest textaligntest trianglefps input blittest subcontext scale_test showimage
include $(TOPDIR)/include.mk
diff --git a/tests/SDL/ppm_test.c b/tests/SDL/scale_test.c
similarity index 90%
rename from tests/SDL/ppm_test.c
rename to tests/SDL/scale_test.c
index a0653b2..4bfc0e8 100644
--- a/tests/SDL/ppm_test.c
+++ b/tests/SDL/scale_test.c
@@ -30,13 +30,22 @@ int main(int argc, char *argv[])
GP_Context *bitmap;
GP_SetDebugLevel(10);
GP_RetCode ret;
+ GP_Size w, h;
- if ((ret = GP_LoadPPM(argv[1], &bitmap))) {
+ if (argc < 4) {
+ fprintf(stderr, "Usage: image w hn");
+ return 1;
+ }
+
+ if ((ret = GP_LoadImage(argv[1], &bitmap))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
return 1;
}
- bitmap = GP_ScaleDown(bitmap);
+ w = atoi(argv[2]);
+ h = atoi(argv[3]);
+
+ bitmap = GP_Scale(bitmap, w, h);
if ((ret = GP_SavePPM("out.ppm", bitmap, "b"))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
-----------------------------------------------------------------------
Summary of changes:
libs/filters/GP_ScaleDown.c | 111 ++++++++++++++++++++++++++++++++
tests/SDL/Makefile | 2 +-
tests/SDL/{ppm_test.c => scale_test.c} | 13 +++-
3 files changed, 123 insertions(+), 3 deletions(-)
rename tests/SDL/{ppm_test.c => scale_test.c} (90%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 6e824825b32a4d3ed3fc9b7e41dee600d010b093
by metan 23 Sep '11
by metan 23 Sep '11
23 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 6e824825b32a4d3ed3fc9b7e41dee600d010b093 (commit)
from efe1e66de81808a60c5c3d3e91ae50eb973cf733 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/6e824825b32a4d3ed3fc9b7e41dee600d010…
commit 6e824825b32a4d3ed3fc9b7e41dee600d010b093
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Thu Sep 22 21:31:53 2011 +0200
Add support for png 1BPP palette (eg. black&white).
diff --git a/libs/loaders/GP_PNG.c b/libs/loaders/GP_PNG.c
index a533287..50395b4 100644
--- a/libs/loaders/GP_PNG.c
+++ b/libs/loaders/GP_PNG.c
@@ -108,7 +108,8 @@ GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res)
png_get_IHDR(png, png_info, &w, &h, &depth,
&color_type, NULL, NULL, NULL);
- GP_DEBUG(2, "Have %s PNG%s size %ux%u depth %i",
+ GP_DEBUG(2, "Have %s%s PNG%s size %ux%u depth %i",
+ color_type & PNG_COLOR_MASK_PALETTE ? "pallete " : "",
color_type & PNG_COLOR_MASK_COLOR ? "color" : "gray",
color_type & PNG_COLOR_MASK_ALPHA ? " with alpha channel" : "",
w, h, depth);
@@ -140,6 +141,17 @@ GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res)
break;
}
break;
+ case PNG_COLOR_TYPE_PALETTE:
+ /* Grayscale with BPP < 8 is usually saved as palette */
+ if (png_get_channels(png, png_info) == 1) {
+ switch (depth) {
+ case 1:
+ png_set_packswap(png);
+ pixel_type = GP_PIXEL_G1;
+ break;
+ }
+ }
+ break;
}
if (pixel_type == GP_PIXEL_UNKNOWN) {
-----------------------------------------------------------------------
Summary of changes:
libs/loaders/GP_PNG.c | 14 +++++++++++++-
1 files changed, 13 insertions(+), 1 deletions(-)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: efe1e66de81808a60c5c3d3e91ae50eb973cf733
by metan 22 Sep '11
by metan 22 Sep '11
22 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via efe1e66de81808a60c5c3d3e91ae50eb973cf733 (commit)
from 570b772500147f49fedbc21b6d2aedc7be13a16b (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/efe1e66de81808a60c5c3d3e91ae50eb973c…
commit efe1e66de81808a60c5c3d3e91ae50eb973cf733
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Thu Sep 22 20:07:31 2011 +0200
Added function to load image of any supported format.
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h
index 99253bf..a245e9a 100644
--- a/include/loaders/GP_Loaders.h
+++ b/include/loaders/GP_Loaders.h
@@ -32,10 +32,17 @@
#ifndef GP_LOADERS_H
#define GP_LOADERS_H
+#include "core/GP_Context.h"
+
#include "GP_PBM.h"
#include "GP_PGM.h"
#include "GP_PPM.h"
#include "GP_PNG.h"
+/*
+ * Tries to load image accordingly to extension.
+ */
+GP_RetCode GP_LoadImage(const char *src_path, GP_Context **res);
+
#endif /* GP_LOADERS_H */
diff --git a/include/loaders/GP_Loaders.h b/libs/loaders/GP_Loaders.c
similarity index 61%
copy from include/loaders/GP_Loaders.h
copy to libs/loaders/GP_Loaders.c
index 99253bf..72e2a31 100644
--- a/include/loaders/GP_Loaders.h
+++ b/libs/loaders/GP_Loaders.c
@@ -16,26 +16,65 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
- * <jiri.bluebear.dluhos(a)gmail.com> *
- * *
* Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
- /*
+/*
+
+ General functions for loading bitmaps.
+
+ */
- Core include file for loaders API.
+#include <string.h>
- */
+#include "GP_Loaders.h"
-#ifndef GP_LOADERS_H
-#define GP_LOADERS_H
+GP_RetCode GP_LoadImage(const char *src_path, GP_Context **res)
+{
+ int len = strlen(src_path);
+ GP_RetCode ret = GP_ENOIMPL;
-#include "GP_PBM.h"
-#include "GP_PGM.h"
-#include "GP_PPM.h"
+ switch (src_path[len - 1]) {
+ /* PNG, JPG, JPEG */
+ case 'g':
+ case 'G':
+ switch (src_path[len - 2]) {
+ case 'n':
+ case 'N':
+ if (src_path[len - 3] == 'p' ||
+ src_path[len - 3] == 'P')
+ ret = GP_LoadPNG(src_path, res);
+ break;
+ }
+ break;
+ /* PPM, PGM, PBM, PNM */
+ case 'm':
+ case 'M':
+ switch (src_path[len - 2]) {
+ case 'b':
+ case 'B':
+ if (src_path[len - 3] == 'p' ||
+ src_path[len - 3] == 'P')
+ ret = GP_LoadPBM(src_path, res);
+ break;
+ case 'g':
+ case 'G':
+ if (src_path[len - 3] == 'p' ||
+ src_path[len - 3] == 'P')
+ ret = GP_LoadPGM(src_path, res);
+ break;
+ case 'p':
+ case 'P':
+ if (src_path[len - 3] == 'p' ||
+ src_path[len - 3] == 'P')
+ ret = GP_LoadPPM(src_path, res);
+ break;
+ }
+ break;
+ }
-#include "GP_PNG.h"
+ //TODO file signature based check
-#endif /* GP_LOADERS_H */
+ return ret;
+}
diff --git a/tests/SDL/showimage.c b/tests/SDL/showimage.c
index 9ef23f1..937296d 100644
--- a/tests/SDL/showimage.c
+++ b/tests/SDL/showimage.c
@@ -92,7 +92,7 @@ int main(int argc, char *argv[])
GP_RetCode ret;
- if ((ret = GP_LoadPPM(argv[1], &bitmap))) {
+ if ((ret = GP_LoadImage(argv[1], &bitmap))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
return 1;
}
-----------------------------------------------------------------------
Summary of changes:
include/loaders/GP_Loaders.h | 7 ++
.../GP_ScaleDown.c => loaders/GP_Loaders.c} | 74 +++++++++++++------
tests/SDL/showimage.c | 2 +-
3 files changed, 58 insertions(+), 25 deletions(-)
copy libs/{filters/GP_ScaleDown.c => loaders/GP_Loaders.c} (62%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 570b772500147f49fedbc21b6d2aedc7be13a16b
by metan 22 Sep '11
by metan 22 Sep '11
22 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 570b772500147f49fedbc21b6d2aedc7be13a16b (commit)
via 784faa3832daa85cf2f0a8d399ef557645b4d271 (commit)
from 0543a967e4953d105ccbb8ca1bd7fd2915a4a298 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/570b772500147f49fedbc21b6d2aedc7be13…
commit 570b772500147f49fedbc21b6d2aedc7be13a16b
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Thu Sep 22 19:43:30 2011 +0200
Added basic PNG support using libpng.
* RGB888
* Grayscale 1BPP - 8BPP
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h
index e03f15d..99253bf 100644
--- a/include/loaders/GP_Loaders.h
+++ b/include/loaders/GP_Loaders.h
@@ -36,4 +36,6 @@
#include "GP_PGM.h"
#include "GP_PPM.h"
+#include "GP_PNG.h"
+
#endif /* GP_LOADERS_H */
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_PNG.h
similarity index 72%
copy from include/loaders/GP_Loaders.h
copy to include/loaders/GP_PNG.h
index e03f15d..2b02deb 100644
--- a/include/loaders/GP_Loaders.h
+++ b/include/loaders/GP_PNG.h
@@ -16,24 +16,36 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
- * <jiri.bluebear.dluhos(a)gmail.com> *
- * *
* Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
/*
-
- Core include file for loaders API.
+
+ PNG support using libpng library.
*/
-#ifndef GP_LOADERS_H
-#define GP_LOADERS_H
+#ifndef GP_PNG_H
+#define GP_PNG_H
+
+#include "core/GP_Context.h"
+
+/*
+ * Opens up file and checks signature. Upon successful return the file
+ * possition would be set to eight bytes (exactly after the PNG signature).
+ */
+GP_RetCode GP_OpenPNG(const char *src_path, FILE **f);
+
+/*
+ * Reads PNG from an open FILE. Expects the file possition set after the eight
+ * bytes PNG signature.
+ */
+GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res);
-#include "GP_PBM.h"
-#include "GP_PGM.h"
-#include "GP_PPM.h"
+/*
+ * Loads a PNG file into GP_Context. The Context is newly allocated.
+ */
+GP_RetCode GP_LoadPNG(const char *src_path, GP_Context **res);
-#endif /* GP_LOADERS_H */
+#endif /* GP_PNG_H */
diff --git a/libs/loaders/GP_PNG.c b/libs/loaders/GP_PNG.c
new file mode 100644
index 0000000..a533287
--- /dev/null
+++ b/libs/loaders/GP_PNG.c
@@ -0,0 +1,181 @@
+/*****************************************************************************
+ * This file is part of gfxprim library. *
+ * *
+ * Gfxprim is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU Lesser General Public *
+ * License as published by the Free Software Foundation; either *
+ * version 2.1 of the License, or (at your option) any later version. *
+ * *
+ * Gfxprim 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with gfxprim; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
+ * Boston, MA 02110-1301 USA *
+ * *
+ * Copyright (C) 2009-2010 Cyril Hrubis <metan(a)ucw.cz> *
+ * *
+ *****************************************************************************/
+
+/*
+
+ PNG image support using libpng.
+
+ */
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#include <errno.h>
+#include <string.h>
+
+#include <png.h>
+
+#include <GP_Context.h>
+#include <GP_Debug.h>
+
+GP_RetCode GP_OpenPNG(const char *src_path, FILE **f)
+{
+ uint8_t sig[8];
+
+ *f = fopen(src_path, "r");
+
+ if (*f == NULL) {
+ GP_DEBUG(1, "Failed to open '%s' : %s",
+ src_path, strerror(errno));
+ return GP_EBADFILE;
+ }
+
+ if (fread(sig, 1, 8, *f) <= 0) {
+ GP_DEBUG(1, "Failed to read '%s' : %s",
+ src_path, strerror(errno));
+ goto err;
+ }
+
+ if (png_sig_cmp(sig, 0, 8)) {
+ GP_DEBUG(1, "Invalid file header, '%s' not a PNG image?",
+ src_path);
+ goto err;
+ }
+
+ GP_DEBUG(1, "Found PNG signature in '%s'", src_path);
+
+ return GP_ESUCCESS;
+err:
+ fclose(*f);
+ *f = NULL;
+ return GP_EBADFILE;
+}
+
+GP_RetCode GP_ReadPNG(FILE *f, GP_Context **res)
+{
+ png_structp png;
+ png_infop png_info = NULL;
+ png_uint_32 w, h;
+ int depth, color_type;
+ GP_PixelType pixel_type = GP_PIXEL_UNKNOWN;
+ GP_RetCode ret;
+
+ png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+
+ if (png == NULL) {
+ GP_DEBUG(1, "Failed to allocate PNG read buffer");
+ ret = GP_ENOMEM;
+ goto err1;
+ }
+
+ png_info = png_create_info_struct(png);
+
+ if (png_info == NULL) {
+ GP_DEBUG(1, "Failed to allocate PNG info buffer");
+ ret = GP_ENOMEM;
+ goto err2;
+ }
+
+ if (setjmp(png_jmpbuf(png))) {
+ GP_DEBUG(1, "Failed to read PNG file :(");
+ ret = GP_EBADFILE;
+ goto err2;
+ }
+
+ png_init_io(png, f);
+ png_set_sig_bytes(png, 8);
+ png_read_info(png, png_info);
+
+ png_get_IHDR(png, png_info, &w, &h, &depth,
+ &color_type, NULL, NULL, NULL);
+
+ GP_DEBUG(2, "Have %s PNG%s size %ux%u depth %i",
+ color_type & PNG_COLOR_MASK_COLOR ? "color" : "gray",
+ color_type & PNG_COLOR_MASK_ALPHA ? " with alpha channel" : "",
+ w, h, depth);
+
+ switch (color_type) {
+ case PNG_COLOR_TYPE_GRAY:
+ switch (depth) {
+ case 1:
+ pixel_type = GP_PIXEL_G1;
+ break;
+ case 2:
+ pixel_type = GP_PIXEL_G2;
+ break;
+ case 4:
+ pixel_type = GP_PIXEL_G4;
+ break;
+ case 8:
+ pixel_type = GP_PIXEL_G8;
+ break;
+ }
+ break;
+ case PNG_COLOR_TYPE_RGB:
+
+ png_set_bgr(png);
+
+ switch (depth) {
+ case 8:
+ pixel_type = GP_PIXEL_RGB888;
+ break;
+ }
+ break;
+ }
+
+ if (pixel_type == GP_PIXEL_UNKNOWN) {
+ ret = GP_ENOIMPL;
+ goto err2;
+ }
+
+ *res = GP_ContextAlloc(w, h, pixel_type);
+
+ if (*res == NULL) {
+ ret = GP_ENOMEM;
+ goto err2;
+ }
+
+ uint32_t y;
+
+ for (y = 0; y < h; y++) {
+ png_bytep addr = GP_PIXEL_ADDR(*res, 0, y);
+ png_read_rows(png, &addr, NULL, 1);
+ }
+
+ ret = GP_ESUCCESS;
+err2:
+ png_destroy_read_struct(&png, png_info ? &png_info : NULL, NULL);
+err1:
+ fclose(f);
+ return ret;
+}
+
+GP_RetCode GP_LoadPNG(const char *src_path, GP_Context **res)
+{
+ FILE *f;
+ GP_RetCode ret;
+
+ if ((ret = GP_OpenPNG(src_path, &f)))
+ return ret;
+
+ return GP_ReadPNG(f, res);
+}
diff --git a/tests/SDL/Makefile b/tests/SDL/Makefile
index 86108c0..e5d9500 100644
--- a/tests/SDL/Makefile
+++ b/tests/SDL/Makefile
@@ -3,7 +3,7 @@ TOPDIR=../..
CSOURCES=$(shell echo *.c)
INCLUDE=core gfx SDL backends
-LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL
+LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL -lpng
APPS=pixeltest fileview fonttest linetest randomshapetest shapetest sierpinsky symbolstest textaligntest trianglefps input blittest subcontext ppm_test
http://repo.or.cz/w/gfxprim.git/commit/784faa3832daa85cf2f0a8d399ef557645b4…
commit 784faa3832daa85cf2f0a8d399ef557645b4d271
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Thu Sep 22 19:35:00 2011 +0200
Add missing braces around macro parameter.
diff --git a/include/core/GP_Context.h b/include/core/GP_Context.h
index b4d5ce6..6bc244c 100644
--- a/include/core/GP_Context.h
+++ b/include/core/GP_Context.h
@@ -63,9 +63,9 @@ static inline GP_PixelType GP_GetContextPixelType(const GP_Context *context)
* Rows and columns are specified in the image's orientation
* (i.e. they might not be XY if the image is rotated).
*/
-#define GP_PIXEL_ADDR(context, x, y) ((context->pixels - + y * context->bytes_per_row - + (x * context->bpp) / 8))
+#define GP_PIXEL_ADDR(context, x, y) (((context)->pixels + + y * (context)->bytes_per_row + + (x * (context)->bpp) / 8))
#define GP_CALC_ROW_SIZE(pixel_type, width) ((GP_PixelSize(pixel_type) * width) / 8 +
-----------------------------------------------------------------------
Summary of changes:
include/core/GP_Context.h | 6 +-
include/loaders/GP_Loaders.h | 2 +
include/{filters/GP_Resize.h => loaders/GP_PNG.h} | 31 +++-
libs/loaders/GP_PNG.c | 181 +++++++++++++++++++++
tests/SDL/Makefile | 2 +-
5 files changed, 210 insertions(+), 12 deletions(-)
copy include/{filters/GP_Resize.h => loaders/GP_PNG.h} (73%)
create mode 100644 libs/loaders/GP_PNG.c
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 0543a967e4953d105ccbb8ca1bd7fd2915a4a298
by metan 21 Sep '11
by metan 21 Sep '11
21 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 0543a967e4953d105ccbb8ca1bd7fd2915a4a298 (commit)
via a078ba651d736964efa6f71b9d0e4728bee803d6 (commit)
from 4df6d59aba3f76c23c9f9155721db580f3b6cff1 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/0543a967e4953d105ccbb8ca1bd7fd2915a4…
commit 0543a967e4953d105ccbb8ca1bd7fd2915a4a298
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 23:19:16 2011 +0200
Added simple brightness filter.
diff --git a/include/filters/GP_Linear.h b/include/filters/GP_Linear.h
new file mode 100644
index 0000000..5465882
--- /dev/null
+++ b/include/filters/GP_Linear.h
@@ -0,0 +1,35 @@
+/*****************************************************************************
+ * This file is part of gfxprim library. *
+ * *
+ * Gfxprim is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU Lesser General Public *
+ * License as published by the Free Software Foundation; either *
+ * version 2.1 of the License, or (at your option) any later version. *
+ * *
+ * Gfxprim 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with gfxprim; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
+ * Boston, MA 02110-1301 USA *
+ * *
+ * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
+ * *
+ *****************************************************************************/
+
+/*
+
+
+ */
+
+#ifndef GP_LINEAR_H
+#define GP_LINEAR_H
+
+#include <GP_Context.h>
+
+GP_Context *GP_FilterBrightness(const GP_Context *src, int32_t inc);
+
+#endif /* GP_LINEAR_H */
diff --git a/libs/filters/GP_Brightness.gen.c.t b/libs/filters/GP_Brightness.gen.c.t
new file mode 100644
index 0000000..b73fb9a
--- /dev/null
+++ b/libs/filters/GP_Brightness.gen.c.t
@@ -0,0 +1,91 @@
+%% extends "base.c.t"
+
+%% block descr
+Brightness filters -- Increments all color channels by a fixed value.
+%% endblock
+
+%% block body
+#include <GP_Context.h>
+#include <GP_Pixel.h>
+#include <GP_GetPutPixel.h>
+
+/*
+ * Simple brightness operations for one channel bitmaps
+ */
+%% for ps in pixelsizes
+%% if ps.size <= 8 and ps.size > 1
+void GP_FilterBrightness_{{ ps.suffix }}(const GP_Context *src, GP_Context *res, int32_t inc)
+{
+ uint32_t x, y;
+
+ for (y = 0; y < src->h; y++)
+ for (x = 0; x < src->w; x++) {
+ int32_t pix = GP_GetPixel_Raw_{{ ps.suffix }}(src, x, y) + inc;
+
+ if (pix < 0)
+ pix = 0;
+
+ if (pix > {{ 2 ** ps.size - 1 }})
+ pix = {{ 2 ** ps.size - 1}};
+
+ GP_PutPixel_Raw_{{ ps.suffix }}(res, x, y, pix);
+ }
+}
+
+%% endif
+%% endfor
+
+/*
+ * Brightness filters for pixel types with more than one channels
+ */
+%% for pt in pixeltypes
+%% if not pt.is_unknown() and len(pt.chanslist) > 1
+void GP_FilterBrightness_{{ pt.name }}(const GP_Context *src, GP_Context *res, int32_t inc)
+{
+ uint32_t x, y;
+
+ for (y = 0; y < src->h; y++)
+ for (x = 0; x < src->w; x++) {
+ GP_Pixel pix = GP_GetPixel_Raw_{{ pt.pixelsize.suffix }}(src, x, y);
+ %% for c in pt.chanslist
+ int32_t {{ c[0] }} = GP_Pixel_GET_{{ c[0] }}_{{ pt.name }}(pix) + inc;
+ %% endfor
+
+ %% for c in pt.chanslist
+ if ({{ c[0] }} < 0)
+ {{ c[0] }} = 0;
+
+ if ({{ c[0] }} > {{ 2 ** c[2] - 1 }})
+ {{ c[0] }} = 255;
+
+ %% endfor
+ pix = GP_Pixel_CREATE_{{ pt.name }}({{ pt.chanslist[0][0] }}{% for c in pt.chanslist[1:] %}, {{ c[0] }}{% endfor %});
+
+ GP_PutPixel_Raw_{{ pt.pixelsize.suffix }}(res, x, y, pix);
+ }
+}
+
+%% endif
+%% endfor
+
+void GP_FilterBrightness_Raw(const GP_Context *src, GP_Context *res, int32_t inc)
+{
+ switch (src->pixel_type) {
+ %% for pt in pixeltypes
+ case GP_PIXEL_{{ pt.name }}:
+ %% if pt.is_unknown() or pt.pixelsize.size < 2
+ return;
+ %% elif len(pt.chanslist) == 1:
+ GP_FilterBrightness_{{ pt.pixelsize.suffix }}(src, res, inc);
+ %% else
+ GP_FilterBrightness_{{ pt.name }}(src, res, inc);
+ %% endif
+ break;
+ %% endfor
+ default:
+ break;
+ }
+}
+
+
+%% endblock body
diff --git a/libs/filters/GP_Linear.c b/libs/filters/GP_Linear.c
new file mode 100644
index 0000000..50ec525
--- /dev/null
+++ b/libs/filters/GP_Linear.c
@@ -0,0 +1,42 @@
+/*****************************************************************************
+ * This file is part of gfxprim library. *
+ * *
+ * Gfxprim is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU Lesser General Public *
+ * License as published by the Free Software Foundation; either *
+ * version 2.1 of the License, or (at your option) any later version. *
+ * *
+ * Gfxprim 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with gfxprim; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
+ * Boston, MA 02110-1301 USA *
+ * *
+ * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
+ * *
+ *****************************************************************************/
+
+#include <GP_Context.h>
+#include <GP_GetPutPixel.h>
+
+#include <GP_Debug.h>
+
+#include "GP_Linear.h"
+
+void GP_FilterBrightness_Raw(const GP_Context *src, GP_Context *res, int32_t inc);
+
+GP_Context *GP_FilterBrightness(const GP_Context *src, int32_t inc)
+{
+ GP_Context *res = GP_ContextCopy(src, 0);
+
+ if (res == NULL)
+ return NULL;
+
+ GP_FilterBrightness_Raw(src, res, inc);
+
+ return res;
+}
diff --git a/libs/filters/Makefile b/libs/filters/Makefile
index 9e5120a..6f48556 100644
--- a/libs/filters/Makefile
+++ b/libs/filters/Makefile
@@ -1,6 +1,10 @@
TOPDIR=../..
-CSOURCES=$(shell ls *.c)
+GENSOURCES=GP_Brightness.gen.c
+GENHEADERS=
+CSOURCES=$(filter-out $(wildcard *.gen.c),$(wildcard *.c))
LIBNAME=filters
INCLUDE=core
+
+include $(TOPDIR)/gen.mk
include $(TOPDIR)/include.mk
include $(TOPDIR)/lib.mk
diff --git a/tests/SDL/showimage.c b/tests/SDL/showimage.c
index 1e0b356..9ef23f1 100644
--- a/tests/SDL/showimage.c
+++ b/tests/SDL/showimage.c
@@ -30,9 +30,12 @@
SDL_Surface *display = NULL;
GP_Context context, *bitmap;
+int brightness = 0;
+
void event_loop(void)
{
SDL_Event event;
+ GP_Context *res;
while (SDL_WaitEvent(&event) > 0) {
@@ -42,6 +45,19 @@ void event_loop(void)
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
return;
+ case SDLK_UP:
+ brightness+=2;
+ case SDLK_DOWN:
+ brightness-=1;
+
+ res = GP_FilterBrightness(bitmap, brightness);
+
+ printf("brightness = %i %ux%un", brightness, res->w, res->h);
+
+ GP_Blit_Naive(res, 0, 0, res->w, res->h, &context, 0, 0);
+ SDL_Flip(display);
+ GP_ContextFree(res);
+ break;
default:
break;
}
diff --git a/tests/SDL/subcontext.c b/tests/SDL/subcontext.c
index cbe110e..b8f68c4 100644
--- a/tests/SDL/subcontext.c
+++ b/tests/SDL/subcontext.c
@@ -139,7 +139,11 @@ void redraw_screen(void)
SDL_LockSurface(display);
uint8_t v = random() % 128 + 50;
- GP_Color col = GP_RGBToContextPixel(v, v, 255, sub_context);
+ GP_Color col;
+ if (sub_context->pixel_type == GP_PIXEL_P8)
+ col = random() % 256;
+ else
+ col = GP_RGBToContextPixel(v, v, 255, sub_context);
/* frame around subcontext */
GP_Rect(&context, 99, 99, context.w - 100, context.h - 100, white);
@@ -224,7 +228,9 @@ int main(int argc, char *argv[])
int i;
for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-16") == 0) {
+ if (strcmp(argv[i], "-8") == 0) {
+ display_bpp = 8;
+ } else if (strcmp(argv[i], "-16") == 0) {
display_bpp = 16;
}
else if (strcmp(argv[i], "-24") == 0) {
http://repo.or.cz/w/gfxprim.git/commit/a078ba651d736964efa6f71b9d0e4728bee8…
commit a078ba651d736964efa6f71b9d0e4728bee803d6
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 17:25:54 2011 +0200
Small GP_ContextCopy() fixup.
diff --git a/include/core/GP_Context.h b/include/core/GP_Context.h
index bd53482..b4d5ce6 100644
--- a/include/core/GP_Context.h
+++ b/include/core/GP_Context.h
@@ -100,7 +100,7 @@ GP_Context *GP_ContextAlloc(GP_Size w, GP_Size h, GP_PixelType type);
/*
* Copy context.
*/
-GP_Context *GP_ContextCopy(GP_Context *context, int flag);
+GP_Context *GP_ContextCopy(const GP_Context *src, int flag);
/*
* Create subcontext.
diff --git a/libs/core/GP_Context.c b/libs/core/GP_Context.c
index fffabba..0ce6342 100644
--- a/libs/core/GP_Context.c
+++ b/libs/core/GP_Context.c
@@ -28,41 +28,40 @@
#include <string.h>
-GP_Context *GP_ContextCopy(GP_Context *context, int flag)
+GP_Context *GP_ContextCopy(const GP_Context *src, int flag)
{
GP_Context *new;
uint8_t *pixels;
- if (context == NULL)
+ if (src == NULL)
return NULL;
new = malloc(sizeof(GP_Context));
- pixels = malloc(context->bytes_per_row * context->h);
+ pixels = malloc(src->bytes_per_row * src->h);
- if (pixels == NULL || context == NULL) {
+ if (pixels == NULL || new == NULL) {
free(pixels);
- free(context);
+ free(new);
return NULL;
}
new->pixels = pixels;
if (flag)
- memcpy(pixels, context->pixels,
- context->bytes_per_row * context->h);
+ memcpy(pixels, src->pixels, src->bytes_per_row * src->h);
- new->bpp = context->bpp;
- new->bytes_per_row = context->bytes_per_row;
+ new->bpp = src->bpp;
+ new->bytes_per_row = src->bytes_per_row;
- new->w = context->w;
- new->h = context->h;
+ new->w = src->w;
+ new->h = src->h;
- new->pixel_type = context->pixel_type;
+ new->pixel_type = src->pixel_type;
/* rotation and mirroring */
- new->axes_swap = context->axes_swap;
- new->y_swap = context->y_swap;
- new->x_swap = context->x_swap;
+ new->axes_swap = src->axes_swap;
+ new->y_swap = src->y_swap;
+ new->x_swap = src->x_swap;
new->free_pixels = 1;
-----------------------------------------------------------------------
Summary of changes:
include/core/GP_Context.h | 2 +-
include/filters/{GP_Resize.h => GP_Linear.h} | 11 ++--
libs/core/GP_Context.c | 29 ++++----
libs/filters/GP_Brightness.gen.c.t | 91 +++++++++++++++++++++++++
libs/{core/GP_Debug.c => filters/GP_Linear.c} | 23 ++++--
libs/filters/Makefile | 6 ++-
tests/SDL/showimage.c | 16 +++++
tests/SDL/subcontext.c | 10 ++-
8 files changed, 155 insertions(+), 33 deletions(-)
copy include/filters/{GP_Resize.h => GP_Linear.h} (91%)
create mode 100644 libs/filters/GP_Brightness.gen.c.t
copy libs/{core/GP_Debug.c => filters/GP_Linear.c} (81%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 4df6d59aba3f76c23c9f9155721db580f3b6cff1
by metan 21 Sep '11
by metan 21 Sep '11
21 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 4df6d59aba3f76c23c9f9155721db580f3b6cff1 (commit)
via 1fcb3cc5e9f943f00a4a724ddbc60f641a84a93c (commit)
via 9ddbb807bfcc5cf721012761d7bfc1a9073a0a75 (commit)
from 4e6371664c0ae7cffb760e86403bae8bd372c555 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/4df6d59aba3f76c23c9f9155721db580f3b6…
commit 4df6d59aba3f76c23c9f9155721db580f3b6cff1
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 15:22:25 2011 +0200
Started to work on bitmap resize filters.
diff --git a/include/filters/GP_Filters.h b/include/filters/GP_Filters.h
index eb63d43..b09b5d2 100644
--- a/include/filters/GP_Filters.h
+++ b/include/filters/GP_Filters.h
@@ -19,7 +19,7 @@
* Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
* <jiri.bluebear.dluhos(a)gmail.com> *
* *
- * Copyright (C) 2009-2010 Cyril Hrubis <metan(a)ucw.cz> *
+ * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
@@ -32,6 +32,8 @@
#ifndef GP_FILTERS_H
#define GP_FILTERS_H
+
#include "GP_Rotate.h"
+#include "GP_Resize.h"
#endif /* GP_FILTERS_H */
diff --git a/include/filters/GP_Filters.h b/include/filters/GP_Resize.h
similarity index 79%
copy from include/filters/GP_Filters.h
copy to include/filters/GP_Resize.h
index eb63d43..4a0f9b6 100644
--- a/include/filters/GP_Filters.h
+++ b/include/filters/GP_Resize.h
@@ -16,22 +16,21 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
- * <jiri.bluebear.dluhos(a)gmail.com> *
- * *
- * Copyright (C) 2009-2010 Cyril Hrubis <metan(a)ucw.cz> *
+ * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
/*
- GP_Context filters.
+ GP_Context resize.
*/
-#ifndef GP_FILTERS_H
-#define GP_FILTERS_H
+#ifndef GP_RESIZE_H
+#define GP_RESIZE_H
+
+#include "core/GP_Context.h"
-#include "GP_Rotate.h"
+GP_Context *GP_ScaleDown(GP_Context *src);
-#endif /* GP_FILTERS_H */
+#endif /* GP_RESIZE_H */
diff --git a/tests/SDL/ppm_test.c b/libs/filters/GP_ScaleDown.c
similarity index 76%
copy from tests/SDL/ppm_test.c
copy to libs/filters/GP_ScaleDown.c
index fce7989..2cc379b 100644
--- a/tests/SDL/ppm_test.c
+++ b/libs/filters/GP_ScaleDown.c
@@ -20,26 +20,35 @@
* *
*****************************************************************************/
-#include <stdio.h>
-#include <stdlib.h>
+#include <GP_Context.h>
+#include <GP_GetPutPixel.h>
-#include "GP.h"
+#include <GP_Debug.h>
-int main(int argc, char *argv[])
+#include <GP_Resize.h>
+
+GP_Context *GP_ScaleDown(GP_Context *src)
{
- GP_Context *bitmap;
- GP_SetDebugLevel(10);
- GP_RetCode ret;
+ uint32_t w, h, x, y;
+ GP_Context *dst;
+
+ if (src->pixel_type != GP_PIXEL_RGB888)
+ return NULL;
+
+ w = src->w/2;
+ h = src->h/2;
+
+ dst = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
+
+ if (dst == NULL)
+ return NULL;
- if ((ret = GP_LoadPPM("ball.ppm", &bitmap))) {
- fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
- return 1;
- }
+ for (y = 0; y < h; y++)
+ for (x = 0; x < w; x++) {
+ GP_Pixel pix = GP_GetPixel_Raw_24BPP(src, 2*x, 2*y);
- if ((ret = GP_SavePPM("ball2.ppm", bitmap, "b"))) {
- fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
- return 1;
- }
+ GP_PutPixel_Raw_24BPP(dst, x, y, pix);
+ }
- return 0;
+ return dst;
}
diff --git a/libs/filters/Makefile b/libs/filters/Makefile
index 7fc92d3..9e5120a 100644
--- a/libs/filters/Makefile
+++ b/libs/filters/Makefile
@@ -1,5 +1,6 @@
TOPDIR=../..
CSOURCES=$(shell ls *.c)
LIBNAME=filters
+INCLUDE=core
include $(TOPDIR)/include.mk
include $(TOPDIR)/lib.mk
diff --git a/tests/SDL/ppm_test.c b/tests/SDL/ppm_test.c
index fce7989..a0653b2 100644
--- a/tests/SDL/ppm_test.c
+++ b/tests/SDL/ppm_test.c
@@ -31,12 +31,14 @@ int main(int argc, char *argv[])
GP_SetDebugLevel(10);
GP_RetCode ret;
- if ((ret = GP_LoadPPM("ball.ppm", &bitmap))) {
+ if ((ret = GP_LoadPPM(argv[1], &bitmap))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
return 1;
}
- if ((ret = GP_SavePPM("ball2.ppm", bitmap, "b"))) {
+ bitmap = GP_ScaleDown(bitmap);
+
+ if ((ret = GP_SavePPM("out.ppm", bitmap, "b"))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
return 1;
}
http://repo.or.cz/w/gfxprim.git/commit/1fcb3cc5e9f943f00a4a724ddbc60f641a84…
commit 1fcb3cc5e9f943f00a4a724ddbc60f641a84a93c
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 15:19:05 2011 +0200
Fix wrong order on PPM load.
diff --git a/libs/loaders/GP_PPM.c b/libs/loaders/GP_PPM.c
index a14dbe3..de6877e 100644
--- a/libs/loaders/GP_PPM.c
+++ b/libs/loaders/GP_PPM.c
@@ -38,14 +38,14 @@
#include "GP_PNM.h"
-int load_binary_ppm(FILE *f, uint32_t w, uint32_t h, uint32_t depth __attribute__((unused)),
+int load_binary_ppm(FILE *f, uint32_t depth __attribute__((unused)),
GP_Context *res)
{
uint32_t x, y;
int r, g, b;
- for (x = 0; x < w; x++)
- for (y = 0; y < h; y++) {
+ for (y = 0; y < res->h; y++)
+ for (x = 0; x < res->w; x++) {
r = fgetc(f);
g = fgetc(f);
b = fgetc(f);
@@ -96,7 +96,7 @@ GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res)
free(res);
return GP_ENOIMPL;
case '6':
- if (load_binary_ppm(f, w, h, depth, *res))
+ if (load_binary_ppm(f, depth, *res))
goto err2;
break;
}
http://repo.or.cz/w/gfxprim.git/commit/9ddbb807bfcc5cf721012761d7bfc1a9073a…
commit 9ddbb807bfcc5cf721012761d7bfc1a9073a0a75
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 15:14:51 2011 +0200
Add showimage test.
diff --git a/tests/SDL/Makefile b/tests/SDL/Makefile
index 185a3ce..86108c0 100644
--- a/tests/SDL/Makefile
+++ b/tests/SDL/Makefile
@@ -6,7 +6,8 @@ INCLUDE=core gfx SDL backends
LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL
APPS=pixeltest fileview fonttest linetest randomshapetest shapetest sierpinsky- symbolstest textaligntest trianglefps input blittest subcontext ppm_test
+ symbolstest textaligntest trianglefps input blittest subcontext ppm_test+ showimage
include $(TOPDIR)/include.mk
include $(TOPDIR)/app.mk
diff --git a/tests/SDL/showimage.c b/tests/SDL/showimage.c
new file mode 100644
index 0000000..1e0b356
--- /dev/null
+++ b/tests/SDL/showimage.c
@@ -0,0 +1,110 @@
+/*****************************************************************************
+ * This file is part of gfxprim library. *
+ * *
+ * Gfxprim is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU Lesser General Public *
+ * License as published by the Free Software Foundation; either *
+ * version 2.1 of the License, or (at your option) any later version. *
+ * *
+ * Gfxprim 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with gfxprim; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
+ * Boston, MA 02110-1301 USA *
+ * *
+ * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
+ * *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <SDL/SDL.h>
+
+#include "GP.h"
+#include "GP_SDL.h"
+
+SDL_Surface *display = NULL;
+GP_Context context, *bitmap;
+
+void event_loop(void)
+{
+ SDL_Event event;
+
+ while (SDL_WaitEvent(&event) > 0) {
+
+ switch (event.type) {
+
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ return;
+ default:
+ break;
+ }
+ break;
+ case SDL_QUIT:
+ return;
+ default:
+ break;
+ }
+ }
+}
+
+int main(int argc, char *argv[])
+{
+ /* Bits per pixel to be set for the display surface. */
+ int display_bpp = 0;
+
+ int i;
+ for (i = 1; i < argc; i++) {
+ if (strcmp(argv[i], "-16") == 0) {
+ display_bpp = 16;
+ }
+ else if (strcmp(argv[i], "-24") == 0) {
+ display_bpp = 24;
+ }
+ else if (strcmp(argv[i], "-32") == 0) {
+ display_bpp = 32;
+ }
+ }
+
+ GP_SetDebugLevel(10);
+
+ GP_RetCode ret;
+
+ if ((ret = GP_LoadPPM(argv[1], &bitmap))) {
+ fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
+ return 1;
+ }
+
+ /* Initialize SDL */
+ if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) {
+ fprintf(stderr, "Could not initialize SDL: %sn", SDL_GetError());
+ return 1;
+ }
+
+ /* Create a window with a software back surface */
+ display = SDL_SetVideoMode(bitmap->w, bitmap->h, display_bpp, SDL_SWSURFACE);
+ if (display == NULL) {
+ fprintf(stderr, "Could not open display: %sn", SDL_GetError());
+ goto fail;
+ }
+
+ GP_SDL_ContextFromSurface(&context, display);
+
+ GP_Blit_Naive(bitmap, 0, 0, bitmap->w, bitmap->h, &context, 0, 0);
+ SDL_Flip(display);
+
+ event_loop();
+
+ SDL_Quit();
+ return 0;
+fail:
+ SDL_Quit();
+ return 1;
+}
+
-----------------------------------------------------------------------
Summary of changes:
include/filters/GP_Filters.h | 4 +-
.../core/GP_Debug.c => include/filters/GP_Resize.h | 21 ++--
.../SDL/ppm_test.c => libs/filters/GP_ScaleDown.c | 41 +++++---
libs/filters/Makefile | 1 +
libs/loaders/GP_PPM.c | 8 +-
tests/SDL/Makefile | 3 +-
tests/SDL/ppm_test.c | 6 +-
tests/{common/GP_Tests.c => SDL/showimage.c} | 112 +++++++++++++-------
8 files changed, 122 insertions(+), 74 deletions(-)
copy libs/core/GP_Debug.c => include/filters/GP_Resize.h (88%)
copy tests/SDL/ppm_test.c => libs/filters/GP_ScaleDown.c (76%)
copy tests/{common/GP_Tests.c => SDL/showimage.c} (52%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: 4e6371664c0ae7cffb760e86403bae8bd372c555
by metan 21 Sep '11
by metan 21 Sep '11
21 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via 4e6371664c0ae7cffb760e86403bae8bd372c555 (commit)
from af5fd7582e11df218ea97fa3d5299a86e0a07194 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/4e6371664c0ae7cffb760e86403bae8bd372…
commit 4e6371664c0ae7cffb760e86403bae8bd372c555
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 14:29:27 2011 +0200
More PPM work (image saving).
diff --git a/include/loaders/GP_PPM.h b/include/loaders/GP_PPM.h
index 78ba9f7..8b57e61 100644
--- a/include/loaders/GP_PPM.h
+++ b/include/loaders/GP_PPM.h
@@ -27,6 +27,9 @@
GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res);
-GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src);
+/*
+ * The fmt may be either "a" for ASCII or "b" for BINARY.
+ */
+GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src, char *fmt);
#endif /* GP_PPM_H */
diff --git a/libs/loaders/GP_PNM.c b/libs/loaders/GP_PNM.c
new file mode 100644
index 0000000..261b75f
--- /dev/null
+++ b/libs/loaders/GP_PNM.c
@@ -0,0 +1,180 @@
+/*****************************************************************************
+ * This file is part of gfxprim library. *
+ * *
+ * Gfxprim is free software; you can redistribute it and/or *
+ * modify it under the terms of the GNU Lesser General Public *
+ * License as published by the Free Software Foundation; either *
+ * version 2.1 of the License, or (at your option) any later version. *
+ * *
+ * Gfxprim 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 *
+ * Lesser General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU Lesser General Public *
+ * License along with gfxprim; if not, write to the Free Software *
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
+ * Boston, MA 02110-1301 USA *
+ * *
+ * Copyright (C) 2009-2010 Cyril Hrubis <metan(a)ucw.cz> *
+ * *
+ *****************************************************************************/
+
+#include <stdint.h>
+#include <inttypes.h>
+
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+
+#include <GP_Debug.h>
+
+#include "GP_PNM.h"
+
+/*
+
+ PNM portable bitmap header
+ --------------------------
+
+ Format:
+
+ a magick number value of 'P' and one of
+ '1' - PBM 2bpp gray ASCII
+ '2' - PGM gray ASCII
+ '3' - PPM rgb888 ASCII
+ '4' - PBM 2bpp gray BINARY
+ '5' - PGM gray BINARY
+ '6' - PPM rgb888 BINARY
+ whitespace (blanks, TABs, CRs, LFs).
+ ascii width
+ whitespace
+ ascii height
+ whitespace
+ maximal value (interval is 0 ... max) (not applicable for PBM)
+ width * height ascii or binary values
+
+ lines starting with '#' are comments to the end of line
+
+ */
+
+static void try_read_comments(FILE *f)
+{
+ char c1, c2;
+
+ while (isspace(c1 = fgetc(f)));
+
+ ungetc(c1, f);
+
+ while ((c1 = fgetc(f)) == '#') {
+ do {
+ c2 = fgetc(f);
+ } while (c2 != 'n' && c2 != EOF);
+ }
+
+ ungetc(c1, f);
+}
+
+
+static char *pnm_names[] = {
+ "ASCII encoded PBM",
+ "ASCII encoded PGM",
+ "ASCII encoded PPM",
+ "BINARY encoded PBM",
+ "BINARY encoded PGM",
+ "BINARY encoded PPM",
+};
+
+FILE *GP_ReadPNM(const char *src_path, char *fmt,
+ uint32_t *w, uint32_t *h, uint32_t *depth)
+{
+ FILE *f = fopen(src_path, "r");
+ int ch;
+
+ if (f == NULL) {
+ GP_DEBUG(1, "Failed to open file '%s': %s",
+ src_path, strerror(errno));
+ return NULL;
+ }
+
+ ch = fgetc(f);
+
+ if (ch != 'P') {
+ GP_DEBUG(1, "Invalid PNM header start '%c' (0x%2x) expecting 'P'",
+ isprint(ch) ? ch : ' ', ch);
+ goto err1;
+ }
+
+ ch = fgetc(f);
+
+ switch (ch) {
+ case '4':
+ case '1':
+ *depth = 1;
+ break;
+ case '2':
+ case '3':
+ case '5':
+ case '6':
+ break;
+ default:
+ GP_DEBUG(1, "Invalid PNM format 'P%c' (0x%2x)",
+ isprint(ch) ? ch : ' ', ch);
+ goto err1;
+ }
+
+ *fmt = ch;
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", w) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header width");
+ goto err1;
+ }
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", h) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header height");
+ goto err1;
+ }
+
+ GP_DEBUG(2, "Have %s size %"PRIu32"x%"PRIu32,
+ pnm_names[*fmt - '1'], *w, *h);
+
+ if (*fmt == '1' || *fmt == '3')
+ return f;
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", depth) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header depth");
+ goto err1;
+ }
+
+ return f;
+err1:
+ fclose(f);
+ return NULL;
+}
+
+#define GFXPRIM_SIGNATURE "# Generated by gfxprim http://gfxprim.ucw.czn"
+
+FILE *GP_WritePNM(const char *dst_path, char fmt,
+ uint32_t w, uint32_t h, uint32_t depth)
+{
+ FILE *f = fopen(dst_path, "w");
+ int ret;
+
+ ret = fprintf(f, "P%cn"GFXPRIM_SIGNATURE
+ "%"PRIu32" %"PRIu32"n%"PRIu32"n",
+ fmt, w, h, depth);
+
+ if (ret < 0) {
+ GP_DEBUG(1, "Failed to write PNM header '%s' : %s",
+ dst_path, strerror(errno));
+ fclose(f);
+ return NULL;
+ }
+
+ return f;
+}
diff --git a/include/loaders/GP_PPM.h b/libs/loaders/GP_PNM.h
similarity index 72%
copy from include/loaders/GP_PPM.h
copy to libs/loaders/GP_PNM.h
index 78ba9f7..af59319 100644
--- a/include/loaders/GP_PPM.h
+++ b/libs/loaders/GP_PNM.h
@@ -16,17 +16,34 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
+ * Copyright (C) 2009-2010 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
-#ifndef GP_PPM_H
-#define GP_PPM_H
+/*
-#include "core/GP_Context.h"
+ Common PNM functions.
-GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res);
+ */
-GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src);
+#ifndef GP_PNM_H
+#define GP_PNM_H
-#endif /* GP_PPM_H */
+#include <stdint.h>
+
+/*
+ * Loads image header, returns pointer to FILE* (with file possition pointing
+ * to the start of the data stream) on success, fills image metadata into
+ * arguments.
+ */
+FILE *GP_ReadPNM(const char *src_path, char *fmt,
+ uint32_t *w, uint32_t *h, uint32_t *depth);
+
+/*
+ * Writes image header.
+ */
+FILE *GP_WritePNM(const char *dst_path, char fmt,
+ uint32_t w, uint32_t h, uint32_t depth);
+
+
+#endif /* GP_PNM_H */
diff --git a/libs/loaders/GP_PPM.c b/libs/loaders/GP_PPM.c
index 1eec6e9..a14dbe3 100644
--- a/libs/loaders/GP_PPM.c
+++ b/libs/loaders/GP_PPM.c
@@ -27,15 +27,18 @@
*/
#include <stdint.h>
+#include <inttypes.h>
+#include <errno.h>
+#include <string.h>
#include <GP_Debug.h>
#include <GP_Context.h>
#include <GP_Pixel.h>
#include <GP_GetPutPixel.h>
-#include "GP_PXMCommon.h"
+#include "GP_PNM.h"
-int load_binary_ppm(FILE *f, uint32_t w, uint32_t h, uint32_t depth,
+int load_binary_ppm(FILE *f, uint32_t w, uint32_t h, uint32_t depth __attribute__((unused)),
GP_Context *res)
{
uint32_t x, y;
@@ -66,7 +69,7 @@ GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res)
char fmt;
FILE *f;
- f = GP_OpenPNM(src_path, &fmt, &w, &h, &depth);
+ f = GP_ReadPNM(src_path, &fmt, &w, &h, &depth);
if (f == NULL)
return GP_EBADFILE;
@@ -76,6 +79,11 @@ GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res)
goto err1;
}
+ if (depth != 255) {
+ GP_DEBUG(1, "Unsupported depth %"PRIu32, depth);
+ goto err1;
+ }
+
*res = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
if (res == NULL)
@@ -102,7 +110,58 @@ err1:
return GP_EBADFILE;
}
-GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src)
+static int write_binary_ppm(FILE *f, GP_Context *src)
+{
+ uint32_t x, y;
+
+ for (y = 0; y < src->h; y++)
+ for (x = 0; x < src->w; x++) {
+ GP_Pixel pix = GP_GetPixel_Raw_24BPP(src, x, y);
+ //TODO endianess?
+
+ GP_SWAP(((uint8_t*)&pix)[0], ((uint8_t*)&pix)[2]);
+
+ if (fwrite(&pix, 3, 1, f) < 1)
+ return 1;
+ }
+
+ return 0;
+}
+
+GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src, char *fmt)
{
- return GP_ENOIMPL;
+ char hfmt;
+ FILE *f;
+
+ if (src->pixel_type != GP_PIXEL_RGB888)
+ return GP_ENOIMPL;
+
+ switch (*fmt) {
+ /* ASCII */
+ case 'a':
+ return GP_ENOIMPL;
+ break;
+ /* binary */
+ case 'b':
+ hfmt = '6';
+ break;
+ default:
+ return GP_ENOIMPL;
+ }
+
+ f = GP_WritePNM(res_path, hfmt, src->w, src->h, 255);
+
+ if (write_binary_ppm(f, src)) {
+ GP_DEBUG(1, "Failed to write buffer");
+ fclose(f);
+ return GP_EBADFILE;
+ }
+
+ if (fclose(f) < 0) {
+ GP_DEBUG(1, "Failed to close file '%s' : %s",
+ res_path, strerror(errno));
+ return GP_EBADFILE;
+ }
+
+ return GP_ESUCCESS;
}
diff --git a/libs/loaders/GP_PXMCommon.c b/libs/loaders/GP_PXMCommon.c
index 9b250bd..0add2f0 100644
--- a/libs/loaders/GP_PXMCommon.c
+++ b/libs/loaders/GP_PXMCommon.c
@@ -386,127 +386,3 @@ GP_RetCode GP_PXMSave8bpp(FILE *f, GP_Context *context)
return GP_ESUCCESS;
}
-
-static void try_read_comments(FILE *f)
-{
- char c1, c2;
-
- while (isspace(c1 = fgetc(f)));
-
- ungetc(c1, f);
-
- while ((c1 = fgetc(f)) == '#') {
- do {
- c2 = fgetc(f);
- } while (c2 != 'n' && c2 != EOF);
- }
-
- ungetc(c1, f);
-}
-
-/*
-
- PNM portable bitmap header loader.
-
- Format:
-
- a magick number value of 'P' and one of
- '1' - PBM 2bpp gray ASCII
- '2' - PGM gray ASCII
- '3' - PPM rgb888 ASCII
- '4' - PBM 2bpp gray BINARY
- '5' - PGM gray BINARY
- '6' - PPM rgb888 BINARY
- whitespace (blanks, TABs, CRs, LFs).
- ascii width
- whitespace
- ascii height
- whitespace
- maximal value (interval is 0 ... max) (not applicable for PBM)
- width * height ascii or binary values
-
- lines starting with '#' are comments to the end of line
-
- */
-
-static char *pnm_names[] = {
- "ASCII encoded PBM",
- "ASCII encoded PGM",
- "ASCII encoded PPM",
- "BINARY encoded PBM",
- "BINARY encoded PGM",
- "BINARY encoded PPM",
-};
-
-FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h,
- uint32_t *depth)
-{
- FILE *f = fopen(src_path, "r");
- int ch;
-
- if (f == NULL) {
- GP_DEBUG(1, "Failed to open file '%s': %s",
- src_path, strerror(errno));
- return NULL;
- }
-
- ch = fgetc(f);
-
- if (ch != 'P') {
- GP_DEBUG(1, "Invalid PNM header start '%c' (0x%2x) expecting 'P'",
- isprint(ch) ? ch : ' ', ch);
- goto err1;
- }
-
- ch = fgetc(f);
-
- switch (ch) {
- case '4':
- case '1':
- *depth = 1;
- break;
- case '2':
- case '3':
- case '5':
- case '6':
- break;
- default:
- GP_DEBUG(1, "Invalid PNM format 'P%c' (0x%2x)",
- isprint(ch) ? ch : ' ', ch);
- goto err1;
- }
-
- *fmt = ch;
-
- try_read_comments(f);
-
- if (fscanf(f, "%"PRIu32"n", w) < 1) {
- GP_DEBUG(1, "Failed to read PNM header width");
- goto err1;
- }
-
- try_read_comments(f);
-
- if (fscanf(f, "%"PRIu32"n", h) < 1) {
- GP_DEBUG(1, "Failed to read PNM header height");
- goto err1;
- }
-
- GP_DEBUG(2, "Have %s size %"PRIu32"x%"PRIu32,
- pnm_names[*fmt - '1'], *w, *h);
-
- if (*fmt == '1' || *fmt == '3')
- return f;
-
- try_read_comments(f);
-
- if (fscanf(f, "%"PRIu32"n", depth) < 1) {
- GP_DEBUG(1, "Failed to read PNM header depth");
- goto err1;
- }
-
- return f;
-err1:
- fclose(f);
- return NULL;
-}
diff --git a/libs/loaders/GP_PXMCommon.h b/libs/loaders/GP_PXMCommon.h
index 0c231e2..1a3fc04 100644
--- a/libs/loaders/GP_PXMCommon.h
+++ b/libs/loaders/GP_PXMCommon.h
@@ -57,7 +57,11 @@ GP_RetCode GP_PXMLoad8bpp(FILE *f, GP_Context *context);
* Loads image header, returns pointer to FILE* on success, fills image
* metadata into arguments.
*/
-FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h,
- uint32_t *depth);
+FILE *GP_ReadHeaderPNM(const char *src_path, char *fmt,
+ uint32_t *w, uint32_t *h, uint32_t *depth);
+
+FILE *GP_WriteHeaderPNM(const char *dst_path, char *fmt,
+ uint32_t w, uint32_t h, uint32_t depth);
+
#endif /* GP_PXM_COMMON_H */
diff --git a/tests/SDL/Makefile b/tests/SDL/Makefile
index d99fdb9..185a3ce 100644
--- a/tests/SDL/Makefile
+++ b/tests/SDL/Makefile
@@ -6,7 +6,7 @@ INCLUDE=core gfx SDL backends
LDLIBS+=-lGP -L$(TOPDIR)/build/ -lGP_SDL -lSDL
APPS=pixeltest fileview fonttest linetest randomshapetest shapetest sierpinsky- symbolstest textaligntest trianglefps input blittest subcontext
+ symbolstest textaligntest trianglefps input blittest subcontext ppm_test
include $(TOPDIR)/include.mk
include $(TOPDIR)/app.mk
diff --git a/include/loaders/GP_PPM.h b/tests/SDL/ppm_test.c
similarity index 79%
copy from include/loaders/GP_PPM.h
copy to tests/SDL/ppm_test.c
index 78ba9f7..fce7989 100644
--- a/include/loaders/GP_PPM.h
+++ b/tests/SDL/ppm_test.c
@@ -20,13 +20,26 @@
* *
*****************************************************************************/
-#ifndef GP_PPM_H
-#define GP_PPM_H
+#include <stdio.h>
+#include <stdlib.h>
-#include "core/GP_Context.h"
+#include "GP.h"
-GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res);
+int main(int argc, char *argv[])
+{
+ GP_Context *bitmap;
+ GP_SetDebugLevel(10);
+ GP_RetCode ret;
-GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src);
+ if ((ret = GP_LoadPPM("ball.ppm", &bitmap))) {
+ fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
+ return 1;
+ }
-#endif /* GP_PPM_H */
+ if ((ret = GP_SavePPM("ball2.ppm", bitmap, "b"))) {
+ fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
+ return 1;
+ }
+
+ return 0;
+}
-----------------------------------------------------------------------
Summary of changes:
include/loaders/GP_PPM.h | 5 +-
libs/loaders/GP_PNM.c | 180 ++++++++++++++++++++
.../filters/GP_Filters.h => libs/loaders/GP_PNM.h | 28 +++-
libs/loaders/GP_PPM.c | 69 +++++++-
libs/loaders/GP_PXMCommon.c | 124 --------------
libs/loaders/GP_PXMCommon.h | 8 +-
tests/SDL/Makefile | 2 +-
libs/core/GP_Debug.c => tests/SDL/ppm_test.c | 26 ++-
8 files changed, 293 insertions(+), 149 deletions(-)
create mode 100644 libs/loaders/GP_PNM.c
copy include/filters/GP_Filters.h => libs/loaders/GP_PNM.h (75%)
copy libs/core/GP_Debug.c => tests/SDL/ppm_test.c (79%)
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0
[repo.or.cz] gfxprim.git branch generate updated: af5fd7582e11df218ea97fa3d5299a86e0a07194
by metan 21 Sep '11
by metan 21 Sep '11
21 Sep '11
This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project gfxprim.git.
The branch, generate has been updated
via af5fd7582e11df218ea97fa3d5299a86e0a07194 (commit)
from c019f09ad68b300c79e25e59465f0a80ef999c7c (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
http://repo.or.cz/w/gfxprim.git/commit/af5fd7582e11df218ea97fa3d5299a86e0a0…
commit af5fd7582e11df218ea97fa3d5299a86e0a07194
Author: Cyril Hrubis <metan(a)ucw.cz>
Date: Wed Sep 21 12:44:06 2011 +0200
Working on PPM loader (binary works).
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_Loaders.h
index e7ac305..e03f15d 100644
--- a/include/loaders/GP_Loaders.h
+++ b/include/loaders/GP_Loaders.h
@@ -34,7 +34,6 @@
#include "GP_PBM.h"
#include "GP_PGM.h"
-
-struct GP_Context *GP_Load(const char *src_path);
+#include "GP_PPM.h"
#endif /* GP_LOADERS_H */
diff --git a/include/loaders/GP_Loaders.h b/include/loaders/GP_PPM.h
similarity index 78%
copy from include/loaders/GP_Loaders.h
copy to include/loaders/GP_PPM.h
index e7ac305..78ba9f7 100644
--- a/include/loaders/GP_Loaders.h
+++ b/include/loaders/GP_PPM.h
@@ -16,25 +16,17 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
- * <jiri.bluebear.dluhos(a)gmail.com> *
- * *
* Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
- /*
-
- Core include file for loaders API.
-
- */
+#ifndef GP_PPM_H
+#define GP_PPM_H
-#ifndef GP_LOADERS_H
-#define GP_LOADERS_H
+#include "core/GP_Context.h"
-#include "GP_PBM.h"
-#include "GP_PGM.h"
+GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res);
-struct GP_Context *GP_Load(const char *src_path);
+GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src);
-#endif /* GP_LOADERS_H */
+#endif /* GP_PPM_H */
diff --git a/include/loaders/GP_Loaders.h b/libs/loaders/GP_PPM.c
similarity index 54%
copy from include/loaders/GP_Loaders.h
copy to libs/loaders/GP_PPM.c
index e7ac305..1eec6e9 100644
--- a/include/loaders/GP_Loaders.h
+++ b/libs/loaders/GP_PPM.c
@@ -16,25 +16,93 @@
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
- * Copyright (C) 2009-2010 Jiri "BlueBear" Dluhos *
- * <jiri.bluebear.dluhos(a)gmail.com> *
- * *
* Copyright (C) 2009-2011 Cyril Hrubis <metan(a)ucw.cz> *
* *
*****************************************************************************/
- /*
+/*
+
+ PPM portable bitmap loader/saver.
+
+ */
+
+#include <stdint.h>
+
+#include <GP_Debug.h>
+#include <GP_Context.h>
+#include <GP_Pixel.h>
+#include <GP_GetPutPixel.h>
+
+#include "GP_PXMCommon.h"
+
+int load_binary_ppm(FILE *f, uint32_t w, uint32_t h, uint32_t depth,
+ GP_Context *res)
+{
+ uint32_t x, y;
+ int r, g, b;
+
+ for (x = 0; x < w; x++)
+ for (y = 0; y < h; y++) {
+ r = fgetc(f);
+ g = fgetc(f);
+ b = fgetc(f);
+
+ if (r == EOF || g == EOF || b == EOF) {
+ GP_DEBUG(1, "Unexpected end of PBM file");
+ return 1;
+ }
+
+ //TODO depth
+ GP_Pixel pix = GP_Pixel_CREATE_RGB888(r, g, b);
+ GP_PutPixel_Raw_24BPP(res, x, y, pix);
+ }
+
+ return 0;
+}
+
+GP_RetCode GP_LoadPPM(const char *src_path, GP_Context **res)
+{
+ uint32_t w, h, depth;
+ char fmt;
+ FILE *f;
+
+ f = GP_OpenPNM(src_path, &fmt, &w, &h, &depth);
+
+ if (f == NULL)
+ return GP_EBADFILE;
- Core include file for loaders API.
+ if (fmt != '3' && fmt != '6') {
+ GP_DEBUG(1, "Asked to load PPM but header is 'P%c'", fmt);
+ goto err1;
+ }
- */
+ *res = GP_ContextAlloc(w, h, GP_PIXEL_RGB888);
-#ifndef GP_LOADERS_H
-#define GP_LOADERS_H
+ if (res == NULL)
+ goto err1;
-#include "GP_PBM.h"
-#include "GP_PGM.h"
+ switch (fmt) {
+ case '3':
+ //TODO
+ fclose(f);
+ free(res);
+ return GP_ENOIMPL;
+ case '6':
+ if (load_binary_ppm(f, w, h, depth, *res))
+ goto err2;
+ break;
+ }
-struct GP_Context *GP_Load(const char *src_path);
+ fclose(f);
+ return GP_ESUCCESS;
+err2:
+ free(*res);
+err1:
+ fclose(f);
+ return GP_EBADFILE;
+}
-#endif /* GP_LOADERS_H */
+GP_RetCode GP_SavePPM(const char *res_path, GP_Context *src)
+{
+ return GP_ENOIMPL;
+}
diff --git a/libs/loaders/GP_PXMCommon.c b/libs/loaders/GP_PXMCommon.c
index 62ee36b..9b250bd 100644
--- a/libs/loaders/GP_PXMCommon.c
+++ b/libs/loaders/GP_PXMCommon.c
@@ -23,10 +23,15 @@
* *
*****************************************************************************/
-#include <stdio.h>
#include <stdint.h>
#include <inttypes.h>
+#include <ctype.h>
+#include <errno.h>
+#include <string.h>
+
+#include <GP_Debug.h>
+
#include "GP_PXMCommon.h"
static int read_comment(FILE *f)
@@ -381,3 +386,127 @@ GP_RetCode GP_PXMSave8bpp(FILE *f, GP_Context *context)
return GP_ESUCCESS;
}
+
+static void try_read_comments(FILE *f)
+{
+ char c1, c2;
+
+ while (isspace(c1 = fgetc(f)));
+
+ ungetc(c1, f);
+
+ while ((c1 = fgetc(f)) == '#') {
+ do {
+ c2 = fgetc(f);
+ } while (c2 != 'n' && c2 != EOF);
+ }
+
+ ungetc(c1, f);
+}
+
+/*
+
+ PNM portable bitmap header loader.
+
+ Format:
+
+ a magick number value of 'P' and one of
+ '1' - PBM 2bpp gray ASCII
+ '2' - PGM gray ASCII
+ '3' - PPM rgb888 ASCII
+ '4' - PBM 2bpp gray BINARY
+ '5' - PGM gray BINARY
+ '6' - PPM rgb888 BINARY
+ whitespace (blanks, TABs, CRs, LFs).
+ ascii width
+ whitespace
+ ascii height
+ whitespace
+ maximal value (interval is 0 ... max) (not applicable for PBM)
+ width * height ascii or binary values
+
+ lines starting with '#' are comments to the end of line
+
+ */
+
+static char *pnm_names[] = {
+ "ASCII encoded PBM",
+ "ASCII encoded PGM",
+ "ASCII encoded PPM",
+ "BINARY encoded PBM",
+ "BINARY encoded PGM",
+ "BINARY encoded PPM",
+};
+
+FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h,
+ uint32_t *depth)
+{
+ FILE *f = fopen(src_path, "r");
+ int ch;
+
+ if (f == NULL) {
+ GP_DEBUG(1, "Failed to open file '%s': %s",
+ src_path, strerror(errno));
+ return NULL;
+ }
+
+ ch = fgetc(f);
+
+ if (ch != 'P') {
+ GP_DEBUG(1, "Invalid PNM header start '%c' (0x%2x) expecting 'P'",
+ isprint(ch) ? ch : ' ', ch);
+ goto err1;
+ }
+
+ ch = fgetc(f);
+
+ switch (ch) {
+ case '4':
+ case '1':
+ *depth = 1;
+ break;
+ case '2':
+ case '3':
+ case '5':
+ case '6':
+ break;
+ default:
+ GP_DEBUG(1, "Invalid PNM format 'P%c' (0x%2x)",
+ isprint(ch) ? ch : ' ', ch);
+ goto err1;
+ }
+
+ *fmt = ch;
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", w) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header width");
+ goto err1;
+ }
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", h) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header height");
+ goto err1;
+ }
+
+ GP_DEBUG(2, "Have %s size %"PRIu32"x%"PRIu32,
+ pnm_names[*fmt - '1'], *w, *h);
+
+ if (*fmt == '1' || *fmt == '3')
+ return f;
+
+ try_read_comments(f);
+
+ if (fscanf(f, "%"PRIu32"n", depth) < 1) {
+ GP_DEBUG(1, "Failed to read PNM header depth");
+ goto err1;
+ }
+
+ return f;
+err1:
+ fclose(f);
+ return NULL;
+}
diff --git a/libs/loaders/GP_PXMCommon.h b/libs/loaders/GP_PXMCommon.h
index 0c52e71..0c231e2 100644
--- a/libs/loaders/GP_PXMCommon.h
+++ b/libs/loaders/GP_PXMCommon.h
@@ -53,4 +53,11 @@ GP_RetCode GP_PXMLoad2bpp(FILE *f, GP_Context *context);
GP_RetCode GP_PXMLoad4bpp(FILE *f, GP_Context *context);
GP_RetCode GP_PXMLoad8bpp(FILE *f, GP_Context *context);
+/*
+ * Loads image header, returns pointer to FILE* on success, fills image
+ * metadata into arguments.
+ */
+FILE *GP_OpenPNM(const char *src_path, char *fmt, uint32_t *w, uint32_t *h,
+ uint32_t *depth);
+
#endif /* GP_PXM_COMMON_H */
diff --git a/tests/SDL/ball.ppm b/tests/SDL/ball.ppm
new file mode 100644
index 0000000..1ced238
Binary files /dev/null and b/tests/SDL/ball.ppm differ
diff --git a/tests/SDL/blittest.c b/tests/SDL/blittest.c
index 2e34564..1f77533 100644
--- a/tests/SDL/blittest.c
+++ b/tests/SDL/blittest.c
@@ -164,7 +164,7 @@ int main(int argc, char *argv[])
GP_RetCode ret;
- if ((ret = GP_LoadPGM("ball.pgm", &bitmap_raw))) {
+ if ((ret = GP_LoadPPM("ball.ppm", &bitmap_raw))) {
fprintf(stderr, "Failed to load bitmap: %sn", GP_RetCodeName(ret));
return 1;
}
-----------------------------------------------------------------------
Summary of changes:
include/loaders/GP_Loaders.h | 3 +-
libs/core/GP_Debug.c => include/loaders/GP_PPM.h | 17 +--
libs/loaders/{GP_PBM.c => GP_PPM.c} | 112 +++++++++----------
libs/loaders/GP_PXMCommon.c | 131 +++++++++++++++++++++-
libs/loaders/GP_PXMCommon.h | 7 +
tests/SDL/ball.ppm | Bin 0 -> 30054 bytes
tests/SDL/blittest.c | 2 +-
7 files changed, 201 insertions(+), 71 deletions(-)
copy libs/core/GP_Debug.c => include/loaders/GP_PPM.h (88%)
copy libs/loaders/{GP_PBM.c => GP_PPM.c} (56%)
create mode 100644 tests/SDL/ball.ppm
repo.or.cz automatic notification. Contact project admin jiri.bluebear.dluhos(a)gmail.com
if you want to unsubscribe, or site admin admin(a)repo.or.cz if you receive
no reply.
--
gfxprim.git ("A simple 2D graphics library with emphasis on correctness and well-defined operation.")
1
0