gamnit: use API 19 services to fix premultiplied texture on Android
[nit.git] / lib / gamnit / android19.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Variation using features from Android API 19
16 #
17 # Add support for `TextureAsset::premultiply_alpha = false` on Android.
18 module android19 is
19 android_api_min 19
20 android_api_target 22
21 end
22
23 import android
24 intrude import display_android
25 intrude import android::load_image
26
27 in "Java" `{
28 import android.graphics.Bitmap;
29 import android.graphics.BitmapFactory;
30 `}
31
32 redef class TextureAsset
33
34 redef fun load_bitmap(asset_manager, path)
35 do
36 var stream = asset_manager.native_assets_manager.open(path.to_java_string)
37 return new NativeBitmap.from_stream_ex(stream, premultiply_alpha)
38 end
39 end
40
41 redef class NativeCByteArray
42
43 # The data was not premultiplied, don't unmultiply it
44 redef fun unmultiply(w, h) do end
45 end
46
47 redef class NativeBitmap
48
49 # Load from `input_stream` with optional `premultiply_alpha`
50 new from_stream_ex(input_stream: NativeInputStream, premultiply_alpha: Bool) in "Java" `{
51 BitmapFactory.Options opts = new BitmapFactory.Options();
52 opts.inPremultiplied = premultiply_alpha; // API 19
53 return BitmapFactory.decodeStream(input_stream, null, opts);
54 `}
55 end