├── AUTHORS ├── LICENSE ├── Makefile ├── README.md ├── bitmap.h ├── gravityblur.h ├── jpegio.h ├── main.c ├── pngio.h └── wasm ├── compile.sh └── shell.html /AUTHORS: -------------------------------------------------------------------------------- 1 | 2 | ### Ilya Kurdyukov 3 | 4 | https://github.com/ilyakurdyukov 5 | https://www.linkedin.com/in/ilya-kurdyukov-a7304119b/ 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 2.1, February 1999 3 | 4 | Copyright (C) 1991, 1999 Free Software Foundation, Inc. 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | [This is the first released version of the Lesser GPL. It also counts 10 | as the successor of the GNU Library Public License, version 2, hence 11 | the version number 2.1.] 12 | 13 | Preamble 14 | 15 | The licenses for most software are designed to take away your 16 | freedom to share and change it. By contrast, the GNU General Public 17 | Licenses are intended to guarantee your freedom to share and change 18 | free software--to make sure the software is free for all its users. 19 | 20 | This license, the Lesser General Public License, applies to some 21 | specially designated software packages--typically libraries--of the 22 | Free Software Foundation and other authors who decide to use it. You 23 | can use it too, but we suggest you first think carefully about whether 24 | this license or the ordinary General Public License is the better 25 | strategy to use in any particular case, based on the explanations below. 26 | 27 | When we speak of free software, we are referring to freedom of use, 28 | not price. Our General Public Licenses are designed to make sure that 29 | you have the freedom to distribute copies of free software (and charge 30 | for this service if you wish); that you receive source code or can get 31 | it if you want it; that you can change the software and use pieces of 32 | it in new free programs; and that you are informed that you can do 33 | these things. 34 | 35 | To protect your rights, we need to make restrictions that forbid 36 | distributors to deny you these rights or to ask you to surrender these 37 | rights. These restrictions translate to certain responsibilities for 38 | you if you distribute copies of the library or if you modify it. 39 | 40 | For example, if you distribute copies of the library, whether gratis 41 | or for a fee, you must give the recipients all the rights that we gave 42 | you. You must make sure that they, too, receive or can get the source 43 | code. If you link other code with the library, you must provide 44 | complete object files to the recipients, so that they can relink them 45 | with the library after making changes to the library and recompiling 46 | it. And you must show them these terms so they know their rights. 47 | 48 | We protect your rights with a two-step method: (1) we copyright the 49 | library, and (2) we offer you this license, which gives you legal 50 | permission to copy, distribute and/or modify the library. 51 | 52 | To protect each distributor, we want to make it very clear that 53 | there is no warranty for the free library. Also, if the library is 54 | modified by someone else and passed on, the recipients should know 55 | that what they have is not the original version, so that the original 56 | author's reputation will not be affected by problems that might be 57 | introduced by others. 58 | 59 | Finally, software patents pose a constant threat to the existence of 60 | any free program. We wish to make sure that a company cannot 61 | effectively restrict the users of a free program by obtaining a 62 | restrictive license from a patent holder. Therefore, we insist that 63 | any patent license obtained for a version of the library must be 64 | consistent with the full freedom of use specified in this license. 65 | 66 | Most GNU software, including some libraries, is covered by the 67 | ordinary GNU General Public License. This license, the GNU Lesser 68 | General Public License, applies to certain designated libraries, and 69 | is quite different from the ordinary General Public License. We use 70 | this license for certain libraries in order to permit linking those 71 | libraries into non-free programs. 72 | 73 | When a program is linked with a library, whether statically or using 74 | a shared library, the combination of the two is legally speaking a 75 | combined work, a derivative of the original library. The ordinary 76 | General Public License therefore permits such linking only if the 77 | entire combination fits its criteria of freedom. The Lesser General 78 | Public License permits more lax criteria for linking other code with 79 | the library. 80 | 81 | We call this license the "Lesser" General Public License because it 82 | does Less to protect the user's freedom than the ordinary General 83 | Public License. It also provides other free software developers Less 84 | of an advantage over competing non-free programs. These disadvantages 85 | are the reason we use the ordinary General Public License for many 86 | libraries. However, the Lesser license provides advantages in certain 87 | special circumstances. 88 | 89 | For example, on rare occasions, there may be a special need to 90 | encourage the widest possible use of a certain library, so that it becomes 91 | a de-facto standard. To achieve this, non-free programs must be 92 | allowed to use the library. A more frequent case is that a free 93 | library does the same job as widely used non-free libraries. In this 94 | case, there is little to gain by limiting the free library to free 95 | software only, so we use the Lesser General Public License. 96 | 97 | In other cases, permission to use a particular library in non-free 98 | programs enables a greater number of people to use a large body of 99 | free software. For example, permission to use the GNU C Library in 100 | non-free programs enables many more people to use the whole GNU 101 | operating system, as well as its variant, the GNU/Linux operating 102 | system. 103 | 104 | Although the Lesser General Public License is Less protective of the 105 | users' freedom, it does ensure that the user of a program that is 106 | linked with the Library has the freedom and the wherewithal to run 107 | that program using a modified version of the Library. 108 | 109 | The precise terms and conditions for copying, distribution and 110 | modification follow. Pay close attention to the difference between a 111 | "work based on the library" and a "work that uses the library". The 112 | former contains code derived from the library, whereas the latter must 113 | be combined with the library in order to run. 114 | 115 | GNU LESSER GENERAL PUBLIC LICENSE 116 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 117 | 118 | 0. This License Agreement applies to any software library or other 119 | program which contains a notice placed by the copyright holder or 120 | other authorized party saying it may be distributed under the terms of 121 | this Lesser General Public License (also called "this License"). 122 | Each licensee is addressed as "you". 123 | 124 | A "library" means a collection of software functions and/or data 125 | prepared so as to be conveniently linked with application programs 126 | (which use some of those functions and data) to form executables. 127 | 128 | The "Library", below, refers to any such software library or work 129 | which has been distributed under these terms. A "work based on the 130 | Library" means either the Library or any derivative work under 131 | copyright law: that is to say, a work containing the Library or a 132 | portion of it, either verbatim or with modifications and/or translated 133 | straightforwardly into another language. (Hereinafter, translation is 134 | included without limitation in the term "modification".) 135 | 136 | "Source code" for a work means the preferred form of the work for 137 | making modifications to it. For a library, complete source code means 138 | all the source code for all modules it contains, plus any associated 139 | interface definition files, plus the scripts used to control compilation 140 | and installation of the library. 141 | 142 | Activities other than copying, distribution and modification are not 143 | covered by this License; they are outside its scope. The act of 144 | running a program using the Library is not restricted, and output from 145 | such a program is covered only if its contents constitute a work based 146 | on the Library (independent of the use of the Library in a tool for 147 | writing it). Whether that is true depends on what the Library does 148 | and what the program that uses the Library does. 149 | 150 | 1. You may copy and distribute verbatim copies of the Library's 151 | complete source code as you receive it, in any medium, provided that 152 | you conspicuously and appropriately publish on each copy an 153 | appropriate copyright notice and disclaimer of warranty; keep intact 154 | all the notices that refer to this License and to the absence of any 155 | warranty; and distribute a copy of this License along with the 156 | Library. 157 | 158 | You may charge a fee for the physical act of transferring a copy, 159 | and you may at your option offer warranty protection in exchange for a 160 | fee. 161 | 162 | 2. You may modify your copy or copies of the Library or any portion 163 | of it, thus forming a work based on the Library, and copy and 164 | distribute such modifications or work under the terms of Section 1 165 | above, provided that you also meet all of these conditions: 166 | 167 | a) The modified work must itself be a software library. 168 | 169 | b) You must cause the files modified to carry prominent notices 170 | stating that you changed the files and the date of any change. 171 | 172 | c) You must cause the whole of the work to be licensed at no 173 | charge to all third parties under the terms of this License. 174 | 175 | d) If a facility in the modified Library refers to a function or a 176 | table of data to be supplied by an application program that uses 177 | the facility, other than as an argument passed when the facility 178 | is invoked, then you must make a good faith effort to ensure that, 179 | in the event an application does not supply such function or 180 | table, the facility still operates, and performs whatever part of 181 | its purpose remains meaningful. 182 | 183 | (For example, a function in a library to compute square roots has 184 | a purpose that is entirely well-defined independent of the 185 | application. Therefore, Subsection 2d requires that any 186 | application-supplied function or table used by this function must 187 | be optional: if the application does not supply it, the square 188 | root function must still compute square roots.) 189 | 190 | These requirements apply to the modified work as a whole. If 191 | identifiable sections of that work are not derived from the Library, 192 | and can be reasonably considered independent and separate works in 193 | themselves, then this License, and its terms, do not apply to those 194 | sections when you distribute them as separate works. But when you 195 | distribute the same sections as part of a whole which is a work based 196 | on the Library, the distribution of the whole must be on the terms of 197 | this License, whose permissions for other licensees extend to the 198 | entire whole, and thus to each and every part regardless of who wrote 199 | it. 200 | 201 | Thus, it is not the intent of this section to claim rights or contest 202 | your rights to work written entirely by you; rather, the intent is to 203 | exercise the right to control the distribution of derivative or 204 | collective works based on the Library. 205 | 206 | In addition, mere aggregation of another work not based on the Library 207 | with the Library (or with a work based on the Library) on a volume of 208 | a storage or distribution medium does not bring the other work under 209 | the scope of this License. 210 | 211 | 3. You may opt to apply the terms of the ordinary GNU General Public 212 | License instead of this License to a given copy of the Library. To do 213 | this, you must alter all the notices that refer to this License, so 214 | that they refer to the ordinary GNU General Public License, version 2, 215 | instead of to this License. (If a newer version than version 2 of the 216 | ordinary GNU General Public License has appeared, then you can specify 217 | that version instead if you wish.) Do not make any other change in 218 | these notices. 219 | 220 | Once this change is made in a given copy, it is irreversible for 221 | that copy, so the ordinary GNU General Public License applies to all 222 | subsequent copies and derivative works made from that copy. 223 | 224 | This option is useful when you wish to copy part of the code of 225 | the Library into a program that is not a library. 226 | 227 | 4. You may copy and distribute the Library (or a portion or 228 | derivative of it, under Section 2) in object code or executable form 229 | under the terms of Sections 1 and 2 above provided that you accompany 230 | it with the complete corresponding machine-readable source code, which 231 | must be distributed under the terms of Sections 1 and 2 above on a 232 | medium customarily used for software interchange. 233 | 234 | If distribution of object code is made by offering access to copy 235 | from a designated place, then offering equivalent access to copy the 236 | source code from the same place satisfies the requirement to 237 | distribute the source code, even though third parties are not 238 | compelled to copy the source along with the object code. 239 | 240 | 5. A program that contains no derivative of any portion of the 241 | Library, but is designed to work with the Library by being compiled or 242 | linked with it, is called a "work that uses the Library". Such a 243 | work, in isolation, is not a derivative work of the Library, and 244 | therefore falls outside the scope of this License. 245 | 246 | However, linking a "work that uses the Library" with the Library 247 | creates an executable that is a derivative of the Library (because it 248 | contains portions of the Library), rather than a "work that uses the 249 | library". The executable is therefore covered by this License. 250 | Section 6 states terms for distribution of such executables. 251 | 252 | When a "work that uses the Library" uses material from a header file 253 | that is part of the Library, the object code for the work may be a 254 | derivative work of the Library even though the source code is not. 255 | Whether this is true is especially significant if the work can be 256 | linked without the Library, or if the work is itself a library. The 257 | threshold for this to be true is not precisely defined by law. 258 | 259 | If such an object file uses only numerical parameters, data 260 | structure layouts and accessors, and small macros and small inline 261 | functions (ten lines or less in length), then the use of the object 262 | file is unrestricted, regardless of whether it is legally a derivative 263 | work. (Executables containing this object code plus portions of the 264 | Library will still fall under Section 6.) 265 | 266 | Otherwise, if the work is a derivative of the Library, you may 267 | distribute the object code for the work under the terms of Section 6. 268 | Any executables containing that work also fall under Section 6, 269 | whether or not they are linked directly with the Library itself. 270 | 271 | 6. As an exception to the Sections above, you may also combine or 272 | link a "work that uses the Library" with the Library to produce a 273 | work containing portions of the Library, and distribute that work 274 | under terms of your choice, provided that the terms permit 275 | modification of the work for the customer's own use and reverse 276 | engineering for debugging such modifications. 277 | 278 | You must give prominent notice with each copy of the work that the 279 | Library is used in it and that the Library and its use are covered by 280 | this License. You must supply a copy of this License. If the work 281 | during execution displays copyright notices, you must include the 282 | copyright notice for the Library among them, as well as a reference 283 | directing the user to the copy of this License. Also, you must do one 284 | of these things: 285 | 286 | a) Accompany the work with the complete corresponding 287 | machine-readable source code for the Library including whatever 288 | changes were used in the work (which must be distributed under 289 | Sections 1 and 2 above); and, if the work is an executable linked 290 | with the Library, with the complete machine-readable "work that 291 | uses the Library", as object code and/or source code, so that the 292 | user can modify the Library and then relink to produce a modified 293 | executable containing the modified Library. (It is understood 294 | that the user who changes the contents of definitions files in the 295 | Library will not necessarily be able to recompile the application 296 | to use the modified definitions.) 297 | 298 | b) Use a suitable shared library mechanism for linking with the 299 | Library. A suitable mechanism is one that (1) uses at run time a 300 | copy of the library already present on the user's computer system, 301 | rather than copying library functions into the executable, and (2) 302 | will operate properly with a modified version of the library, if 303 | the user installs one, as long as the modified version is 304 | interface-compatible with the version that the work was made with. 305 | 306 | c) Accompany the work with a written offer, valid for at 307 | least three years, to give the same user the materials 308 | specified in Subsection 6a, above, for a charge no more 309 | than the cost of performing this distribution. 310 | 311 | d) If distribution of the work is made by offering access to copy 312 | from a designated place, offer equivalent access to copy the above 313 | specified materials from the same place. 314 | 315 | e) Verify that the user has already received a copy of these 316 | materials or that you have already sent this user a copy. 317 | 318 | For an executable, the required form of the "work that uses the 319 | Library" must include any data and utility programs needed for 320 | reproducing the executable from it. However, as a special exception, 321 | the materials to be distributed need not include anything that is 322 | normally distributed (in either source or binary form) with the major 323 | components (compiler, kernel, and so on) of the operating system on 324 | which the executable runs, unless that component itself accompanies 325 | the executable. 326 | 327 | It may happen that this requirement contradicts the license 328 | restrictions of other proprietary libraries that do not normally 329 | accompany the operating system. Such a contradiction means you cannot 330 | use both them and the Library together in an executable that you 331 | distribute. 332 | 333 | 7. You may place library facilities that are a work based on the 334 | Library side-by-side in a single library together with other library 335 | facilities not covered by this License, and distribute such a combined 336 | library, provided that the separate distribution of the work based on 337 | the Library and of the other library facilities is otherwise 338 | permitted, and provided that you do these two things: 339 | 340 | a) Accompany the combined library with a copy of the same work 341 | based on the Library, uncombined with any other library 342 | facilities. This must be distributed under the terms of the 343 | Sections above. 344 | 345 | b) Give prominent notice with the combined library of the fact 346 | that part of it is a work based on the Library, and explaining 347 | where to find the accompanying uncombined form of the same work. 348 | 349 | 8. You may not copy, modify, sublicense, link with, or distribute 350 | the Library except as expressly provided under this License. Any 351 | attempt otherwise to copy, modify, sublicense, link with, or 352 | distribute the Library is void, and will automatically terminate your 353 | rights under this License. However, parties who have received copies, 354 | or rights, from you under this License will not have their licenses 355 | terminated so long as such parties remain in full compliance. 356 | 357 | 9. You are not required to accept this License, since you have not 358 | signed it. However, nothing else grants you permission to modify or 359 | distribute the Library or its derivative works. These actions are 360 | prohibited by law if you do not accept this License. Therefore, by 361 | modifying or distributing the Library (or any work based on the 362 | Library), you indicate your acceptance of this License to do so, and 363 | all its terms and conditions for copying, distributing or modifying 364 | the Library or works based on it. 365 | 366 | 10. Each time you redistribute the Library (or any work based on the 367 | Library), the recipient automatically receives a license from the 368 | original licensor to copy, distribute, link with or modify the Library 369 | subject to these terms and conditions. You may not impose any further 370 | restrictions on the recipients' exercise of the rights granted herein. 371 | You are not responsible for enforcing compliance by third parties with 372 | this License. 373 | 374 | 11. If, as a consequence of a court judgment or allegation of patent 375 | infringement or for any other reason (not limited to patent issues), 376 | conditions are imposed on you (whether by court order, agreement or 377 | otherwise) that contradict the conditions of this License, they do not 378 | excuse you from the conditions of this License. If you cannot 379 | distribute so as to satisfy simultaneously your obligations under this 380 | License and any other pertinent obligations, then as a consequence you 381 | may not distribute the Library at all. For example, if a patent 382 | license would not permit royalty-free redistribution of the Library by 383 | all those who receive copies directly or indirectly through you, then 384 | the only way you could satisfy both it and this License would be to 385 | refrain entirely from distribution of the Library. 386 | 387 | If any portion of this section is held invalid or unenforceable under any 388 | particular circumstance, the balance of the section is intended to apply, 389 | and the section as a whole is intended to apply in other circumstances. 390 | 391 | It is not the purpose of this section to induce you to infringe any 392 | patents or other property right claims or to contest validity of any 393 | such claims; this section has the sole purpose of protecting the 394 | integrity of the free software distribution system which is 395 | implemented by public license practices. Many people have made 396 | generous contributions to the wide range of software distributed 397 | through that system in reliance on consistent application of that 398 | system; it is up to the author/donor to decide if he or she is willing 399 | to distribute software through any other system and a licensee cannot 400 | impose that choice. 401 | 402 | This section is intended to make thoroughly clear what is believed to 403 | be a consequence of the rest of this License. 404 | 405 | 12. If the distribution and/or use of the Library is restricted in 406 | certain countries either by patents or by copyrighted interfaces, the 407 | original copyright holder who places the Library under this License may add 408 | an explicit geographical distribution limitation excluding those countries, 409 | so that distribution is permitted only in or among countries not thus 410 | excluded. In such case, this License incorporates the limitation as if 411 | written in the body of this License. 412 | 413 | 13. The Free Software Foundation may publish revised and/or new 414 | versions of the Lesser General Public License from time to time. 415 | Such new versions will be similar in spirit to the present version, 416 | but may differ in detail to address new problems or concerns. 417 | 418 | Each version is given a distinguishing version number. If the Library 419 | specifies a version number of this License which applies to it and 420 | "any later version", you have the option of following the terms and 421 | conditions either of that version or of any later version published by 422 | the Free Software Foundation. If the Library does not specify a 423 | license version number, you may choose any version ever published by 424 | the Free Software Foundation. 425 | 426 | 14. If you wish to incorporate parts of the Library into other free 427 | programs whose distribution conditions are incompatible with these, 428 | write to the author to ask for permission. For software which is 429 | copyrighted by the Free Software Foundation, write to the Free 430 | Software Foundation; we sometimes make exceptions for this. Our 431 | decision will be guided by the two goals of preserving the free status 432 | of all derivatives of our free software and of promoting the sharing 433 | and reuse of software generally. 434 | 435 | NO WARRANTY 436 | 437 | 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO 438 | WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. 439 | EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR 440 | OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY 441 | KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE 442 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 443 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE 444 | LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME 445 | THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 446 | 447 | 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN 448 | WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY 449 | AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU 450 | FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR 451 | CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE 452 | LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING 453 | RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A 454 | FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF 455 | SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH 456 | DAMAGES. 457 | 458 | END OF TERMS AND CONDITIONS 459 | 460 | How to Apply These Terms to Your New Libraries 461 | 462 | If you develop a new library, and you want it to be of the greatest 463 | possible use to the public, we recommend making it free software that 464 | everyone can redistribute and change. You can do so by permitting 465 | redistribution under these terms (or, alternatively, under the terms of the 466 | ordinary General Public License). 467 | 468 | To apply these terms, attach the following notices to the library. It is 469 | safest to attach them to the start of each source file to most effectively 470 | convey the exclusion of warranty; and each file should have at least the 471 | "copyright" line and a pointer to where the full notice is found. 472 | 473 | 474 | Copyright (C) 475 | 476 | This library is free software; you can redistribute it and/or 477 | modify it under the terms of the GNU Lesser General Public 478 | License as published by the Free Software Foundation; either 479 | version 2.1 of the License, or (at your option) any later version. 480 | 481 | This library is distributed in the hope that it will be useful, 482 | but WITHOUT ANY WARRANTY; without even the implied warranty of 483 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 484 | Lesser General Public License for more details. 485 | 486 | You should have received a copy of the GNU Lesser General Public 487 | License along with this library; if not, write to the Free Software 488 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 489 | USA 490 | 491 | Also add information on how to contact you by electronic and paper mail. 492 | 493 | You should also get your employer (if you work as a programmer) or your 494 | school, if any, to sign a "copyright disclaimer" for the library, if 495 | necessary. Here is a sample; alter the names: 496 | 497 | Yoyodyne, Inc., hereby disclaims all copyright interest in the 498 | library `Frob' (a library for tweaking knobs) written by James Random 499 | Hacker. 500 | 501 | , 1 April 1990 502 | Ty Coon, President of Vice 503 | 504 | That's all there is to it! 505 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | 2 | APPNAME ?= gravblur 3 | SRCNAME ?= main.c 4 | 5 | $(APPNAME): bitmap.h gravityblur.h pngio.h jpegio.h 6 | 7 | MFLAGS := -march=native 8 | CFLAGS := -Wall -Wextra -pedantic -O3 $(MFLAGS) 9 | LIBS := -ljpeg -lpng 10 | 11 | .PHONY: clean all 12 | 13 | all: $(APPNAME) 14 | 15 | clean: 16 | rm -f $(APPNAME) 17 | 18 | $(APPNAME): $(SRCNAME) 19 | $(CC) -DAPPNAME=$(APPNAME) $(CFLAGS) -s -o $@ $< $(LIBS) -lm 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gravity Blur 2 | 3 | This is an image smoothing algorithm inspired by [JPEG Quant Smooth](https://github.com/ilyakurdyukov/jpeg-quantsmooth). This is called *gravity blur* because the pixel values gravitate towards each other, the force of this *gravity* is inversely proportional to the distance between them. 4 | 5 | This algorithm preserves sharp edges if the distance between pixel values on the sides of the edges is greater than *range*. With more iterations, it is not only smoothes the image, but can also make it look like CG art or poster. 6 | 7 | The original project page is [here](https://github.com/ilyakurdyukov/gravityblur). 8 | 9 | ## WebAssembly 10 | 11 | Web version available [here](https://ilyakurdyukov.github.io/gravityblur/). 12 | Images are processed locally on your computer. 13 | 14 | - Click the "Load" button or drag-n-drop image into the browser window. After processing is complete, you can save the result by clicking the "Save" button. You can edit the *filename* field before saving to change the image format (jpeg or png, JPEG images are saved at 100% quality). The *options* field is passed to the *wasm* code when you initiate a processing by loading a file. 15 | 16 | ## Usage 17 | 18 | `gravblur [options] input.[png|jpg|jpeg] output.[png|jpg|jpeg]` 19 | 20 | ## Options 21 | 22 | `-r, --range f` Gravity range (0-100) 23 | `-s, --sharp f` Sharpness (0-100) 24 | `-n, --niter n` Number of iterations (default is 3) 25 | `-R, --rgb` Process in RGB (default) 26 | `-Y, --yuv` Process in YUV 27 | `-S, --separate` Separate color components 28 | `-a, --alpha` Use alpha channel 29 | `-i, --info n` Print gravblur debug output: 0 - silent, 3 - all (default) 30 | 31 | ## Examples 32 | 33 | - Images 3x zoomed. 34 | 35 | ![](https://ilyakurdyukov.github.io/gravityblur/images/lena_orig.png) 36 | ![](https://ilyakurdyukov.github.io/gravityblur/images/lena_new.png) 37 | 38 | ## Buliding 39 | 40 | If your system have *libjpeg* and *libpng* development packages installed, just type `make`. 41 | Or you can specify their location with `LIBS` variable to `make`. 42 | 43 | -------------------------------------------------------------------------------- /bitmap.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Ilya Kurdyukov 3 | * 4 | * common bitmap routines 5 | */ 6 | 7 | typedef struct { 8 | int width, height, bpp, stride; uint8_t *data; 9 | } bitmap_t; 10 | 11 | static bitmap_t *bitmap_create(int width, int height, int bpp) { 12 | bitmap_t *image; int stride = width * bpp; 13 | if ((unsigned)((width - 1) | (height - 1)) >= 0x8000) return 0; 14 | image = (bitmap_t*)malloc(sizeof(bitmap_t) + stride * height); 15 | if (!image) return 0; 16 | image->width = width; 17 | image->height = height; 18 | image->bpp = bpp; 19 | image->stride = stride; 20 | image->data = (uint8_t*)(image + 1); 21 | return image; 22 | } 23 | 24 | static void bitmap_free(bitmap_t *in) { 25 | if (in) free(in); 26 | } 27 | 28 | #define DESCALE(a, x) a = (x + 0x2000) >> 14; a = a < 0 ? 0 : a > 255 ? 255 : a; 29 | #define FIX(a) ((int)((a) * (1 << 14) + 0.5)) 30 | 31 | #define FIX_Y_R FIX(0.299) 32 | #define FIX_Y_G FIX(0.587) 33 | #define FIX_Y_B FIX(0.114) 34 | #define FIX_Cb_BY FIX(1 / 1.772) 35 | #define FIX_Cr_RY FIX(1 / 1.402) 36 | #define FIX_R_Cr FIX(1.402) 37 | #define FIX_Y_Cb FIX(0.34414) 38 | #define FIX_Y_Cr FIX(0.71414) 39 | #define FIX_B_Cb FIX(1.772) 40 | 41 | static void RGB2YCbCr(bitmap_t *in) { 42 | int x, y, w = in->width, h = in->height, bpp = in->bpp, stride = in->stride; 43 | uint8_t *data = in->data; 44 | 45 | if (bpp >= 3) 46 | for (y = 0; y < h; y++) { 47 | for (x = 0; x < w; x++) { 48 | uint8_t *ptr = &data[y*stride + x*bpp]; 49 | int R = ptr[0], G = ptr[1], B = ptr[2], Y, Cb, Cr; 50 | Y = (R * FIX_Y_R + G * FIX_Y_G + B * FIX_Y_B + 0x2000) >> 14; 51 | DESCALE(Cb, (B - Y) * FIX_Cb_BY + (128 << 14)); 52 | DESCALE(Cr, (R - Y) * FIX_Cr_RY + (128 << 14)); 53 | ptr[0] = Y; ptr[1] = Cb; ptr[2] = Cr; 54 | } 55 | } 56 | } 57 | 58 | static void YCbCr2RGB(bitmap_t *in) { 59 | int x, y, w = in->width, h = in->height, bpp = in->bpp, stride = in->stride; 60 | uint8_t *data = in->data; 61 | 62 | if (bpp >= 3) 63 | for (y = 0; y < h; y++) { 64 | for (x = 0; x < w; x++) { 65 | uint8_t *ptr = &data[y*stride + x*bpp]; 66 | int Y = ptr[0], Cb = ptr[1], Cr = ptr[2], R, G, B; 67 | DESCALE(R, (Y << 14) + (Cr - 128) * FIX_R_Cr); 68 | DESCALE(G, (Y << 14) - (Cb - 128) * FIX_Y_Cb - (Cr - 128) * FIX_Y_Cr); 69 | DESCALE(B, (Y << 14) + (Cb - 128) * FIX_B_Cb); 70 | ptr[0] = R; ptr[1] = G; ptr[2] = B; 71 | } 72 | } 73 | } 74 | #undef FIX 75 | #undef DESCALE 76 | 77 | -------------------------------------------------------------------------------- /gravityblur.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Ilya Kurdyukov 3 | * 4 | * This file is part of gravity blur 5 | * 6 | * This program is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | static void gravityblur(bitmap_t *in, bitmap_t *out, float range, float c0, int num_iter) { 21 | uint8_t *in_data = in->data, *out_data = out->data, *temp; 22 | int w = in->width, h = in->height, bpp = in->bpp; 23 | int stride = in->stride, ostride = out->stride; 24 | int iter, j, x, y; 25 | float c1 = c0 * sqrtf(0.5f); 26 | 27 | #define M4(i) \ 28 | b##i -= (int)roundf(a##i / an); \ 29 | if (b##i < 0) b##i = 0; \ 30 | if (b##i > 255) b##i = 255; 31 | 32 | #define M3(b, x, y) int b##0 = in_data[(y)*stride+(x)*bpp+j]; 33 | 34 | #define M2(M) for (j = 0; j < bpp; j++) { M3(b, x, y) \ 35 | float a0 = 0, an = 0, aw, t; \ 36 | M(1, x-1, y-1) M(0, x, y-1) M(1, x+1, y-1) \ 37 | M(0, x-1, y) M(0, x+1, y) \ 38 | M(1, x-1, y+1) M(0, x, y+1) M(1, x+1, y+1) \ 39 | if (an > 0.0f) { M4(0) } \ 40 | out_data[y*ostride+x*bpp+j] = b0; } 41 | 42 | #define CORE_CHK(i, x, y) if (x >= 0 && x < w && y >= 0 && y < h) CORE(i, x, y) 43 | #define CORE(i, x, y) { M3(d, x, y) \ 44 | float t0 = b0 - d0; \ 45 | t = range - fabsf(t0); \ 46 | if (t < 0) t = 0; t *= t; aw = c##i * t; \ 47 | a0 += t0 * t * aw; an += aw * aw; } 48 | 49 | for (iter = 0; iter < num_iter; iter++) { 50 | // slower processing for borders 51 | for (y = 0; y < h; y++) { 52 | int xa = y > 0 && y < h-1 ? w-1 : 1; 53 | for (x = 0; x < w; x += xa) M2(CORE_CHK) 54 | } 55 | // processing without checks 56 | for (y = 1; y < h-1; y++) 57 | for (x = 1; x < w-1; x++) M2(CORE) 58 | 59 | temp = in_data; in_data = out_data; out_data = temp; 60 | j = stride; stride = ostride; ostride = j; 61 | } 62 | #undef M2 63 | #undef M3 64 | #undef CORE 65 | 66 | if (~num_iter & 1) { 67 | for (y = 0; y < h; y++) 68 | memcpy(in_data + y * stride, out_data + y * ostride, w * bpp); 69 | } 70 | } 71 | 72 | static void gravityblur3(bitmap_t *in, bitmap_t *out, float range, float c0, int num_iter) { 73 | uint8_t *in_data = in->data, *out_data = out->data, *temp; 74 | int w = in->width, h = in->height, bpp = in->bpp; 75 | int stride = in->stride, ostride = out->stride; 76 | int iter, j, x, y; 77 | float c1 = c0 * sqrtf(0.5f); 78 | 79 | // copy extra channels 80 | for (y = 0; y < h; y++) 81 | for (x = 0; x < w; x++) 82 | for (j = 3; j < bpp; j++) { 83 | int c = in_data[y*stride+x*bpp+j]; 84 | out_data[y*ostride+x*bpp+j] = c; 85 | } 86 | 87 | #define M3(b, x, y) \ 88 | int b##0 = in_data[(y)*stride+(x)*bpp]; \ 89 | int b##1 = in_data[(y)*stride+(x)*bpp+1]; \ 90 | int b##2 = in_data[(y)*stride+(x)*bpp+2]; 91 | 92 | #define M2(M) { M3(b, x, y) \ 93 | float a0 = 0, a1 = 0, a2 = 0, an = 0, aw, t; \ 94 | M(1, x-1, y-1) M(0, x, y-1) M(1, x+1, y-1) \ 95 | M(0, x-1, y) M(0, x+1, y) \ 96 | M(1, x-1, y+1) M(0, x, y+1) M(1, x+1, y+1) \ 97 | if (an > 0.0f) { M4(0) M4(1) M4(2) } \ 98 | out_data[y*ostride+x*bpp] = b0; \ 99 | out_data[y*ostride+x*bpp+1] = b1; \ 100 | out_data[y*ostride+x*bpp+2] = b2; } 101 | 102 | #define CORE(i, x, y) { M3(d, x, y) \ 103 | float t0 = b0 - d0, t1 = b1 - d1, t2 = b2 - d2; \ 104 | t = range - sqrtf(t0*t0 + t1*t1 + t2*t2); \ 105 | if (t < 0) t = 0; t *= t; aw = c##i * t; t *= aw; \ 106 | a0 += t0 * t; a1 += t1 * t; a2 += t2 * t; an += aw * aw; } 107 | 108 | for (iter = 0; iter < num_iter; iter++) { 109 | // slower processing for borders 110 | for (y = 0; y < h; y++) { 111 | int xa = y > 0 && y < h-1 ? w-1 : 1; 112 | for (x = 0; x < w; x += xa) M2(CORE_CHK) 113 | } 114 | 115 | // processing without checks 116 | for (y = 1; y < h-1; y++) 117 | for (x = 1; x < w-1; x++) M2(CORE) 118 | temp = in_data; in_data = out_data; out_data = temp; 119 | j = stride; stride = ostride; ostride = j; 120 | } 121 | #undef M2 122 | #undef M3 123 | #undef CORE 124 | 125 | if (~num_iter & 1) { 126 | for (y = 0; y < h; y++) 127 | memcpy(in_data + y * stride, out_data + y * ostride, w * bpp); 128 | } 129 | } 130 | 131 | static void gravityblur4(bitmap_t *in, bitmap_t *out, float range, float c0, int num_iter) { 132 | uint8_t *in_data = in->data, *out_data = out->data, *temp; 133 | int w = in->width, h = in->height, bpp = in->bpp; 134 | int stride = in->stride, ostride = out->stride; 135 | int iter, j, x, y; 136 | float c1 = c0 * sqrtf(0.5f); 137 | 138 | // copy extra channels 139 | for (y = 0; y < h; y++) 140 | for (x = 0; x < w; x++) 141 | for (j = 4; j < bpp; j++) { 142 | int c = in_data[y*stride+x*bpp+j]; 143 | out_data[y*ostride+x*bpp+j] = c; 144 | } 145 | 146 | #define M3(b, x, y) \ 147 | int b##0 = in_data[(y)*stride+(x)*bpp]; \ 148 | int b##1 = in_data[(y)*stride+(x)*bpp+1]; \ 149 | int b##2 = in_data[(y)*stride+(x)*bpp+2]; \ 150 | int b##3 = in_data[(y)*stride+(x)*bpp+3]; 151 | 152 | #define M2(M) { M3(b, x, y) \ 153 | float a0 = 0, a1 = 0, a2 = 0, an = 0, aw, t; \ 154 | M(1, x-1, y-1) M(0, x, y-1) M(1, x+1, y-1) \ 155 | M(0, x-1, y) M(0, x+1, y) \ 156 | M(1, x-1, y+1) M(0, x, y+1) M(1, x+1, y+1) \ 157 | if (an > 0.0f) { M4(0) M4(1) M4(2) } \ 158 | out_data[y*ostride+x*bpp] = b0; \ 159 | out_data[y*ostride+x*bpp+1] = b1; \ 160 | out_data[y*ostride+x*bpp+2] = b2; \ 161 | out_data[y*ostride+x*bpp+3] = b3; } 162 | 163 | #define CORE(i, x, y) { M3(d, x, y) \ 164 | float t0 = b0 - d0, t1 = b1 - d1, t2 = b2 - d2; \ 165 | t = range - sqrtf(t0*t0 + t1*t1 + t2*t2); \ 166 | if (t < 0) t = 0; t *= t; t *= d3; aw = c##i * t; t *= aw; \ 167 | a0 += t0 * t; a1 += t1 * t; a2 += t2 * t; an += aw * aw; } 168 | 169 | for (iter = 0; iter < num_iter; iter++) { 170 | // slower processing for borders 171 | for (y = 0; y < h; y++) { 172 | int xa = y > 0 && y < h-1 ? w-1 : 1; 173 | for (x = 0; x < w; x += xa) M2(CORE_CHK) 174 | } 175 | 176 | // processing without checks 177 | for (y = 1; y < h-1; y++) 178 | for (x = 1; x < w-1; x++) M2(CORE) 179 | temp = in_data; in_data = out_data; out_data = temp; 180 | j = stride; stride = ostride; ostride = j; 181 | } 182 | #undef M2 183 | #undef M3 184 | #undef M4 185 | #undef CORE 186 | #undef CORE_CHK 187 | 188 | if (~num_iter & 1) { 189 | for (y = 0; y < h; y++) 190 | memcpy(in_data + y * stride, out_data + y * ostride, w * bpp); 191 | } 192 | } 193 | 194 | -------------------------------------------------------------------------------- /jpegio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Ilya Kurdyukov 3 | * 4 | * jpeg support using libjpeg 5 | */ 6 | 7 | #include "jpeglib.h" 8 | 9 | #define bitmap_read_jpeg(fn) bitmap_read_jpeg_impl(fn, JCS_RGB) 10 | #define bitmap_read_jpeg_gray(fn) bitmap_read_jpeg_impl(fn, JCS_GRAYSCALE) 11 | #define bitmap_read_jpeg_yuv(fn) bitmap_read_jpeg_impl(fn, JCS_YCbCr) 12 | 13 | typedef struct { 14 | struct jpeg_error_mgr pub; 15 | jmp_buf setjmp_buffer; 16 | } bitmap_jpeg_err_ctx; 17 | 18 | static void bitmap_jpeg_err(j_common_ptr cinfo) { 19 | char errorMsg[JMSG_LENGTH_MAX]; 20 | bitmap_jpeg_err_ctx* jerr = (bitmap_jpeg_err_ctx*)cinfo->err; 21 | (*(cinfo->err->format_message))(cinfo, errorMsg); 22 | printf("%s\n", errorMsg); 23 | longjmp(jerr->setjmp_buffer, 1); 24 | } 25 | 26 | bitmap_t* bitmap_read_jpeg_impl(const char *filename, J_COLOR_SPACE color) { 27 | struct jpeg_decompress_struct ci; 28 | FILE * volatile fp; int volatile ok = 0; 29 | bitmap_t * volatile bm = NULL; 30 | void * volatile mem = NULL; 31 | 32 | bitmap_jpeg_err_ctx jerr; 33 | ci.err = jpeg_std_error(&jerr.pub); 34 | jerr.pub.error_exit = bitmap_jpeg_err; 35 | if (!setjmp(jerr.setjmp_buffer)) { 36 | unsigned y, width, height, stride; uint8_t *data; 37 | bitmap_t *bm1; JSAMPROW *scanline; 38 | 39 | jpeg_create_decompress(&ci); 40 | if (!(fp = fopen(filename, "rb"))) goto err; 41 | jpeg_stdio_src(&ci, fp); 42 | jpeg_read_header(&ci, TRUE); 43 | ci.out_color_space = color; 44 | // ci.do_fancy_upsampling = FALSE; 45 | // ci.dct_method = JDCT_ISLOW; 46 | jpeg_start_decompress(&ci); 47 | 48 | width = ci.output_width; 49 | height = ci.output_height; 50 | bm = bm1 = bitmap_create(width, height, ci.output_components); 51 | if (bm1) { 52 | stride = bm1->stride; data = bm1->data; 53 | mem = malloc(height * sizeof(JSAMPROW)); 54 | if (mem) { 55 | scanline = (JSAMPROW*)mem; 56 | for (y = 0; y < height; y++) 57 | scanline[y] = (JSAMPLE*)(data + y * stride); 58 | 59 | while ((y = ci.output_scanline) < height) { 60 | jpeg_read_scanlines(&ci, scanline + y, height - y); 61 | } 62 | ok = 1; 63 | } 64 | } 65 | jpeg_finish_decompress(&ci); 66 | } 67 | err: 68 | if (mem) free(mem); 69 | if (!ok) { bitmap_free(bm); bm = NULL; } 70 | jpeg_destroy_decompress(&ci); 71 | if (fp) fclose(fp); 72 | return bm; 73 | } 74 | 75 | #define bitmap_write_jpeg(bm, fn) bitmap_write_jpeg_impl(bm, fn, JCS_RGB) 76 | #define bitmap_write_jpeg_yuv(bm, fn) bitmap_write_jpeg_impl(bm, fn, JCS_YCbCr) 77 | 78 | void bitmap_write_jpeg_impl(bitmap_t *bm, const char *filename, J_COLOR_SPACE color) { 79 | struct jpeg_compress_struct ci; 80 | FILE * volatile fp = NULL; 81 | void * volatile mem = NULL; 82 | J_COLOR_SPACE volatile in_color; 83 | 84 | if (!bm || !filename) goto err; 85 | if (bm->bpp == 1) color = JCS_GRAYSCALE; 86 | else if (bm->bpp != 3) goto err; 87 | in_color = color; 88 | 89 | bitmap_jpeg_err_ctx jerr; 90 | ci.err = jpeg_std_error(&jerr.pub); 91 | jerr.pub.error_exit = bitmap_jpeg_err; 92 | if (!setjmp(jerr.setjmp_buffer)) { 93 | unsigned y, width, height, stride; uint8_t *data; 94 | JSAMPROW *scanline; int quality = 100; 95 | 96 | jpeg_create_compress(&ci); 97 | if (!(fp = fopen(filename, "wb"))) goto err; 98 | jpeg_stdio_dest(&ci, fp); 99 | 100 | width = bm->width; height = bm->height; 101 | data = bm->data; stride = bm->stride; 102 | 103 | ci.image_width = width; 104 | ci.image_height = height; 105 | ci.input_components = bm->bpp; 106 | ci.in_color_space = in_color; 107 | jpeg_set_defaults(&ci); 108 | ci.optimize_coding = TRUE; 109 | // ci.dct_method = JDCT_ISLOW; 110 | // for JCS_YCbCr libjpeg selects 4:2:0 subsampling by default 111 | ci.comp_info[0].h_samp_factor = 1; 112 | ci.comp_info[0].v_samp_factor = 1; 113 | jpeg_set_quality(&ci, quality, TRUE); 114 | jpeg_start_compress(&ci, TRUE); 115 | 116 | mem = malloc(height * sizeof(JSAMPROW)); 117 | if (mem) { 118 | scanline = (JSAMPROW*)mem; 119 | for (y = 0; y < height; y++) 120 | scanline[y] = (JSAMPLE*)(data + y * stride); 121 | 122 | while ((y = ci.next_scanline) < height) { 123 | jpeg_write_scanlines(&ci, scanline + y, height - y); 124 | } 125 | } 126 | jpeg_finish_compress(&ci); 127 | } 128 | err: 129 | if (mem) free(mem); 130 | jpeg_destroy_compress(&ci); 131 | if (fp) fclose(fp); 132 | } 133 | 134 | -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Ilya Kurdyukov 3 | * 4 | * This file is part of gravity blur 5 | * 6 | * This program is free software; you can redistribute it and/or 7 | * modify it under the terms of the GNU Lesser General Public 8 | * License as published by the Free Software Foundation; either 9 | * version 2.1 of the License, or (at your option) any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 | * Lesser General Public License for more details. 15 | * 16 | * You should have received a copy of the GNU Lesser General Public License 17 | * along with this program. If not, see . 18 | */ 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | #include 25 | 26 | #ifdef __EMSCRIPTEN__ 27 | #include 28 | #else 29 | #define EMSCRIPTEN_KEEPALIVE 30 | #endif 31 | 32 | #include 33 | #include 34 | static int64_t get_time_usec() { 35 | struct timeval time; 36 | gettimeofday(&time, NULL); 37 | return (int64_t)time.tv_sec * 1000000 + time.tv_usec; 38 | } 39 | 40 | #include "bitmap.h" 41 | #ifndef WASM 42 | #include "pngio.h" 43 | #include "jpegio.h" 44 | #define stricmp stricmp_simple 45 | static int stricmp(const char *s1, const char *s2) { 46 | char a, b; 47 | do { 48 | a = *s1++; b = *s2++; 49 | a += (unsigned)(a - 'a') <= 'z' - 'a' ? 'A' - 'a' : 0; 50 | b += (unsigned)(b - 'a') <= 'z' - 'a' ? 'A' - 'a' : 0; 51 | } while (a == b && a); 52 | return a - b; 53 | } 54 | #endif 55 | 56 | #include "gravityblur.h" 57 | 58 | #define BITMAP_RGB 0 59 | #define BITMAP_YUV 1 60 | 61 | static bitmap_t* process_image(bitmap_t *in, 62 | int type1, int type2, int type3, 63 | int flags, float range, float sharp, int num_iter) { 64 | int w = in->width, h = in->height, bpp = in->bpp; 65 | bitmap_t *out; 66 | 67 | if (type1 != type2) { 68 | if (type2 == BITMAP_RGB) YCbCr2RGB(in); 69 | else RGB2YCbCr(in); 70 | } 71 | out = bitmap_create(w, h, bpp); 72 | if (out) { 73 | float range2 = range * 256, sharp2 = 1.0f / sharp; 74 | if (bpp < 3 || flags & 1) gravityblur(in, out, range2, sharp2, num_iter); 75 | if (bpp > 3 && flags & 2) gravityblur4(in, out, range2, sharp2, num_iter); 76 | else gravityblur3(in, out, range2, sharp2, num_iter); 77 | 78 | if (type2 != type3) { 79 | if (type3 == BITMAP_RGB) YCbCr2RGB(out); 80 | else RGB2YCbCr(out); 81 | } 82 | } 83 | return out; 84 | } 85 | 86 | #ifndef LESS_CONV 87 | #define LESS_CONV 1 88 | #endif 89 | 90 | #ifdef WASM 91 | static char** make_argv(char *str, int *argc_ret) { 92 | int i = 0, eol = 0, argc = 1; char **argv; 93 | for (;;) { 94 | char a = str[i++]; 95 | if (!a) break; 96 | if (eol) { 97 | if (a == eol) eol = 0; 98 | } else { 99 | if (a != ' ') { 100 | eol = ' '; 101 | if (a == '"' || a == '\'') eol = a; 102 | argc++; 103 | } 104 | } 105 | } 106 | *argc_ret = argc; 107 | argv = (char**)malloc(argc * sizeof(char*)); 108 | if (!argv) return argv; 109 | argv[0] = NULL; eol = 0; argc = 1; 110 | for (;;) { 111 | char a = *str++; 112 | if (!a) break; 113 | if (eol) { 114 | if (a == eol) { str[-1] = 0; eol = 0; } 115 | } else { 116 | if (a != ' ') { 117 | eol = ' '; 118 | if (a == '"' || a == '\'') { eol = a; str++; } 119 | argv[argc++] = str - 1; 120 | } 121 | } 122 | } 123 | return argv; 124 | } 125 | 126 | EMSCRIPTEN_KEEPALIVE 127 | void* web_process(void *web_ptr, char *cmdline) { 128 | #else 129 | int main(int argc, char **argv) { 130 | #endif 131 | int64_t time = 0; bitmap_t *in, *out; 132 | int type = BITMAP_RGB, in_type = type, out_type = type; 133 | 134 | int flags = 0, num_iter = 3, info = 3; 135 | float range = 10, sharp = 50; 136 | #ifdef WASM 137 | int argc = 0; 138 | char **argv_ptr = make_argv(cmdline, &argc), **argv = argv_ptr; 139 | in = (bitmap_t*)web_ptr; 140 | #else 141 | const char *ifn = 0, *ofn = 0; const char *ext; 142 | int read_jpeg = 0, write_jpeg = 0; 143 | const char *progname = "gravblur"; 144 | #endif 145 | 146 | while (argc > 1) { 147 | const char *arg1 = argv[1], *arg2 = argc > 2 ? argv[2] : NULL, *arg = arg1; char c; 148 | if (arg[0] != '-' || !(c = arg[1])) break; 149 | if (c != '-') switch (c) { 150 | case 'S': arg = "--separate"; c = 0; break; 151 | case 'a': arg = "--alpha"; c = 0; break; 152 | case 'r': arg = "--range"; break; 153 | case 's': arg = "--sharp"; break; 154 | case 'n': arg = "--niter"; break; 155 | case 'i': arg = "--info"; break; 156 | case 'R': arg = "--rgb"; c = 0; break; 157 | case 'Y': arg = "--yuv"; c = 0; break; 158 | default: c = '-'; 159 | } 160 | if (c != '-' && arg1[2]) { 161 | if (!c) break; 162 | arg2 = arg1 + 2; argc++; argv--; 163 | } 164 | 165 | #define CHECKNUM if ((unsigned)(arg2[0] - '0') > 9) break; 166 | if (!strcmp("--separate", arg)) { 167 | flags |= 1; argc--; argv++; 168 | } else if (!strcmp("--alpha", arg)) { 169 | flags |= 2; argc--; argv++; 170 | } else if (argc > 2 && !strcmp("--range", arg)) { 171 | CHECKNUM 172 | range = atof(arg2); 173 | argc -= 2; argv += 2; 174 | } else if (argc > 2 && !strcmp("--sharp", arg)) { 175 | CHECKNUM 176 | sharp = atof(arg2); 177 | argc -= 2; argv += 2; 178 | } else if (argc > 2 && !strcmp("--niter", arg)) { 179 | CHECKNUM 180 | num_iter = atoi(arg2); 181 | argc -= 2; argv += 2; 182 | } else if (argc > 2 && !strcmp("--info", arg)) { 183 | CHECKNUM 184 | info = atoi(arg2); 185 | argc -= 2; argv += 2; 186 | } else if (!strcmp("--rgb", arg)) { 187 | type = BITMAP_RGB; argc--; argv++; 188 | } else if (!strcmp("--yuv", arg)) { 189 | type = BITMAP_YUV; argc--; argv++; 190 | } else break; 191 | #undef CHECKNUM 192 | } 193 | 194 | #ifdef WASM 195 | free(argv_ptr); 196 | if (argc != 1) { 197 | printf("Unrecognized command line option.\n"); 198 | return NULL; 199 | } 200 | #else 201 | if (argc != 3) { 202 | printf( 203 | "Gravity Blur : Copyright (c) 2020 Ilya Kurdyukov\n" 204 | "Build date: " __DATE__ "\n" 205 | "\n" 206 | "Usage:\n" 207 | " %s [options] input.[png|jpg|jpeg] output.[png|jpg|jpeg]\n" 208 | "\n" 209 | "Options:\n" 210 | " -r, --range f Gravity range (0-100)\n" 211 | " -s, --sharp f Sharpness (0-100)\n" 212 | " -n, --niter n Number of iterations (default is 3)\n" 213 | " -R, --rgb Process in RGB (default)\n" 214 | " -Y, --yuv Process in YUV\n" 215 | " -S, --separate Separate color components\n" 216 | " -a, --alpha Use alpha channel\n" 217 | " -i, --info n Print gravblur debug output:\n" 218 | " 0 - silent, 3 - all (default)\n" 219 | "\n", progname); 220 | return 1; 221 | } 222 | 223 | ifn = argv[1]; 224 | ext = strrchr(ifn, '.'); 225 | if (ext && (!stricmp(ext, ".jpg") || !stricmp(ext, ".jpeg"))) read_jpeg = 1; 226 | ofn = argv[2]; 227 | ext = strrchr(ofn, '.'); 228 | if (ext && (!stricmp(ext, ".jpg") || !stricmp(ext, ".jpeg"))) write_jpeg = 1; 229 | 230 | if (read_jpeg) { 231 | if (LESS_CONV && type == BITMAP_YUV) { 232 | in = bitmap_read_jpeg_yuv(ifn); 233 | in_type = BITMAP_YUV; 234 | } else { 235 | in = bitmap_read_jpeg(ifn); 236 | } 237 | if (!in) { printf("read_jpeg failed\n"); return 1; } 238 | } else { 239 | in = bitmap_read_png(ifn); 240 | if (!in) { printf("read_png failed\n"); return 1; } 241 | } 242 | 243 | if (LESS_CONV && write_jpeg) out_type = type; 244 | #endif 245 | 246 | range /= 100; sharp /= 100; 247 | if (range > 2) range = 2; 248 | if (range < 0) range = 0; 249 | if (sharp > 1) sharp = 1; 250 | if (sharp < 0.01f) sharp = 0.01f; 251 | 252 | if (info & 1) time = get_time_usec(); 253 | out = process_image(in, in_type, type, out_type, 254 | flags, range, sharp, num_iter); 255 | if (info & 1) { 256 | time = get_time_usec() - time; 257 | printf("gravblur: %.2f ms\n", time * 0.001); 258 | } 259 | 260 | #ifdef WASM 261 | return out; 262 | #else 263 | if (out) { 264 | if (info & 2) time = get_time_usec(); 265 | if (write_jpeg) { 266 | if (out_type == BITMAP_YUV) bitmap_write_jpeg_yuv(out, ofn); 267 | else bitmap_write_jpeg(out, ofn); 268 | } else { 269 | bitmap_write_png(out, ofn); 270 | } 271 | if (info & 2) { 272 | time = get_time_usec() - time; 273 | printf("writing %s: %.2f ms\n", 274 | write_jpeg ? "jpeg" : "png", time * 0.001); 275 | } 276 | bitmap_free(out); 277 | } 278 | bitmap_free(in); 279 | return 0; 280 | #endif 281 | } 282 | 283 | -------------------------------------------------------------------------------- /pngio.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (C) 2020 Ilya Kurdyukov 3 | * 4 | * png support using libpng 5 | */ 6 | 7 | #define PNG_DEBUG 0 8 | #include 9 | 10 | // http://www.libpng.org/pub/png/libpng-1.2.5-manual.html 11 | 12 | bitmap_t* bitmap_read_png(const char *filename) { 13 | unsigned char header[8]; png_structp png_ptr = NULL; 14 | png_infop info_ptr = NULL, end_info = NULL; 15 | bitmap_t * volatile bm = NULL; FILE * volatile fp = NULL; 16 | const char * volatile errstr = "param_check"; 17 | void * volatile mem = NULL; 18 | 19 | if (!filename) goto err; 20 | errstr = "fopen"; 21 | if (!(fp = fopen(filename, "rb"))) goto err; 22 | errstr = "sig_cmp"; 23 | if (fread(header, 1, 8, fp) != 8 || png_sig_cmp(header, 0, 8)) goto err; 24 | 25 | errstr = "create_read_struct"; 26 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 27 | if (!png_ptr) goto err; 28 | 29 | errstr = "create_info_struct"; 30 | info_ptr = png_create_info_struct(png_ptr); 31 | if (!info_ptr) goto err; 32 | 33 | if (!setjmp(png_jmpbuf(png_ptr))) { 34 | int y, width, height, bpp, stride; 35 | png_bytep *row_pointers; 36 | uint8_t *data; png_byte b; 37 | bitmap_t *bm1; 38 | 39 | errstr = "init_io"; 40 | png_init_io(png_ptr, fp); 41 | png_set_sig_bytes(png_ptr, 8); 42 | 43 | errstr = "read_info"; 44 | png_read_info(png_ptr, info_ptr); 45 | 46 | width = png_get_image_width(png_ptr, info_ptr); 47 | height = png_get_image_height(png_ptr, info_ptr); 48 | png_read_update_info(png_ptr, info_ptr); 49 | 50 | // b = png_get_color_type(png_ptr, info_ptr); 51 | errstr = "bit_depth == 8"; 52 | b = png_get_bit_depth(png_ptr, info_ptr); 53 | if (b != 8) goto err; 54 | 55 | bpp = png_get_channels(png_ptr, info_ptr); 56 | bm = bm1 = bitmap_create(width, height, bpp); 57 | errstr = "malloc"; 58 | if (!bm1) goto err; 59 | stride = bm1->stride; data = bm1->data; 60 | 61 | mem = malloc(height * sizeof(png_bytep)); 62 | if (!mem) goto err; 63 | row_pointers = (png_bytep*)mem; 64 | for (y = 0; y < height; y++) 65 | row_pointers[y] = (png_byte*)(data + y * stride); 66 | 67 | png_read_image(png_ptr, row_pointers); 68 | errstr = NULL; 69 | } 70 | err: 71 | if (mem) free(mem); 72 | if (errstr) printf("[read_png] %s failed\n", errstr); 73 | if (png_ptr) png_destroy_read_struct(&png_ptr, &info_ptr, &end_info); 74 | if (fp) fclose(fp); 75 | return bm; 76 | } 77 | 78 | void bitmap_write_png(bitmap_t *bm, const char *filename) { 79 | png_structp png_ptr = NULL; png_infop info_ptr = NULL; 80 | int y, width, height, bpp, stride; FILE * volatile fp = NULL; 81 | png_byte volatile color_type; 82 | const char * volatile errstr = "param_check"; 83 | void * volatile mem = NULL; 84 | 85 | if (!bm || !filename) goto err; 86 | width = bm->width; height = bm->height; 87 | bpp = bm->bpp; stride = bm->stride; 88 | if (bpp < 1 || bpp > 4) goto err; 89 | color_type = 90 | bpp == 3 ? PNG_COLOR_TYPE_RGB : 91 | bpp == 4 ? PNG_COLOR_TYPE_RGB_ALPHA : 92 | bpp == 1 ? PNG_COLOR_TYPE_GRAY : PNG_COLOR_TYPE_GRAY_ALPHA; 93 | 94 | errstr = "fopen"; 95 | if (!(fp = fopen(filename, "wb"))) goto err; 96 | 97 | errstr = "create_write_struct"; 98 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 99 | if (!png_ptr) goto err; 100 | 101 | errstr = "create_info_struct"; 102 | info_ptr = png_create_info_struct(png_ptr); 103 | if (!info_ptr) goto err; 104 | 105 | errstr = "init_io"; 106 | if (!setjmp(png_jmpbuf(png_ptr))) { 107 | uint8_t *data = bm->data; png_byte bit_depth = 8; 108 | png_bytep *row_pointers; 109 | 110 | png_init_io(png_ptr, fp); 111 | 112 | errstr = "write_info"; 113 | png_set_IHDR(png_ptr, info_ptr, width, height, 114 | bit_depth, color_type, PNG_INTERLACE_NONE, 115 | PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 116 | png_write_info(png_ptr, info_ptr); 117 | 118 | mem = malloc(height * sizeof(png_bytep)); 119 | errstr = "malloc"; 120 | if (!mem) goto err; 121 | row_pointers = (png_bytep*)mem; 122 | for (y = 0; y < height; y++) 123 | row_pointers[y] = (png_byte*)(data + y * stride); 124 | 125 | errstr = "write_image"; 126 | png_write_image(png_ptr, row_pointers); 127 | 128 | errstr = "write_end"; 129 | png_write_end(png_ptr, NULL); 130 | errstr = NULL; 131 | } 132 | err: 133 | if (mem) free(mem); 134 | if (errstr) printf("[write_png] %s failed\n", errstr); 135 | if (png_ptr) png_destroy_write_struct(&png_ptr, &info_ptr); 136 | if (fp) fclose(fp); 137 | } 138 | 139 | -------------------------------------------------------------------------------- /wasm/compile.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | source ../emsdk/emsdk_env.sh --build=Release 4 | 5 | debug=0 6 | emflags="-O3 -g0 --closure 1" 7 | [ $debug == 1 ] && emflags="-O2 -g1 -s ASSERTIONS=1" 8 | 9 | emcc -DWASM main.c -o gravityblur.html --shell-file shell.html \ 10 | $emflags -s EXPORTED_FUNCTIONS="['_malloc', '_free']" -s ALLOW_MEMORY_GROWTH=1 11 | mv gravityblur.html index.html 12 | 13 | echo "press enter..."; read dummy < /dev/tty 14 | 15 | -------------------------------------------------------------------------------- /wasm/shell.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Gravity Blur (WebAssembly) 6 | 7 | 8 |
9 | 10 | 11 |
12 | Options:
13 |
14 | Filename:
15 |
16 |
17 |
You need to enable JavaScript to run this app.
18 |
19 | 20 |
21 | 22 |
23 | 24 | 208 | {{{ SCRIPT }}} 209 | 210 | 211 | --------------------------------------------------------------------------------