1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <android/log.h>
18#include <stdlib.h>
19#include <string.h>
20
21#define  LOGI(...) __android_log_print(ANDROID_LOG_INFO, "MCA", __VA_ARGS__)
22#define  LOGW(...) __android_log_print(ANDROID_LOG_WARN, "MCA", __VA_ARGS__)
23#define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "MCA", __VA_ARGS__)
24
25typedef struct {
26  float brightness;
27} BrightnessParameters;
28
29typedef union {
30    int value;
31    char rgba[4];
32} Pixel;
33
34void brightness_init(void** user_data) {
35  (*user_data) = malloc(sizeof(BrightnessParameters));
36}
37
38void brightness_teardown(void* user_data) {
39  free(user_data);
40}
41
42void brightness_setvalue(const char* key, const char* value, void* user_data) {
43  if (strcmp(key, "brightness") == 0)
44    ((BrightnessParameters*)user_data)->brightness = atof(value);
45  else
46    LOGE("Unknown parameter: %s!", key);
47}
48
49int brightness_process(const char** inputs,
50                       const int* input_sizes,
51                       int input_count,
52                       char* output,
53                       int output_size,
54                       void* user_data) {
55  // Make sure we have exactly one input
56  if (input_count != 1) {
57    LOGE("Brightness: Incorrect input count! Expected 1 but got %d!", input_count);
58    return 0;
59  }
60
61  // Make sure sizes match up
62  if (input_sizes[0] != output_size) {
63    LOGE("Brightness: Input-output sizes do not match up. %d vs. %d!", input_sizes[0], output_size);
64    return 0;
65  }
66
67  // Get the input and output pointers
68  const int* input_ptr = (int*)inputs[0];
69  int* output_ptr = (int*)output;
70  const int* end_ptr = input_ptr + (output_size / 4);
71  if (!input_ptr || !output_ptr) {
72    LOGE("Brightness: No input or output pointer found!");
73    return 0;
74  }
75
76  // Get the parameters
77  BrightnessParameters* params = (BrightnessParameters*)user_data;
78  const float brightness = params->brightness;
79
80  // Run the brightness adjustment
81  const int factor = (int)(brightness * 255.0f);
82  Pixel pixel;
83  while (input_ptr < end_ptr) {
84    pixel.value = *(input_ptr++);
85
86    const short r = (pixel.rgba[0] * factor) / 255;
87    const short g = (pixel.rgba[1] * factor) / 255;
88    const short b = (pixel.rgba[2] * factor) / 255;
89
90    *(output_ptr++) = (r > 255 ? 255 : r)
91                    | ((g > 255 ? 255 : g) << 8)
92                    | ((b > 255 ? 255 : b) << 16)
93                    | (pixel.rgba[3] << 24);
94  }
95
96  return 1;
97}
98
99