You are here

filtersie.module in FiltersIE 7

Filters Image Effect (filtersie)

File

filtersie.module
View source
<?php

/**
 * @file
 * Filters Image Effect (filtersie)
 */

/**
 * Include admin form functions.
 */
module_load_include('inc', 'filtersie', 'filtersie.admin');

/**
 * Include gd toolkit implementations.
 */
module_load_include('inc', 'filtersie', 'filtersie.gd');

/**
 * Include imagemagick toolkit implentations.
 */
module_load_include('inc', 'filtersie', 'filtersie.imagemagick');

/**
 * Implements hook_theme().
 */
function filtersie_theme() {
  return array(
    'filtersie_generic_summary' => array(
      'variables' => array(
        'data' => NULL,
      ),
    ),
    'filtersie_sharpen_summary' => array(
      'variables' => array(
        'data' => NULL,
      ),
    ),
    'filtersie_sharpenUSM_summary' => array(
      'variables' => array(
        'data' => NULL,
      ),
    ),
  );
}

/**
 * Implements hook_image_effect_info.
 */
function filtersie_image_effect_info() {
  return array(
    'filtersie_generic' => array(
      'label' => t('Generic convolution filter'),
      'help' => t('Filter an image giving a 3x3 kernel matrix and a divisor parameter.'),
      'effect callback' => 'filtersie_generic_effect',
      'form callback' => 'filtersie_generic_form',
      'summary theme' => 'filtersie_generic_summary',
      'dimensions passthrough' => TRUE,
    ),
    'filtersie_sharpen' => array(
      'label' => t('Sharpen'),
      'help' => t('Sharpen an image giving a parameter.'),
      'effect callback' => 'filtersie_sharpen_effect',
      'form callback' => 'filtersie_sharpen_form',
      'summary theme' => 'filtersie_sharpen_summary',
      'dimensions passthrough' => TRUE,
    ),
    'filtersie_sharpenUSM' => array(
      'label' => t('Sharpen using Unsharp masking (USM) technique'),
      'help' => t('Sharpen using Unsharp masking (USM) technique.'),
      'effect callback' => 'filtersie_sharpenUSM_effect',
      'form callback' => 'filtersie_sharpenUSM_form',
      'summary theme' => 'filtersie_sharpenUSM_summary',
      'dimensions passthrough' => TRUE,
    ),
  );
}

/**
 * Image effect callback; Apply a convolution to an image resource.
 * 
 * @param type $image
 *  An image object returned by image_load().
 * @param type $data
 * An array of attributes to use when performing the sharpen effect with the
 *   following items:
 *   - "matrix": A matrix loaded of float values.
 *   - "divisor": A float.
 * 
 * @return type 
 *   TRUE on success. FALSE on failure to make the convolution.
 * 
 * @see filtersie_sharpen()
 */
