brightness.c revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
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
20#define  LOGI(...) __android_log_print(ANDROID_LOG_INFO, "MCA", __VA_ARGS__)
21#define  LOGW(...) __android_log_print(ANDROID_LOG_WARN, "MCA", __VA_ARGS__)
22#define  LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "MCA", __VA_ARGS__)
23
24typedef struct {
25  float brightness;
26} BrightnessParameters;
27
28typedef union {
29    int value;
30    char rgba[4];
31} Pixel;
32
33void brightness_init(void** user_data) {
34  (*user_data) = malloc(sizeof(BrightnessParameters));
35}
36
37void brightness_teardown(void* user_data) {
38  free(user_data);
39}
40
41void brightness_setvalue(const char* key, const char* value, void* user_data) {
42  if (strcmp(key, "brightness") == 0)
43    ((BrightnessParameters*)user_data)->brightness = atof(value);
44  else
45    LOGE("Unknown parameter: %s!", key);
46}
47
48int brightness_process(const char** inputs,
49                       const int* input_sizes,
50                       int input_count,
51                       char* output,
52                       int output_size,
53                       void* user_data) {
54  // Make sure we have exactly one input
55  if (input_count != 1) {
56    LOGE("Brightness: Incorrect input count! Expected 1 but got %d!", input_count);
57    return 0;
58  }
59
60  // Make sure sizes match up
61  if (input_sizes[0] != output_size) {
62    LOGE("Brightness: Input-output sizes do not match up. %d vs. %d!", input_sizes[0], output_size);
63    return 0;
64  }
65
66  // Get the input and output pointers
67  const int* input_ptr = (int*)inputs[0];
68  int* output_ptr = (int*)output;
69  const int* end_ptr = input_ptr + (output_size / 4);
70  if (!input_ptr || !output_ptr) {
71    LOGE("Brightness: No input or output pointer found!");
72    return 0;
73  }
74
75  // Get the parameters
76  BrightnessParameters* params = (BrightnessParameters*)user_data;
77  const float brightness = params->brightness;
78
79  // Run the brightness adjustment
80  const int factor = (int)(brightness * 255.0f);
81  Pixel pixel;
82  while (input_ptr < end_ptr) {
83    pixel.value = *(input_ptr++);
84
85    const short r = (pixel.rgba[0] * factor) / 255;
86    const short g = (pixel.rgba[1] * factor) / 255;
87    const short b = (pixel.rgba[2] * factor) / 255;
88
89    *(output_ptr++) = (r > 255 ? 255 : r)
90                    | ((g > 255 ? 255 : g) << 8)
91                    | ((b > 255 ? 255 : b) << 16)
92                    | (pixel.rgba[3] << 24);
93  }
94
95  return 1;
96}
97
98