source: libpipi/trunk/pipi/stock.c @ 2709

Last change on this file since 2709 was 2707, checked in by Sam Hocevar, 15 years ago
  • stock.c: allow arbitrary sizes for the bayer stock image, even if they give shitty results when used for ordered dithering.
File size: 2.6 KB
Line 
1/*
2 *  libpipi       Proper image processing implementation library
3 *  Copyright (c) 2004-2008 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id$
7 *
8 *  This library is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 * stock.c: stock images
17 */
18
19#include "config.h"
20#include "common.h"
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include "pipi.h"
27#include "pipi_internals.h"
28
29pipi_image_t *pipi_load_stock(char const *name)
30{
31    pipi_image_t *ret;
32    pipi_pixels_t *pix;
33    float *data;
34
35    /* Generate a Bayer dithering pattern. */
36    if(!strncmp(name, "bayer", 5))
37    {
38        int i, j, w, h, n;
39
40        w = atoi(name + 5);
41        name = strchr(name + 5, 'x');
42        if(!name)
43            return NULL;
44        h = atoi(name + 1);
45        if(w <= 0 || h <= 0)
46            return NULL;
47
48        for(n = 1; n < w || n < h; n *= 2)
49            ;
50
51        ret = pipi_new(w, h);
52        pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
53        data = (float *)pix->pixels;
54
55        for(j = 0; j < h; j++)
56            for(i = 0; i < w; i++)
57            {
58                int k, l, x = 0;
59
60                for(k = 1, l = n * n / 4; k < n; k *= 2, l /= 4)
61                {
62                    if((i & k) && (j & k))
63                        x += l;
64                    else if(i & k)
65                        x += 3 * l;
66                    else if(j & k)
67                        x += 2 * l;
68                }
69
70                data[j * w + i] = (double)(x + 1) / (n * n + 1);
71            }
72
73        return ret;
74    }
75
76    /* Generate a completely random image. */
77    if(!strncmp(name, "random", 6))
78    {
79        unsigned int ctx = 1;
80        int x, y, w, h;
81
82        w = atoi(name + 6);
83        name = strchr(name + 6, 'x');
84        if(!name)
85            return NULL;
86        h = atoi(name + 1);
87        if(w <= 0 || h <= 0)
88            return NULL;
89
90        ret = pipi_new(w, h);
91        pix = pipi_getpixels(ret, PIPI_PIXELS_Y_F);
92        data = (float *)pix->pixels;
93
94        for(y = 0; y < h; y++)
95            for(x = 0; x < w; x++)
96            {
97                long hi, lo;
98
99                hi = ctx / 12773L;
100                lo = ctx % 12773L;
101                ctx = 16807L * lo - 2836L * hi;
102                if(ctx <= 0)
103                    ctx += 0x7fffffffL;
104
105                data[y * w + x] = (double)((ctx % 65536) / 65535.);
106            }
107
108        return ret;
109    }
110
111    return NULL;
112}
113
Note: See TracBrowser for help on using the repository browser.