function filtersie_generic_effect(&$image, $data) {
  $matrix = $data['matrix']['entries'];
  $divisor = $data['divisor'];
  $offset = $data['offset'];
  if (!filtersie_generic($image, $matrix, $divisor, $offset)) {
    watchdog('filtersie', 'Filters Image Effects generic failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array(
      '%toolkit' => $image->toolkit,
      '%path' => $image->source,
      '%mimetype' => $image->info['mime_type'],
      '%dimensions' => $image->info['height'] . 'x' . $image->info['height'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}

/**
 * Image effect callback; Sharpen an image resource.
 * 
 * @param type $image
 *  An image object returned by image_load().
 * @param type $data
 * An array of attributes to use when performing the sharpen effect with the
 *   following items:
 *   - "sharpenlevel": An integer representing the desired sharpen level.
 * 
 * @return type 
 *   TRUE on success. FALSE on failure to sharpen image.
 * 
 * @see filtersie_sharpen()
 */
function filtersie_sharpen_effect(&$image, $data) {
  if (!filtersie_sharpen($image, $data['sharpenlevel'])) {
    watchdog('filtersie', 'Filters Image Effects sharpen failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array(
      '%toolkit' => $image->toolkit,
      '%path' => $image->source,
      '%mimetype' => $image->info['mime_type'],
      '%dimensions' => $image->info['height'] . 'x' . $image->info['height'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}

/**
 * Image effect callback; Sharpen an image resource using USM.
 * 
 * @param type $image
 *  An image object returned by image_load().
 * @param type $data
 * An array of attributes to use when performing the sharpen effect with the
 *   following items:
 *   - "sharpenlevel": An integer representing the desired sharpen level.
 * 
 * @return type 
 *   TRUE on success. FALSE on failure to sharpen image.
 * 
 * @see filtersie_sharpen()
 */
function filtersie_sharpenUSM_effect(&$image, $data) {
  $data = $data + array(
    'sigma' => NULL,
  );
  if (!filtersie_sharpenUSM($image, $data['amount'], $data['radius'], $data['threshold'], $data['sigma'])) {
    watchdog('filtersie', 'Filters Image Effects sharpen USM technique failed using the %toolkit toolkit on %path (%mimetype, %dimensions)', array(
      '%toolkit' => $image->toolkit,
      '%path' => $image->source,
      '%mimetype' => $image->info['mime_type'],
      '%dimensions' => $image->info['height'] . 'x' . $image->info['height'],
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  return TRUE;
}

/**
 * Apply a convolution to an image given a kernel matrix.
 *
 * @param $image
 *   An image object returned by image_load().
 * @param $matrix
 *   The kernel matrix.
 * @param $divisor
 *   The $matrix divisor.
 *
 * @return
 *   TRUE or FALSE, based on success.
 *
 * @see image_load()
 * @see image_<gd|imagemagick>_filtersie_convolution()
 */
function filtersie_generic($image, $matrix, $divisor, $offset) {
  return image_toolkit_invoke('filtersie_convolution', $image, array(
    $matrix,
    $divisor,
    $offset,
  ));
}

/**
 * Sharpen an image given a sharpen level.
 * It use convolution.
 *
 * @param $image
 *   An image object returned by image_load().
 * @param $sharpenlevel
 *   The sharpenlevel to be applied.
 *
 * @return
 *   TRUE or FALSE, based on success.
 *
 * @see image_load()
 * @see image_gd_filtersie_sharpen()
 */
function filtersie_sharpen($image, $sharpenlevel) {
  $sharpenlevel = $sharpenlevel / 100;
  $matrix = array(
    array(
      -$sharpenlevel,
      -$sharpenlevel,
      -$sharpenlevel,
    ),
    array(
      -$sharpenlevel,
      8 * $sharpenlevel + 1,
      -$sharpenlevel,
    ),
    array(
      -$sharpenlevel,
      -$sharpenlevel,
      -$sharpenlevel,
    ),
  );
  $divisor = 1;
  $offset = 0;
  return image_toolkit_invoke('filtersie_convolution', $image, array(
    $matrix,
    $divisor,
    $offset,
  ));
}

/**
 * Sharpen an image usin USM technique.
 *
 * @param $image
 *   An image object returned by image_load().
 * @param $amount, $radius, $threshold, and $sigma
 *   @see http://en.wikipedia.org/wiki/Unsharp_masking#Digital_unsharp_masking
 *
 * @return
 *   TRUE or FALSE, based on success.
 *
 * @see image_load()
 * @see image_gd_filtersie_sharpen()
 */
function filtersie_sharpenUSM($image, $amount, $radius, $threshold, $sigma) {
  return image_toolkit_invoke('filtersie_UnsharpMask', $image, array(
    $amount,
    $radius,
    $threshold,
    $sigma,
  ));
}

/**
 * Returns HTML for a summary of an filtersie generic effect.
 *
 * @param $variables
 *   An associative array containing:
 *   - data: The current configuration for this resize effect.
 *
 * @ingroup themeable
 */
function theme_filtersie_generic_summary($variables) {
  $data = $variables['data'];
  return ' - ' . check_plain($data['label']);
}

/**
 * Returns HTML for a summary of an filtersie sharpen effect.
 *
 * @param $variables
 *   An associative array containing:
 *   - data: The current configuration for this resize effect.
 *
 * @ingroup themeable
 */
function theme_filtersie_sharpen_summary($variables) {
  $data = $variables['data'];
  return t('level') . ' ' . check_plain($data['sharpenlevel']);
}

/**
 * Returns HTML for a summary of an filtersie sharpen USM effect.
 *
 * @param $variables
 *   An associative array containing:
 *   - data: The current configuration for this resize effect.
 *
 * @ingroup themeable
 */
function theme_filtersie_sharpenUSM_summary($variables) {
  $data = $variables['data'];
  $out = t('amount') . ': ' . check_plain($data['amount']) . ' ' . t('radius') . ': ' . check_plain($data['radius']) . ' ' . t('threshold') . ': ' . check_plain($data['threshold']);
  if (image_get_toolkit() == 'imagemagick') {
    $out .= ' ' . t('sigma') . ': ' . check_plain($data['sigma']);
  }
  return '(' . $out . ')';
}

Functions

Namesort descending Description
filtersie_generic Apply a convolution to an image given a kernel matrix.
filtersie_generic_effect Image effect callback; Apply a convolution to an image resource.
filtersie_image_effect_info Implements hook_image_effect_info.
filtersie_sharpen Sharpen an image given a sharpen level. It use convolution.
filtersie_sharpenUSM Sharpen an image usin USM technique.
filtersie_sharpenUSM_effect Image effect callback; Sharpen an image resource using USM.
filtersie_sharpen_effect Image effect callback; Sharpen an image resource.
filtersie_theme Implements hook_theme().
theme_filtersie_generic_summary Returns HTML for a summary of an filtersie generic effect.
theme_filtersie_sharpenUSM_summary Returns HTML for a summary of an filtersie sharpen USM effect.
theme_filtersie_sharpen_summary Returns HTML for a summary of an filtersie sharpen effect.