pygimp-image.c 38.8 KB
Newer Older
1
/* -*- Mode: C; c-basic-offset: 4 -*-
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * Gimp-Python - allows the writing of Gimp plugins in Python.
 * Copyright (C) 1997-2002  James Henstridge <james@daa.com.au>
 *
 * 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 3 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
16
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 18 19 20 21
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
22

23 24 25 26 27 28
#include "pygimp.h"

static PyObject *
img_add_channel(PyGimpImage *self, PyObject *args)
{
    PyGimpChannel *chn;
29
    int pos = -1;
30

31 32
    if (!PyArg_ParseTuple(args, "O!|i:add_channel",
	                        &PyGimpChannel_Type, &chn, &pos))
33 34
	return NULL;

35
    if (!gimp_image_insert_channel(self->ID, chn->ID, -1, pos)) {
36 37 38 39 40 41 42 43 44
	PyErr_Format(pygimp_error,
		     "could not add channel (ID %d) to image (ID %d)",
		     chn->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
45

46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static PyObject *
img_insert_channel(PyGimpImage *self, PyObject *args, PyObject *kwargs)
{
    PyGimpChannel *chn;
    PyGimpChannel *parent = NULL;
    int pos = -1;

    static char *kwlist[] = { "channel", "parent", "position", NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "O!|O!i:insert_channel", kwlist,
                                     &PyGimpChannel_Type, &chn,
                                     &PyGimpChannel_Type, &parent,
                                     &pos))
	return NULL;

    if (!gimp_image_insert_channel(self->ID,
                                   chn->ID, parent ? parent->ID : -1, pos)) {
	PyErr_Format(pygimp_error,
		     "could not insert channel (ID %d) to image (ID %d)",
		     chn->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

74 75 76 77
static PyObject *
img_add_layer(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
78
    int pos = -1;
79

80
    if (!PyArg_ParseTuple(args, "O!|i:add_layer", &PyGimpLayer_Type, &lay,
81
			  &pos))
82 83
	return NULL;

84
    if (!gimp_image_insert_layer(self->ID, lay->ID, -1, pos)) {
85 86 87 88 89 90 91 92 93
	PyErr_Format(pygimp_error,
		     "could not add layer (ID %d) to image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
static PyObject *
img_insert_layer(PyGimpImage *self, PyObject *args, PyObject *kwargs)
{
    PyGimpLayer *lay;
    PyGimpLayer *parent = NULL;
    int pos = -1;

    static char *kwlist[] = { "layer", "parent", "position", NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "O!|O!i:insert_layer", kwlist,
                                     &PyGimpLayer_Type, &lay,
                                     &PyGimpLayer_Type, &parent,
                                     &pos))
	return NULL;

    if (!gimp_image_insert_layer(self->ID,
                                 lay->ID, parent ? parent->ID : -1, pos)) {
	PyErr_Format(pygimp_error,
		     "could not insert layer (ID %d) to image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

123 124 125 126 127 128 129 130 131 132 133
static PyObject *
img_new_layer(PyGimpImage *self, PyObject *args, PyObject *kwargs)
{
    char *layer_name;
    int layer_id;
    int width, height;
    int layer_type;
    int offs_x = 0, offs_y = 0;
    gboolean alpha = TRUE;
    int pos = -1;
    double opacity = 100.0;
134
    GimpLayerMode mode = GIMP_LAYER_MODE_NORMAL;
135 136 137 138 139 140 141 142 143
    GimpFillType fill_mode = -1;

    static char *kwlist[] = { "name", "width", "height", "offset_x", "offset_y",
                              "alpha", "pos", "opacity", "mode", "fill_mode",
                              NULL };

    layer_name = "New Layer";

    width = gimp_image_width(self->ID);
144
    height = gimp_image_height(self->ID);
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
                                     "|siiiiiidii:new_layer", kwlist,
                                     &layer_name, &width, &height,
                                     &offs_x, &offs_y, &alpha, &pos,
                                     &opacity, &mode, &fill_mode))
        return NULL;


    switch (gimp_image_base_type(self->ID))  {
        case GIMP_RGB:
            layer_type = alpha ? GIMP_RGBA_IMAGE: GIMP_RGB_IMAGE;
            break;
        case GIMP_GRAY:
            layer_type = alpha ? GIMP_GRAYA_IMAGE: GIMP_GRAY_IMAGE;
            break;
        case GIMP_INDEXED:
            layer_type = alpha ? GIMP_INDEXEDA_IMAGE: GIMP_INDEXED_IMAGE;
            break;
        default:
            PyErr_SetString(pygimp_error, "Unknown image base type");
166
            return NULL;
167 168 169
    }

    if (fill_mode == -1)
170
        fill_mode = alpha ? GIMP_FILL_TRANSPARENT: GIMP_FILL_BACKGROUND;
171 172 173 174 175 176 177 178 179 180 181 182 183


    layer_id = gimp_layer_new(self->ID, layer_name, width, height,
                              layer_type, opacity, mode);

    if (!layer_id) {
        PyErr_Format(pygimp_error,
                     "could not create new layer in image (ID %d)",
                     self->ID);
        return NULL;
    }

    if (!gimp_drawable_fill(layer_id, fill_mode)) {
184
        gimp_item_delete(layer_id);
185 186 187 188 189 190
        PyErr_Format(pygimp_error,
                     "could not fill new layer with fill mode %d",
                     fill_mode);
        return NULL;
    }

191
    if (!gimp_image_insert_layer(self->ID, layer_id, -1, pos)) {
192
        gimp_item_delete(layer_id);
193 194 195 196 197 198 199 200 201 202 203 204 205 206
        PyErr_Format(pygimp_error,
                     "could not add layer (ID %d) to image (ID %d)",
                     layer_id, self->ID);
        return NULL;
    }

    if (!gimp_layer_set_offsets(layer_id, offs_x, offs_y)) {
        gimp_image_remove_layer(self->ID, layer_id);
        PyErr_Format(pygimp_error,
                     "could not set offset %d, %d on layer (ID %d)",
                      offs_x, offs_y, layer_id);
        return NULL;
    }

207
    return pygimp_group_layer_new(layer_id);
208 209 210
}


211
static PyObject *
212
img_clean_all(PyGimpImage *self)
213
{
214 215 216 217 218
    if (!gimp_image_clean_all(self->ID)) {
	PyErr_Format(pygimp_error, "could not clean all on image (ID %d)",
		     self->ID);
	return NULL;
    }
219

220 221 222
    Py_INCREF(Py_None);
    return Py_None;
}
223 224

static PyObject *
225
img_disable_undo(PyGimpImage *self)
226
{
227
    return PyBool_FromLong(gimp_image_undo_disable(self->ID));
228 229 230
}

static PyObject *
231
img_enable_undo(PyGimpImage *self)
232
{
233
    return PyBool_FromLong(gimp_image_undo_enable(self->ID));
234 235 236
}

static PyObject *
237
img_flatten(PyGimpImage *self)
238
{
239
    return pygimp_group_layer_new(gimp_image_flatten(self->ID));
240 241 242 243 244 245
}

static PyObject *
img_lower_channel(PyGimpImage *self, PyObject *args)
{
    PyGimpChannel *chn;
246

247 248
    if (!PyArg_ParseTuple(args, "O!:lower_channel", &PyGimpChannel_Type, &chn))
	return NULL;
249

250
    if (!gimp_image_lower_item(self->ID, chn->ID)) {
251 252 253 254 255 256 257 258
	PyErr_Format(pygimp_error,
		     "could not lower channel (ID %d) on image (ID %d)",
		     chn->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
259 260 261 262 263 264
}

static PyObject *
img_lower_layer(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
265

266 267
    if (!PyArg_ParseTuple(args, "O!:lower_layer", &PyGimpLayer_Type, &lay))
	return NULL;
268

269
    if (!gimp_image_lower_item(self->ID, lay->ID)) {
270 271 272 273 274 275 276 277
	PyErr_Format(pygimp_error,
		     "could not lower layer (ID %d) on image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
278 279
}

280 281 282 283
static PyObject *
img_lower_layer_to_bottom(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
284

285 286 287
    if (!PyArg_ParseTuple(args, "O!:lower_layer_to_bottom",
			  &PyGimpLayer_Type, &lay))
	return NULL;
288

289
    if (!gimp_image_lower_item_to_bottom(self->ID, lay->ID)) {
290 291 292 293 294 295 296 297
	PyErr_Format(pygimp_error,
		     "could not lower layer (ID %d) to bottom on image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
298
}
299 300 301 302 303 304

static PyObject *
img_merge_visible_layers(PyGimpImage *self, PyObject *args)
{
    gint32 id;
    int merge;
305

306 307
    if (!PyArg_ParseTuple(args, "i:merge_visible_layers", &merge))
	return NULL;
308

309
    id = gimp_image_merge_visible_layers(self->ID, merge);
310

311
    if (id == -1) {
312 313 314 315
	PyErr_Format(pygimp_error,
		     "could not merge visible layers on image (ID %d) "
		     "with merge type %d",
		     self->ID, merge);
316 317
	return NULL;
    }
318

319
    return pygimp_group_layer_new(id);
320 321
}

322 323 324 325 326 327 328
static PyObject *
img_merge_down(PyGimpImage *self, PyObject *args)
{
    gint32 id;
    PyGimpLayer *layer;
    int merge;

329
    if (!PyArg_ParseTuple(args, "O!i:merge_down",
330 331
			  &PyGimpLayer_Type, &layer, &merge))
	return NULL;
332

333
    id = gimp_image_merge_down(self->ID, layer->ID, merge);
334

335
    if (id == -1) {
336 337 338 339
	PyErr_Format(pygimp_error,
		     "could not merge down layer (ID %d) on image (ID %d) "
		     "with merge type %d",
		     layer->ID, self->ID, merge);
340 341
	return NULL;
    }
342

343
    return pygimp_group_layer_new(id);
344
}
345 346 347 348 349 350

static PyObject *
img_pick_correlate_layer(PyGimpImage *self, PyObject *args)
{
    int x,y;
    gint32 id;
351

352 353
    if (!PyArg_ParseTuple(args, "ii:pick_correlate_layer", &x, &y))
	return NULL;
354

355
    id = gimp_image_pick_correlate_layer(self->ID, x, y);
356

357 358 359 360
    if (id == -1) {
	Py_INCREF(Py_None);
	return Py_None;
    }
361

362
    return pygimp_group_layer_new(id);
363 364 365 366 367 368
}

static PyObject *
img_raise_channel(PyGimpImage *self, PyObject *args)
{
    PyGimpChannel *chn;
369

370 371
    if (!PyArg_ParseTuple(args, "O!:raise_channel", &PyGimpChannel_Type, &chn))
	return NULL;
372

373
    if (!gimp_image_raise_item(self->ID, chn->ID)) {
374 375 376 377 378 379 380 381
	PyErr_Format(pygimp_error,
		     "could not raise channel (ID %d) on image (ID %d)",
		     chn->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
382 383 384 385 386 387
}

static PyObject *
img_raise_layer(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
388

389 390
    if (!PyArg_ParseTuple(args, "O!:raise_layer", &PyGimpLayer_Type, &lay))
	return NULL;
391

392
    if (!gimp_image_raise_item(self->ID, lay->ID)) {
393 394 395 396 397 398 399 400
	PyErr_Format(pygimp_error,
		     "could not raise layer (ID %d) on image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
401 402
}

403 404 405 406
static PyObject *
img_raise_layer_to_top(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
407

408 409 410
    if (!PyArg_ParseTuple(args, "O!:raise_layer_to_top",
			  &PyGimpLayer_Type, &lay))
	return NULL;
411

412
    if (!gimp_image_raise_item_to_top(self->ID, lay->ID)) {
413 414 415 416 417 418 419 420
	PyErr_Format(pygimp_error,
		     "could not raise layer (ID %d) to top on image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
421
}
422 423 424 425 426

static PyObject *
img_remove_channel(PyGimpImage *self, PyObject *args)
{
    PyGimpChannel *chn;
427

428 429 430
    if (!PyArg_ParseTuple(args, "O!:remove_channel", &PyGimpChannel_Type, &chn))
	return NULL;

431 432 433 434 435 436 437 438 439 440
    if (!gimp_image_remove_channel(self->ID, chn->ID)) {
	PyErr_Format(pygimp_error,
		     "could not remove channel (ID %d) from image (ID %d)",
		     chn->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
441 442 443 444 445

static PyObject *
img_remove_layer(PyGimpImage *self, PyObject *args)
{
    PyGimpLayer *lay;
446

447 448 449
    if (!PyArg_ParseTuple(args, "O!:remove_layer", &PyGimpLayer_Type, &lay))
	return NULL;

450 451 452 453 454 455 456 457 458 459
    if (!gimp_image_remove_layer(self->ID, lay->ID)) {
	PyErr_Format(pygimp_error,
		     "could not remove layer (ID %d) from image (ID %d)",
		     lay->ID, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
460 461

static PyObject *
462
img_resize(PyGimpImage *self, PyObject *args, PyObject *kwargs)
463
{
464
    int new_w, new_h;
465
    int offs_x = 0, offs_y = 0;
466

467 468 469 470 471
    static char *kwlist[] = { "width", "height", "offset_x", "offset_y",
			      NULL };

    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii:resize", kwlist,
				     &new_w, &new_h, &offs_x, &offs_y))
472
	return NULL;
473 474 475

    if (!gimp_image_resize(self->ID, new_w, new_h, offs_x, offs_y)) {
	PyErr_Format(pygimp_error,
476
		     "could not resize image (ID %d) to %dx%d, offset %d, %d",
477 478 479 480 481 482
		     self->ID, new_w, new_h, offs_x, offs_y);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
483 484
}

485 486 487 488 489 490 491 492 493 494 495 496 497 498
static PyObject *
img_resize_to_layers(PyGimpImage *self)
{
    if (!gimp_image_resize_to_layers(self->ID)) {
	PyErr_Format(pygimp_error, "could not resize to layers on image "
	                           "(ID %d)",
		     self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

499
static PyObject *
500
img_scale(PyGimpImage *self, PyObject *args, PyObject *kwargs)
501
{
502
    int new_width, new_height;
503
    int interpolation = -1;
504

505
    static char *kwlist[] = { "width", "height", "interpolation", NULL };
506

507 508
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|i:scale", kwlist,
				     &new_width, &new_height, &interpolation))
509
	return NULL;
510

511
    if (interpolation != -1) {
512 513 514 515 516 517 518 519 520
        gimp_context_push();
        gimp_context_set_interpolation(interpolation);
    }

    if (!gimp_image_scale(self->ID, new_width, new_height)) {
        PyErr_Format(pygimp_error, "could not scale image (ID %d) to %dx%d",
                     self->ID, new_width, new_height);
        if (interpolation != -1) {
            gimp_context_pop();
521
        }
522 523 524 525 526
        return NULL;
    }

    if (interpolation != -1) {
        gimp_context_pop();
527 528 529 530
    }

    Py_INCREF(Py_None);
    return Py_None;
531 532 533
}

static PyObject *
534
img_crop(PyGimpImage *self, PyObject *args, PyObject *kwargs)
535 536
{
    int new_w, new_h;
537 538 539 540
    int offs_x = 0, offs_y = 0;

    static char *kwlist[] = { "width", "height", "offset_x", "offset_y",
			      NULL };
541

542 543
    if (!PyArg_ParseTupleAndKeywords(args, kwargs, "ii|ii:crop", kwlist,
				     &new_w, &new_h, &offs_x, &offs_y))
544
	return NULL;
545 546 547

    if (!gimp_image_crop(self->ID, new_w, new_h, offs_x, offs_y)) {
	PyErr_Format(pygimp_error,
548
		     "could not crop image (ID %d) to %dx%d, offset %d, %d",
549 550 551 552 553 554
		     self->ID, new_w, new_h, offs_x, offs_y);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
555 556 557 558 559
}

static PyObject *
img_free_shadow(PyGimpImage *self)
{
560
    /* this procedure is deprecated and does absolutely nothing */
