~ubuntu-branches/ubuntu/saucy/python-imaging/saucy-proposed

« back to all changes in this revision

Viewing changes to libImaging/Quant.c

  • Committer: Package Import Robot
  • Author(s): Matthias Klose
  • Date: 2013-03-20 16:44:01 UTC
  • mfrom: (2.1.13 experimental)
  • Revision ID: package-import@ubuntu.com-20130320164401-ptf6m0ttg4zw72az
Tags: 1.1.7+2.0.0-1
Pillow 2.0.0 release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
#include <time.h>
27
27
 
28
28
#include "Quant.h"
 
29
#include "QuantOctree.h"
29
30
 
30
31
#include "QuantDefines.h"
31
32
#include "QuantHash.h"
135
136
static void
136
137
exists_count_func(const HashTable h, const void *key, void **val)
137
138
{
138
 
    (*(int *)val)+=1;
 
139
    *(unsigned long*)val+=1;
139
140
}
140
141
 
141
142
static void
142
143
new_count_func(const HashTable h, const void *key, void **val)
143
144
{
144
 
    (*(int *)val)=1;
 
145
    *(unsigned long*)val=1;
145
146
}
146
147
 
147
148
static void
151
152
               void *newkey,
152
153
               void *newval)
153
154
{
154
 
    *valp = (void *)(((int) *valp) + ((int) newval));
 
155
    *valp = (void *)(((unsigned long) *valp) + ((unsigned long) newval));
155
156
}
156
157
 
157
158
/* %% */
244
245
   Pixel *pixel=(Pixel *)&key;
245
246
   int i;
246
247
   Pixel q;
247
 
   int count=(int) val;
 
248
   int count=(unsigned long) val;
248
249
 
249
250
   PIXEL_SCALE(pixel,&q,d->scale);
250
251
 
1485
1486
    int result;
1486
1487
    unsigned long* newData;
1487
1488
    Imaging imOut;
 
1489
    int withAlpha = 0;
 
1490
    ImagingSectionCookie cookie;
1488
1491
 
1489
1492
    if (!im)
1490
1493
        return ImagingError_ModeError();
1494
1497
        return (Imaging) ImagingError_ValueError("bad number of colors");
1495
1498
 
1496
1499
    if (strcmp(im->mode, "L") != 0 && strcmp(im->mode, "P") != 0 &&
1497
 
        strcmp(im->mode, "RGB"))
 
1500
        strcmp(im->mode, "RGB") != 0 && strcmp(im->mode, "RGBA") !=0)
1498
1501
        return ImagingError_ModeError();
1499
1502
 
 
1503
    /* only octree supports RGBA */
 
1504
    if (!strcmp(im->mode, "RGBA") && mode != 2)
 
1505
       return ImagingError_ModeError();
 
1506
 
1500
1507
    p = malloc(sizeof(Pixel) * im->xsize * im->ysize);
1501
1508
    if (!p)
1502
1509
        return ImagingError_MemoryError();
1529
1536
                p[i].c.b = pp[v*4+2];
1530
1537
            }
1531
1538
 
1532
 
    } else if (!strcmp(im->mode, "RGB")) {
 
1539
    } else if (!strcmp(im->mode, "RGB") || !strcmp(im->mode, "RGBA")) {
1533
1540
        /* true colour */
1534
1541
 
1535
1542
        for (i = y = 0; y < im->ysize; y++)
1541
1548
        return (Imaging) ImagingError_ValueError("internal error");
1542
1549
    }
1543
1550
 
 
1551
    ImagingSectionEnter(&cookie);
 
1552
 
1544
1553
    switch (mode) {
1545
1554
    case 0:
1546
1555
        /* median cut */
1566
1575
            kmeans
1567
1576
            );
1568
1577
        break;
 
1578
    case 2:
 
1579
        if (!strcmp(im->mode, "RGBA")) {
 
1580
            withAlpha = 1; 
 
1581
        }
 
1582
        result = quantize_octree(
 
1583
            p,
 
1584
            im->xsize*im->ysize,
 
1585
            colors,
 
1586
            &palette,
 
1587
            &paletteLength,
 
1588
            &newData,
 
1589
            withAlpha
 
1590
            );
 
1591
        break;
1569
1592
    default:
1570
1593
        result = 0;
1571
1594
        break;
1572
1595
    }
1573
1596
 
1574
1597
    free(p);
 
1598
    ImagingSectionLeave(&cookie);
1575
1599
 
1576
1600
    if (result) {
1577
 
 
1578
1601
        imOut = ImagingNew("P", im->xsize, im->ysize);
 
1602
        ImagingSectionEnter(&cookie);
1579
1603
 
1580
1604
        for (i = y = 0; y < im->ysize; y++)
1581
1605
            for (x=0; x < im->xsize; x++)
1589
1613
            *pp++ = palette[i].c.r;
1590
1614
            *pp++ = palette[i].c.g;
1591
1615
            *pp++ = palette[i].c.b;
1592
 
            *pp++ = 255;
 
1616
            if (withAlpha) {
 
1617
               *pp++ = palette[i].c.a;
 
1618
            } else {
 
1619
               *pp++ = 255;
 
1620
            }
1593
1621
        }
1594
1622
        for (; i < 256; i++) {
1595
1623
            *pp++ = 0;
1598
1626
            *pp++ = 255;
1599
1627
        }
1600
1628
 
 
1629
        if (withAlpha) {
 
1630
            strcpy(imOut->palette->mode, "RGBA");
 
1631
        }
 
1632
 
1601
1633
        free(palette);
 
1634
        ImagingSectionLeave(&cookie);
1602
1635
 
1603
1636
        return imOut;
1604
1637