index 59e64b558e367d040a2c987a6ccfe3e818c82a72..d69b6fe547eb96243f8f1bcfa74e5f2af75513ea 100644 (file)
*
* Authors:
* (C) 1999-2002 Lauris Kaplinski <lauris@kaplinski.com>
+ * 2008, Jasper van de Gronde <th.v.d.gonde@hccnet.nl>
*
* This code is in the Public Domain
*/
+#include <cstring>
+#include <string>
+#include <string.h>
+#include <glib/gmem.h>
#include "nr-pixblock.h"
/// Size of buffer that needs no allocation (default 4).
pb->data.px = nr_pixelstore_1M_new (clear, 0x0);
} else {
pb->size = NR_PIXBLOCK_SIZE_BIG;
- pb->data.px = nr_new (unsigned char, size);
+ pb->data.px = NULL;
+ if (size > 100000000) { // Don't even try to allocate more than 100Mb (5000x5000 RGBA
+ // pixels). It'll just bog the system down even if successful. FIXME:
+ // Can anyone suggest something better than the magic number?
+ g_warning ("%lu bytes requested for pixel buffer, I won't try to allocate that.", (long unsigned) size);
+ return;
+ }
+ pb->data.px = g_try_new (unsigned char, size);
+ if (pb->data.px == NULL) { // memory allocation failed
+ g_warning ("Could not allocate %lu bytes for pixel buffer!", (long unsigned) size);
+ return;
+ }
if (clear) memset (pb->data.px, 0x0, size);
}
@@ -72,13 +88,14 @@ nr_pixblock_setup_fast (NRPixBlock *pb, NR_PIXBLOCK_MODE mode, int x0, int y0, i
}
/**
- * Pixbuf initialisation using nr_new.
+ * Pixbuf initialisation using g_new.
*
* After allocating memory, the buffer is cleared if the clear flag is set.
* \param pb Pointer to the pixbuf struct.
* \param mode Indicates grayscale/RGB/RGBA.
* \param clear True if buffer should be cleared.
* \pre x1>=x0 && y1>=y0 && pb!=NULL
+ FIXME: currently unused except for nr_pixblock_new and pattern tiles, replace with _fast and delete?
*/
void
nr_pixblock_setup (NRPixBlock *pb, NR_PIXBLOCK_MODE mode, int x0, int y0, int x1, int y1, bool clear)
if (clear) memset (pb->data.p, 0x0, size);
} else {
pb->size = NR_PIXBLOCK_SIZE_BIG;
- pb->data.px = nr_new (unsigned char, size);
+ pb->data.px = g_new (unsigned char, size);
if (clear) memset (pb->data.px, 0x0, size);
}
nr_pixelstore_1M_free (pb->data.px);
break;
case NR_PIXBLOCK_SIZE_BIG:
- nr_free (pb->data.px);
+ g_free (pb->data.px);
break;
case NR_PIXBLOCK_SIZE_STATIC:
break;
* Allocates NRPixBlock and sets it up.
*
* \return Pointer to fresh pixblock.
- * Calls nr_new() and nr_pixblock_setup().
+ * Calls g_new() and nr_pixblock_setup().
+FIXME: currently unused, delete? JG: Should be used more often! (To simplify memory management.)
*/
NRPixBlock *
nr_pixblock_new (NR_PIXBLOCK_MODE mode, int x0, int y0, int x1, int y1, bool clear)
{
NRPixBlock *pb;
- pb = nr_new (NRPixBlock, 1);
+ pb = g_new (NRPixBlock, 1);
+ if (!pb) return 0;
nr_pixblock_setup (pb, mode, x0, y0, x1, y1, clear);
+ if (pb->size!=NR_PIXBLOCK_SIZE_TINY && !pb->data.px) {
+ g_free(pb);
+ return 0;
+ }
return pb;
}
+/**
+ * Allocates NRPixBlock and sets it up.
+ *
+ * \return Pointer to fresh pixblock.
+ * Calls g_new() and nr_pixblock_setup().
+ */
+NRPixBlock *
+nr_pixblock_new_fast (NR_PIXBLOCK_MODE mode, int x0, int y0, int x1, int y1, bool clear)
+{
+ NRPixBlock *pb;
+
+ pb = g_new (NRPixBlock, 1);
+ if (!pb) return 0;
+
+ nr_pixblock_setup_fast (pb, mode, x0, y0, x1, y1, clear);
+ if (pb->size!=NR_PIXBLOCK_SIZE_TINY && !pb->data.px) {
+ g_free(pb);
+ return 0;
+ }
+
+ return pb;
+}
+
/**
* Frees all memory taken by pixblock.
*
{
nr_pixblock_release (pb);
- nr_free (pb);
+ g_free (pb);
return NULL;
}
nr_4K_len -= 1;
px = nr_4K_px[nr_4K_len];
} else {
- px = nr_new (unsigned char, 4096);
+ px = g_new (unsigned char, 4096);
}
if (clear) memset (px, val, 4096);
{
if (nr_4K_len == nr_4K_size) {
nr_4K_size += NR_4K_BLOCK;
- nr_4K_px = nr_renew (nr_4K_px, unsigned char *, nr_4K_size);
+ nr_4K_px = g_renew (unsigned char *, nr_4K_px, nr_4K_size);
}
nr_4K_px[nr_4K_len] = px;
nr_16K_len -= 1;
px = nr_16K_px[nr_16K_len];
} else {
- px = nr_new (unsigned char, 16384);
+ px = g_new (unsigned char, 16384);
}
if (clear) memset (px, val, 16384);
{
if (nr_16K_len == nr_16K_size) {
nr_16K_size += NR_16K_BLOCK;
- nr_16K_px = nr_renew (nr_16K_px, unsigned char *, nr_16K_size);
+ nr_16K_px = g_renew (unsigned char *, nr_16K_px, nr_16K_size);
}
nr_16K_px[nr_16K_len] = px;
nr_64K_len -= 1;
px = nr_64K_px[nr_64K_len];
} else {
- px = nr_new (unsigned char, 65536);
+ px = g_new (unsigned char, 65536);
}
if (clear) memset (px, val, 65536);
{
if (nr_64K_len == nr_64K_size) {
nr_64K_size += NR_64K_BLOCK;
- nr_64K_px = nr_renew (nr_64K_px, unsigned char *, nr_64K_size);
+ nr_64K_px = g_renew (unsigned char *, nr_64K_px, nr_64K_size);
}
nr_64K_px[nr_64K_len] = px;
nr_256K_len -= 1;
px = nr_256K_px[nr_256K_len];
} else {
- px = nr_new (unsigned char, NR_256K);
+ px = g_new (unsigned char, NR_256K);
}
if (clear) memset (px, val, NR_256K);
{
if (nr_256K_len == nr_256K_size) {
nr_256K_size += NR_256K_BLOCK;
- nr_256K_px = nr_renew (nr_256K_px, unsigned char *, nr_256K_size);
+ nr_256K_px = g_renew (unsigned char *, nr_256K_px, nr_256K_size);
}
nr_256K_px[nr_256K_len] = px;
nr_1M_len -= 1;
px = nr_1M_px[nr_1M_len];
} else {
- px = nr_new (unsigned char, NR_1M);
+ px = g_new (unsigned char, NR_1M);
}
if (clear) memset (px, val, NR_1M);
{
if (nr_1M_len == nr_1M_size) {
nr_1M_size += NR_1M_BLOCK;
- nr_1M_px = nr_renew (nr_1M_px, unsigned char *, nr_1M_size);
+ nr_1M_px = g_renew (unsigned char *, nr_1M_px, nr_1M_size);
}
nr_1M_px[nr_1M_len] = px;
fill-column:99
End:
*/
-// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :
+// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :