bare-ico 1.0.0-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CMakeLists.txt +29 -0
- package/LICENSE +201 -0
- package/README.md +27 -0
- package/binding.c +367 -0
- package/binding.js +1 -0
- package/index.js +20 -0
- package/package.json +38 -0
- package/prebuilds/darwin-arm64/bare-ico.bare +0 -0
package/CMakeLists.txt
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
cmake_minimum_required(VERSION 3.31)
|
|
2
|
+
|
|
3
|
+
find_package(cmake-bare REQUIRED PATHS node_modules/cmake-bare)
|
|
4
|
+
find_package(cmake-fetch REQUIRED PATHS node_modules/cmake-fetch)
|
|
5
|
+
|
|
6
|
+
project(bare_ico C)
|
|
7
|
+
|
|
8
|
+
# Fetch stb_image.h for robust image decoding
|
|
9
|
+
include(FetchContent)
|
|
10
|
+
FetchContent_Declare(
|
|
11
|
+
stb
|
|
12
|
+
URL https://github.com/nothings/stb/archive/refs/heads/master.zip
|
|
13
|
+
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
|
|
14
|
+
)
|
|
15
|
+
FetchContent_MakeAvailable(stb)
|
|
16
|
+
|
|
17
|
+
add_bare_module(bare_ico)
|
|
18
|
+
|
|
19
|
+
target_sources(
|
|
20
|
+
${bare_ico}
|
|
21
|
+
PRIVATE
|
|
22
|
+
binding.c
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
target_include_directories(
|
|
26
|
+
${bare_ico}
|
|
27
|
+
PRIVATE
|
|
28
|
+
${stb_SOURCE_DIR}
|
|
29
|
+
)
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Holepunch Inc
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# bare-ico
|
|
2
|
+
|
|
3
|
+
ICO support for Bare.
|
|
4
|
+
|
|
5
|
+
```
|
|
6
|
+
npm i bare-ico
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```js
|
|
12
|
+
const ico = require('bare-ico')
|
|
13
|
+
|
|
14
|
+
const image = require('./my-image.ico', { with: { type: 'binary' } })
|
|
15
|
+
|
|
16
|
+
const decoded = ico.decode(image)
|
|
17
|
+
// {
|
|
18
|
+
// width: 200,
|
|
19
|
+
// height: 400,
|
|
20
|
+
// data: <Buffer>,
|
|
21
|
+
// sizes: [16, 32, 48, 256]
|
|
22
|
+
// }
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
## License
|
|
26
|
+
|
|
27
|
+
Apache-2.0
|
package/binding.c
ADDED
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
#include <assert.h>
|
|
2
|
+
#include <bare.h>
|
|
3
|
+
#include <js.h>
|
|
4
|
+
#include <stdbool.h>
|
|
5
|
+
#include <stdint.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
#define STB_IMAGE_IMPLEMENTATION
|
|
10
|
+
#define STBI_NO_STDIO
|
|
11
|
+
#include "stb_image.h"
|
|
12
|
+
|
|
13
|
+
// ICO file format structures
|
|
14
|
+
typedef struct __attribute__((packed)) {
|
|
15
|
+
uint16_t reserved; // Must be 0
|
|
16
|
+
uint16_t type; // 1 for ICO, 2 for CUR
|
|
17
|
+
uint16_t count; // Number of images
|
|
18
|
+
} IconDir;
|
|
19
|
+
|
|
20
|
+
typedef struct __attribute__((packed)) {
|
|
21
|
+
uint8_t width; // Width (0 = 256)
|
|
22
|
+
uint8_t height; // Height (0 = 256)
|
|
23
|
+
uint8_t color_count; // Number of colors (0 if >= 8bpp)
|
|
24
|
+
uint8_t reserved; // Must be 0
|
|
25
|
+
uint16_t planes; // Color planes
|
|
26
|
+
uint16_t bit_count; // Bits per pixel
|
|
27
|
+
uint32_t bytes_in_res; // Size of image data
|
|
28
|
+
uint32_t image_offset; // Offset to image data
|
|
29
|
+
} IconDirEntry;
|
|
30
|
+
|
|
31
|
+
static void
|
|
32
|
+
bare_ico__on_finalize(js_env_t *env, void *data, void *finalize_hint) {
|
|
33
|
+
free(data);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Decode ICO file to RGBA format
|
|
38
|
+
* Extracts largest image or closest to requested size
|
|
39
|
+
*
|
|
40
|
+
* Arguments:
|
|
41
|
+
* - buffer: ICO data
|
|
42
|
+
* - options: { size? }
|
|
43
|
+
*
|
|
44
|
+
* Returns: {width, height, data, sizes: [available sizes]}
|
|
45
|
+
*/
|
|
46
|
+
static js_value_t *
|
|
47
|
+
bare_ico_decode(js_env_t *env, js_callback_info_t *info) {
|
|
48
|
+
int err;
|
|
49
|
+
|
|
50
|
+
size_t argc = 2;
|
|
51
|
+
js_value_t *argv[2];
|
|
52
|
+
|
|
53
|
+
err = js_get_callback_info(env, info, &argc, argv, NULL, NULL);
|
|
54
|
+
assert(err == 0);
|
|
55
|
+
assert(argc >= 1);
|
|
56
|
+
|
|
57
|
+
// Get ICO buffer
|
|
58
|
+
uint8_t *ico_data;
|
|
59
|
+
size_t ico_len;
|
|
60
|
+
err = js_get_typedarray_info(env, argv[0], NULL, (void **) &ico_data, &ico_len, NULL, NULL);
|
|
61
|
+
assert(err == 0);
|
|
62
|
+
|
|
63
|
+
if (ico_len < sizeof(IconDir)) {
|
|
64
|
+
err = js_throw_error(env, NULL, "Invalid ICO file: too small");
|
|
65
|
+
assert(err == 0);
|
|
66
|
+
return NULL;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Parse ICO header
|
|
70
|
+
IconDir *header = (IconDir *) ico_data;
|
|
71
|
+
if (header->reserved != 0 || header->type != 1) {
|
|
72
|
+
err = js_throw_error(env, NULL, "Invalid ICO file: bad header");
|
|
73
|
+
assert(err == 0);
|
|
74
|
+
return NULL;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
uint16_t count = header->count;
|
|
78
|
+
if (count == 0 || ico_len < sizeof(IconDir) + count * sizeof(IconDirEntry)) {
|
|
79
|
+
err = js_throw_error(env, NULL, "Invalid ICO file: bad entry count");
|
|
80
|
+
assert(err == 0);
|
|
81
|
+
return NULL;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// Parse requested size
|
|
85
|
+
int requested_size = 0;
|
|
86
|
+
if (argc >= 2) {
|
|
87
|
+
bool has_size;
|
|
88
|
+
err = js_has_named_property(env, argv[1], "size", &has_size);
|
|
89
|
+
assert(err == 0);
|
|
90
|
+
|
|
91
|
+
if (has_size) {
|
|
92
|
+
js_value_t *size_val;
|
|
93
|
+
err = js_get_named_property(env, argv[1], "size", &size_val);
|
|
94
|
+
assert(err == 0);
|
|
95
|
+
|
|
96
|
+
int64_t s;
|
|
97
|
+
err = js_get_value_int64(env, size_val, &s);
|
|
98
|
+
assert(err == 0);
|
|
99
|
+
requested_size = (int) s;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Find best matching image
|
|
104
|
+
IconDirEntry *entries = (IconDirEntry *) (ico_data + sizeof(IconDir));
|
|
105
|
+
int best_idx = 0;
|
|
106
|
+
int best_size = entries[0].width == 0 ? 256 : entries[0].width;
|
|
107
|
+
|
|
108
|
+
if (requested_size > 0) {
|
|
109
|
+
// Find closest size to requested
|
|
110
|
+
int best_diff = abs(best_size - requested_size);
|
|
111
|
+
for (int i = 1; i < count; i++) {
|
|
112
|
+
int w = entries[i].width == 0 ? 256 : entries[i].width;
|
|
113
|
+
int diff = abs(w - requested_size);
|
|
114
|
+
if (diff < best_diff) {
|
|
115
|
+
best_diff = diff;
|
|
116
|
+
best_size = w;
|
|
117
|
+
best_idx = i;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
} else {
|
|
121
|
+
// Find largest image
|
|
122
|
+
for (int i = 1; i < count; i++) {
|
|
123
|
+
int w = entries[i].width == 0 ? 256 : entries[i].width;
|
|
124
|
+
if (w > best_size) {
|
|
125
|
+
best_size = w;
|
|
126
|
+
best_idx = i;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Extract image data
|
|
132
|
+
IconDirEntry *entry = &entries[best_idx];
|
|
133
|
+
uint32_t offset = entry->image_offset;
|
|
134
|
+
uint32_t size = entry->bytes_in_res;
|
|
135
|
+
|
|
136
|
+
if (offset + size > ico_len) {
|
|
137
|
+
err = js_throw_error(env, NULL, "Invalid ICO file: image data out of bounds");
|
|
138
|
+
assert(err == 0);
|
|
139
|
+
return NULL;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
uint8_t *img_data = ico_data + offset;
|
|
143
|
+
|
|
144
|
+
// Check if PNG (magic: 0x89 0x50 0x4E 0x47)
|
|
145
|
+
bool is_png = (size >= 8 && img_data[0] == 0x89 && img_data[1] == 0x50 &&
|
|
146
|
+
img_data[2] == 0x4E && img_data[3] == 0x47);
|
|
147
|
+
|
|
148
|
+
int width, height, channels;
|
|
149
|
+
uint8_t *rgba_data = NULL;
|
|
150
|
+
|
|
151
|
+
if (is_png) {
|
|
152
|
+
// PNG format - can decode directly
|
|
153
|
+
rgba_data = stbi_load_from_memory(
|
|
154
|
+
img_data, size, &width, &height, &channels, 4
|
|
155
|
+
);
|
|
156
|
+
} else {
|
|
157
|
+
// BMP-in-ICO format: has DIB header but no BITMAPFILEHEADER
|
|
158
|
+
// Height is doubled (includes AND mask)
|
|
159
|
+
// We need to decode the BMP ourselves
|
|
160
|
+
|
|
161
|
+
if (size < 40) {
|
|
162
|
+
err = js_throw_error(env, NULL, "Invalid BMP-in-ICO: too small");
|
|
163
|
+
assert(err == 0);
|
|
164
|
+
return NULL;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Read BMP DIB header
|
|
168
|
+
uint32_t header_size;
|
|
169
|
+
int32_t bmp_width, bmp_height;
|
|
170
|
+
uint16_t bmp_planes, bmp_bpp;
|
|
171
|
+
uint32_t bmp_compression;
|
|
172
|
+
|
|
173
|
+
memcpy(&header_size, img_data + 0, 4);
|
|
174
|
+
memcpy(&bmp_width, img_data + 4, 4);
|
|
175
|
+
memcpy(&bmp_height, img_data + 8, 4);
|
|
176
|
+
memcpy(&bmp_planes, img_data + 12, 2);
|
|
177
|
+
memcpy(&bmp_bpp, img_data + 14, 2);
|
|
178
|
+
memcpy(&bmp_compression, img_data + 16, 4);
|
|
179
|
+
|
|
180
|
+
// Validate
|
|
181
|
+
if (header_size < 40 || bmp_width <= 0 || bmp_height <= 0) {
|
|
182
|
+
err = js_throw_error(env, NULL, "Invalid BMP-in-ICO header");
|
|
183
|
+
assert(err == 0);
|
|
184
|
+
return NULL;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// ICO BMP height is doubled (image data + AND mask)
|
|
188
|
+
int32_t actual_height = bmp_height / 2;
|
|
189
|
+
|
|
190
|
+
// Calculate row size (must be multiple of 4)
|
|
191
|
+
int row_size = ((bmp_width * bmp_bpp + 31) / 32) * 4;
|
|
192
|
+
int pixel_data_size = row_size * actual_height;
|
|
193
|
+
|
|
194
|
+
// Allocate output RGBA buffer
|
|
195
|
+
width = bmp_width;
|
|
196
|
+
height = actual_height;
|
|
197
|
+
rgba_data = (uint8_t *)malloc(width * height * 4);
|
|
198
|
+
if (!rgba_data) {
|
|
199
|
+
err = js_throw_error(env, NULL, "Memory allocation failed");
|
|
200
|
+
assert(err == 0);
|
|
201
|
+
return NULL;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Find pixel data start (after header and color palette if present)
|
|
205
|
+
uint8_t *pixel_start = img_data + header_size;
|
|
206
|
+
if (bmp_bpp <= 8) {
|
|
207
|
+
// Has color palette
|
|
208
|
+
int palette_size = (1 << bmp_bpp) * 4;
|
|
209
|
+
pixel_start += palette_size;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// Decode based on bit depth
|
|
213
|
+
if (bmp_bpp == 32 && bmp_compression == 0) {
|
|
214
|
+
// 32-bit RGBA - direct copy with BGR→RGB conversion
|
|
215
|
+
for (int y = 0; y < actual_height; y++) {
|
|
216
|
+
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
|
|
217
|
+
uint8_t *dst = rgba_data + (y * width * 4);
|
|
218
|
+
for (int x = 0; x < width; x++) {
|
|
219
|
+
dst[x * 4 + 0] = src[x * 4 + 2]; // R
|
|
220
|
+
dst[x * 4 + 1] = src[x * 4 + 1]; // G
|
|
221
|
+
dst[x * 4 + 2] = src[x * 4 + 0]; // B
|
|
222
|
+
dst[x * 4 + 3] = src[x * 4 + 3]; // A
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
} else if (bmp_bpp == 24 && bmp_compression == 0) {
|
|
226
|
+
// 24-bit RGB - convert to RGBA
|
|
227
|
+
for (int y = 0; y < actual_height; y++) {
|
|
228
|
+
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
|
|
229
|
+
uint8_t *dst = rgba_data + (y * width * 4);
|
|
230
|
+
for (int x = 0; x < width; x++) {
|
|
231
|
+
dst[x * 4 + 0] = src[x * 3 + 2]; // R
|
|
232
|
+
dst[x * 4 + 1] = src[x * 3 + 1]; // G
|
|
233
|
+
dst[x * 4 + 2] = src[x * 3 + 0]; // B
|
|
234
|
+
dst[x * 4 + 3] = 255; // A
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
} else if (bmp_bpp == 8 && bmp_compression == 0) {
|
|
238
|
+
// 8-bit indexed color - use palette
|
|
239
|
+
uint8_t *palette = img_data + header_size;
|
|
240
|
+
for (int y = 0; y < actual_height; y++) {
|
|
241
|
+
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
|
|
242
|
+
uint8_t *dst = rgba_data + (y * width * 4);
|
|
243
|
+
for (int x = 0; x < width; x++) {
|
|
244
|
+
uint8_t index = src[x];
|
|
245
|
+
dst[x * 4 + 0] = palette[index * 4 + 2]; // R
|
|
246
|
+
dst[x * 4 + 1] = palette[index * 4 + 1]; // G
|
|
247
|
+
dst[x * 4 + 2] = palette[index * 4 + 0]; // B
|
|
248
|
+
dst[x * 4 + 3] = 255; // A
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
} else if (bmp_bpp == 4 && bmp_compression == 0) {
|
|
252
|
+
// 4-bit indexed color - use palette
|
|
253
|
+
uint8_t *palette = img_data + header_size;
|
|
254
|
+
for (int y = 0; y < actual_height; y++) {
|
|
255
|
+
uint8_t *src = pixel_start + ((actual_height - 1 - y) * row_size);
|
|
256
|
+
uint8_t *dst = rgba_data + (y * width * 4);
|
|
257
|
+
for (int x = 0; x < width; x++) {
|
|
258
|
+
uint8_t byte = src[x / 2];
|
|
259
|
+
uint8_t index;
|
|
260
|
+
if (x % 2 == 0) {
|
|
261
|
+
index = (byte >> 4) & 0x0F;
|
|
262
|
+
} else {
|
|
263
|
+
index = byte & 0x0F;
|
|
264
|
+
}
|
|
265
|
+
dst[x * 4 + 0] = palette[index * 4 + 2]; // R
|
|
266
|
+
dst[x * 4 + 1] = palette[index * 4 + 1]; // G
|
|
267
|
+
dst[x * 4 + 2] = palette[index * 4 + 0]; // B
|
|
268
|
+
dst[x * 4 + 3] = 255; // A (opaque)
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
// Unsupported format
|
|
273
|
+
free(rgba_data);
|
|
274
|
+
char msg[100];
|
|
275
|
+
snprintf(msg, sizeof(msg), "Unsupported BMP format: %d-bit, compression=%d", bmp_bpp, bmp_compression);
|
|
276
|
+
err = js_throw_error(env, NULL, msg);
|
|
277
|
+
assert(err == 0);
|
|
278
|
+
return NULL;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!rgba_data) {
|
|
283
|
+
err = js_throw_error(env, NULL, "Failed to decode ICO image");
|
|
284
|
+
assert(err == 0);
|
|
285
|
+
return NULL;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// Create result object
|
|
289
|
+
js_value_t *result;
|
|
290
|
+
err = js_create_object(env, &result);
|
|
291
|
+
assert(err == 0);
|
|
292
|
+
|
|
293
|
+
// Set width
|
|
294
|
+
js_value_t *width_val;
|
|
295
|
+
err = js_create_int64(env, width, &width_val);
|
|
296
|
+
assert(err == 0);
|
|
297
|
+
err = js_set_named_property(env, result, "width", width_val);
|
|
298
|
+
assert(err == 0);
|
|
299
|
+
|
|
300
|
+
// Set height
|
|
301
|
+
js_value_t *height_val;
|
|
302
|
+
err = js_create_int64(env, height, &height_val);
|
|
303
|
+
assert(err == 0);
|
|
304
|
+
err = js_set_named_property(env, result, "height", height_val);
|
|
305
|
+
assert(err == 0);
|
|
306
|
+
|
|
307
|
+
// Set data
|
|
308
|
+
size_t data_len = width * height * 4;
|
|
309
|
+
uint8_t *result_data = malloc(data_len);
|
|
310
|
+
if (!result_data) {
|
|
311
|
+
stbi_image_free(rgba_data);
|
|
312
|
+
err = js_throw_error(env, NULL, "Memory allocation failed");
|
|
313
|
+
assert(err == 0);
|
|
314
|
+
return NULL;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
memcpy(result_data, rgba_data, data_len);
|
|
318
|
+
stbi_image_free(rgba_data);
|
|
319
|
+
|
|
320
|
+
js_value_t *buffer;
|
|
321
|
+
err = js_create_external_arraybuffer(
|
|
322
|
+
env, result_data, data_len, bare_ico__on_finalize, NULL, &buffer
|
|
323
|
+
);
|
|
324
|
+
assert(err == 0);
|
|
325
|
+
err = js_set_named_property(env, result, "data", buffer);
|
|
326
|
+
assert(err == 0);
|
|
327
|
+
|
|
328
|
+
// Set available sizes array
|
|
329
|
+
js_value_t *sizes_array;
|
|
330
|
+
err = js_create_array_with_length(env, count, &sizes_array);
|
|
331
|
+
assert(err == 0);
|
|
332
|
+
|
|
333
|
+
for (int i = 0; i < count; i++) {
|
|
334
|
+
int w = entries[i].width == 0 ? 256 : entries[i].width;
|
|
335
|
+
js_value_t *size_val;
|
|
336
|
+
err = js_create_int64(env, w, &size_val);
|
|
337
|
+
assert(err == 0);
|
|
338
|
+
err = js_set_element(env, sizes_array, i, size_val);
|
|
339
|
+
assert(err == 0);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
err = js_set_named_property(env, result, "sizes", sizes_array);
|
|
343
|
+
assert(err == 0);
|
|
344
|
+
|
|
345
|
+
return result;
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static js_value_t *
|
|
349
|
+
bare_ico_exports(js_env_t *env, js_value_t *exports) {
|
|
350
|
+
int err;
|
|
351
|
+
|
|
352
|
+
#define V(name, fn) \
|
|
353
|
+
{ \
|
|
354
|
+
js_value_t *val; \
|
|
355
|
+
err = js_create_function(env, name, -1, fn, NULL, &val); \
|
|
356
|
+
assert(err == 0); \
|
|
357
|
+
err = js_set_named_property(env, exports, name, val); \
|
|
358
|
+
assert(err == 0); \
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
V("decode", bare_ico_decode)
|
|
362
|
+
#undef V
|
|
363
|
+
|
|
364
|
+
return exports;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
BARE_MODULE(bare_ico, bare_ico_exports)
|
package/binding.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require.addon()
|
package/index.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const binding = require('./binding')
|
|
2
|
+
|
|
3
|
+
exports.decode = function decode(ico, opts = {}) {
|
|
4
|
+
const result = binding.decode(ico, opts)
|
|
5
|
+
|
|
6
|
+
return {
|
|
7
|
+
width: result.width,
|
|
8
|
+
height: result.height,
|
|
9
|
+
data: Buffer.from(result.data),
|
|
10
|
+
sizes: result.sizes
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
exports.encode = function encode(rgba, opts = {}) {
|
|
15
|
+
throw new Error('ICO encoding not yet implemented')
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
exports.encodeAnimated = function encodeAnimated(frames, opts = {}) {
|
|
19
|
+
throw new Error('Animated ICO not supported')
|
|
20
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "bare-ico",
|
|
3
|
+
"version": "1.0.0-0",
|
|
4
|
+
"description": "ICO support for Bare",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"exports": {
|
|
7
|
+
"./package": "./package.json",
|
|
8
|
+
".": "./index.js"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"index.js",
|
|
12
|
+
"binding.c",
|
|
13
|
+
"binding.js",
|
|
14
|
+
"CMakeLists.txt",
|
|
15
|
+
"prebuilds"
|
|
16
|
+
],
|
|
17
|
+
"addon": true,
|
|
18
|
+
"scripts": {
|
|
19
|
+
"test": "prettier . --check && bare test.js"
|
|
20
|
+
},
|
|
21
|
+
"repository": {
|
|
22
|
+
"type": "git",
|
|
23
|
+
"url": "git+https://github.com/holepunchto/bare-ico.git"
|
|
24
|
+
},
|
|
25
|
+
"author": "Holepunch Inc",
|
|
26
|
+
"license": "Apache-2.0",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/holepunchto/bare-ico/issues"
|
|
29
|
+
},
|
|
30
|
+
"homepage": "https://github.com/holepunchto/bare-ico#readme",
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"brittle": "^3.4.0",
|
|
33
|
+
"cmake-bare": "^1.1.2",
|
|
34
|
+
"cmake-fetch": "^1.0.0",
|
|
35
|
+
"prettier": "^3.3.3",
|
|
36
|
+
"prettier-config-holepunch": "^2.0.0"
|
|
37
|
+
}
|
|
38
|
+
}
|
|
Binary file
|