static Array HHVM_METHOD(ImagickPixel, getHSL) { auto wand = getPixelWandResource(this_); double hue, saturation, luminosity; PixelGetHSL(wand->getWand(), &hue, &saturation, &luminosity); return make_map_array( s_hue, hue, s_saturation, saturation, s_luminosity, luminosity); }
/* {{{ proto array ImagickPixel::getHSL() Returns the normalized HSL color of the pixel wand in an array with the keys "hue", "saturation", and "luminosity". */ PHP_METHOD(imagickpixel, gethsl) { php_imagickpixel_object *internp; double hue, saturation, luminosity; if (zend_parse_parameters_none() == FAILURE) { return; } internp = Z_IMAGICKPIXEL_P(getThis()); PixelGetHSL(internp->pixel_wand, &hue, &saturation, &luminosity); array_init(return_value); add_assoc_double(return_value, "hue", hue); add_assoc_double(return_value, "saturation", saturation); add_assoc_double(return_value, "luminosity", luminosity); return; }
/* {{{ proto array ImagickPixel::getHSL() Returns the normalized HSL color of the pixel wand in an array with the keys "hue", "saturation", and "luminosity". */ PHP_METHOD(imagickpixel, gethsl) { php_imagickpixel_object *internp; double hue, saturation, luminosity; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) { return; } internp = (php_imagickpixel_object *)zend_object_store_get_object(getThis() TSRMLS_CC); PixelGetHSL(internp->pixel_wand, &hue, &saturation, &luminosity); array_init(return_value); add_assoc_double(return_value, "hue", hue); add_assoc_double(return_value, "saturation", saturation); add_assoc_double(return_value, "luminosity", luminosity); return; }
/** Returns the kurtosis and lightness value for an image file. @param[in] path Absolute path of the image file. @param[out] kurtosis The kurtosis value. @param[out] lightness The lightness value. @return Retuns 0 on success, -1 on failure. */ int get_image_info(const char *path, double *kurtosis, double *lightness) { MagickBooleanType status; MagickWand *magick_wand; PixelWand *pixel_wand; double hue, saturation, skewness; MagickWandGenesis(); magick_wand = NewMagickWand(); pixel_wand = NewPixelWand(); // Read the image status = MagickReadImage(magick_wand, path); if (status == MagickFalse) return -1; // Get the kurtosis value MagickGetImageChannelKurtosis(magick_wand, DefaultChannels, kurtosis, &skewness); // Resize the image to 1x1 pixel (results in average color) MagickResizeImage(magick_wand, 1, 1, LanczosFilter, 1); // Get pixel color status = MagickGetImagePixelColor(magick_wand, 0, 0, pixel_wand); if (status == MagickFalse) { return -1; } // Get the lightness value PixelGetHSL(pixel_wand, &hue, &saturation, lightness); pixel_wand = DestroyPixelWand(pixel_wand); magick_wand = DestroyMagickWand(magick_wand); MagickWandTerminus(); return 0; }
int main(int argc, char* argv[]) { if (argc < 3) { fprintf(stderr, "Usage: %s IN_FILE OUT_FILE", argv[0]); exit(1); } MagickWand* m_wand; // Initialize MagickWandGenesis(); m_wand = NewMagickWand(); // read image if (MagickReadImage(m_wand, argv[1]) == MagickFalse) { fprintf(stderr, "Cannot read image: %s\n", argv[1]); exit(1); } // resize MagickResizeImage(m_wand, WIDTH, HEIGHT, LanczosFilter, 1.0); // ready for using AAlib aa_context* c; aa_savedata save_data = { argv[2], &aa_text_format, NULL }; // Initialize AAlib c = aa_init(&save_d, &aa_defparams, (const void*) &save_data); if (c == NULL) { fprintf(stderr, "Cannot initialize AA-lib\n"); exit(1); } // record image data to AAlib image buffer PixelIterator* iter = NewPixelIterator(m_wand); PixelWand** pix; unsigned long num_wands; double h, s, l; int x, y; y = 0; while ((pix = PixelGetNextIteratorRow(iter, &num_wands)) != NULL) { for (x = 0; x < num_wands; ++x) { PixelGetHSL(pix[x], &h, &s, &l); aa_putpixel(c, x, y, 256*l); } y++; } // rendering ascii and print file aa_fastrender(c, 0, 0, aa_scrwidth(c), aa_scrheight(c)); aa_flush(c); // terminate aa_close(c); // finalize to finish if (m_wand) { m_wand = DestroyMagickWand(m_wand); } MagickWandTerminus(); return 0; }
uint64_t phash_dct(const char *fname) { uint64_t retval = 0; extern int errno; MagickWandGenesis(); MagickWand *m_wand = NewMagickWand(); if (MagickReadImage(m_wand, fname) == MagickFalse) { DestroyMagickWand(m_wand); MagickWandTerminus(); errno = EINVAL; return retval; } MagickResizeImage(m_wand, 32, 32, PointFilter, 1.0); /* Intensity map */ double intensity[32][32]; for (int x = 0; x < 32; ++x) { for (int y = 0; y < 32; ++y) { PixelWand *colour = NewPixelWand(); double h, s; MagickGetImagePixelColor(m_wand, x, y, colour); PixelGetHSL(colour, &h, &s, &intensity[x][y]); DestroyPixelWand(colour); } } DestroyMagickWand(m_wand); MagickWandTerminus(); /* Discrete cosine transform */ double seq[64]; unsigned seq_i = 0; for (int u = 0; u < 8; ++u) { for (int v = 0; v < 8; ++v) { double acc = 0.0; for (int x = 0; x < 32; ++x) { for (int y = 0; y < 32; ++y) { acc += intensity[x][y] * cos(M_PI / 32.0 * (x + .5) * u) * cos(M_PI / 32.0 * (y + .5) * v); } } seq[seq_i++] = acc; } } double avg = 0.0; for (seq_i = 1; seq_i < 63; ++seq_i) avg += seq[seq_i]; avg /= 63; for (seq_i = 0; seq_i < 64; ++seq_i) { uint64_t x = seq[seq_i] > avg; retval |= x << seq_i; } return retval; }