-
Notifications
You must be signed in to change notification settings - Fork 1
/
bitmap.html
593 lines (583 loc) · 29.3 KB
/
bitmap.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<title> bitmap - in-memory bitmaps </title>
<script src="jquery.js"></script>
<script src="jquery-cookie.js"></script>
<script src="jquery-tablesorter.js"></script>
<script src="strftime.js"></script>
<script src="mustache.js"></script>
<script src="config.js"></script>
<script src="main.js"></script>
<link rel="stylesheet" type="text/css" href="templates/reset.css" />
<link rel="stylesheet" type="text/css" href="templates/hack.css" />
<link rel="stylesheet" type="text/css" id="lights_css" />
<script>
var DOCNAME = 'bitmap'
var PROJECT = 'bitmap'
//set the lights before rendering starts
set_lights()
</script>
</head>
<body>
<header>
<div class="container">
<div class="btn-container btn-lights-container">
<a href="#" class="btn btn-lights" id="lights">lights</a>
</div>
<div class="btn-container btn-home-container">
<a href="/" class="btn btn-home">luapower</a>
</div>
<table id="header_table">
<tr>
<td style="vertical-align: middle;" width="100%">
<h1> bitmap </h1>
<h2> in-memory bitmaps </h2>
</td>
<td style="vertical-align: middle;" align="right" style="height: 150px">
<table><tr><td>
<div class="doc" id="package_info_container">
<div id="package_info"> </div>
<div id="commit_log"> </div>
</div>
<a href="https://github.com/luapower/bitmap" class="btn btn-rightside btn-github"><span class="icon"></span>View on GitHub</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/bitmap/tarball/master" class="btn btn-rightside">Download as .tar.gz</a>
</td></tr><tr><td>
<a href="https://github.com/luapower/bitmap/zipball/master" class="btn btn-rightside">Download as .zip</a>
</td></tr></table>
</td>
</tr>
</table>
<div class="btn-container btn-discuss-container">
<a href="https://github.com/luapower/bitmap/issues/new" target="_blank"
class="btn btn-rightside btn-discuss"><span class="icon"></span>Discuss</a>
</div>
</div>
</header>
<div class="bg-container">
<div class="bg-center-container">
<div class="bg bg-bitmap" style="background-image: url('bg/bitmap.png');"></div>
</div>
</div>
<div class="under-header">
<div class="container">
<div id="toc_container" class="toc_container doc"></div>
<button id=tab1_button class="tab_button hidden">Documentation</button>
<button id=tab2_button class="tab_button hidden">Package Info</button>
<div id="tabs_cointainer">
<div id="tab1_container">
<section class="doc">
<span id="module_info"></span>
<h2 id="local-bitmap-requirebitmap"><code>local bitmap = require'bitmap'</code></h2>
<h2 id="features">Features</h2>
<ul>
<li>multiple pixel formats, color spaces, channel layouts, scanline orderings, row strides, and bit depths.
<ul>
<li>arbitrary row strides, including sub-byte strides.</li>
<li>top-down and bottom-up scanline order.</li>
</ul></li>
<li>conversion between most formats.</li>
<li>reading and writing pixel data in a uniform way, independent of the pixel format.</li>
<li>dithering, pixel effects, filters.</li>
<li>fast (see benchmarks).</li>
</ul>
<h2 id="limitations">Limitations</h2>
<ul>
<li>only packed formats, no separate plane formats
<ul>
<li>but: custom conversions to gray8 and gray16 can be used to separate the channels of any format into separate bitmaps.</li>
</ul></li>
<li>only expanded formats, no palette formats
<ul>
<li>but: custom formats with a custom reader and writer can be easily made to use a palette which itself can be a one-row bitmap.</li>
</ul></li>
<li>no conversions to cmyk (would need color profiling)</li>
<li>no conversions to ycc and ycck</li>
</ul>
<h2 id="whats-a-bitmap">What's a bitmap?</h2>
<p>A bitmap is a table with the following fields:</p>
<ul>
<li><code>w</code>, <code>h</code> - bitmap dimensions, in pixels.</li>
<li><code>stride</code> - row stride in bytes. must be at least <code>w * bpp / 8</code> (can be fractional for < 8bpp formats).</li>
<li><code>bottom_up</code> - if <code>true</code>, the rows are are arranged bottom-up instead of top-down.</li>
<li><code>data</code> - the pixel buffer (string or a cdata buffer). the pixels must be packed in <code>stride</code>-long rows, top-down or bottom-up.</li>
<li><code>size</code> - size of the pixel buffer, in bytes.</li>
<li><code>format</code> - the pixel format, either a string naming a predefined format (below table), or a table specifying a custom format (see customization).</li>
</ul>
<h3 id="predefined-formats">Predefined formats</h3>
<table>
<thead>
<tr class="header">
<th align="left"><strong>name</strong></th>
<th align="left"><strong>colortype</strong></th>
<th align="left"><strong>channels</strong></th>
<th align="left"><strong>bits/channel</strong></th>
<th align="left"><strong>bits/pixel</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">rgb8, bgr8</td>
<td align="left">rgba8</td>
<td align="left">RGB</td>
<td align="left">8</td>
<td align="left">24</td>
</tr>
<tr class="even">
<td align="left">rgb16, bgr16</td>
<td align="left">rgba16</td>
<td align="left">RGB</td>
<td align="left">16</td>
<td align="left">48</td>
</tr>
<tr class="odd">
<td align="left">rgbx8, bgrx8, xrgb8, xbgr8</td>
<td align="left">rgba8</td>
<td align="left">RGB</td>
<td align="left">8</td>
<td align="left">32</td>
</tr>
<tr class="even">
<td align="left">rgbx16, bgrx16, xrgb16, xbgr16</td>
<td align="left">rgba16</td>
<td align="left">RGB</td>
<td align="left">16</td>
<td align="left">64</td>
</tr>
<tr class="odd">
<td align="left">rgba8, bgra8, argb8, abgr8</td>
<td align="left">rgba8</td>
<td align="left">RGB+alpha</td>
<td align="left">8</td>
<td align="left">32</td>
</tr>
<tr class="even">
<td align="left">rgba16, bgra16, argb16, abgr16</td>
<td align="left">rgba16</td>
<td align="left">RGB+alpha</td>
<td align="left">16</td>
<td align="left">64</td>
</tr>
<tr class="odd">
<td align="left">rgb565</td>
<td align="left">rgba8</td>
<td align="left">RGB</td>
<td align="left">5/6/5</td>
<td align="left">16</td>
</tr>
<tr class="even">
<td align="left">rgb555</td>
<td align="left">rgba8</td>
<td align="left">RGB</td>
<td align="left">5</td>
<td align="left">16</td>
</tr>
<tr class="odd">
<td align="left">rgb444</td>
<td align="left">rgba8</td>
<td align="left">RGB</td>
<td align="left">4</td>
<td align="left">16</td>
</tr>
<tr class="even">
<td align="left">rgba4444</td>
<td align="left">rgba8</td>
<td align="left">RGB+alpha</td>
<td align="left">4</td>
<td align="left">16</td>
</tr>
<tr class="odd">
<td align="left">rgba5551</td>
<td align="left">rgba8</td>
<td align="left">RGB+alpha</td>
<td align="left">5/5/5/1</td>
<td align="left">16</td>
</tr>
<tr class="even">
<td align="left">ga8, ag8</td>
<td align="left">ga8</td>
<td align="left">GRAY+alpha</td>
<td align="left">8</td>
<td align="left">16</td>
</tr>
<tr class="odd">
<td align="left">ga16, ag16</td>
<td align="left">ga16</td>
<td align="left">GRAY+alpha</td>
<td align="left">16</td>
<td align="left">32</td>
</tr>
<tr class="even">
<td align="left">g1</td>
<td align="left">ga8</td>
<td align="left">GRAY</td>
<td align="left">1</td>
<td align="left">1</td>
</tr>
<tr class="odd">
<td align="left">g2</td>
<td align="left">ga8</td>
<td align="left">GRAY</td>
<td align="left">2</td>
<td align="left">2</td>
</tr>
<tr class="even">
<td align="left">g4</td>
<td align="left">ga8</td>
<td align="left">GRAY</td>
<td align="left">4</td>
<td align="left">4</td>
</tr>
<tr class="odd">
<td align="left">g8</td>
<td align="left">ga8</td>
<td align="left">GRAY</td>
<td align="left">8</td>
<td align="left">8</td>
</tr>
<tr class="even">
<td align="left">g16</td>
<td align="left">ga16</td>
<td align="left">GRAY</td>
<td align="left">16</td>
<td align="left">16</td>
</tr>
<tr class="odd">
<td align="left">cmyk8</td>
<td align="left">cmyk8</td>
<td align="left">inverse CMYK</td>
<td align="left">8</td>
<td align="left">32</td>
</tr>
<tr class="even">
<td align="left">ycc8</td>
<td align="left">ycc8</td>
<td align="left">JPEG YCbCr 8</td>
<td align="left">8</td>
<td align="left">24</td>
</tr>
<tr class="odd">
<td align="left">ycck8</td>
<td align="left">ycck8</td>
<td align="left">JPEG YCbCrK 8</td>
<td align="left">8</td>
<td align="left">32</td>
</tr>
<tr class="even">
<td align="left">rgbaf</td>
<td align="left">rgbaf</td>
<td align="left">RGB+alpha</td>
<td align="left">32</td>
<td align="left">128</td>
</tr>
<tr class="odd">
<td align="left">rgbad</td>
<td align="left">rgbaf</td>
<td align="left">RGB+alpha</td>
<td align="left">64</td>
<td align="left">256</td>
</tr>
</tbody>
</table>
<h3 id="predefined-colortypes">Predefined colortypes</h3>
<table>
<thead>
<tr class="header">
<th align="left"><strong>name</strong></th>
<th align="left"><strong>channels</strong></th>
<th align="left"><strong>value type</strong></th>
<th align="left"><strong>value range</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">rgba8</td>
<td align="left">r, g, b, a</td>
<td align="left">integer</td>
<td align="left">0..0xff</td>
</tr>
<tr class="even">
<td align="left">rgba16</td>
<td align="left">r, g, b, a</td>
<td align="left">integer</td>
<td align="left">0..0xffff</td>
</tr>
<tr class="odd">
<td align="left">ga8</td>
<td align="left">g, a</td>
<td align="left">integer</td>
<td align="left">0..0xff</td>
</tr>
<tr class="even">
<td align="left">ga16</td>
<td align="left">g, a</td>
<td align="left">integer</td>
<td align="left">0..0xffff</td>
</tr>
<tr class="odd">
<td align="left">cmyk8</td>
<td align="left">c, m, y, k</td>
<td align="left">integer</td>
<td align="left">0..0xff</td>
</tr>
<tr class="even">
<td align="left">ycc8</td>
<td align="left">y, c, c</td>
<td align="left">integer</td>
<td align="left">0..0xff</td>
</tr>
<tr class="odd">
<td align="left">ycck8</td>
<td align="left">y, c, c, k</td>
<td align="left">integer</td>
<td align="left">0..0xff</td>
</tr>
<tr class="even">
<td align="left">rgbaf</td>
<td align="left">r, g, b, a</td>
<td align="left">float or double</td>
<td align="left">0..1</td>
</tr>
</tbody>
</table>
<h2 id="quick-api-reference">Quick API Reference</h2>
<table>
<tbody>
<tr class="odd">
<td align="left"><strong>format conversion</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.new(w, h, ...) -> dst</code></td>
<td align="left">create a bitmap</td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.copy(src[, format], ...) -> dst</code></td>
<td align="left">copy and convert a bitmap</td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.paint(src, dst, ...) -> dst</code></td>
<td align="left">paint a bitmap</td>
</tr>
<tr class="odd">
<td align="left"><strong>cropping</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.sub(src, x, y, w, h) -> dst</code></td>
<td align="left">make a sub-bitmap</td>
</tr>
<tr class="odd">
<td align="left"><strong>pixel access</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.pixel_interface(src) -> getpixel, setpixel</code></td>
<td align="left">get a pixel interface</td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.channel_interface(bmp, n) -> getval, setval</code></td>
<td align="left">get a channel interface</td>
</tr>
<tr class="even">
<td align="left"><strong>dithering</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.dither.fs|ordered(bmp, rN, gN, bN, aN)</code></td>
<td align="left">apply dithering</td>
</tr>
<tr class="even">
<td align="left"><strong>effects</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.invert(bmp)</code></td>
<td align="left">invert colors</td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.grayscale(bmp)</code></td>
<td align="left">desaturate</td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.convolve(src, kernel, [edge]) -> dst</code></td>
<td align="left">convolve</td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.sharpen(src[, threshold]) -> dst</code></td>
<td align="left">sharpen</td>
</tr>
<tr class="odd">
<td align="left"><strong>alpha blending</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.blend(src, dst, [operator], [x], [y])</code></td>
<td align="left">blend source into dest bitmap</td>
</tr>
<tr class="odd">
<td align="left"><strong>resizing</strong></td>
<td align="left"></td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.resize.nearest|bilinear(src, w, h) -> dst</code></td>
<td align="left">resize to new</td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.resize.nearest|bilinear(src, dst) -> dst</code></td>
<td align="left">resize to dest</td>
</tr>
<tr class="even">
<td align="left"><strong>utilities</strong></td>
<td align="left"></td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.min_stride(format, width) -> min_stride</code></td>
<td align="left">minimum stride for width</td>
</tr>
<tr class="even">
<td align="left"><code>bitmap.aligned_stride(stride) -> aligned_stride</code></td>
<td align="left">minimum stride that is aligned</td>
</tr>
<tr class="odd">
<td align="left"><code>bitmap.row_size(bmp) -> size</code></td>
<td align="left">row size in bytes</td>
</tr>
</tbody>
</table>
<h2 id="bitmap-operations">Bitmap operations</h2>
<h2 id="bitmap.neww-h-format-bottom_up-stride_aligned-stride---new_bmp"><code>bitmap.new(w, h, format, [bottom_up], [stride_aligned], [stride]) -> new_bmp</code></h2>
<p>Create a bitmap object. If <code>stride_aligned</code> is <code>true</code> and no specific <code>stride</code> is given, the stride will be a multiple of 4 bytes.</p>
<h2 id="bitmap.copybmp-format-bottom_up-stride_aligned-stride---new_bmp"><code>bitmap.copy(bmp, [format], [bottom_up], [stride_aligned], [stride]) -> new_bmp</code></h2>
<p>Copy a bitmap, optionally to a new format, orientation and stride. If <code>format</code> is not specified, stride and orientation default to those of source bitmap's, otherwise they default to top-down, minimum stride.</p>
<h2 id="bitmap.paintsource_bmp-dest_bmp-convert_pixel---dest_bmp"><code>bitmap.paint(source_bmp, dest_bmp[, convert_pixel]) -> dest_bmp</code></h2>
<p>Paint a source bitmap into a destination bitmap of the same width and height, with all the necessary pixel and colortype conversions.</p>
<p>The optional <code>convert_pixel</code> is a pixel conversion function to be called for each pixel as <code>convert_pixel(a, b, c, ...) -> x, y, z, ...</code>. It receives the channel values of the source bitmap according to its colortype and must return the converted channel values for the destination bitmap according to its colortype.</p>
<p>In some cases, the destination bitmap is allowed to have the same data buffer as the source bitmap. Specifically, it must have the same orientation, smaller or equal stride and smaller or equal pixel size. The destination bitmap can also be the source bitmap itself, which is useful for performing custom transformations via the <code>convert_pixel</code> callback.</p>
<h2 id="bitmap.subbmp-x-y-w-h---sub_bmp"><code>bitmap.sub(bmp, [x], [y], [w], [h]) -> sub_bmp</code></h2>
<p>Crop a bitmap without copying the pixels (the <code>data</code> field of the sub-bitmap is a pointer into the <code>data</code> buffer of the parent bitmap). The parent bitmap is pinned in the <code>parent</code> field of the sub-bitmap to prevent garbage collection of the data buffer. Other than that, the sub-bitmap behaves exactly like a normal bitmap (it can be further sub'ed for instance). The coordinates default to <code>0, 0, bmp.w, bmp.h</code> respectively. The coordinates are adjusted to fit the parent bitmap. If they result in zero width or height, nothing is returned.</p>
<p>To get real cropping, just copy the bitmap, specifying the format and orientation to reset the stride:</p>
<pre><code>sub = bitmap.copy(sub, sub.format, sub.bottom_up)</code></pre>
<blockquote>
<p>NOTE: For 1, 2, 4 bpp formats, the coordinates must be such that the data pointer points to the beginning of a byte (that is, is not fractional). For a non-fractional stride, this means the <code>x</code> coordinate must be a multiple of 8, 4, 2 respectively. For fractional strides don't even bother.</p>
</blockquote>
<h2 id="pixel-interface">Pixel interface</h2>
<h2 id="bitmap.pixel_interfacebitmap-colortype---getpixel-setpixel"><code>bitmap.pixel_interface(bitmap[, colortype]) -> getpixel, setpixel</code></h2>
<p>Return an API for getting and setting individual pixels of a bitmap object:</p>
<ul>
<li><code>getpixel(x, y) -> a, b, c, ...</code></li>
<li><code>setpixel(x, y, a, b, c, ...)</code></li>
</ul>
<p>where a, b, c are the individual color channels, converted to the specified colortype or in the colortype of the bitmap (i.e. r, g, b, a for the 'rgba' colortype, etc.).</p>
<h4 id="example">Example:</h4>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">function</span> darken<span class="ot">(</span><span class="kw">r</span><span class="ot">,</span> <span class="kw">g</span><span class="ot">,</span> <span class="kw">b</span><span class="ot">,</span> <span class="kw">a</span><span class="ot">)</span>
<span class="kw">return</span> <span class="kw">r</span> <span class="ot">/</span> <span class="dv">2</span><span class="ot">,</span> <span class="kw">g</span> <span class="ot">/</span> <span class="dv">2</span><span class="ot">,</span> <span class="kw">b</span> <span class="ot">/</span> <span class="dv">2</span><span class="ot">,</span> <span class="kw">a</span> <span class="ot">/</span> <span class="dv">2</span><span class="ot">)</span> <span class="co">--make 2x darker</span>
<span class="kw">end</span>
<span class="kw">local</span> <span class="kw">getpixel</span><span class="ot">,</span> <span class="kw">setpixel</span> <span class="ot">=</span> <span class="kw">bitmap</span><span class="ot">.</span>pixel_interface<span class="ot">(</span><span class="kw">bmp</span><span class="ot">)</span>
<span class="kw">for</span> <span class="kw">y</span> <span class="ot">=</span> <span class="dv">0</span><span class="ot">,</span> <span class="kw">bmp</span><span class="ot">.</span><span class="kw">h</span><span class="ot">-</span><span class="dv">1</span> <span class="kw">do</span>
<span class="kw">for</span> <span class="kw">x</span> <span class="ot">=</span> <span class="dv">0</span><span class="ot">,</span> <span class="kw">bmp</span><span class="ot">.</span><span class="kw">w</span><span class="ot">-</span><span class="dv">1</span> <span class="kw">do</span>
setpixel<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">,</span> darken<span class="ot">(</span>getpixel<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">)))</span>
<span class="kw">end</span>
<span class="kw">end</span>
<span class="co">--the above has the same effect as:</span>
<span class="kw">bitmap</span><span class="ot">.</span>convert<span class="ot">(</span><span class="kw">bmp</span><span class="ot">,</span> <span class="kw">bmp</span><span class="ot">,</span> <span class="kw">darken</span><span class="ot">)</span></code></pre>
<h2 id="channel-interface">Channel interface</h2>
<h2 id="bitmap.channel_interfacebitmap-channel---getvalue-setvalue"><code>bitmap.channel_interface(bitmap, channel) -> getvalue, setvalue</code></h2>
<p>Return an API for getting and setting values from a single color channel:</p>
<ul>
<li><code>getvalue(x, y) -> v</code></li>
<li><code>setvalue(x, y, v)</code></li>
</ul>
<h2 id="dithering">Dithering</h2>
<h2 id="bitmap.dither.fsbmp-rbits-gbits-bbits-abits"><code>bitmap.dither.fs(bmp, rbits, gbits, bbits, abits)</code></h2>
<p>Dither a bitmap using the <a href="http://en.wikipedia.org/wiki/Floyd%E2%80%93Steinberg_dithering">Floyd-Steinberg dithering</a> algorithm. <code>*bits</code> specify the number of bits of color to keep for each channel (eg. <code>bitmap.dither.fs(bmp, 5, 6, 5, 0)</code> dithers a bitmap so that its colors fit into the <code>rgb565</code> format). Only implemented for 4-channel colortypes.</p>
<h2 id="bitmap.dither.orderedbmp-mapsize"><code>bitmap.dither.ordered(bmp, mapsize)</code></h2>
<p>Dither a bitmap using the <a href="http://en.wikipedia.org/wiki/Ordered_dithering">ordered dithering</a> algorithm. <code>mapsize</code> specifies the threshold map to use and can be 2, 3, 4 or 8. Use the demo to see how this parameter affects the output quality depending on the output format (it's not a clear-cut choice). Implemented for 2-channel and 4-channel colortypes. Note that actual clipping of the low bits is not done, it will be done naturally when converting the bitmap to a lower bit depth.</p>
<h2 id="pixel-effects">Pixel effects</h2>
<h2 id="bitmap.invertbmp"><code>bitmap.invert(bmp)</code></h2>
<p>Invert colors.</p>
<h2 id="bitmap.grayscalebmp"><code>bitmap.grayscale(bmp)</code></h2>
<p>Convert pixels to grayscale, without changing the format.</p>
<h2 id="bitmap.convolvebmp-kernel-edge---new_bmp"><code>bitmap.convolve(bmp, kernel, [edge]) -> new_bmp</code></h2>
<p>Convolve a bitmap using a kernel matrix (a Lua array of arrays of the same length). <code>edge</code> can be <code>crop</code>, <code>wrap</code> or <code>extend</code> (default is <code>extend</code>).</p>
<h2 id="bitmap.sharpenbmp-threshold---new_bmp"><code>bitmap.sharpen(bmp[, threshold]) -> new_bmp</code></h2>
<p>Sharpen a bitmap.</p>
<h2 id="alpha-blending">Alpha Blending</h2>
<h2 id="bitmap.blendsource_bmp-dest_bmp-operator-x-y"><code>bitmap.blend(source_bmp, dest_bmp, [operator], [x], [y])</code></h2>
<p>Blend <code>source_bmp</code> into <code>dest_bmp</code> using a blending operator at <code>x,y</code> coordinates in the target bitmap (default is <code>0,0</code>). Operators are in the <code>bitmap.blend_op</code> table for inspection.</p>
<h4 id="example-1">Example:</h4>
<p>Painting a bitmap onto another at specific coordinates, based on bitmap.fit and sub-bitmaps:</p>
<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> <span class="kw">box2d</span> <span class="ot">=</span> require<span class="st">'box2d'</span>
<span class="kw">function</span> paint_at_xy<span class="ot">(</span><span class="kw">src</span><span class="ot">,</span> <span class="kw">dst</span><span class="ot">,</span> <span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">,</span> <span class="kw">w</span><span class="ot">,</span> <span class="kw">h</span> <span class="ot">=</span> <span class="kw">box2d</span><span class="ot">.</span>clip<span class="ot">(</span><span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">,</span> <span class="kw">src</span><span class="ot">.</span><span class="kw">w</span><span class="ot">,</span> <span class="kw">src</span><span class="ot">.</span><span class="kw">h</span><span class="ot">,</span> <span class="dv">0</span><span class="ot">,</span> <span class="dv">0</span><span class="ot">,</span> <span class="kw">dst</span><span class="ot">.</span><span class="kw">w</span><span class="ot">,</span> <span class="kw">dst</span><span class="ot">.</span><span class="kw">h</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">src</span> <span class="ot">=</span> <span class="kw">bitmap</span><span class="ot">.</span>sub<span class="ot">(</span><span class="kw">src</span><span class="ot">,</span> <span class="dv">0</span><span class="ot">,</span> <span class="dv">0</span><span class="ot">,</span> <span class="kw">w</span><span class="ot">,</span> <span class="kw">h</span><span class="ot">)</span>
<span class="kw">local</span> <span class="kw">dst</span> <span class="ot">=</span> <span class="kw">bitmap</span><span class="ot">.</span>sub<span class="ot">(</span><span class="kw">dst</span><span class="ot">,</span> <span class="kw">x</span><span class="ot">,</span> <span class="kw">y</span><span class="ot">,</span> <span class="kw">w</span><span class="ot">,</span> <span class="kw">h</span><span class="ot">)</span>
<span class="kw">if</span> <span class="kw">src</span> <span class="kw">and</span> <span class="kw">dst</span> <span class="kw">then</span>
<span class="kw">bitmap</span><span class="ot">.</span>paint<span class="ot">(</span><span class="kw">src</span><span class="ot">,</span> <span class="kw">dst</span><span class="ot">)</span>
<span class="kw">end</span>
<span class="kw">end</span></code></pre>
<h2 id="resizing">Resizing</h2>
<h2 id="bitmap.resize.methodbmp-w-h---new_bmp"><code>bitmap.resize.<method>(bmp, w, h) -> new_bmp</code></h2>
<h2 id="bitmap.resize.methodsource_bmp-dest_bmp---dest_bmp"><code>bitmap.resize.<method>(source_bmp, dest_bmp) -> dest_bmp</code></h2>
<p>Resize a bitmap. The method can be <code>nearest</code> or <code>bilinear</code>.</p>
<h2 id="utilities">Utilities</h2>
<h2 id="bitmap.min_strideformat-width---min_stride"><code>bitmap.min_stride(format, width) -> min_stride</code></h2>
<p>Return the minimum stride in bytes given a format and width. A bitmap data buffer should never be smaller than <code>min_stride * height</code>.</p>
<h2 id="bitmap.aligned_stridestride---aligned_stride"><code>bitmap.aligned_stride(stride) -> aligned_stride</code></h2>
<p>Given a stride, return the smallest stride that is a multiple of 4 bytes.</p>
<h2 id="bitmap.row_sizebmp---size"><code>bitmap.row_size(bmp) -> size</code></h2>
<p>Bitmap's row size, in bytes, i.e. bitmap's minimum stride.</p>
<h2 id="introspection">Introspection</h2>
<h2 id="bitmap.conversionssource_format---iter---name-def"><code>bitmap.conversions(source_format) -> iter() -> name, def</code></h2>
<p>Given a source bitmap format, iterate through all the formats that the source format can be converted to. <code>name</code> is the format name and <code>def</code> is the format definition which is a table with the fields <code>bpp</code>, <code>ctype</code>, <code>colortype</code>, <code>read</code>, <code>write</code>.</p>
<h2 id="bitmap.dumpinfo"><code>bitmap.dumpinfo()</code></h2>
<p>Print the list of supported pixel formats and the list of supported colortype conversions.</p>
<h2 id="extending">Extending</h2>
<p>Extending the <code>bitmap</code> module with new colortypes, formats, conversions and module functions is easy. Look at the <code>bitmap_rgbaf</code> sub-module for an example on how to do that. For the submodule to be loaded automatically you need to reference it in the <code>bitmap</code> module too in a few key spots. Again, look at how <code>rgbaf</code> does it.</p>
<h3 id="custom-formats">Custom formats</h3>
<p>A custom pixel format definition is a table with the following fields:</p>
<ul>
<li><code>bpp</code> - pixel size, in bits (must be an even number of bits).</li>
<li><code>ctype</code> - C type to cast <code>data</code> to when reading and writing pixels (see below).</li>
<li><code>colortype</code> - a string naming a standard color type or a table specifying a custom color type. The channel values that <code>read</code> and <code>write</code> refer to depend on the colortype, eg. for the 'rgba8' colortype, the read function must return 4 numbers in the 0-255 range corresponding to the R, G, B, A channels.</li>
<li><code>read</code> - a function to be called as <code>read(data, i) -> a, b, c, ...</code>; the function must decode the pixel at <code>data[i]</code> and return its channel values according to colortype.</li>
<li><code>write</code> - a function to be called as <code>write(data, i, a, b, c, ...)</code>; the function must encode the given channel values according to colortype and write the pixel at <code>data[i]</code>.
<ul>
<li>for formats that have bpp < 8, the index i is fractional and the bit offset of the pixel is at <code>bit.band(i * 8, 7)</code>.</li>
</ul></li>
</ul>
<h3 id="custom-colortypes">Custom colortypes</h3>
<p>A custom colortype definition is a table with the following fields:</p>
<ul>
<li><code>channels</code> - a string with each letter a channel name, eg. 'rgba', so that <code>#channels</code> indicates the number of channels.</li>
<li><code>max</code> - maximum value to which the channel values need to be clipped.</li>
<li><code>bpc</code> - bits/channel - same meaning as <code>max</code> but in bits.</li>
</ul>
<h2 id="todo">TODO</h2>
<ul>
<li>premuliply / unpremultiply alpha</li>
</ul>
</section>
</div>
<div id="tab2_container" class="doc"></div>
</div>
</div>
<div class="container">
<footer>
<div id="disqus_thread"></div>
<div class="faint">[email protected] | <a href="http://unlicense.org/">public domain</a></div>
</footer>
</div>
</div>
<script type="text/x-mustache" id=info_tab_template>
<h3>Modules</h3>
<ul>
{{#module_array}}
<li>{{name}}</li>
{{/module_array}}
</ul>
</script>
</body>
</html>