Recent Posts

How to adapt your AE plugin for 8, 16, 32bit color

Hello. In previous article “First After Effects Effects-Plug-In Development“, I implemented a simple After Effects plugin. This article is supplement to it.

What is color (bit) depth

Colors are represented as numbers in computers. In RGB color system, color is represented (R, G, B) such as (100, 120, 144) or (0.5, 0.2, 0.7). In gray scale mode, color is represented as one value such as a integer form 0 to 255 or float from 0 to 1.0.

Color bit depth is resolution of each values. When the color is 8 bpc(bit per channel) each channel, R, G, B and A is expressed using 8bit. When the color is 8bpp(bit per pixel), each pixel color is expressed using 8bit, 3bit for R and G and 2 bit for B.

The following is the image of the difference between 4bit color (4bpc) and 8bit color (8bpc).

Difference 4bpc and 8bpc

Color bit depth in After Effects

In After Effects, you can use 8-bpc, 16-bpc, or 32-bpc color. (check “Color depth and high dynamic range color” for details)

8bit: 0-255 for each channel (256 gradation)
16bit: 0-32767 for each channel (32,768 gradation)
32bit: The range represented by a 32-bit float for each channel (4,294,967,296 gradation)

The color is represented by integer in 8bpc and 16bpc color. Float value is clamped into [0.0 – 1.0] and convert to [0-255] or [0-32767] in 8bpc and 16bpc.

Color bit depth in AE plug-in development

When you develop After Effects plug-in, you should consider all color 8/16/32bit.

8bit

8ARGB color is stored using PF_Pixel8

//this is from AE_Effect.h

// Basic pixel defn's
typedef struct {
	A_u_char	alpha, red, green, blue;
} PF_Pixel;

typedef PF_Pixel		PF_Pixel8;
typedef PF_Pixel		PF_UnionablePixel;

16bit

//this is from AE_Effect.h

typedef struct {
	#ifdef PF_PIXEL16_RENAME_COMPONENTS
		// this style is useful for debugging code converted from 8 bit
		A_u_short		alphaSu, redSu, greenSu, blueSu;
	#else
		A_u_short		alpha, red, green, blue;
	#endif
} PF_Pixel16;

32bit

32bit color is supported in CS6 (11.0) and more.

//this is from AE_Effect.h

typedef A_FpShort			PF_FpShort;
typedef A_FpLong			PF_FpLong;
typedef struct {
	PF_FpShort				alpha, red, green, blue;
} PF_PixelFloat, PF_Pixel32;

And as you can see below, each channel is char type (8bit) in PF_Pixel or PF_Pixel8, unsigned short type (16bit) in PF_Pixel16 and float type (32bit) in PF_PixelFloat or PF_Pixel32.

//picked up from A.h

typedef char			A_char;
typedef float			A_FpShort;
typedef unsigned short	A_u_short;

Tips of color depth handling in AE plug-in development

Global setup

To make your effect handle 16-bpc color, set out_data->out_flags PF_OutFlag_DEEP_COLOR_AWARE.

And to make your effect supports 32-bpc colors, set out_data->out_flags2 PF_OutFlag2_FLOAT_COLOR_AWARE and PF_OutFlag2_SUPPORTS_SMART_RENDER.

Set pixel format

Before CS6, PF_CHECKOUT_PARAM() only returned 8-bit ARGB buffers, regardless of the pixel format currently being used for rendering. Starting in CS6, an effect can opt in to get frames in the same format as the render request, whether it is 32-bit float, YUV, etc.

from  After Effects SDK Guide

Render

In Render function, the iterate function scan input flame and calculate output frame as pixel to pixel operation, pixel function. The iterate functions is selected separately depending on the PrPixelFormat of destination pixels. Check After Effects SDK Guide “Progress During Iteration” for details.

Smart Render

In Smart Render function, pixel functions is selected separately depending on the PrPixelFormat like Render function.

If you want to know about the difference between Render function and SmartRender function, check the article “The difference of Render and Smart Render in After Effects”. (sorry, it will come soon)

