-
Notifications
You must be signed in to change notification settings - Fork 368
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Made changes to indentation and declared common functions in common.h
- Loading branch information
1 parent
cbe93d4
commit 8deeac3
Showing
12 changed files
with
139 additions
and
202 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef __BLURHASH_COMMON_H__ | ||
#define __BLURHASH_COMMON_H__ | ||
|
||
#include<math.h> | ||
|
||
#ifndef M_PI | ||
#define M_PI 3.14159265358979323846 | ||
#endif | ||
|
||
static inline int linearTosRGB(float value) { | ||
float v = fmaxf(0, fminf(1, value)); | ||
if(v <= 0.0031308) return v * 12.92 * 255 + 0.5; | ||
else return (1.055 * powf(v, 1 / 2.4) - 0.055) * 255 + 0.5; | ||
} | ||
|
||
static inline float sRGBToLinear(int value) { | ||
float v = (float)value / 255; | ||
if(v <= 0.04045) return v / 12.92; | ||
else return powf((v + 0.055) / 1.055, 2.4); | ||
} | ||
|
||
static inline float signPow(float value, float exp) { | ||
return copysignf(powf(fabsf(value), exp), value); | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
#ifndef __BLURHASH_DECODE_H__ | ||
|
||
#define __BLURHASH_DECODE_H | ||
|
||
#include <math.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
/* | ||
decode : Returns the pixel array of the result image given the blurhash string, | ||
Parameters : | ||
blurhash : A string representing the blurhash to be decoded. | ||
width : Width of the resulting image | ||
height : Height of the resulting image | ||
punch : The factor to improve the contrast, default = 1 | ||
nChannels : Number of channels in the resulting image array, 3 = RGB, 4 = RGBA | ||
Returns : A pointer to memory region where pixels are stored in (H, W, C) format | ||
*/ | ||
uint8_t * decode(const char * blurhash, int width, int height, int punch, int nChannels); | ||
|
||
/* | ||
decodeToArray : Decodes the blurhash and copies the pixels to pixelArray, | ||
This method is suggested if you use an external memory allocator for pixelArray. | ||
pixelArray should be of size : width * height * nChannels | ||
Parameters : | ||
blurhash : A string representing the blurhash to be decoded. | ||
width : Width of the resulting image | ||
height : Height of the resulting image | ||
punch : The factor to improve the contrast, default = 1 | ||
nChannels : Number of channels in the resulting image array, 3 = RGB, 4 = RGBA | ||
pixelArray : Pointer to memory region where pixels needs to be copied. | ||
Returns : int, -1 if error 0 if successful | ||
*/ | ||
int decodeToArray(const char * blurhash, int width, int height, int punch, int nChannels, uint8_t * pixelArray); | ||
|
||
/* | ||
isValidBlurhash : Checks if the Blurhash is valid or not. | ||
Parameters : | ||
blurhash : A string representing the blurhash | ||
Returns : bool (true if it is a valid blurhash, else false) | ||
*/ | ||
bool isValidBlurhash(const char * blurhash); | ||
|
||
/* | ||
freePixelArray : Frees the pixel array | ||
Parameters : | ||
pixelArray : Pixel array pointer which will be freed. | ||
Returns : void (None) | ||
*/ | ||
void freePixelArray(uint8_t * pixelArray); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include "decode.h" | ||
|
||
#define STB_IMAGE_WRITE_IMPLEMENTATION | ||
#include "stb_writer.h" | ||
|
||
int main(int argc, char **argv) { | ||
if(argc < 5) { | ||
fprintf(stderr, "Usage: %s hash width height output_file [punch]\n", argv[0]); | ||
return 1; | ||
} | ||
|
||
int width, height, punch = 1; | ||
char * hash = argv[1]; | ||
width = atoi(argv[2]); | ||
height = atoi(argv[3]); | ||
char * output_file = argv[4]; | ||
|
||
const int nChannels = 4; | ||
|
||
if(argc == 6) | ||
punch = atoi(argv[5]); | ||
|
||
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; | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.