Module: DECODER

Included in:
BlurhashRuby
Defined in:
ext/blurhash_decoder/blurhash_decoder.c

Instance Method Summary collapse

Instance Method Details

#decode(path, blurhash, heigh, widt, punc) ⇒ Object

Ruby Module



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'ext/blurhash_decoder/blurhash_decoder.c', line 9

VALUE method_decode(VALUE self, VALUE path, VALUE blurhash, VALUE heigh, VALUE widt, VALUE punc) {
    const char * hash = StringValuePtr(blurhash);
    int height = NUM2INT(heigh);
    int width = NUM2INT(widt);
    int punch = NUM2INT(punc);

    const char * output_file = StringValuePtr(path);

    const int nChannels = 4;

    uint8_t * bytes = decode(hash, width, height, punch, nChannels);
    if (!bytes) {
		fprintf(stderr, "%s is not a valid blurhash, decoding failed.\n", hash);
		return 1;
	}

	if (stbi_write_png(output_file, width, height, nChannels, bytes, nChannels * width) == 0) {
		fprintf(stderr, "Failed to write PNG file %s\n", output_file);
		return 1;
	}
    freePixelArray(bytes);
    fprintf(stdout, "Decoded blurhash successfully, wrote PNG file %s\n", output_file);
    return 0;
}