The conditional branch by switch sentence is following.

 switch (format) {
    case PF_PixelFormat_ARGB128:
        iterateFloatSuite ->iterate(
            in_data, 0, output_worldP->height, input_worldP, 
            NULL, (void*)infoP, FilterImage32, output_worldP);
        break;
    case PF_PixelFormat_ARGB64:
        iterate16Suite ->iterate(
            in_data, 0, output_worldP->height, input_worldP,
            NULL, (void*)infoP, FilterImage16, output_worldP);
        break;
    case PF_PixelFormat_ARGB32:
        iterate8Suite ->iterate(
            in_data, 0, output_worldP->height, input_worldP,
            NULL, (void*)infoP, FilterImage8, output_worldP);
        break;
     default:
         err = PF_Err_BAD_CALLBACK_PARAM;
         break;
}

Pixel functions

Prepare each functions for 8/16/32 bit separately. The follow example is in SDK_Noise.cpp.

The differences are following.

*inP and *outP

// 8bit
static PF_Err FilterImage8 (
    void *refcon, A_long xL, A_long yL, 
    PF_Pixel8 *inP, PF_Pixel8 *outP)

// 16bit
static PF_Err FilterImage16 (
    void *refcon, A_long xL, A_long yL, 
    PF_Pixel16 *inP, PF_Pixel16 *outP)

// 32bit
static PF_Err FilterImage32 (
    void *refcon, A_long xL, A_long yL, 
    PF_PixelFloat *inP, PF_PixelFloat *outP)

tempF

// 8bit   clamp in range [0, PF_MAX_CHAN8]
tempF = rand() % PF_MAX_CHAN8;
tempF *= (niP->valF / SLIDER_MAX);

// 16bit   clamp in range [0, PF_MAX_CHAN16]
tempF = rand() % PF_MAX_CHAN16;
tempF *= (niP->valF / SLIDER_MAX);

// 32bit    clamp in range [0, 1 = PF_MAX_CHAN16/PF_MAX_CHAN16]
tempF = (PF_FpShort)(rand() % PF_MAX_CHAN16);
tempF *= (PF_FpShort)(niP->valF / (SLIDER_MAX * PF_MAX_CHAN16));

channels of outP

// 8bit  clamp in the range [0, PF_MAX_CHAN8] 
outP->red = MIN(PF_MAX_CHAN8, inP->red + (A_u_char) tempF);

// 16bit  clamp in the range[0, PF_MAX_CHAN16] 
outP->red = MIN(PF_MAX_CHAN16, inP->red + (A_u_short) tempF);

// 32bit
outP->red = (inP->red + tempF);

Understanding typedef in C/C++

GOAL

To understand “typedef” in C++

What is typedef?

typedef is a reserved keyword that gives a new name to existing data type used in C/C++ programming. That new name like this is called alias or synonym.

The following is an example of typedef. typedef is used just for user convenience.

typedef int USER_Id;
USER_Id john_id; //this is same as int john_id
john_id = 24;

When to use typedef

These are some of many use cases of type def.

To clarify the meaning of variable

For example, when you handle data of date many times, you may want to identify month and day.

typedef  int Month;  //define alias "Month"
typedef int Day;  // define alias "Day"

This is an example of use.

#include <iostream>
using namespace std;

typedef  int Month;
typedef int Day;

void print_birthday(Month m,  Day d){
    printf("your birthday is %d/%d\n",m, d);
}
Day last_day(Month m){
    if(m == 4 && m==6 && m==9 && m==11)
        return 30;
    if(m == 2)
        return 28; //ignore leap year 
    else
        return 31;
}
int main(void){
    print_birthday(12,24);  /* output is "your birthday is 12/24" */
    cout << last_day(10);  /* output is "31"*/
}

To omit long type

typedef long long unsigned int LLUInt;

int main(void){
    LLUInt test1 = 10;
    LLUInt test2 = 20;
    cout << test1+test2 << endl;  /* output is "30"*/
}

To omit the “struct” keyword in C language

When you declare a defined structure, you should use the “struct” keyword in C language.

struct person{
    char *nickname;
    int age;
};
//declare
struct person john;

If you use typedef and rename the structure, you don’t need the “struct” keyword.

typedef struct person{
    char *nickname;
    int age;
} person_type;

//declare
person_type john;

The definition of the structure in above source code is the same as this.

struct person{
    char *nickname;
    int age;
};
typedef person person_type;

self-referencing structure

The following is the Node structure used in linked list.

struct Node {
    int data;
    struct Node *next;
}

This self-referencing structure is also defined by using typedef.

typedef struct _node{
int data;
struct _node * next;
} Node;
Node* top;

