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 contrast;
26} ContrastParameters;
27
28void contrast_init(void** user_data) {
29  (*user_data) = malloc(sizeof(ContrastParameters));
30}
31
32void contrast_teardown(void* user_data) {
33  free(user_data);
34}
35
36void contrast_setvalue(const char* key, const char* value, void* user_data) {
37  if (strcmp(key, "contrast") == 0)
38    ((ContrastParameters*)user_data)->contrast = atof(value);
39  else
40    LOGE("Unknown parameter: %s!", key);
41}
42
43int contrast_process(const char** inputs,
44                     const int* input_sizes,
45                     int input_count,
46                     char* output,
47                     int output_size,
48                     void* user_data) {
49  // Make sure we have exactly one input
50  if (input_count != 1) {
51    LOGE("Contrast: Incorrect input count! Expected 1 but got %d!", input_count);
52    return 0;
53  }
54
55  // Make sure sizes match up
56  if (input_sizes[0] != output_size) {
57    LOGE("Contrast: Input-output sizes do not match up. %d vs. %d!", input_sizes[0], output_size);
58    return 0;
59  }
60
61  // Get the input and output pointers
62  const char* input_ptr = inputs[0];
63  char* output_ptr = output;
64  if (!input_ptr || !output_ptr) {
65    LOGE("Contrast: No input or output pointer found!");
66    return 0;
67  }
68
69  // Get the parameters
70  ContrastParameters* params = (ContrastParameters*)user_data;
71  const float contrast = params->contrast;
72
73  // Run the contrast adjustment
74  int i;
75  for (i = 0; i < output_size; ++i) {
76    float px = *(input_ptr++) / 255.0;
77    px -= 0.5;
78    px *= contrast;
79    px += 0.5;
80    *(output_ptr++) = (char)(px > 1.0 ? 255.0 : (px < 0.0 ? 0.0 : px * 255.0));
81  }
82
83  return 1;
84}
85
86