We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesGraphicsArkGraphics 2DOffline Image ProcessingAdding Image Effects (C/C++)

Adding Image Effects (C/C++)

When to Use

When processing images offline, you can apply various image effects to achieve different visual presentations, such as setting the blur level, adjusting brightness and grayscale, and inverting colors.

Image effects are primarily applied through filters.

Available APIs

The common APIs for setting image effects using filters are listed in the following table. For details about their usage and parameters, see effect_filter.

Expand
API Description
EffectErrorCode OH_Filter_CreateEffect(OH_PixelmapNative* pixelmap, OH_Filter** filter) Creates a filter object based on a pixel map object.
EffectErrorCode OH_Filter_Blur(OH_Filter* filter, float radius) Creates a blur effect and adds it to the filter.
EffectErrorCode OH_Filter_Brighten(OH_Filter* filter, float brightness) Creates a brighten effect and adds it to the filter.
EffectErrorCode OH_Filter_GrayScale(OH_Filter* filter) Creates a grayscale effect and adds it to the filter.
EffectErrorCode OH_Filter_Invert(OH_Filter* filter) Creates an invert effect and adds it to the filter.
EffectErrorCode OH_Filter_GetEffectPixelMap(OH_Filter* filter, OH_PixelmapNative** pixelmap) Obtains the pixel map object generated after filter processing.
EffectErrorCode OH_Filter_Release(OH_Filter* filter) Releases the filter object.

How to Develop

  1. Add the following link libraries to src/main/cpp/CMakeLists.txt in the native project.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. target_link_libraries(entry PUBLIC libnative_drawing.so)
    2. target_link_libraries(entry PUBLIC libhilog_ndk.z.so)
    3. target_link_libraries(entry PUBLIC libnative_effect.so)
    4. target_link_libraries(entry PUBLIC libpixelmap.so)
  2. Include the required header files.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. #include "multimedia/image_framework/image/pixelmap_native.h"
    2. #include "native_effect/effect_filter.h"
  3. Create an OH_PixelmapNative pixel map object.

    Creating a filter requires a pixel map object (OH_PixelmapNative) defined by the image framework. You can create a custom pixel map using OH_PixelmapNative_CreatePixelmap(), or import a pixel map from an external source using OH_PixelmapNative_ConvertPixelmapNativeFromNapi().

    This section uses OH_PixelmapNative_CreatePixelmap() as an example to create an OH_PixelmapNative object. This function accepts four parameters: the first parameter is the buffer of image pixel data, used to initialize the pixels of the pixel map; the second parameter is the buffer length; the third parameter is the pixel map format (including width, height, color type, alpha type, etc.); and the fourth parameter is the OH_PixelmapNative object, which serves as an output parameter.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. // The image width and height are 600 * 400 respectively.
    2. uint32_t width = 600;
    3. uint32_t height = 400;
    4. const uint16_t RGBA_MIN = 0x00;
    5. const uint16_t RGBA_MAX = 0xFF;
    6. const uint16_t RGBA_SIZE = 4;
    7. // Byte length. Each pixel in RGBA_8888 occupies 4 bytes.
    8. size_t bufferSize = width * height * RGBA_SIZE;
    9. uint8_t *pixels = new uint8_t[bufferSize];
    10. for (uint32_t i = 0; i < width * height; ++i) {
    11. // Iterate over and edit each pixel to form alternating red, green, and blue stripes.
    12. uint32_t n = i / 20 % 3;
    13. pixels[i * RGBA_SIZE] = RGBA_MIN; // Red channel.
    14. pixels[i * RGBA_SIZE + 1] = RGBA_MIN; // +1 indicates the green channel.
    15. pixels[i * RGBA_SIZE + 2] = RGBA_MIN; // +2 indicates the blue channel.
    16. pixels[i * RGBA_SIZE + 3] = RGBA_MAX; // +3 indicates the opacity channel.
    17. if (n == 0) {
    18. pixels[i * RGBA_SIZE] = RGBA_MAX; // Assigns the red channel, making the color appear red.
    19. } else if (n == 1) {
    20. pixels[i * RGBA_SIZE + 1] = RGBA_MAX; // +1 assigns the green channel; other channels are 0, producing green.
    21. } else {
    22. pixels[i * RGBA_SIZE + 2] = RGBA_MAX; // +2 assigns the blue channel; other channels are 0, producing blue.
    23. }
    24. }
    25. // Set the pixel map format (width, height, color type, alpha type).
    26. OH_Pixelmap_InitializationOptions *createOps = nullptr;
    27. OH_PixelmapInitializationOptions_Create(&createOps);
    28. OH_PixelmapInitializationOptions_SetWidth(createOps, width);
    29. OH_PixelmapInitializationOptions_SetHeight(createOps, height);
    30. OH_PixelmapInitializationOptions_SetPixelFormat(createOps, PIXEL_FORMAT_RGBA_8888);
    31. OH_PixelmapInitializationOptions_SetAlphaType(createOps, PIXELMAP_ALPHA_TYPE_UNKNOWN);
    32. // Create the OH_PixelmapNative object.
    33. OH_PixelmapNative *pixelMapNative = nullptr;
    34. OH_PixelmapNative_CreatePixelmap(pixels, bufferSize, createOps, &pixelMapNative);
    35. // The pixel data has been copied to the pixel map. Release the pixel data buffer after the creation is complete.
    36. delete[] pixels;
    37. // Release the initialization options object.
    38. OH_PixelmapInitializationOptions_Release(createOps);
  4. Based on the OH_PixelmapNative object generated above, create an OH_Filter object using the OH_Filter_CreateEffect() API.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. OH_Filter *filter = nullptr;
    2. EffectErrorCode errCodeCreate = OH_Filter_CreateEffect(pixelMapNative, &filter);
  5. Add different image effects to the filter as needed.

    • You can use the OH_Filter_Blur() API to add a blur effect to the filter.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. float radius = 20.0; // Blur radius.
      2. EffectErrorCode errCodeEffect = OH_Filter_Blur(filter, radius);
    • You can use the OH_Filter_Brighten() API to add a brightening effect to the filter.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. float brightness = 0.5; // Brightness level (0 to 1).
      2. EffectErrorCode errCodeEffect = OH_Filter_Brighten(filter, brightness);
    • You can use the OH_Filter_GrayScale() API to add a grayscale effect to the filter.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. EffectErrorCode errCodeEffect = OH_Filter_GrayScale(filter);
    • You can use the OH_Filter_Invert() API to add an invert effect to the filter.

      Collapse
      Word wrap
      Dark theme
      Copy code
      1. EffectErrorCode errCodeEffect = OH_Filter_Invert(filter);
  6. Obtain the OH_PixelmapNative object processed by the filter.

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. OH_PixelmapNative* filterResult = nullptr;
    2. EffectErrorCode errCodeResult = OH_Filter_GetEffectPixelMap(filter, &filterResult);
  7. When the image effect generated by the filter is no longer needed, release the OH_Filter object in a timely manner using OH_Filter_Release(). Similarly, when the original pixelmap and the pixel map generated after filter processing are no longer needed, release them in a timely manner using OH_PixelmapNative_Release().

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. EffectErrorCode errCodeRelease = OH_Filter_Release(filter);
    2. // Release the pixel map object generated after filter processing.
    3. OH_PixelmapNative_Release(filterResult);
    4. // Release the original pixel map object.
    5. OH_PixelmapNative_Release(pixelMapNative);

    The following table lists the rendering effects.

    Expand
    Image Processing Rendering Effect
    Original image
    With blur effect
    With brightening effect
    With grayscale effect
    With invert effect