|
1 | 1 | package de.berlindroid.zepatch.ui |
2 | 2 |
|
3 | | -import android.graphics.BitmapFactory |
4 | | -import android.util.Log |
5 | 3 | import androidx.compose.foundation.Image |
6 | 4 | import androidx.compose.foundation.layout.Column |
7 | 5 | import androidx.compose.foundation.layout.fillMaxWidth |
8 | 6 | import androidx.compose.material3.Button |
9 | 7 | import androidx.compose.material3.CircularProgressIndicator |
10 | 8 | import androidx.compose.material3.Text |
11 | 9 | import androidx.compose.runtime.Composable |
12 | | -import androidx.compose.runtime.getValue |
13 | | -import androidx.compose.runtime.mutableStateOf |
14 | | -import androidx.compose.runtime.remember |
15 | | -import androidx.compose.runtime.rememberCoroutineScope |
16 | | -import androidx.compose.runtime.setValue |
17 | 10 | import androidx.compose.ui.Modifier |
18 | 11 | import androidx.compose.ui.graphics.ImageBitmap |
19 | 12 | import androidx.compose.ui.graphics.asAndroidBitmap |
20 | | -import androidx.compose.ui.graphics.asImageBitmap |
21 | | -import androidx.compose.ui.platform.LocalContext |
22 | 13 | import androidx.compose.ui.tooling.preview.Preview |
23 | | -import androidx.core.graphics.scale |
24 | 14 | import com.embroidermodder.punching.Histogram |
25 | 15 | import com.embroidermodder.punching.colorHistogram |
26 | | -import de.berlindroid.zepatch.stiches.StitchToPES |
27 | | -import de.berlindroid.zepatch.stiches.StitchToPES.createEmbroideryFromBitmap |
28 | | -import de.berlindroid.zepatch.utils.multiLet |
29 | | -import kotlinx.coroutines.Dispatchers |
30 | | -import kotlinx.coroutines.launch |
31 | 16 |
|
32 | 17 | @Composable |
33 | 18 | fun BitmapToStitches( |
34 | 19 | modifier: Modifier = Modifier, |
35 | 20 | reducedImageBitmap: ImageBitmap, |
36 | 21 | reducedHistogram: Histogram, |
37 | 22 | name: String, |
38 | | - onEmbroidery: (pes: ByteArray, preview: ImageBitmap) -> Unit |
| 23 | + onCreateEmbroidery: (name: String, bitmap: ImageBitmap, histogram: Histogram) -> Unit, |
| 24 | + previewImage: ImageBitmap?, |
| 25 | + creatingEmbroidery: Boolean, |
39 | 26 | ) { |
40 | | - val coroutineScope = rememberCoroutineScope() |
41 | | - val context = LocalContext.current |
42 | | - var displayImage by remember { mutableStateOf<ImageBitmap?>(null) } |
43 | | - |
44 | 27 | Column(modifier = modifier.fillMaxWidth()) { |
45 | 28 | Image( |
46 | 29 | bitmap = reducedImageBitmap, |
47 | 30 | contentDescription = "patch bitmap", |
48 | 31 | modifier = Modifier.fillMaxWidth() |
49 | 32 | ) |
50 | 33 | Button(onClick = { |
51 | | - coroutineScope.launch(Dispatchers.IO) { |
52 | | - val aspect = reducedImageBitmap.width / reducedImageBitmap.height.toFloat() |
53 | | - val embroidery = createEmbroideryFromBitmap( |
54 | | - name, |
55 | | - bitmap = reducedImageBitmap.asAndroidBitmap(), |
56 | | - histogram = reducedHistogram, |
57 | | - mmWidth = 500f * aspect, |
58 | | - mmHeight = 500f, |
59 | | - mmDensityX = 4f, |
60 | | - mmDensityY = 2f, |
61 | | - ) |
62 | | - |
63 | | - val pes = StitchToPES.convert(context, embroidery) |
64 | | - if (pes == null || pes.isEmpty()) { |
65 | | - Log.e("EMBNO", "No pes found.") |
66 | | - } |
67 | | - |
68 | | - val png = StitchToPES.convert(context, embroidery, "png") |
69 | | - displayImage = if (png == null || png.isEmpty()) { |
70 | | - Log.e("PNGNO", "No png returned.") |
71 | | - null |
72 | | - } else { |
73 | | - val decoded = BitmapFactory.decodeByteArray(png, 0, png.size) |
74 | | - |
75 | | - decoded |
76 | | - .scale(decoded.width * 2, decoded.height * 2) |
77 | | - .asImageBitmap() |
78 | | - } |
79 | | - |
80 | | - pes?.multiLet(displayImage) { pes, image -> |
81 | | - onEmbroidery( |
82 | | - pes, image |
83 | | - ) |
84 | | - } |
85 | | - } |
86 | | - }) { Text("Do it") } // Compose the content through the composable utility; it will invoke the callback when ready. |
| 34 | + onCreateEmbroidery(name, reducedImageBitmap, reducedHistogram) |
| 35 | + }) { Text("Do it") } |
87 | 36 |
|
88 | | - displayImage?.let { |
| 37 | + val preview = previewImage |
| 38 | + if (preview != null) { |
89 | 39 | Image( |
90 | | - bitmap = it, |
| 40 | + bitmap = preview, |
91 | 41 | contentDescription = "patch bitmap", |
92 | 42 | modifier = Modifier.fillMaxWidth() |
93 | 43 | ) |
94 | | - } ?: CircularProgressIndicator() |
| 44 | + } else if (creatingEmbroidery) { |
| 45 | + CircularProgressIndicator() |
| 46 | + } |
95 | 47 | } |
96 | 48 | } |
97 | 49 |
|
98 | 50 | @Preview |
99 | 51 | @Composable |
100 | 52 | fun BitmapToStitchesPreview() { |
101 | | - val imageBitmap = |
102 | | - ImageBitmap(width = 100, height = 100) // Replace with a real ImageBitmap if needed |
| 53 | + val imageBitmap = ImageBitmap(width = 100, height = 100) |
103 | 54 | val histogram = imageBitmap.asAndroidBitmap().colorHistogram() |
104 | 55 | BitmapToStitches( |
105 | 56 | reducedImageBitmap = imageBitmap, |
106 | 57 | reducedHistogram = histogram, |
107 | 58 | name = "MyPatch", |
108 | | - onEmbroidery = { _, _ -> } |
| 59 | + onCreateEmbroidery = { _, _, _ -> }, |
| 60 | + previewImage = null, |
| 61 | + creatingEmbroidery = false, |
109 | 62 | ) |
110 | 63 | } |
111 | 64 |
|
0 commit comments