To create a pointer type

Usually you should use * to declare a pointer.

int *p1, *p2, *p3, *p4;

You can define pointer type by using typedef.

typedef int* IntP
IntP p1, p2, p3, p4;

string

#include <iostream>
using namespace std;

typedef char* my_str;

int main(void){
    my_str name = "Nako";
    printf("My name is %s\n", name); /* output is "My name is Nako"*/
}

Variations

Structure and its pointer

typedef struct _node{
    int data;
    struct _node * next;
} Node, *pNode;

Function

In this example, variable f can store a pointer to function int FUNC(int n).

#include <iostream>
using namespace std;

int next_int(int x) { return x + 1; } //example of int FUNC(int n)
int half_int(int x) { return x/2;}  //example of int FUNC(int n)

int main(void){
    int (*f1)(int n);
    int (*f2)(int n);
    f1 = next_int;
    f2 = half_int;
    cout << f1(4) << " " << f2(4) << endl;  /*output is "5 2"*/
}

Typedef can add the name to the pointer of function.

typedef int (*FUNCT)(int n);

The above example of function pointer can be changed as below.

#include <iostream>
using namespace std;

int next_int(int x) { return x + 1; }
int half_int(int x) { return x/2;}

typedef int (*FUNCT)(int n);

int main(void){
    FUNCT f1;
    FUNCT f2;
    f1 = next_int;
    f2 = half_int;
    cout << f1(4) << " " << f2(4) << endl;    /*output is "5 2"*/
}

list

typedef can define list as type.

#include <iostream>
using namespace std;

typedef int Members[5];

int main(void){
    Members team_a = {13, 25, 66, 89, 100};
    Members team_b = {21, 22, 50, 78, 81};
    for(int i=0; i<5; i++){
        cout << team_a[i] << " ";  /*output is "13 25 66 89 100 "*/
    }
    cout << endl;
    for(int i=0; i<5; i++){
        cout << team_b[i] << " ";  /*output is "21 22 50 78 81 "*/
    }
}

using in C++

The “using” declaration introduces a name into the declarative region in C++. The “using” declaration is usually used for making a declaration of namespace such as “using namespace std;”, but it can be also used for alias.

using Month = int;
using Day = int;
using FUNCT = int (*)(int);

I’ll write about the difference between typedef and using and its features someday. Thank you!

What is Hermite interpolation?

GOAL

To understand Hermite interpolation.

Abstract

Hermite interpolation is one of the interpolation method that needs n points and slopes of each point.

$$ nth\quad point: (x_n, f_n)$$ $$nth\quad sloop: (x_n, {f_n}^{(1)})$$

@2020 Nako

Hermite interpolation polynomial

$$f(x) = \sum_{i=0}^{n}h_{i0}(x)f_i + \sum_{i=0}^{n}h_{i1}(x){f_i}^{(1)}$$

$$h_{i0}(x) = p_i(x)[\frac{1}{p_i(x_i)} – (x – x_i) \frac {{{p_i}^{(1)}}(x_i)}{{[p_i(x_i)]}^2}$$

$$h_{i1}(x) = (x – x_i) \frac {{p_i}(x)}{{p_i}(x_i)}$$

$$p_i(x) = {(x-x_0)}^2{(x-x_1)}^2… {(x-x_{i-1})}^2{(x-x_{i+1})}^2…{(x-x_n)}^2$$

Proof

1

Because

2

Because

3

Derivation method

Derivation of interpolated function from 2 points and 2 slopes on that points. Given values are point (x_0, f(x_0)), slope f'(x_0), point (x_1, f(x_1)) and slope f'(x_1).

f[x], f[x, y], f[x, y, … ,z] is divided difference. Please refer to “Divided Difference” for detail.

$$f[x,x]=f'(x)$$ $$f[x_0,x_1,…,x_n]=\frac{f[x_1,x_2,…,f_n]- f[x_0,x_1,…,x_{n-1}] }{x_n-x_0}$$

f(x) is computed in the following way.

Categories

AfterEffects Algorithm Artificial Intelligence Blender C++ Computer Graphics Computer Science Daily Life DataAnalytics Event Game ImageProcessing JavaScript Kotlin mathematics Maya PHP Python SoftwareEngineering Tips Today's paper Tools TroubleShooting Unity Visual Sudio Web Windows WordPress 未分類