• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2010 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/bitmap.h>
18 
19 #include <cstdlib>
20 
21 #include "utils.h"
22 
23 using android::apps::photoeditor::utils::clamp;
24 using android::apps::photoeditor::utils::LockBitmaps;
25 using android::apps::photoeditor::utils::pixel32_t;
26 using android::apps::photoeditor::utils::UnlockBitmaps;
27 
28 namespace android {
29 namespace apps {
30 namespace photoeditor {
31 namespace convolution {
32 
33 /*
34  * Used for convolution of following kernel:
35  *   n n n
36  *   n c n
37  *   n n n
38  * n: neighbor, c: center
39  */
SpecialConvolution(AndroidBitmapInfo * src_info,AndroidBitmapInfo * dst_info,void * src_pixels,void * dst_pixels,float neighbor)40 void SpecialConvolution(AndroidBitmapInfo *src_info, AndroidBitmapInfo *dst_info,
41     void *src_pixels, void *dst_pixels, float neighbor) {
42 
43   if (neighbor == 0) {
44     memcpy(dst_pixels, src_pixels, src_info->stride * src_info->height);
45     return;
46   }
47 
48   /* Copy the first row to dst_pixels */
49   memcpy(dst_pixels, src_pixels, src_info->stride);
50   src_pixels = reinterpret_cast<char*>(src_pixels) + src_info->stride;
51   dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info->stride;
52 
53   const int32_t kShiftBits = 10;
54   int32_t fixed_neighbor = static_cast<int32_t>((1 << kShiftBits) * neighbor);
55   int32_t fixed_center = static_cast<int32_t>((1 << kShiftBits) * (1 - neighbor * 8));
56 
57   for (uint32_t scan_line = 1; scan_line < dst_info->height - 1; scan_line++) {
58     pixel32_t* src = reinterpret_cast<pixel32_t*>(src_pixels);
59     uint32_t* src_prev = reinterpret_cast<uint32_t*>
60         (reinterpret_cast<char*>(src_pixels) - src_info->stride);
61     uint32_t* src_next = reinterpret_cast<uint32_t*>
62         (reinterpret_cast<char*>(src_pixels) + src_info->stride);
63 
64     uint32_t* dst = reinterpret_cast<uint32_t*>(dst_pixels);
65     *dst++ = src->rgba32;
66     src++;
67     src_prev++;
68     src_next++;
69 
70     uint32_t* dst_line_end = dst + dst_info->width - 1;
71     while (dst < dst_line_end) {
72       int32_t src_red = src->rgba8[0];
73       int32_t src_green = src->rgba8[1];
74       int32_t src_blue = src->rgba8[2];
75       int32_t src_alpha = src->rgba8[3];
76 
77       uint8_t* n1 = reinterpret_cast<uint8_t*>(src_prev - 1);
78       uint8_t* n2 = reinterpret_cast<uint8_t*>(src - 1);
79       uint8_t* n3 = reinterpret_cast<uint8_t*>(src_next - 1);
80       uint8_t* n4 = reinterpret_cast<uint8_t*>(src_prev);
81       uint8_t* n5 = reinterpret_cast<uint8_t*>(src_next);
82       uint8_t* n6 = reinterpret_cast<uint8_t*>(src_prev + 1);
83       uint8_t* n7 = reinterpret_cast<uint8_t*>(src + 1);
84       uint8_t* n8 = reinterpret_cast<uint8_t*>(src_next + 1);
85 
86       int32_t red = n1[0] + n2[0] + n3[0] + n4[0] + n5[0] + n6[0] + n7[0] + n8[0];
87       int32_t green = n1[1] + n2[1] + n3[1] + n4[1] + n5[1] + n6[1] + n7[1] + n8[1];
88       int32_t blue = n1[2] + n2[2] + n3[2] + n4[2] + n5[2] + n6[2] + n7[2] + n8[2];
89 
90       red = (fixed_neighbor * red + fixed_center * src_red) >> kShiftBits;
91       green = (fixed_neighbor * green + fixed_center * src_green) >> kShiftBits;
92       blue = (fixed_neighbor * blue + fixed_center * src_blue) >> kShiftBits;
93 
94       red = clamp(red, 0, 255);
95       green = clamp(green, 0, 255);
96       blue = clamp(blue, 0, 255);
97 
98       *dst = (src_alpha << 24) | (blue << 16) | (green << 8) | red;
99       dst++;
100       src++;
101       src_prev++;
102       src_next++;
103     }
104     *dst = src->rgba32;
105     src_pixels = reinterpret_cast<char*>(src_pixels) + src_info->stride;
106     dst_pixels = reinterpret_cast<char*>(dst_pixels) + dst_info->stride;
107   }
108 
109   /* Copy the last row to dst_pixels */
110   memcpy(dst_pixels, src_pixels, src_info->stride);
111 }
112 
113 }  // namespace convolution
114 }  // namespace photoeditor
115 }  // namespace apps
116 }  // namespace android
117