561 562 563

    Py_INCREF(Py_None);
    return Py_None;
564 565 566 567 568
}

static PyObject *
img_unset_active_channel(PyGimpImage *self)
{
569 570 571 572 573 574 575 576 577
    if (!gimp_image_unset_active_channel(self->ID)) {
	PyErr_Format(pygimp_error,
		     "could not unset active channel on image (ID %d)",
		     self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
578
}
579 580 581 582 583

static PyObject *
img_get_component_active(PyGimpImage *self, PyObject *args)
{
    int comp;
584

585 586
    if (!PyArg_ParseTuple(args, "i:get_component_active", &comp))
	return NULL;
587 588

    return PyBool_FromLong(gimp_image_get_component_active(self->ID, comp));
589 590 591 592 593 594 595
}


static PyObject *
img_get_component_visible(PyGimpImage *self, PyObject *args)
{
    int comp;
596

597 598
    if (!PyArg_ParseTuple(args, "i:get_component_visible", &comp))
	return NULL;
599

600
    return PyBool_FromLong(gimp_image_get_component_visible(self->ID, comp));
601 602 603 604 605 606 607
}


static PyObject *
img_set_component_active(PyGimpImage *self, PyObject *args)
{
    int comp, a;
608

609 610 611
    if (!PyArg_ParseTuple(args, "ii:set_component_active", &comp, &a))
	return NULL;

612 613 614 615 616 617 618 619 620 621
    if (!gimp_image_set_component_active(self->ID, comp, a)) {
	PyErr_Format(pygimp_error,
		     "could not set component (%d) %sactive on image (ID %d)",
		     comp, a ? "" : "in", self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
}
622 623 624 625 626

static PyObject *
img_set_component_visible(PyGimpImage *self, PyObject *args)
{
    int comp, v;
627

628 629
    if (!PyArg_ParseTuple(args, "ii:set_component_visible", &comp, &v))
	return NULL;
630 631 632 633 634 635 636 637 638 639

    if (!gimp_image_set_component_visible(self->ID, comp, v)) {
	PyErr_Format(pygimp_error,
		     "could not set component (%d) %svisible on image (ID %d)",
		     comp, v ? "" : "in", self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
640 641 642 643 644 645
}

static PyObject *
img_parasite_find(PyGimpImage *self, PyObject *args)
{
    char *name;
646

647 648
    if (!PyArg_ParseTuple(args, "s:parasite_find", &name))
	return NULL;
649

650
    return pygimp_parasite_new (gimp_image_get_parasite (self->ID, name));
651 652 653 654 655 656
}

static PyObject *
img_parasite_attach(PyGimpImage *self, PyObject *args)
{
    PyGimpParasite *parasite;
657

658 659 660
    if (!PyArg_ParseTuple(args, "O!:parasite_attach", &PyGimpParasite_Type,
			  &parasite))
	return NULL;
661

662
    if (! gimp_image_attach_parasite (self->ID, parasite->para)) {
663 664 665 666 667 668 669 670
	PyErr_Format(pygimp_error,
		     "could not attach parasite '%s' to image (ID %d)",
		     parasite->para->name, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
671 672 673
}

static PyObject *
674
img_attach_new_parasite(PyGimpImage *self, PyObject *args, PyObject *kwargs)
675
{
676
    char *name;
677
    int flags, size;
678
    guint8 *data;
679 680
    GimpParasite *parasite;
    gboolean success;
681 682

    static char *kwlist[] = { "name", "flags", "data", NULL };
683

684 685 686
    if (!PyArg_ParseTupleAndKeywords(args, kwargs,
				     "sis#:attach_new_parasite", kwlist,
				     &name, &flags, &data, &size))
687
	return NULL;
688

689
    parasite = gimp_parasite_new (name, flags, size, data);
690
    success = gimp_image_attach_parasite (self->ID, parasite);
691 692 693
    gimp_parasite_free (parasite);

    if (!success) {
694 695 696 697 698
	PyErr_Format(pygimp_error,
		     "could not attach new parasite '%s' to image (ID %d)",
		     name, self->ID);
	return NULL;
    }
699

700 701 702 703 704 705 706 707
    Py_INCREF(Py_None);
    return Py_None;
}

static PyObject *
img_parasite_detach(PyGimpImage *self, PyObject *args)
{
    char *name;
708

709 710
    if (!PyArg_ParseTuple(args, "s:parasite_detach", &name))
	return NULL;
711

712
    if (!gimp_image_detach_parasite (self->ID, name)) {
713 714 715 716 717 718 719 720
	PyErr_Format(pygimp_error,
		     "could not detach parasite '%s' from image (ID %d)",
		     name, self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
721 722 723 724 725 726 727
}

static PyObject *
img_parasite_list(PyGimpImage *self)
{
    gint num_parasites;
    gchar **parasites;
728 729
    PyObject *ret;
    gint i;
730

731 732
    parasites = gimp_image_get_parasite_list (self->ID, &num_parasites);

733
    ret = PyTuple_New(num_parasites);
734

735 736
    for (i = 0; i < num_parasites; i++)
        PyTuple_SetItem(ret, i, PyString_FromString(parasites[i]));
737

738 739
    g_strfreev(parasites);
    return ret;
740 741 742 743 744 745
}

static PyObject *
img_get_layer_by_tattoo(PyGimpImage *self, PyObject *args)
{
    int tattoo;
746

747 748
    if (!PyArg_ParseTuple(args, "i:get_layer_by_tattoo", &tattoo))
	return NULL;
749

750
    return pygimp_group_layer_new(gimp_image_get_layer_by_tattoo(self->ID, tattoo));
751 752 753 754 755 756
}

static PyObject *
img_get_channel_by_tattoo(PyGimpImage *self, PyObject *args)
{
    int tattoo;
757

758 759
    if (!PyArg_ParseTuple(args, "i:get_channel_by_tattoo", &tattoo))
	return NULL;
760

761 762 763 764 765 766 767 768
    return pygimp_channel_new(gimp_image_get_channel_by_tattoo(self->ID,
							       tattoo));
}

static PyObject *
img_add_hguide(PyGimpImage *self, PyObject *args)
{
    int ypos;
769

770 771
    if (!PyArg_ParseTuple(args, "i:add_hguide", &ypos))
	return NULL;
772

773
    return PyInt_FromLong(gimp_image_add_hguide(self->ID, ypos));
774 775 776 777 778 779
}

static PyObject *
img_add_vguide(PyGimpImage *self, PyObject *args)
{
    int xpos;
780

781 782
    if (!PyArg_ParseTuple(args, "i:add_vguide", &xpos))
	return NULL;
783

784
    return PyInt_FromLong(gimp_image_add_vguide(self->ID, xpos));
785 786 787 788 789 790
}

static PyObject *
img_delete_guide(PyGimpImage *self, PyObject *args)
{
    int guide;
791

792 793
    if (!PyArg_ParseTuple(args, "i:delete_guide", &guide))
	return NULL;
794 795 796 797

    if (!gimp_image_delete_guide(self->ID, guide)) {
	PyErr_Format(pygimp_error,
		     "could not delete guide (ID %d) from image (ID %d)",
798
		     guide, self->ID);
799 800 801 802 803
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
804 805 806 807 808 809
}

static PyObject *
img_find_next_guide(PyGimpImage *self, PyObject *args)
{
    int guide;
810

811 812
    if (!PyArg_ParseTuple(args, "i:find_next_guide", &guide))
	return NULL;
813

814 815 816 817 818 819 820
    return PyInt_FromLong(gimp_image_find_next_guide(self->ID, guide));
}

static PyObject *
img_get_guide_orientation(PyGimpImage *self, PyObject *args)
{
    int guide;
821

822 823
    if (!PyArg_ParseTuple(args, "i:get_guide_orientation", &guide))
	return NULL;
824

825 826 827 828 829 830 831
    return PyInt_FromLong(gimp_image_get_guide_orientation(self->ID, guide));
}

static PyObject *
img_get_guide_position(PyGimpImage *self, PyObject *args)
{
    int guide;
832

833 834
    if (!PyArg_ParseTuple(args, "i:get_guide_position", &guide))
	return NULL;
835

836 837 838
    return PyInt_FromLong(gimp_image_get_guide_position(self->ID, guide));
}

839 840 841
static PyObject *
img_undo_is_enabled(PyGimpImage *self)
{
842
    return PyBool_FromLong(gimp_image_undo_is_enabled(self->ID));
843 844 845 846 847
}

static PyObject *
img_undo_freeze(PyGimpImage *self)
{
848
    return PyBool_FromLong(gimp_image_undo_freeze(self->ID));
849 850 851 852 853
}

static PyObject *
img_undo_thaw(PyGimpImage *self)
{
854
    return PyBool_FromLong(gimp_image_undo_thaw(self->ID));
855 856 857 858 859 860 861 862
}

static PyObject *
img_duplicate(PyGimpImage *self)
{
    return pygimp_image_new(gimp_image_duplicate(self->ID));
}

863 864 865
static PyObject *
img_undo_group_start(PyGimpImage *self)
{
866 867 868 869 870 871 872 873 874
    if (!gimp_image_undo_group_start(self->ID)) {
	PyErr_Format(pygimp_error,
		     "could not start undo group on image (ID %d)",
		     self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
875 876 877 878 879
}

static PyObject *
img_undo_group_end(PyGimpImage *self)
{
880 881 882 883 884 885 886 887 888
    if (!gimp_image_undo_group_end(self->ID)) {
	PyErr_Format(pygimp_error,
		     "could not end undo group on image (ID %d)",
		     self->ID);
	return NULL;
    }

    Py_INCREF(Py_None);
    return Py_None;
889 890
}

891
static PyMethodDef img_methods[] = {
892
    {"add_channel",	(PyCFunction)img_add_channel,	METH_VARARGS},
893
    {"insert_channel",	(PyCFunction)img_insert_channel,	METH_VARARGS | METH_KEYWORDS},
894
    {"add_layer",	(PyCFunction)img_add_layer,	METH_VARARGS},
895
    {"insert_layer",	(PyCFunction)img_insert_layer,	METH_VARARGS | METH_KEYWORDS},
896
    {"new_layer",       (PyCFunction)img_new_layer, METH_VARARGS | METH_KEYWORDS},
897 898 899 900
    {"clean_all",	(PyCFunction)img_clean_all,	METH_NOARGS},
    {"disable_undo",	(PyCFunction)img_disable_undo,	METH_NOARGS},
    {"enable_undo",	(PyCFunction)img_enable_undo,	METH_NOARGS},
    {"flatten",	(PyCFunction)img_flatten,	METH_NOARGS},
901 902
    {"lower_channel",	(PyCFunction)img_lower_channel,	METH_VARARGS},
    {"lower_layer",	(PyCFunction)img_lower_layer,	METH_VARARGS},
903
    {"lower_layer_to_bottom",	(PyCFunction)img_lower_layer_to_bottom,	METH_VARARGS},
904
    {"merge_visible_layers",	(PyCFunction)img_merge_visible_layers,	METH_VARARGS},
905
    {"merge_down",	(PyCFunction)img_merge_down,	METH_VARARGS},
906 907 908
    {"pick_correlate_layer",	(PyCFunction)img_pick_correlate_layer,	METH_VARARGS},
    {"raise_channel",	(PyCFunction)img_raise_channel,	METH_VARARGS},
    {"raise_layer",	(PyCFunction)img_raise_layer,	METH_VARARGS},
909
    {"raise_layer_to_top",	(PyCFunction)img_raise_layer_to_top,	METH_VARARGS},
910 911
    {"remove_channel",	(PyCFunction)img_remove_channel,	METH_VARARGS},
    {"remove_layer",	(PyCFunction)img_remove_layer,	METH_VARARGS},
912
    {"resize",	(PyCFunction)img_resize,	METH_VARARGS | METH_KEYWORDS},
913
    {"resize_to_layers",	(PyCFunction)img_resize_to_layers,	METH_NOARGS},
914 915 916 917 918 919
    {"get_component_active",	(PyCFunction)img_get_component_active,	METH_VARARGS},
    {"get_component_visible",	(PyCFunction)img_get_component_visible,	METH_VARARGS},
    {"set_component_active",	(PyCFunction)img_set_component_active,	METH_VARARGS},
    {"set_component_visible",	(PyCFunction)img_set_component_visible,	METH_VARARGS},
    {"parasite_find",       (PyCFunction)img_parasite_find,      METH_VARARGS},
    {"parasite_attach",     (PyCFunction)img_parasite_attach,    METH_VARARGS},
920
    {"attach_new_parasite", (PyCFunction)img_attach_new_parasite,	METH_VARARGS | METH_KEYWORDS},
921
    {"parasite_detach",     (PyCFunction)img_parasite_detach,    METH_VARARGS},
922
    {"parasite_list", (PyCFunction)img_parasite_list, METH_NOARGS},
923 924 925 926 927 928 929 930
    {"get_layer_by_tattoo",(PyCFunction)img_get_layer_by_tattoo,METH_VARARGS},
    {"get_channel_by_tattoo",(PyCFunction)img_get_channel_by_tattoo,METH_VARARGS},
    {"add_hguide", (PyCFunction)img_add_hguide, METH_VARARGS},
    {"add_vguide", (PyCFunction)img_add_vguide, METH_VARARGS},
    {"delete_guide", (PyCFunction)img_delete_guide, METH_VARARGS},
    {"find_next_guide", (PyCFunction)img_find_next_guide, METH_VARARGS},
    {"get_guide_orientation",(PyCFunction)img_get_guide_orientation,METH_VARARGS},
    {"get_guide_position", (PyCFunction)img_get_guide_position, METH_VARARGS},
931 932
    {"scale", (PyCFunction)img_scale, METH_VARARGS | METH_KEYWORDS},
    {"crop", (PyCFunction)img_crop, METH_VARARGS | METH_KEYWORDS},
933 934 935 936 937 938
    {"free_shadow", (PyCFunction)img_free_shadow, METH_NOARGS},
    {"unset_active_channel", (PyCFunction)img_unset_active_channel, METH_NOARGS},
    {"undo_is_enabled", (PyCFunction)img_undo_is_enabled, METH_NOARGS},
    {"undo_freeze", (PyCFunction)img_undo_freeze, METH_NOARGS},
    {"undo_thaw", (PyCFunction)img_undo_thaw, METH_NOARGS},
    {"duplicate", (PyCFunction)img_duplicate, METH_NOARGS},
939 940
    {"undo_group_start", (PyCFunction)img_undo_group_start, METH_NOARGS},
    {"undo_group_end", (PyCFunction)img_undo_group_end, METH_NOARGS},
941 942 943
    {NULL,		NULL}		/* sentinel */
};

944 945 946 947 948
static PyObject *
img_get_ID(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(self->ID);
}
949

950 951
static PyObject *
img_get_active_channel(PyGimpImage *self, void *closure)
952
{
953
    gint32 id = gimp_image_get_active_channel(self->ID);
954

955
    if (id == -1) {
956 957 958
	Py_INCREF(Py_None);
	return Py_None;
    }
959

960
    return pygimp_channel_new(id);
961 962
}

963 964
static int
img_set_active_channel(PyGimpImage *self, PyObject *value, void *closure)
965
{
966 967
    PyGimpChannel *chn;

968
    if (value == NULL) {
969
	PyErr_SetString(PyExc_TypeError, "cannot delete active_channel");
970 971
	return -1;
    }
972

973
    if (!pygimp_channel_check(value)) {
974
	PyErr_SetString(PyExc_TypeError, "type mismatch");
975 976
	return -1;
    }
977 978 979 980 981 982 983 984 985 986

    chn = (PyGimpChannel *)value;

    if (!gimp_image_set_active_channel(self->ID, chn->ID)) {
	PyErr_Format(pygimp_error,
		     "could not set active channel (ID %d) on image (ID %d)",
		     chn->ID, self->ID);
        return -1;
    }

987
    return 0;
988 989
}

990 991 992
static PyObject *
img_get_active_drawable(PyGimpImage *self, void *closure)
{
993
    gint32 id = gimp_image_get_active_drawable(self->ID);
994 995 996 997 998

    if (id == -1) {
	Py_INCREF(Py_None);
	return Py_None;
    }
999

1000 1001 1002
    return pygimp_drawable_new(NULL, id);
}

1003
static PyObject *
1004
img_get_active_layer(PyGimpImage *self, void *closure)
1005
{
1006 1007 1008 1009 1010
    gint32 id = gimp_image_get_active_layer(self->ID);

    if (id == -1) {
	Py_INCREF(Py_None);
	return Py_None;
1011
    }
1012

1013
    return pygimp_group_layer_new(id);
1014 1015 1016 1017 1018
}

static int
img_set_active_layer(PyGimpImage *self, PyObject *value, void *closure)
{
1019 1020
    PyGimpLayer *lay;

1021
    if (value == NULL) {
1022
	PyErr_SetString(PyExc_TypeError, "cannot delete active_layer");
1023
	return -1;
1024
    }
1025

1026
    if (!pygimp_layer_check(value)) {
1027
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1028
	return -1;
1029
    }
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039

    lay = (PyGimpLayer *)value;

    if (!gimp_image_set_active_layer(self->ID, lay->ID)) {
	PyErr_Format(pygimp_error,
		     "could not set active layer (ID %d) on image (ID %d)",
		     lay->ID, self->ID);
        return -1;
    }

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    return 0;
}

static PyObject *
img_get_base_type(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_base_type(self->ID));
}

static PyObject *
img_get_channels(PyGimpImage *self, void *closure)
{
    gint32 *channels;
    gint n_channels, i;
    PyObject *ret;
1055

1056
    channels = gimp_image_get_channels(self->ID, &n_channels);
1057

1058
    ret = PyList_New(n_channels);
1059

1060 1061
    for (i = 0; i < n_channels; i++)
	PyList_SetItem(ret, i, pygimp_channel_new(channels[i]));
1062

1063
    g_free(channels);
1064

1065 1066 1067 1068
    return ret;
}

static PyObject *
1069
img_get_colormap(PyGimpImage *self, void *closure)
1070 1071 1072 1073
{
    guchar *cmap;
    gint n_colours;
    PyObject *ret;
1074

1075
    cmap = gimp_image_get_colormap(self->ID, &n_colours);
1076 1077 1078 1079 1080 1081

    if (cmap == NULL) {
	PyErr_Format(pygimp_error, "could not get colormap for image (ID %d)",
		     self->ID);
	return NULL;
    }
1082

1083
    ret = PyString_FromStringAndSize((char *)cmap, n_colours * 3);
1084
    g_free(cmap);
1085

1086 1087 1088 1089
    return ret;
}

static int
1090
img_set_colormap(PyGimpImage *self, PyObject *value, void *closure)
1091 1092
{
    if (value == NULL) {
1093
	PyErr_SetString(PyExc_TypeError, "cannot delete colormap");
1094
	return -1;
1095
    }
1096

1097
    if (!PyString_Check(value)) {
1098
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1099
	return -1;
1100
    }
1101

1102
    if (!gimp_image_set_colormap(self->ID, (guchar *)PyString_AsString(value),
1103 1104 1105 1106 1107 1108
                                 PyString_Size(value) / 3)) {
	PyErr_Format(pygimp_error, "could not set colormap on image (ID %d)",
		     self->ID);
        return -1;
    }

1109 1110
    return 0;
}
1111

1112 1113 1114 1115 1116 1117
static PyObject *
img_get_is_dirty(PyGimpImage *self, void *closure)
{
    return PyBool_FromLong(gimp_image_is_dirty(self->ID));
}

1118 1119 1120 1121 1122 1123
static PyObject *
img_get_filename(PyGimpImage *self, void *closure)
{
    gchar *filename;

    filename = gimp_image_get_filename(self->ID);
1124

1125 1126 1127
    if (filename) {
	PyObject *ret = PyString_FromString(filename);
	g_free(filename);
1128 1129
	return ret;
    }
1130

1131 1132
    Py_INCREF(Py_None);
    return Py_None;
1133 1134 1135
}

static int
1136
img_set_filename(PyGimpImage *self, PyObject *value, void *closure)
1137
{
1138
    if (value == NULL) {
1139
	PyErr_SetString(PyExc_TypeError, "cannot delete filename");
1140 1141
	return -1;
    }
1142

1143
    if (!PyString_Check(value)) {
1144
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1145
	return -1;
1146
    }
1147 1148 1149 1150 1151 1152 1153

    if (!gimp_image_set_filename(self->ID, PyString_AsString(value))) {
        PyErr_SetString(PyExc_TypeError, "could not set filename "
	                                 "(possibly bad encoding)");
        return -1;
    }

1154 1155 1156
    return 0;
}

1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
static PyObject *
img_get_uri(PyGimpImage *self, void *closure)
{
    gchar *uri;

    uri = gimp_image_get_uri(self->ID);

    if (uri) {
	PyObject *ret = PyString_FromString(uri);
	g_free(uri);
	return ret;
    }

    Py_INCREF(Py_None);
    return Py_None;
}

1174 1175 1176 1177 1178
static PyObject *
img_get_floating_selection(PyGimpImage *self, void *closure)
{
    gint32 id;

1179
    id = gimp_image_get_floating_sel(self->ID);
1180

1181 1182 1183
    if (id == -1) {
	Py_INCREF(Py_None);
	return Py_None;
1184
    }
1185

1186 1187 1188
    return pygimp_layer_new(id);
}

1189 1190 1191 1192 1193 1194
static PyObject *
img_get_floating_sel_attached_to(PyGimpImage *self, void *closure)
{
    gint32 id;

    id = gimp_image_floating_sel_attached_to(self->ID);
1195

1196 1197 1198 1199
    if (id == -1) {
	Py_INCREF(Py_None);
	return Py_None;
    }
1200

1201 1202 1203
    return pygimp_layer_new(id);
}

1204 1205 1206 1207 1208 1209
static PyObject *
img_get_layers(PyGimpImage *self, void *closure)
{
    gint32 *layers;
    gint n_layers, i;
    PyObject *ret;
1210

1211
    layers = gimp_image_get_layers(self->ID, &n_layers);
1212

1213
    ret = PyList_New(n_layers);
1214

1215
    for (i = 0; i < n_layers; i++)
1216
	PyList_SetItem(ret, i, pygimp_group_layer_new(layers[i]));
1217

1218
    g_free(layers);
1219

1220 1221 1222
    return ret;
}

1223 1224 1225 1226 1227 1228
static PyObject *
img_get_name(PyGimpImage *self, void *closure)
{
    gchar *name;

    name = gimp_image_get_name(self->ID);
1229

1230 1231 1232 1233 1234
    if (name) {
	PyObject *ret = PyString_FromString(name);
	g_free(name);
	return ret;
    }
1235

1236 1237 1238 1239
    Py_INCREF(Py_None);
    return Py_None;
}

1240 1241 1242 1243 1244 1245
static PyObject *
img_get_selection(PyGimpImage *self, void *closure)
{
    return pygimp_channel_new(gimp_image_get_selection(self->ID));
}

1246 1247 1248 1249 1250 1251 1252 1253 1254 1255
static PyObject *
img_get_tattoo_state(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_get_tattoo_state(self->ID));
}

static int
img_set_tattoo_state(PyGimpImage *self, PyObject *value, void *closure)
{
    if (value == NULL) {
1256
	PyErr_SetString(PyExc_TypeError, "cannot delete tattoo_state");
1257 1258
	return -1;
    }
1259

1260
    if (!PyInt_Check(value)) {
1261
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1262 1263
	return -1;
    }
1264

1265
    gimp_image_set_tattoo_state(self->ID, PyInt_AsLong(value));
1266

1267 1268 1269
    return 0;
}

1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
static PyObject *
img_get_height(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_height(self->ID));
}

static PyObject *
img_get_width(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_width(self->ID));
}

1282 1283 1284 1285 1286 1287 1288

static PyObject *
img_get_precision(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_get_precision(self->ID));
}

1289 1290 1291 1292 1293 1294
static PyObject *
img_get_resolution(PyGimpImage *self, void *closure)
{
    double xres, yres;

    gimp_image_get_resolution(self->ID, &xres, &yres);
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304
    return Py_BuildValue("(dd)", xres, yres);
}

static int
img_set_resolution(PyGimpImage *self, PyObject *value, void *closure)
{
    gdouble xres, yres;

    if (value == NULL) {
1305
	PyErr_SetString(PyExc_TypeError, "cannot delete resolution");
1306
	return -1;
1307
    }
1308

1309 1310 1311
    if (!PySequence_Check(value) ||
	!PyArg_ParseTuple(value, "dd", &xres, &yres)) {
	PyErr_Clear();
1312
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1313
	return -1;
1314
    }
1315 1316 1317 1318 1319 1320

    if (!gimp_image_set_resolution(self->ID, xres, yres)) {
        PyErr_SetString(PyExc_TypeError, "could not set resolution");
        return -1;
    }

1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333
    return 0;
}

static PyObject *
img_get_unit(PyGimpImage *self, void *closure)
{
    return PyInt_FromLong(gimp_image_get_unit(self->ID));
}

static int
img_set_unit(PyGimpImage *self, PyObject *value, void *closure)
{
    if (value == NULL) {
1334
	PyErr_SetString(PyExc_TypeError, "cannot delete unit");
1335
	return -1;
1336
    }
1337

1338
    if (!PyInt_Check(value)) {
1339
	PyErr_SetString(PyExc_TypeError, "type mismatch");
1340 1341
	return -1;
    }
1342 1343 1344 1345 1346 1347

    if (!gimp_image_set_unit(self->ID, PyInt_AsLong(value))) {
        PyErr_SetString(PyExc_TypeError, "could not set unit");
        return -1;
    }

1348 1349 1350
    return 0;
}

1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
static PyObject *
img_get_vectors(PyGimpImage *self, void *closure)
{
    int *vectors;
    int i, num_vectors;
    PyObject *ret;

    vectors = gimp_image_get_vectors(self->ID, &num_vectors);

    ret = PyList_New(num_vectors);

    for (i = 0; i < num_vectors; i++)
        PyList_SetItem(ret, i, pygimp_vectors_new(vectors[i]));

    g_free(vectors);

    return ret;
}

1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409
static PyObject *
img_get_active_vectors(PyGimpImage *self, void *closure)
{
    gint32 id = gimp_image_get_active_vectors(self->ID);

    if (id == -1) {
        Py_INCREF(Py_None);
        return Py_None;
    }

    return pygimp_vectors_new(id);
}

static int
img_set_active_vectors(PyGimpImage *self, PyObject *value, void *closure)
{
    PyGimpVectors *vtr;

    if (value == NULL) {
        PyErr_SetString(PyExc_TypeError, "cannot delete active_vectors");
        return -1;
    }

    if (!pygimp_vectors_check(value)) {
        PyErr_SetString(PyExc_TypeError, "type mismatch");
        return -1;
    }

    vtr = (PyGimpVectors *)value;

    if (!gimp_image_set_active_vectors(self->ID, vtr->ID)) {
        PyErr_Format(pygimp_error,
                     "could not set active vectors (ID %d) on image (ID %d)",
                     vtr->ID, self->ID);
        return -1;
    }

    return 0;
}

James Henstridge's avatar