Search code examples
iosobjective-caccelerate-frameworkvimage

How to compile vImage emboss effect sample code?


Here is the code found in the documentation:

int myEmboss(void *inData,
unsigned int inRowBytes,
void *outData,
unsigned int outRowBytes,
unsigned int height,
unsigned int width,
void *kernel,
unsigned int kernel_height,
unsigned int kernel_width,
int divisor ,
vImage_Flags flags ) {
   uint_8 kernel = {-2, -2, 0, -2, 6, 0, 0, 0, 0}; // 1
   vImage_Buffer src = { inData, height, width, inRowBytes }; // 2
   vImage_Buffer dest = { outData, height, width, outRowBytes }; // 3
   unsigned char bgColor[4] = { 0, 0, 0, 0 }; // 4
   vImage_Error err; // 5
   err = vImageConvolve_ARGB8888(    &src,     //const vImage_Buffer *src
                                     &dest,    //const vImage_Buffer *dest,
                                      NULL,
                                      0,    //unsigned int srcOffsetToROI_X,
                                      0,    //unsigned int srcOffsetToROI_Y,
                                      kernel,    //const signed int *kernel,
                                      kernel_height,     //unsigned int
                                      kernel_width,    //unsigned int
                                      divisor,    //int
                                      bgColor,
                                      flags | kvImageBackgroundColorFill
                                      //vImage_Flags flags
                                    );


   return err;
}

Here is the problem: the kernel variable seems to refer to three different types:

  1. void * kernel in the formal parameter list
  2. an undefined unsigned int uint_8 kernel, as a new variable which presumably would shadow the formal parameter
  3. a const signed int *kernel when calling vImageConvolve_ARGB8888.

Is this actual code ? How may I compile this function ?


Solution

  • You are correct that that function is pretty messed up. I recommend using the Provide Feedback widget to let Apple know.

    I think you should remove the kernel, kernel_width, and kernel_height parameters from the function signature. Those seem to be holdovers from a function that applies a caller-supplied kernel, but this example is about applying an internally-defined kernel.

    Fixed the declaration of the kernel local variable to make it an array of uint8_t, like so:

        uint8_t kernel[] = {-2, -2, 0, -2, 6, 0, 0, 0, 0}; // 1
    

    Then, at the call to vImageConvolve_ARGB8888(), replace kernel_width and kernel_height by 3. Since the kernel is hard-coded, the dimensions can be as well.