# ahd_bayer.c File Reference

Adaptive Homogeneity-Directed Bayer array conversion routine. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <math.h>`
`#include <time.h>`
`#include "config.h"`
`#include "bayer.h"`
`#include <gphoto2/gphoto2-result.h>`
`#include <gphoto2/gphoto2-port-log.h>`

## Defines

#define | GP_MODULE "ahd_bayer"
#define | MAX(x, y) ((x < y) ? (y) : (x))
#define | MIN(x, y) ((x > y) ? (y) : (x))
#define | CLAMP(x) MAX(MIN(x,0xff),0)
#define | RED 0
#define | GREEN 1
#define | BLUE 2
#define | AD(x, y, w) ((y)*(w)*3+3*(x))

## Functions

int | gp_ahd_interpolate (unsigned char *image, int w, int h, BayerTile tile)
| Interpolate a expanded bayer array into an RGB image.
int | gp_ahd_decode (unsigned char *input, int w, int h, unsigned char *output, BayerTile tile)
| Convert a bayer raster style image to a RGB raster.

## Detailed Description

Adaptive Homogeneity-Directed Bayer array conversion routine.

Author:
  Copyright March 12, 2008 Theodore Kilgore <kilgota@auburn.edu>

  gp_ahd_interpolate() from Eero Salminen <esalmine@gmail.com> and Theodore Kilgore. The work of Eero Salminen is for partial completion of a Diploma in Information and Computer Science, Helsinki University of Technology, Finland.

  The algorithm is based upon the paper

  Adaptive Homogeneity-Directed Democsaicing Algoritm, Keigo Hirakawa and Thomas W. Parks, presented in the IEEE Transactions on Image Processing, vol. 14, no. 3, March 2005.

License
  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

## Function Documentation

int gp_ahd_decode | ( | unsigned char * | input,
|  | int | w,
|  | int | h,
|  | unsigned char * | output,
|  | BayerTile | tile |
| ) |  |  |

Convert a bayer raster style image to a RGB raster.

Parameters:

  | input | the bayer CCD array as linear input
  | w | width of the above array
  | h | height of the above array
  | output | RGB output array (linear, 3 bytes of R,G,B for every pixel)
  | tile | how the 2x2 bayer array is layed out

A regular CCD uses a raster of 2 green, 1 blue and 1 red components to cover a 2x2 pixel area. The camera or the driver then interpolates a 2x2 RGB pixel set out of this data.

This function expands and interpolates the bayer array to 3 times larger bitmap with RGB values interpolated. It does the same job as gp_bayer_decode() but it calls gp_ahd_interpolate() instead of calling gp_bayer_interpolate(). Use this instead of gp_bayer_decode() if you want to use or to test AHD interpolation in a camera library.

Returns:
  a gphoto error code

int gp_ahd_interpolate | ( | unsigned char * | image,
|  | int | w,
|  | int | h,
|  | BayerTile | tile |
| ) |  |  |

Interpolate a expanded bayer array into an RGB image.

Parameters:

  | image | the linear RGB array as both input and output
  | w | width of the above array
  | h | height of the above array
  | tile | how the 2x2 bayer array is layed out

This function interpolates a bayer array which has been pre-expanded by gp_bayer_expand() to an RGB image. It applies the method of adaptive homogeneity-directed demosaicing.

Returns:
  a gphoto error code

  In outline, the interpolation algorithm used here does the following:

  In principle, the first thing which is done is to split off from the image two copies. In one of these, interpolation will be done in the vertical direction only, and in the other copy only in the horizontal direction. "Cross-color" data is used throughout, on the principle that it can be used as a corrector for brightness even if it is derived from the "wrong" color. Finally, at each pixel there is a choice criterion to decide whether to use the result of the vertical interpolation, the horizontal interpolation, or an average of the two.

  Memory use and speed are optimized by using two sliding windows, one for the vertical interpolation and the other for the horizontal interpolation instead of using two copies of the entire input image. The nterpolation and the choice algorithm are then implemented entirely within these windows, too. When this has been done, a completed row is written back to the image. Then the windows are moved, and the process repeats.