顾海波

【需求】适配鸿蒙照片转rgb

... ... @@ -5,16 +5,16 @@ packages:
dependency: transitive
description:
name: archive
sha256: "0c8368c9b3f0abbc193b9d6133649a614204b528982bebc7026372d61677ce3a"
url: "https://pub.dev"
sha256: "0c64e928dcbefddecd234205422bcfc2b5e6d31be0b86fef0d0dd48d7b4c9742"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.3.7"
version: "4.0.4"
async:
dependency: transitive
description:
name: async
sha256: "947bfcf187f74dbc5e146c9eb9c0f10c9f8b30743e341481c1e2ed3ecc18c20c"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.11.0"
boolean_selector:
... ... @@ -22,55 +22,55 @@ packages:
description:
name: boolean_selector
sha256: "6cfb5af12253eaf2b368f07bacc5a80d1301a071c73360d746b7f2e32d762c66"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
camera:
dependency: "direct main"
description:
name: camera
sha256: b4cede7c66f44fa476272d21bfe143d5f32e75de1ea56f737e3eaf982da23bab
url: "https://pub.dev"
sha256: dfa8fc5a1adaeb95e7a54d86a5bd56f4bb0e035515354c8ac6d262e35cec2ec8
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.10.5+3"
version: "0.10.6"
camera_android:
dependency: transitive
description:
name: camera_android
sha256: ffbcf60f107776e0bfda7ed787e7ae6ec7f4cb962598973756daa2ad70d8f191
url: "https://pub.dev"
sha256: "32f04948a284b71d938fe275616faf4957d07f9b3aab8021bfc8c418301a289e"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.10.8+7"
version: "0.10.9+11"
camera_avfoundation:
dependency: transitive
description:
name: camera_avfoundation
sha256: "332747f20cf911980e38c8442108102d4456752711781108fda237635baf362c"
url: "https://pub.dev"
sha256: ba48b65a3a97004276ede882e6b838d9667642ff462c95a8bb57ca8a82b6bd25
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.9.13+3"
version: "0.9.18+11"
camera_platform_interface:
dependency: transitive
description:
name: camera_platform_interface
sha256: "60fa0bb62a4f3bf3a7c413e31e4cd01b69c779ccc8e4668904a24581b86c316b"
url: "https://pub.dev"
sha256: "953e7baed3a7c8fae92f7200afeb2be503ff1a17c3b4e4ed7b76f008c2810a31"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.5.1"
version: "2.9.0"
camera_web:
dependency: transitive
description:
name: camera_web
sha256: "894df2a4e9ddd77ffecee9553d5980eeabb8bf09d98e53934859e67dc367933b"
url: "https://pub.dev"
sha256: "595f28c89d1fb62d77c73c633193755b781c6d2e0ebcd8dc25b763b514e6ba8f"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.3.2+1"
version: "0.3.5"
characters:
dependency: transitive
description:
name: characters
sha256: "04a925763edad70e8443c99234dc3328f442e811f1d8fd1a72f1c8ad0f69a605"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.0"
clock:
... ... @@ -78,65 +78,57 @@ packages:
description:
name: clock
sha256: cb6d7f03e1de671e34607e909a7213e31d7752be4fb66a86d29fe1eb14bfb5cf
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.1.1"
collection:
dependency: transitive
description:
name: collection
sha256: f092b211a4319e98e5ff58223576de6c2803db36221657b46c82574721240687
url: "https://pub.dev"
sha256: ee67cb0715911d28db6bf4af1026078bd6f0128b07a5f66fb2ed94ec6783c09a
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.17.2"
convert:
dependency: transitive
description:
name: convert
sha256: "0f08b14755d163f6e2134cb58222dd25ea2a2ee8a195e53983d57c075324d592"
url: "https://pub.dev"
source: hosted
version: "3.1.1"
version: "1.18.0"
cross_file:
dependency: transitive
description:
name: cross_file
sha256: "0b0036e8cccbfbe0555fd83c1d31a6f30b77a96b598b35a5d36dd41f718695e9"
url: "https://pub.dev"
sha256: "7caf6a750a0c04effbb52a676dce9a4a592e10ad35c34d6d2d0e4811160d5670"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.3.3+4"
version: "0.3.4+2"
crypto:
dependency: transitive
description:
name: crypto
sha256: ff625774173754681d66daaf4a448684fb04b78f902da9cb3d308c19cc5e8bab
url: "https://pub.dev"
sha256: "1e445881f28f22d6140f181e07737b22f1e099a5e1ff94b0af2f9e4a463f4855"
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.0.3"
version: "3.0.6"
cupertino_icons:
dependency: "direct main"
description:
name: cupertino_icons
sha256: e35129dc44c9118cee2a5603506d823bab99c68393879edb440e0090d07586be
url: "https://pub.dev"
sha256: ba631d1c7f7bef6b729a622b7b752645a2d076dba9976925b8f25725a30e1ee6
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.0.5"
version: "1.0.8"
fake_async:
dependency: transitive
description:
name: fake_async
sha256: "511392330127add0b769b75a987850d136345d9227c6b94c96a04cf4a391bf78"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.1"
ffi:
dependency: transitive
description:
name: ffi
sha256: "7bf0adc28a23d395f19f3f1eb21dd7cfd1dd9f8e1c50051c069122e6853bc878"
url: "https://pub.dev"
sha256: "16ed7b077ef01ad6170a3d0c57caa4a112a38d7a2ed5602e0aca9ca6f3d98da6"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
version: "2.1.3"
flutter:
dependency: "direct main"
description: flutter
... ... @@ -146,18 +138,18 @@ packages:
dependency: "direct dev"
description:
name: flutter_lints
sha256: "2118df84ef0c3ca93f96123a616ae8540879991b8b57af2f81b76a7ada49b2a4"
url: "https://pub.dev"
sha256: a25a15ebbdfc33ab1cd26c63a6ee519df92338a9c10f122adda92938253bef04
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.2"
version: "2.0.3"
flutter_plugin_android_lifecycle:
dependency: transitive
description:
name: flutter_plugin_android_lifecycle
sha256: "950e77c2bbe1692bc0874fc7fb491b96a4dc340457f4ea1641443d0a6c1ea360"
url: "https://pub.dev"
sha256: "9ee02950848f61c4129af3d6ec84a1cfc0e47931abc746b03e7a3bc3e8ff6eda"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.15"
version: "2.0.22"
flutter_test:
dependency: "direct dev"
description: flutter
... ... @@ -172,103 +164,111 @@ packages:
dependency: "direct main"
description:
name: image
sha256: a72242c9a0ffb65d03de1b7113bc4e189686fc07c7147b8b41811d0dd0e0d9bf
url: "https://pub.dev"
sha256: "13d3349ace88f12f4a0d175eb5c12dcdd39d35c4c109a8a13dfeb6d0bd9e31c3"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.5.3"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "7f0df31977cb2c0b88585095d168e689669a2cc9b97c309665e3386f3e9d341a"
url: "https://pub.flutter-io.cn"
source: hosted
version: "10.0.4"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: "06e98f569d004c1315b991ded39924b21af84cf14cc94791b8aea337d25b57f8"
url: "https://pub.flutter-io.cn"
source: hosted
version: "4.0.17"
js:
version: "3.0.3"
leak_tracker_testing:
dependency: transitive
description:
name: js
sha256: f2c445dce49627136094980615a031419f7f3eb393237e4ecd97ac15dea343f3
url: "https://pub.dev"
name: leak_tracker_testing
sha256: "6ba465d5d76e67ddf503e1161d1f4a6bc42306f9d66ca1e8f079a47290fb06d3"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.7"
version: "3.0.1"
lints:
dependency: transitive
description:
name: lints
sha256: "0a217c6c989d21039f1498c3ed9f3ed71b354e69873f13a8dfc3c9fe76f1b452"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
matcher:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
url: "https://pub.dev"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
url: "https://pub.dev"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: "3c74dbf8763d36539f114c799d8a2d87343b5067e9d796ca22b5eb8437090ee3"
url: "https://pub.dev"
sha256: "7687075e408b093f36e6bbf6c91878cc0d4cd10f409506f7bc996f68220b9136"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.9.1"
version: "1.12.0"
path:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
url: "https://pub.dev"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.8.3"
version: "1.9.0"
petitparser:
dependency: transitive
description:
name: petitparser
sha256: cb3798bef7fc021ac45b308f4b51208a152792445cce0448c9a4ba5879dd8750
url: "https://pub.dev"
sha256: c15605cd28af66339f8eb6fbe0e541bfe2d1b72d5825efc6598f3e0a31b9ad27
url: "https://pub.flutter-io.cn"
source: hosted
version: "5.4.0"
version: "6.0.2"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "43798d895c929056255600343db8f049921cbec94d31ec87f1dc5c16c01935dd"
url: "https://pub.dev"
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.5"
pointycastle:
version: "2.1.8"
posix:
dependency: transitive
description:
name: pointycastle
sha256: "7c1e5f0d23c9016c5bbd8b1473d0d3fb3fc851b876046039509e18e0c7485f2c"
url: "https://pub.dev"
name: posix
sha256: a0117dc2167805aa9125b82eee515cc891819bac2f538c83646d355b16f58b9a
url: "https://pub.flutter-io.cn"
source: hosted
version: "3.7.3"
version: "6.0.1"
processing_camera_image:
dependency: "direct main"
description:
path: ".."
relative: true
source: path
version: "0.0.10"
quiver:
dependency: transitive
description:
name: quiver
sha256: b1c1ac5ce6688d77f65f3375a9abb9319b3cb32486bdc7a1e0fdf004d7ba4e47
url: "https://pub.dev"
source: hosted
version: "3.2.1"
version: "0.0.12"
rxdart:
dependency: "direct main"
description:
name: rxdart
sha256: "0c7c0cedd93788d996e33041ffecda924cc54389199cde4e6a34b440f50044cb"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.27.7"
sky_engine:
... ... @@ -281,39 +281,39 @@ packages:
description:
name: source_span
sha256: "53e943d4206a5e30df338fd4c6e7a077e02254531b138a15aec3bd143c1a8b3c"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.10.0"
stack_trace:
dependency: transitive
description:
name: stack_trace
sha256: c3c7d8edb15bee7f0f74debd4b9c5f3c2ea86766fe4178eb2a18eb30a0bdaed5
url: "https://pub.dev"
sha256: "73713990125a6d93122541237550ee3352a2d84baad52d375a4cad2eb9b7ce0b"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.11.0"
version: "1.11.1"
stream_channel:
dependency: transitive
description:
name: stream_channel
sha256: "83615bee9045c1d322bbbd1ba209b7a749c2cbcdcb3fdd1df8eb488b3279c1c8"
url: "https://pub.dev"
sha256: ba2aa5d8cc609d96bbb2899c28934f9e1af5cddbd60a827822ea467161eb54e7
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.1"
version: "2.1.2"
stream_transform:
dependency: transitive
description:
name: stream_transform
sha256: "14a00e794c7c11aa145a170587321aedce29769c08d7f58b1d141da75e3b1c6f"
url: "https://pub.dev"
sha256: ad47125e588cfd37a9a7f86c7d6356dde8dfe89d071d293f80ca9e9273a33871
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
version: "2.1.1"
string_scanner:
dependency: transitive
description:
name: string_scanner
sha256: "556692adab6cfa87322a115640c11f13cb77b3f076ddcc5d6ae3c20242bedcde"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.0"
term_glyph:
... ... @@ -321,23 +321,23 @@ packages:
description:
name: term_glyph
sha256: a29248a84fbb7c79282b40b8c72a1209db169a2e0542bce341da992fe1bc7e84
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
sha256: "75760ffd7786fffdfb9597c35c5b27eaeec82be8edfb6d71d32651128ed7aab8"
url: "https://pub.dev"
sha256: "9955ae474176f7ac8ee4e989dadfb411a58c30415bcfb648fa04b2b8a03afa7f"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.6.0"
version: "0.7.0"
typed_data:
dependency: transitive
description:
name: typed_data
sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.3.2"
vector_math:
... ... @@ -345,25 +345,33 @@ packages:
description:
name: vector_math
sha256: "80b3257d1492ce4d091729e3a67a60407d227c27241d6927be0130c98e741803"
url: "https://pub.dev"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.4"
vm_service:
dependency: transitive
description:
name: vm_service
sha256: "3923c89304b715fb1eb6423f017651664a03bf5f4b29983627c4da791f74a4ec"
url: "https://pub.flutter-io.cn"
source: hosted
version: "14.2.1"
web:
dependency: transitive
description:
name: web
sha256: dc8ccd225a2005c1be616fe02951e2e342092edf968cf0844220383757ef8f10
url: "https://pub.dev"
sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a"
url: "https://pub.flutter-io.cn"
source: hosted
version: "0.1.4-beta"
version: "1.1.1"
xml:
dependency: transitive
description:
name: xml
sha256: "5bc72e1e45e941d825fd7468b9b4cc3b9327942649aeb6fc5cdbf135f0a86e84"
url: "https://pub.dev"
sha256: b015a8ad1c488f66851d762d3090a21c600e479dc75e68328c52774040cf9226
url: "https://pub.flutter-io.cn"
source: hosted
version: "6.3.0"
version: "6.5.0"
sdks:
dart: ">=3.1.0-185.0.dev <4.0.0"
flutter: ">=3.7.0"
dart: ">=3.4.0 <4.0.0"
flutter: ">=3.22.0"
... ...
... ... @@ -315,4 +315,56 @@ uint32_t *convert_image_nv12_to_rgb(uint8_t *plane0, uint8_t *plane1, int bytesP
{
return _rotaion_image_32bit(src, angleRotation, width, height, background_color);
}
}
uint32_t *convert_image_nv21_to_rgb(uint8_t *plane0, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal)
{
int x, y, uvIndex, index;
int yp, up, vp;
int r, g, b;
int rt, gt, bt;
int frameSize = width * height;
uint32_t *src = malloc(sizeof(uint32_t) * (width * height));
for (x = 0; x < width; ++x)
{
for (y = 0; y < height; ++y)
{
// 获取 Y 分量
index = y * width + x;
yp = plane0[index] & 0xFF;
// 获取 U 和 V 分量
uvIndex = frameSize + (y / 2) * width + (x / 2) * 2;
vp = plane0[uvIndex] & 0xFF;
up = plane0[uvIndex + 1] & 0xFF;
// YUV 转 RGB
rt = (int)(yp + 1.402 * (vp - 128));
gt = (int)(yp - 0.344 * (up - 128) - 0.714 * (vp - 128));
bt = (int)(yp + 1.772 * (up - 128));
r = _clamp(0, 255, rt);
g = _clamp(0, 255, gt);
b = _clamp(0, 255, bt);
src[x + y * width] = (HEXFF << 24) | (b << 16) | (g << 8) | r;
}
}
if (flip_horizontal)
{
_flip_horizontal_32bit(width, height, src);
}
if (flip_vertical)
{
_flip_vertical_32bit(width, height, src);
}
if (angleRotation == 0)
{
return src;
}
else
{
return _rotaion_image_32bit(src, angleRotation, width, height, background_color);
}
}
\ No newline at end of file
... ...
... ... @@ -16,7 +16,7 @@ extern "C"
uint32_t *convert_image_nv12_to_rgb(uint8_t *plane0, uint8_t *plane1, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_yuv420p_to_gray(uint8_t *plane0, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_yuv420p_to_rgb(uint8_t *plane0, uint8_t *plane1, uint8_t *plane2, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_nv21_to_rgb(uint8_t *plane0, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
#ifdef __cplusplus
}
#endif
... ...
... ... @@ -57,3 +57,12 @@ typedef ConvertImageNV12ToRGBC = Pointer<Uint32> Function(Pointer<Uint8>,
Pointer<Uint8>, Int32, Int32, Int32, Int32, Double, Uint32, Bool, Bool);
typedef ConvertImageNV12ToRGBFlutter = Pointer<Uint32> Function(Pointer<Uint8>,
Pointer<Uint8>, int, int, int, int, double, int, bool, bool);
/*
native convert camera image NV21 to rgb
*/
typedef ConvertImageNV21ToRGBC = Pointer<Uint32> Function(Pointer<Uint8>,
Int32, Int32, Double, Uint32, Bool, Bool);
typedef ConvertImageNV21ToRGBFlutter = Pointer<Uint32> Function(Pointer<Uint8>,
int, int, double, int, bool, bool);
\ No newline at end of file
... ...
... ... @@ -33,6 +33,17 @@ abstract class ProcessingCameraImage {
bool isFlipVectical = false,
});
/// [processCameraImageToRGBOHOS]. for IOS with NV21.
imglib.Image? processCameraImageToRGBOHOS({
int? width,
int? height,
Uint8List? plane0,
double? rotationAngle,
int backGroundColor = 0xFFFFFFFF,
bool isFlipHoriozntal = false,
bool isFlipVectical = false,
});
/// [processCameraImageToRGBIOS]. for IOS with NV12.
imglib.Image? processCameraImageToRGBIOS({
int? width,
... ...
... ... @@ -16,6 +16,7 @@ class IProcessingCameraImage implements ProcessingCameraImage {
late final ConvertImageYuv420pToGray8BitFlutter
_convertImageYuv420pToGray8Bit;
late final ConvertImageNV12ToRGBFlutter _convertImageNV12ToRGB;
late final ConvertImageNV21ToRGBFlutter _convertImageNV21ToRGB;
factory IProcessingCameraImage() {
_instance ??= IProcessingCameraImage._();
... ... @@ -44,6 +45,11 @@ class IProcessingCameraImage implements ProcessingCameraImage {
.lookup<NativeFunction<ConvertImageNV12ToRGBC>>(
'convert_image_nv12_to_rgb')
.asFunction<ConvertImageNV12ToRGBFlutter>();
_convertImageNV21ToRGB = convertImageLib
.lookup<NativeFunction<ConvertImageNV21ToRGBC>>(
'convert_image_nv21_to_rgb')
.asFunction<ConvertImageNV21ToRGBFlutter>();
}
/// [ProcessCameraImageToRGB].
... ... @@ -229,6 +235,55 @@ class IProcessingCameraImage implements ProcessingCameraImage {
);
}
/// [processCameraImageToRGBIOS]. for IOS with YUV420.
@override
imglib.Image? processCameraImageToRGBOHOS({
int? width,
int? height,
Uint8List? plane0,
double? rotationAngle,
int backGroundColor = 0xFFFFFFFF,
bool isFlipHoriozntal = false,
bool isFlipVectical = false,
}) {
if (width == null ||
height == null ||
plane0 == null ||
plane0.isEmpty) {
return null;
}
rotationAngle ??= 0;
Pointer<Uint8> p = ffi.malloc.allocate(plane0.length);
Uint8List pointerList = p.asTypedList(plane0.length);
pointerList.setRange(0, plane0.length, plane0);
Pointer<Uint32> imgP = _convertImageNV21ToRGB(
p,
width,
height,
rotationAngle,
backGroundColor,
isFlipVectical,
isFlipHoriozntal,
);
final imgData = imgP.asTypedList(width * height);
imglib.Image img = imglib.Image.fromBytes(
bytes: imgData.buffer,
width: width,
height: height,
order: imglib.ChannelOrder.rgba);
ffi.malloc.free(p);
ffi.malloc.free(imgP);
return img;
}
/// [processCameraImageToRGBIOS]. for IOS with YUV420.
@override
imglib.Image? processCameraImageToRGBIOS({
... ...
... ... @@ -11,5 +11,5 @@ endif()
include_directories(${NATIVERENDER_ROOT_PATH}
${NATIVERENDER_ROOT_PATH}/include)
add_library(convertImage SHARED napi_init.cpp converter.c)
add_library(convertImage SHARED napi_init.cpp ../../../../ios/native/converter.c)
target_link_libraries(convertImage PUBLIC libace_napi.z.so)
\ No newline at end of file
... ...
//
// Created by thuanpm on 4/22/22.
//
#include <stdio.h>
#include "converter.h"
#include <math.h>
#include <stdlib.h>
#define HEXFF 255
int _clamp(int lower, int higher, int val)
{
if (val < lower)
return 0;
else if (val > higher)
return 255;
else
return val;
}
uint32_t *_rotaion_image_32bit(uint32_t *src, double angle, int width, int height, uint32_t background_color)
{
double rad = (angle * M_PI / 180.0);
double sinVal = sin(rad);
double cosVal = cos(rad);
int new_width = (int)(fabs(sinVal * height) + fabs(cosVal * width));
int new_height = (int)(fabs(sinVal * width) + fabs(cosVal * height));
double w2 = 0.5 * width;
double h2 = 0.5 * height;
double dw2 = 0.5 * new_width;
double dh2 = 0.5 * new_height;
uint32_t *dest = malloc(sizeof(uint32_t) * (new_width * new_height));
for (int i = 0; i < new_height; ++i)
{
for (int j = 0; j < new_width; ++j)
{
int oriX = (int)(w2 + (j - dw2) * cosVal + (i - dh2) * sinVal);
int oriY = (int)(h2 - (j - dw2) * sinVal + (i - dh2) * cosVal);
if (oriX >= 0 && oriX < width && oriY >= 0 && oriY < height)
{
dest[i * new_width + j] = src[oriX + oriY * width];
}
else
{
dest[i * new_width + j] = background_color;
}
}
}
free(src);
return dest;
}
uint8_t *_rotaion_image_8bit(uint8_t *src, double angle, int width, int height, uint8_t background_color)
{
double rad = (angle * M_PI / 180.0);
double sinVal = sin(rad);
double cosVal = cos(rad);
int new_width = (int)(fabs(sinVal * height) + fabs(cosVal * width));
int new_height = (int)(fabs(sinVal * width) + fabs(cosVal * height));
double w2 = 0.5 * width;
double h2 = 0.5 * height;
double dw2 = 0.5 * new_width;
double dh2 = 0.5 * new_height;
uint8_t *dest = malloc(sizeof(uint8_t) * (new_width * new_height));
for (int i = 0; i < new_height; ++i)
{
for (int j = 0; j < new_width; ++j)
{
int oriX = (int)(w2 + (j - dw2) * cosVal + (i - dh2) * sinVal);
int oriY = (int)(h2 - (j - dw2) * sinVal + (i - dh2) * cosVal);
if (oriX >= 0 && oriX < width && oriY >= 0 && oriY < height)
{
dest[i * new_width + j] = src[oriX + oriY * width];
}
else
{
dest[i * new_width + j] = background_color;
}
}
}
free(src);
return dest;
}
void _flip_horizontal_32bit(int width, int height, uint32_t *src)
{
int dw2 = (int)(width / 2);
for (int y = 0; y < height; ++y)
{
int y1 = (y * width);
for (int x = 0; x < dw2; ++x)
{
int x2 = (width - 1 - x);
uint32_t t = src[y1 + x2];
src[y1 + x2] = src[y1 + x];
src[y1 + x] = t;
}
}
}
void _flip_horizontal_8bit(int width, int height, uint8_t *src)
{
int dw2 = (int)(width / 2);
for (int y = 0; y < height; ++y)
{
int y1 = (y * width);
for (int x = 0; x < dw2; ++x)
{
int x2 = (width - 1 - x);
uint8_t t = src[y1 + x2];
src[y1 + x2] = src[y1 + x];
src[y1 + x] = t;
}
}
}
void _flip_vertical_32bit(int width, int height, uint32_t *src)
{
int h2 = (int)(height / 2);
for (int y = 0; y < h2; ++y)
{
int y1 = (y * width);
int y2 = (height - 1 - y) * width;
for (int x = 0; x < width; ++x)
{
uint32_t t = src[y2 + x];
src[y2 + x] = src[y1 + x];
src[y1 + x] = t;
}
}
}
void _flip_vertical_8bit(int width, int height, uint8_t *src)
{
int h2 = (int)(height / 2);
for (int y = 0; y < h2; ++y)
{
int y1 = (y * width);
int y2 = (height - 1 - y) * width;
for (int x = 0; x < width; ++x)
{
uint8_t t = src[y2 + x];
src[y2 + x] = src[y1 + x];
src[y1 + x] = t;
}
}
}
uint32_t *convert_image_yuv420p_to_gray(uint8_t *plane0, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal)
{
int x, y;
int yp, index;
uint32_t *src = malloc(sizeof(uint32_t) * (width * height));
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
index = y * width + x;
yp = plane0[index];
src[x + y * width] = (HEXFF << 24) | (yp << 16) | (yp << 8) | yp;
}
}
if (flip_horizontal)
{
_flip_horizontal_32bit(width, height, src);
}
if (flip_vertical)
{
_flip_vertical_32bit(width, height, src);
}
if (angleRotation == 0)
{
return src;
}
else
{
return _rotaion_image_32bit(src, angleRotation, width, height, background_color);
}
}
uint8_t *convert_image_yuv420p_to_gray_8bit(uint8_t *plane0, int width, int height, double angleRotation, uint8_t background_color, bool flip_vertical, bool flip_horizontal)
{
int x, y;
int index;
uint8_t yp;
uint8_t *src = malloc(sizeof(uint8_t) * (width * height));
for (x = 0; x < width; x++)
{
for (y = 0; y < height; y++)
{
index = y * width + x;
yp = plane0[index];
src[x + y * width] = yp;
}
}
if (flip_horizontal)
{
_flip_horizontal_8bit(width, height, src);
}
if (flip_vertical)
{
_flip_vertical_8bit(width, height, src);
}
if (angleRotation == 0)
{
return src;
}
else
{
return _rotaion_image_8bit(src, angleRotation, width, height, background_color);
}
}
uint32_t *convert_image_yuv420p_to_rgb(uint8_t *plane0, uint8_t *plane1, uint8_t *plane2, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal)
{
int x, y, uvIndex, index;
int yp, up, vp;
int r, g, b;
int rt, gt, bt;
uint32_t *src = malloc(sizeof(uint32_t) * (width * height));
for (x = 0; x < width; ++x)
{
for (y = 0; y < height; ++y)
{
uvIndex = bytesPerPixel * ((int)floor(x / 2)) + bytesPerRow * ((int)floor(y / 2));
index = y * width + x;
yp = plane0[index];
up = plane1[uvIndex];
vp = plane2[uvIndex];
rt = round(yp + vp * 1436 / 1024 - 179);
gt = round(yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91);
bt = round(yp + up * 1814 / 1024 - 227);
r = _clamp(0, 255, rt);
g = _clamp(0, 255, gt);
b = _clamp(0, 255, bt);
src[x + y * width] = (HEXFF << 24) | (b << 16) | (g << 8) | r;
}
}
if (flip_horizontal)
{
_flip_horizontal_32bit(width, height, src);
}
if (flip_vertical)
{
_flip_vertical_32bit(width, height, src);
}
if (angleRotation == 0)
{
return src;
}
else
{
return _rotaion_image_32bit(src, angleRotation, width, height, background_color);
}
}
uint32_t *convert_image_nv12_to_rgb(uint8_t *plane0, uint8_t *plane1, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal)
{
int x, y, uvIndex, index;
int yp, up, vp;
int r, g, b;
int rt, gt, bt;
uint32_t *src = malloc(sizeof(uint32_t) * (width * height));
for (x = 0; x < width; ++x)
{
for (y = 0; y < height; ++y)
{
uvIndex = bytesPerPixel * ((int)floor(x / 2)) + bytesPerRow * ((int)floor(y / 2));
index = y * width + x;
yp = plane0[index];
up = plane1[uvIndex];
vp = plane1[uvIndex + 1];
rt = round(yp + vp * 1436 / 1024 - 179);
gt = round(yp - up * 46549 / 131072 + 44 - vp * 93604 / 131072 + 91);
bt = round(yp + up * 1814 / 1024 - 227);
r = _clamp(0, 255, rt);
g = _clamp(0, 255, gt);
b = _clamp(0, 255, bt);
src[x + y * width] = (HEXFF << 24) | (b << 16) | (g << 8) | r;
}
}
if (flip_horizontal)
{
_flip_horizontal_32bit(width, height, src);
}
if (flip_vertical)
{
_flip_vertical_32bit(width, height, src);
}
if (angleRotation == 0)
{
return src;
}
else
{
return _rotaion_image_32bit(src, angleRotation, width, height, background_color);
}
}
\ No newline at end of file
//
// Created by thuanpm on 4/22/22.
//
#ifndef PROCESSING_CAMERA_IMAGE_CONVERTER_H
#define PROCESSING_CAMERA_IMAGE_CONVERTER_H
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdbool.h>
uint8_t *convert_image_yuv420p_to_gray_8bit(uint8_t *plane0, int width, int height, double angleRotation, uint8_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_nv12_to_rgb(uint8_t *plane0, uint8_t *plane1, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_yuv420p_to_gray(uint8_t *plane0, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
uint32_t *convert_image_yuv420p_to_rgb(uint8_t *plane0, uint8_t *plane1, uint8_t *plane2, int bytesPerRow, int bytesPerPixel, int width, int height, double angleRotation, uint32_t background_color, bool flip_vertical, bool flip_horizontal);
#ifdef __cplusplus
}
#endif
#endif // PROCESSING_CAMERA_IMAGE_CONVERTER_H
\ No newline at end of file