├── Debug ├── SDL Imag.35E7CB6B.tlog │ ├── CL.read.1.tlog │ ├── CL.write.1.tlog │ ├── SDL Image Manipulations.lastbuildstate │ ├── cl.command.1.tlog │ ├── link.command.1.tlog │ ├── link.read.1.tlog │ └── link.write.1.tlog ├── SDL Image Manipulations.log ├── main.obj ├── transform.obj ├── vc120.idb └── vc120.pdb ├── LICENSE ├── README.md ├── Resources ├── arial.ttf ├── saved.png └── test.jpg ├── SDL Image Manipulations.vcxproj ├── SDL Image Manipulations.vcxproj.filters ├── Screens ├── AdjustContrast_HistogramAverage.jpg ├── AdjustContrast_HistogramAverage.txt ├── AdjustHighlights_HistogramAverage.jpg ├── AdjustShadows.jpg ├── ColorAdjust_HistoVaried.jpg ├── ColorAdjust_HistoVaried.txt ├── Desaturate.jpg ├── Kernel_Blur.jpg ├── Kernel_Edge.jpg ├── Kernel_Emboss.jpg ├── Offset.jpg ├── Posterize_Histo.jpg ├── Posterize_Histo_High_C.jpg ├── Scaling.jpg ├── Threshold.jpg ├── Trace.jpg └── Trace.txt ├── Transform.h ├── Util ├── GUI.h ├── Segment.h ├── gui.cpp ├── kiss_fft │ ├── Makefile │ ├── _kiss_fft_guts.h │ ├── kiss_fft.c │ ├── kiss_fft.h │ ├── kissfft.hh │ └── tools │ │ ├── Makefile │ │ ├── fftutil.c │ │ ├── kfc.c │ │ ├── kfc.h │ │ ├── kiss_fastfir.c │ │ ├── kiss_fftnd.c │ │ ├── kiss_fftnd.h │ │ ├── kiss_fftndr.c │ │ ├── kiss_fftndr.h │ │ ├── kiss_fftr.c │ │ ├── kiss_fftr.h │ │ └── psdpng.c └── segment.cpp ├── main.cpp └── transform.cpp /Debug/SDL Imag.35E7CB6B.tlog/CL.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/CL.read.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/CL.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/CL.write.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/SDL Image Manipulations.lastbuildstate: -------------------------------------------------------------------------------- 1 | #TargetFrameworkVersion=v4.0:PlatformToolSet=v120:EnableManagedIncrementalBuild=false:VCToolArchitecture=Native32Bit 2 | Debug|Win32|C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\| 3 | -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/cl.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/cl.command.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/link.command.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/link.command.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/link.read.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/link.read.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Imag.35E7CB6B.tlog/link.write.1.tlog: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/SDL Imag.35E7CB6B.tlog/link.write.1.tlog -------------------------------------------------------------------------------- /Debug/SDL Image Manipulations.log: -------------------------------------------------------------------------------- 1 | Build started 10/4/2016 9:23:34 AM. 2 | 1>Project "C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\SDL Image Manipulations\SDL Image Manipulations.vcxproj" on node 2 (Build target(s)). 3 | 1>ClCompile: 4 | C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\CL.exe /c /ZI /nologo /W3 /WX- /sdl /Od /Oy- /D _MBCS /Gm /EHsc /RTC1 /MDd /GS /fp:precise /Zc:wchar_t /Zc:forScope /Fo"Debug\\" /Fd"Debug\vc120.pdb" /Gd /TP /analyze- /errorReport:prompt main.cpp 5 | main.cpp 6 | Link: 7 | C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\Debug\SDL Image Manipulations.exe" /INCREMENTAL /NOLOGO SDL2.lib SDL2main.lib SDL2_image.lib SDL2_ttf.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\Debug\SDL Image Manipulations.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\Debug\SDL Image Manipulations.lib" /MACHINE:X86 Debug\main.obj 8 | Debug\transform.obj 9 | SDL Image Manipulations.vcxproj -> C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\Debug\SDL Image Manipulations.exe 10 | 1>Done Building Project "C:\Users\Dan\documents\visual studio 2013\Projects\SDL Image Manipulations\SDL Image Manipulations\SDL Image Manipulations.vcxproj" (Build target(s)). 11 | 12 | Build succeeded. 13 | 14 | Time Elapsed 00:00:00.83 15 | -------------------------------------------------------------------------------- /Debug/main.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/main.obj -------------------------------------------------------------------------------- /Debug/transform.obj: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/transform.obj -------------------------------------------------------------------------------- /Debug/vc120.idb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/vc120.idb -------------------------------------------------------------------------------- /Debug/vc120.pdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Debug/vc120.pdb -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | {one line to give the program's name and a brief idea of what it does.} 635 | Copyright (C) {year} {name of author} 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | {project} Copyright (C) {year} {fullname} 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # C++ and SDL - Image Processing Library 2 | A portable library of image processing, manipulation, and generation algorithms, used to modify and analyze surfaces before they are converted to textures. 3 | 4 | Feel free to use and include your own functions as well. To include in your project, simply copy the 'transform.h' and 'transform.cpp' to your target project destination and include within your main source. Requires SDL2 to use. It is recommended to also include SDL2_ttf and SDL2_image if possible. Enjoy and share your work! 5 | 6 | If any bugs or issues are found, please email me at danielfalk.composer@gmail.com so that I can sort it out! 7 | 8 | Kernel Operation - Emboss 9 | ![alt tag](Screens/Kernel_Emboss.jpg) 10 | 11 | Trace Binary 12 | ![alt tag](Screens/Trace.jpg) 13 | 14 | Adjust Contrast - Average Histogram Comparison 15 | ![alt tag](Screens/AdjustContrast_HistogramAverage.jpg) 16 | -------------------------------------------------------------------------------- /Resources/arial.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Resources/arial.ttf -------------------------------------------------------------------------------- /Resources/saved.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Resources/saved.png -------------------------------------------------------------------------------- /Resources/test.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Resources/test.jpg -------------------------------------------------------------------------------- /SDL Image Manipulations.vcxproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | Win32 7 | 8 | 9 | Release 10 | Win32 11 | 12 | 13 | 14 | {35E7CB6B-7F3D-4084-9ABF-DDD8A50557E4} 15 | SDL_Image_Manipulations 16 | 17 | 18 | 19 | Application 20 | true 21 | v120 22 | MultiByte 23 | 24 | 25 | Application 26 | false 27 | v120 28 | true 29 | MultiByte 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | C:\_Development Libraries\sdl2_vc\SDL2-2.0.4\include;C:\_Development Libraries\SDL2_ttf-2.0.14\include;$(IncludePath) 43 | C:\_Development Libraries\SDL2_ttf-2.0.14\lib\x86;C:\_Development Libraries\sdl2_vc\SDL2-2.0.4\lib\x86;$(LibraryPath) 44 | 45 | 46 | C:\_Development Libraries\sdl2_vc\SDL2-2.0.4\include;C:\_Development Libraries\SDL2_ttf-2.0.14\include;$(IncludePath) 47 | C:\_Development Libraries\SDL2_ttf-2.0.14\lib\x86;C:\_Development Libraries\sdl2_vc\SDL2-2.0.4\lib\x86;$(LibraryPath) 48 | 49 | 50 | 51 | Level3 52 | Disabled 53 | true 54 | 55 | 56 | true 57 | SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;%(AdditionalDependencies) 58 | Console 59 | 60 | 61 | 62 | 63 | Level3 64 | MaxSpeed 65 | true 66 | true 67 | true 68 | 69 | 70 | true 71 | true 72 | true 73 | SDL2.lib;SDL2main.lib;SDL2_image.lib;SDL2_ttf.lib;%(AdditionalDependencies) 74 | Console 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | -------------------------------------------------------------------------------- /SDL Image Manipulations.vcxproj.filters: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | {4FC737F1-C7A5-4376-A066-2A32D752A2FF} 6 | cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx 7 | 8 | 9 | {93995380-89BD-4b04-88EB-625FBE52EBFB} 10 | h;hh;hpp;hxx;hm;inl;inc;xsd 11 | 12 | 13 | {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} 14 | rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms 15 | 16 | 17 | 18 | 19 | Source Files 20 | 21 | 22 | Source Files 23 | 24 | 25 | 26 | 27 | Header Files 28 | 29 | 30 | -------------------------------------------------------------------------------- /Screens/AdjustContrast_HistogramAverage.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/AdjustContrast_HistogramAverage.jpg -------------------------------------------------------------------------------- /Screens/AdjustContrast_HistogramAverage.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SDL.h" 4 | #include "SDL_image.h" 5 | #include "SDL_ttf.h" 6 | #include "Transform.h" 7 | 8 | using namespace std; 9 | using namespace transform; 10 | 11 | const int SCREEN_WID = 1280; 12 | const int SCREEN_HEI = 960; 13 | const int WINDOW_WID = SCREEN_WID; 14 | const int WINDOW_HEI = SCREEN_HEI; 15 | 16 | int main(int argc, char* argv[]){ 17 | if (SDL_Init(SDL_INIT_EVERYTHING) >0){ 18 | cout << "Error: SDL failed to initialize." << endl; 19 | return 0; 20 | } 21 | 22 | SDL_Window* window = SDL_CreateWindow("SDL Image Manipulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WID, WINDOW_HEI, NULL); 23 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 24 | SDL_Event event; 25 | 26 | //Load and modify image 27 | SDL_Surface* image = IMG_Load("Resources/test.jpg"); //Load source image 28 | SDL_Surface* filtered = CopySurface(image); //Copy source image's pixel format, pixel data, and dimensions 29 | SDL_Surface* graphA = EmptySurface(image); //Create empty surface with source image's pixel format and dimensions 30 | SDL_Surface* graphB = EmptySurface(image); //Create empty surface with source image's pixel format and dimensions 31 | 32 | //Perform image processes 33 | AdjustContrast(filtered, 60); 34 | 35 | //Generate graph for unmodified surface 36 | Fill(graphA, CreateColor(30, 30, 30, 255)); 37 | DrawGridlines(graphA, CreateColor(45, 45, 45, 255), GRID_DEFAULT, 64, 4, 0, 0); 38 | Histogram(image, graphA, CHANNEL_ALL, GRAPH_FILLED, 64, 1, 6000); 39 | 40 | //Generate graph for filtered surface 41 | Fill(graphB, CreateColor(30, 30, 30, 255)); 42 | DrawGridlines(graphB, CreateColor(45, 45, 45, 255), GRID_DEFAULT, 64, 4, 0, 0); 43 | Histogram(filtered, graphB, CHANNEL_ALL, GRAPH_FILLED, 64, 1, 6000); 44 | 45 | SDL_Texture* tImage = SDL_CreateTextureFromSurface(renderer, image); 46 | SDL_Texture* tPost = SDL_CreateTextureFromSurface(renderer, filtered); 47 | SDL_Texture* tGraphA = SDL_CreateTextureFromSurface(renderer, graphA); 48 | SDL_Texture* tGraphB = SDL_CreateTextureFromSurface(renderer, graphB); 49 | 50 | //Clean Up 51 | SDL_FreeSurface(image); 52 | SDL_FreeSurface(filtered); 53 | SDL_FreeSurface(graphA); 54 | SDL_FreeSurface(graphB); 55 | 56 | //Main loop 57 | bool quit = false; 58 | 59 | while (quit != true){ 60 | while (SDL_PollEvent(&event) != 0){ 61 | switch (event.key.keysym.sym){ 62 | case SDLK_ESCAPE: 63 | quit = true; 64 | break; 65 | } 66 | } 67 | 68 | //Render media 69 | SDL_RenderClear(renderer); 70 | SDL_RenderCopy(renderer, tImage, NULL, &CreateRect(0,0, SCREEN_WID/2, SCREEN_HEI/2)); 71 | SDL_RenderCopy(renderer, tPost, NULL, &CreateRect(0, SCREEN_HEI/2, SCREEN_WID/2, SCREEN_HEI/2)); 72 | SDL_RenderCopy(renderer, tGraphA, NULL, &CreateRect(SCREEN_WID/2, 0, SCREEN_WID/2, SCREEN_HEI/2)); 73 | SDL_RenderCopy(renderer, tGraphB, NULL, &CreateRect(SCREEN_WID/2, SCREEN_HEI/2, SCREEN_WID/2, SCREEN_HEI/2)); 74 | SDL_RenderPresent(renderer); 75 | } 76 | 77 | //Clean up 78 | SDL_Quit(); 79 | 80 | return 0; 81 | } -------------------------------------------------------------------------------- /Screens/AdjustHighlights_HistogramAverage.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/AdjustHighlights_HistogramAverage.jpg -------------------------------------------------------------------------------- /Screens/AdjustShadows.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/AdjustShadows.jpg -------------------------------------------------------------------------------- /Screens/ColorAdjust_HistoVaried.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/ColorAdjust_HistoVaried.jpg -------------------------------------------------------------------------------- /Screens/ColorAdjust_HistoVaried.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SDL.h" 4 | #include "SDL_image.h" 5 | #include "SDL_ttf.h" 6 | #include "Transform.h" 7 | 8 | using namespace std; 9 | using namespace transform; 10 | 11 | const int SCREEN_WID = 1280; 12 | const int SCREEN_HEI = 960; 13 | const int WINDOW_WID = SCREEN_WID; 14 | const int WINDOW_HEI = SCREEN_HEI; 15 | 16 | int main(int argc, char* argv[]){ 17 | if (SDL_Init(SDL_INIT_EVERYTHING) >0){ 18 | cout << "Error: SDL failed to initialize." << endl; 19 | return 0; 20 | } 21 | 22 | SDL_Window* window = SDL_CreateWindow("SDL Image Manipulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WID, WINDOW_HEI, NULL); 23 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 24 | SDL_Event event; 25 | 26 | //Load and modify image 27 | SDL_Surface* image = IMG_Load("Resources/test.jpg"); //Load source image 28 | SDL_Surface* filtered = CopySurface(image); //Copy source image's pixel format, pixel data, and dimensions 29 | SDL_Surface* graphA = EmptySurface(image); //Create empty surface with source image's pixel format and dimensions 30 | SDL_Surface* graphB = EmptySurface(image); //Create empty surface with source image's pixel format and dimensions 31 | 32 | //Perform image processes 33 | AdjustRed(filtered, 30); 34 | 35 | //Generate graph for unmodified surface 36 | Fill(graphA, CreateColor(30, 30, 30, 255)); 37 | DrawGridlines(graphA, CreateColor(45, 45, 45, 255), GRID_DEFAULT, 64, 4, 0, 0); 38 | Histogram(image, graphA, CHANNEL_ALL, GRAPH_FILLED, 64, 1, 6000); 39 | Histogram(image, graphA, CHANNEL_RED, GRAPH_DOTTED, 64, 1, 6000); 40 | Histogram(image, graphA, CHANNEL_GREEN, GRAPH_DOTTED, 64, 1, 6000); 41 | Histogram(image, graphA, CHANNEL_BLUE, GRAPH_DOTTED, 64, 1, 6000); 42 | 43 | //Generate graph for filtered surface 44 | Fill(graphB, CreateColor(30, 30, 30, 255)); 45 | DrawGridlines(graphB, CreateColor(45, 45, 45, 255), GRID_DEFAULT, 64, 4, 0, 0); 46 | Histogram(image, graphB, CHANNEL_RED, GRAPH_LINEAR, 64, 1, 6000); 47 | 48 | SDL_Texture* tImage = SDL_CreateTextureFromSurface(renderer, image); 49 | SDL_Texture* tPost = SDL_CreateTextureFromSurface(renderer, filtered); 50 | SDL_Texture* tGraphA = SDL_CreateTextureFromSurface(renderer, graphA); 51 | SDL_Texture* tGraphB = SDL_CreateTextureFromSurface(renderer, graphB); 52 | 53 | //Clean Up 54 | SDL_FreeSurface(image); 55 | SDL_FreeSurface(filtered); 56 | SDL_FreeSurface(graphA); 57 | SDL_FreeSurface(graphB); 58 | 59 | //Main loop 60 | bool quit = false; 61 | 62 | while (quit != true){ 63 | while (SDL_PollEvent(&event) != 0){ 64 | switch (event.key.keysym.sym){ 65 | case SDLK_ESCAPE: 66 | quit = true; 67 | break; 68 | } 69 | } 70 | 71 | //Render media 72 | SDL_RenderClear(renderer); 73 | SDL_RenderCopy(renderer, tImage, NULL, &CreateRect(0,0, SCREEN_WID/2, SCREEN_HEI/2)); 74 | SDL_RenderCopy(renderer, tPost, NULL, &CreateRect(0, SCREEN_HEI/2, SCREEN_WID/2, SCREEN_HEI/2)); 75 | SDL_RenderCopy(renderer, tGraphA, NULL, &CreateRect(SCREEN_WID/2, 0, SCREEN_WID/2, SCREEN_HEI/2)); 76 | SDL_RenderCopy(renderer, tGraphB, NULL, &CreateRect(SCREEN_WID/2, SCREEN_HEI/2, SCREEN_WID/2, SCREEN_HEI/2)); 77 | SDL_RenderPresent(renderer); 78 | } 79 | 80 | //Clean up 81 | SDL_Quit(); 82 | 83 | return 0; 84 | } -------------------------------------------------------------------------------- /Screens/Desaturate.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Desaturate.jpg -------------------------------------------------------------------------------- /Screens/Kernel_Blur.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Kernel_Blur.jpg -------------------------------------------------------------------------------- /Screens/Kernel_Edge.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Kernel_Edge.jpg -------------------------------------------------------------------------------- /Screens/Kernel_Emboss.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Kernel_Emboss.jpg -------------------------------------------------------------------------------- /Screens/Offset.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Offset.jpg -------------------------------------------------------------------------------- /Screens/Posterize_Histo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Posterize_Histo.jpg -------------------------------------------------------------------------------- /Screens/Posterize_Histo_High_C.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Posterize_Histo_High_C.jpg -------------------------------------------------------------------------------- /Screens/Scaling.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Scaling.jpg -------------------------------------------------------------------------------- /Screens/Threshold.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Threshold.jpg -------------------------------------------------------------------------------- /Screens/Trace.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dfalkcreative/sdl-image-processing/e743f90fd78c559494827686e7b907a8ef0a1cef/Screens/Trace.jpg -------------------------------------------------------------------------------- /Screens/Trace.txt: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SDL.h" 4 | #include "SDL_image.h" 5 | #include "SDL_ttf.h" 6 | #include "Transform.h" 7 | 8 | using namespace std; 9 | using namespace transform; 10 | 11 | const int SCREEN_WID = 1280; 12 | const int SCREEN_HEI = 960; 13 | const int WINDOW_WID = SCREEN_WID; 14 | const int WINDOW_HEI = SCREEN_HEI; 15 | 16 | int main(int argc, char* argv[]){ 17 | if (SDL_Init(SDL_INIT_EVERYTHING) >0){ 18 | cout << "Error: SDL failed to initialize." << endl; 19 | return 0; 20 | } 21 | 22 | SDL_Window* window = SDL_CreateWindow("SDL Image Manipulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WID, WINDOW_HEI, NULL); 23 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 24 | SDL_Event event; 25 | 26 | //Load and modify image 27 | SDL_Surface* image = IMG_Load("Resources/test.jpg"); //Load source image 28 | SDL_Surface* filtered = CopySurface(image); //Copy source image's pixel format, pixel data, and dimensions 29 | SDL_Surface* bg = EmptySurface(image, SCREEN_WID, SCREEN_HEI); //Create empty surface for background 30 | 31 | //Perform image processes 32 | filtered = FilterBlur(image, 5, EDGE_EXTEND); 33 | filtered = Trace(filtered, 150, getColor(COLOR_WHITE)); 34 | 35 | //Generate background 36 | Fill(bg, CreateColor(50,50,50,255)); 37 | 38 | SDL_Texture* tImage = SDL_CreateTextureFromSurface(renderer, image); 39 | SDL_Texture* tPost = SDL_CreateTextureFromSurface(renderer, filtered); 40 | SDL_Texture* back = SDL_CreateTextureFromSurface(renderer, bg); 41 | 42 | //Clean Up 43 | SDL_FreeSurface(image); 44 | SDL_FreeSurface(filtered); 45 | 46 | //Main loop 47 | bool quit = false; 48 | 49 | while (quit != true){ 50 | while (SDL_PollEvent(&event) != 0){ 51 | switch (event.key.keysym.sym){ 52 | case SDLK_ESCAPE: 53 | quit = true; 54 | break; 55 | } 56 | } 57 | 58 | //Render media 59 | SDL_RenderClear(renderer); 60 | SDL_RenderCopy(renderer, back, NULL, NULL); 61 | SDL_RenderCopy(renderer, tImage, NULL, &CreateRect(0,SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2)); 62 | SDL_RenderCopy(renderer, tPost, NULL, &CreateRect(SCREEN_WID/2, SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2)); 63 | SDL_RenderPresent(renderer); 64 | } 65 | 66 | //Clean up 67 | SDL_Quit(); 68 | 69 | return 0; 70 | } -------------------------------------------------------------------------------- /Transform.h: -------------------------------------------------------------------------------- 1 | #ifndef TRANSFORM_H 2 | #define TRANSFORM_H 3 | 4 | typedef int flag; 5 | 6 | namespace transform{ 7 | const int FILTER_NONE = 0; 8 | const int FILTER_BLUR = 1; 9 | const int FILTER_SHARPEN = 2; 10 | const int FILTER_EDGE = 3; 11 | const int FILTER_EMBOSS = 4; 12 | const int FILTER_SOBEL_LEFT = 5; 13 | const int FILTER_SOBEL_RIGHT = 6; 14 | const int FILTER_SOBEL_TOP = 7; 15 | const int FILTER_SOBEL_BOTTOM = 8; 16 | const int FILTER_UNSHARP_MASK = 9; 17 | const int EDGE_CROP_WHITE = 0; //For kernel operations: Replaces out-of-boundary coordinate color values with NULL values rgb(0,0,0). 18 | const int EDGE_CROP_BLACK = 1; 19 | const int EDGE_EXTEND = 2; //For kernel operations: Replaces out-of-boundary coordinate color values with last used value along image edge. 20 | const int EDGE_WRAP = 3; //For kernel operations: Replaces out-of-boundary coordinate color values with respective value from opposite edge (for seamless images). 21 | const int GRAY_AVERAGE = 0; //(R +G + B) / 3 22 | const int GRAY_LUMINANCE = 1; //0.72R + 0.21G + 0.07B 23 | const int GRAY_LIGHTNESS = 2; //(min(R, G, B) + max(R, G, B)) / 2 24 | const int GRAY_RED = 3; //Set grayscale equal to red channel color values 25 | const int GRAY_GREEN = 4; //Set grayscale equal to green channel color values 26 | const int GRAY_BLUE = 5; //Set grayscale equal to blue channel color values 27 | const int CHANNEL_ALL = 0; 28 | const int CHANNEL_RED = 1; 29 | const int CHANNEL_GREEN = 2; 30 | const int CHANNEL_BLUE = 3; 31 | const int CHANNEL_HSV = 4; 32 | const int CHANNEL_HUE = 5; 33 | const int CHANNEL_SATURATION = 6; 34 | const int CHANNEL_VALUE = 7; 35 | 36 | //For use with 'GetColor' function 37 | const int COLOR_WHITE = 0; 38 | const int COLOR_GRAY = 1; 39 | const int COLOR_BLACK = 2; 40 | const int COLOR_RED = 3; 41 | const int COLOR_ORANGE = 4; 42 | const int COLOR_YELLOW = 5; 43 | const int COLOR_GREEN = 6; 44 | const int COLOR_BLUE = 7; 45 | const int COLOR_INDIGO = 8; 46 | const int COLOR_PURPLE = 9; 47 | const int COLOR_SALMON = 10; 48 | const int COLOR_BROWN = 11; 49 | const int COLOR_ORANGE_RED = 12; 50 | const int COLOR_DARK_ORANGE = 13; 51 | const int COLOR_BEIGE = 14; 52 | const int COLOR_TAN = 15; 53 | 54 | //Histogram and Graph Styling 55 | const int GRAPH_DOTTED = 0; 56 | const int GRAPH_LINEAR = 1; 57 | const int GRAPH_FILLED = 2; 58 | const int GRID_DEFAULT = 0; 59 | const int GRID_HORIZONTAL = 1; 60 | const int GRID_VERTICAL = 2; 61 | 62 | //Noise Styling 63 | const int NOISE_GRAYSCALE = 0; 64 | const int NOISE_COLORED = 1; 65 | const int NOISE_RED = 2; 66 | const int NOISE_GREEN = 3; 67 | const int NOISE_BLUE = 4; 68 | 69 | //Posterize Styling 70 | const int POST_AVERAGE = 0; 71 | const int POST_NONE = 1; 72 | const int POST_HIGH_C = 2; 73 | const int POST_BW = 3; 74 | 75 | //Interlace Styling 76 | const int IL_DEFAULT = 0; 77 | const int IL_HORIZONTAL = 1; 78 | const int IL_VERTICAL = 2; 79 | const int IL_WEAVE = 3; 80 | const int IL_DOTTED = 4; 81 | 82 | //Blend Flags 83 | const int BLEND_DEFAULT = 0; 84 | const int BLEND_ADD = 1; 85 | const int BLEND_SUBTRACT = 2; 86 | const int BLEND_MULTIPLY = 3; 87 | const int BLEND_DIVIDE = 4; 88 | 89 | //Math Variables 90 | const double PI = 3.141592; 91 | 92 | //GUI Constants 93 | const int WINPOS_CENTERED = 0; 94 | const int WINPOS_TOP_L = 1; 95 | const int WINPOS_TOP_R = 2; 96 | const int WINPOS_BOTTOM_L = 3; 97 | const int WINPOS_BOTTOM_R = 4; 98 | 99 | //GUI::Window States 100 | const int GUI_STATE_DEFAULT = 0; 101 | const int GUI_STATE_HOVER = 1; 102 | const int GUI_STATE_DRAG = 2; 103 | const int GUI_STATE_RESIZE = 3; 104 | } 105 | 106 | struct Vec2i{ 107 | int x, y; 108 | 109 | Vec2i(); 110 | Vec2i(int X, int Y):x(X), y(Y){} 111 | }; 112 | 113 | struct Vec2d{ 114 | double x, y; 115 | 116 | Vec2d(double X, double Y) :x(X), y(Y){} 117 | }; 118 | 119 | struct UMatrix3x3{ 120 | Uint8 val[3][3]; 121 | }; 122 | 123 | struct DMatrix3x3{ 124 | double val[3][3]; 125 | }; 126 | 127 | struct UMatrix5x5{ 128 | Uint8 val[5][5]; 129 | }; 130 | 131 | struct DMatrix5x5{ 132 | double val[5][5]; 133 | }; 134 | 135 | struct Kernel3x3: public DMatrix3x3{ 136 | //Kernel operations 137 | void Normalize(); 138 | void Scale(double); 139 | 140 | //Kernel presets 141 | void Blur(){ 142 | val[0][0] = 0.0625; val[0][1] = 0.125; val[0][2] = 0.0625; 143 | val[1][0] = 0.125; val[1][1] = 0.25; val[1][2] = 0.125; 144 | val[2][0] = 0.0625; val[2][1] = 0.125; val[2][2] = 0.0625; 145 | } 146 | void Sharpen(){ 147 | val[0][0] = 0.0; val[0][1] = -1.0; val[0][2] = 0.0; 148 | val[1][0] = -1.0; val[1][1] = 5.0; val[1][2] = -1.0; 149 | val[2][0] = 0.0; val[2][1] = -1.0; val[2][2] = 0.0; 150 | } 151 | void Emboss(){ 152 | val[0][0] = -2.0; val[0][1] = -1.0; val[0][2] = 0.0; 153 | val[1][0] = -1.0; val[1][1] = 1.0; val[1][2] = 1.0; 154 | val[2][0] = 0.0; val[2][1] = 1.0; val[2][2] = 2.0; 155 | } 156 | void Edge(){ 157 | val[0][0] = 0.0; val[0][1] = -1.0; val[0][2] = 0.0; 158 | val[1][0] = -1.0; val[1][1] = 4.0; val[1][2] = -1.0; 159 | val[2][0] = 0.0; val[2][1] = -1.0; val[2][2] = 0.0; 160 | } 161 | void Sobel(int); 162 | }; 163 | 164 | struct Kernel5x5 : public DMatrix5x5{ 165 | void Normalize(); 166 | void Scale(double); 167 | 168 | void Identity(); 169 | void Blur(); 170 | void Sharpen(); 171 | void Emboss(); 172 | void Edge(); 173 | void Sobel(int); 174 | }; 175 | 176 | //----------------------------------------------------------------------------------------------------------------------// 177 | // Constructor Functions: // 178 | //----------------------------------------------------------------------------------------------------------------------// 179 | 180 | //'SDL_Rect' constructor function 181 | inline SDL_Rect CreateRect(int x, int y, int w, int h); 182 | 183 | //'SDL_Rect' constructor function 184 | inline SDL_Rect CreateRect(double x, double y, double w, double h); 185 | 186 | //'SDL_Rect' constructor function 187 | inline SDL_Rect CreateRect(float x, float y, float w, float h); 188 | 189 | //'SDL_Color' constructor function 190 | inline SDL_Color CreateColor(Uint8 r, Uint8 g, Uint8 b, Uint8 a); 191 | 192 | //'SDL_Color' constructor function 193 | inline SDL_Color CreateColor(int r, int g, int b, int a); 194 | 195 | //'SDL_Color' constructor function 196 | inline SDL_Color CreateColor(double r, double g, double b, double a); 197 | 198 | //'SDL_Color' constructor function 199 | inline SDL_Color CreateColor(float r, float g, float b, float a); 200 | 201 | //'Vec2i' constructor function 202 | inline Vec2i CreateVec2i(int x, int y); 203 | 204 | //'Vec2i' constructor function 205 | inline Vec2i CreateVec2i(double x, double y); 206 | 207 | //'Vec2i' constructor function 208 | inline Vec2i CreateVec2i(float x, float y); 209 | 210 | //'Vec2d' constructor function 211 | inline Vec2d CreateVec2d(double x, double y); 212 | 213 | //'Vec2d' constructor function 214 | inline Vec2d CreateVec2d(int x, int y); 215 | 216 | //'Vec2d' constructor function 217 | inline Vec2d CreateVec2d(float x, float y); 218 | 219 | //----------------------------------------------------------------------------------------------------------------------// 220 | // Assist Functions: // 221 | //----------------------------------------------------------------------------------------------------------------------// 222 | 223 | //Returns center position of specified surface as Vec2i 224 | Vec2i getCenter(SDL_Surface* surface); 225 | 226 | //Returns SDL_Color for basic colors identifiers (COLOR_WHITE, COLOR_GRAY, etc.) 227 | SDL_Color getColor(flag id); 228 | 229 | //Returns SDL_Color at specified point on surface 230 | SDL_Color grabColor(SDL_Surface* surface, int x, int y); 231 | 232 | //Applies gamma correction to specified color 233 | SDL_Color correctGamma(SDL_Color color, double amount); 234 | 235 | //Returns the number of colors found on a surface 236 | int getColors(SDL_Surface* surface); 237 | 238 | //Returns true if point is within surface boundary 239 | bool hasPoint(SDL_Surface* surface, Vec2i point); 240 | 241 | //Returns max value found within numerical (integer) vector array 242 | int findMax(std::vector vec); 243 | 244 | //Returns max value found within numerical (double) vector array 245 | double findMax(std::vector vec); 246 | 247 | //Returns max value found within numerical (float) vector array 248 | float findMax(std::vector vec); 249 | 250 | //Returns all points whose color values fall between specified color and a given threshold 251 | std::vector extractPoints(SDL_Surface* surface, SDL_Color color, Uint8 threshold); 252 | 253 | //Returns all points whose hue values fall between specified angles 254 | std::vector extractHues(SDL_Surface* surface); 255 | 256 | //----------------------------------------------------------------------------------------------------------------------// 257 | // Manipulation Functions: // 258 | //----------------------------------------------------------------------------------------------------------------------// 259 | 260 | //Get pixel data from specified surface at point x, y 261 | Uint32 GetPixel(SDL_Surface* surface, int x, int y); 262 | 263 | //Replace individual pixel on surface with target Uint32 color 264 | void SetPixel(SDL_Surface* surface, int x, int y, Uint32 pixel); 265 | 266 | //Create empty surface with another surface's size & format information 267 | SDL_Surface* EmptySurface(SDL_Surface* surface); 268 | 269 | //Create empty surface of a custom size with another surface's format 270 | SDL_Surface* EmptySurface(SDL_Surface* surface, int wid, int hei); 271 | 272 | //Create empty surface of default format with specified size 273 | SDL_Surface* EmptySurface(int wid, int hei); 274 | 275 | //Copy surface contents to another surface 276 | SDL_Surface* CopySurface(SDL_Surface* surface); 277 | 278 | //Copy region of surface to another surface at specified region 279 | void CopyRegion(SDL_Surface* srcSurface, SDL_Surface* dstSurface, SDL_Rect srcRect, SDL_Rect dstRect); 280 | 281 | //Scale surface in both directions by scalar amount 282 | SDL_Surface* ScaleSurface(SDL_Surface* surface, double scale); 283 | 284 | //Scale surface to specified x scale and y scale 285 | SDL_Surface* ScaleSurface(SDL_Surface* surface, double scaleX, double scaleY); 286 | 287 | //Resize surface to a specified width and height 288 | SDL_Surface* ResizeSurface(SDL_Surface* surface, int wid, int hei); 289 | 290 | SDL_Surface* ScaleRegion(SDL_Surface* surface, SDL_Rect rect); 291 | 292 | //Add surface pixels scaled to opacity (between 0.0 and 1.0) to target 293 | void AddSurface(SDL_Surface* target, SDL_Surface* source, double opacity); 294 | 295 | //Subtract surface pixels scaled to opacity (between 0.0 and 1.0) from target 296 | void SubtractSurface(SDL_Surface* target, SDL_Surface* source, double opacity); 297 | 298 | //----------------------------------------------------------------------------------------------------------------------// 299 | // Kernel & Filter Functions: // 300 | //----------------------------------------------------------------------------------------------------------------------// 301 | 302 | //Apply pre-created convolution matrix to surface 303 | void ApplyKernel(SDL_Surface* srcSurface, SDL_Surface* dstSurface, Kernel3x3 kernel); 304 | 305 | //Specify and handle covolution matrix filter 306 | SDL_Surface* FilterSurface(SDL_Surface* surface, flag method, flag edge); 307 | 308 | //Apply blur kernel to surface specified number of passes 309 | SDL_Surface* FilterBlur(SDL_Surface* surface, int passes, flag edge); 310 | 311 | //Offset image in either direction 312 | SDL_Surface* FilterOffset(SDL_Surface* surface, int x, int y); 313 | 314 | //Apply blur and subtract from original if difference is above set threshold 315 | void FilterUnsharpMask(); 316 | 317 | //Flip surface horizontally 318 | SDL_Surface* FlipHorizontal(SDL_Surface* surface); 319 | 320 | //Flip surface vertically 321 | SDL_Surface* FlipVertical(SDL_Surface* surface); 322 | 323 | //----------------------------------------------------------------------------------------------------------------------// 324 | // Image Adjustment Functions: // 325 | //----------------------------------------------------------------------------------------------------------------------// 326 | 327 | //Set surface to grayscale using various gray algorithms 328 | void Grayscale(SDL_Surface* surface, flag method); 329 | 330 | //Percentage-based desaturation (1.0 = Full Color, 0.0 = Grayscale) 331 | void Desaturate(SDL_Surface* surface, double value); 332 | 333 | //Convert image to binary based on specified color channel 334 | void Threshold(SDL_Surface* surface, flag channel, int max); 335 | 336 | //Use to replace a color (within a set threshold) to another color. 337 | void ReplaceColor(SDL_Surface* surface, SDL_Color srcColor, SDL_Color dstColor, int threshold); 338 | 339 | //Removes every (amount) pixel from surface 340 | void Interlace(SDL_Surface* surface, int amount); 341 | 342 | //Replaces every (amount) pixel from surface to target color 343 | void Interlace(SDL_Surface* surface, int amount, SDL_Color color); 344 | 345 | //Replaces every (amount) pixel from surface to target color through any IL_* flag (IL_HORIZONTAL, IL_VERTICAL, etc.) 346 | void Interlace(SDL_Surface* surface, int amount, SDL_Color color, flag style); 347 | 348 | //Use to replace or blend every (amount) pixel from surface to target color through any IL_* flag (IL_HORIZONTAL, IL_VERTICAL, etc.). 349 | //Blend modes include BLEND_ADD, BLEND_SUBTRACT, and BLEND_DEFAULT 350 | void Interlace(SDL_Surface* surface, int amount, SDL_Color color, flag style, flag blend); 351 | 352 | //Use to reduce the amount of colors in a surface. "colors" indicates the max number of colors per channel (Total colors can range from 3^3^3) 353 | void Posterize(SDL_Surface* surface, int colors); 354 | 355 | //Use to reduce the amount of colors in a surface. "colors" indicates the max number of colors per channel (Total colors can range from 3^3^3). 356 | //The "style" flag can be used to introduce effects inline through POST_* constants (POST_BW, POST_HIGH_C, etc.) 357 | void Posterize(SDL_Surface* surface, int colors, flag style); 358 | 359 | //Shrink binary image 360 | SDL_Surface* Erode(SDL_Surface* surface); 361 | 362 | //Expand binary image 363 | SDL_Surface* Dilate(SDL_Surface* surface); 364 | 365 | //Trace edge of binary image (Recommended to blur first using either 'FilterBlur' or 'FilterSurface(Blur)' 366 | SDL_Surface* Trace(SDL_Surface* surface, int max, SDL_Color col); 367 | 368 | //Invert colors of a specified surface 369 | void Invert(SDL_Surface* surface); 370 | 371 | //Add amount to surface red channel 372 | void AdjustRed(SDL_Surface* surface, int offset); 373 | 374 | //Add amount to surface 'green' channel 375 | void AdjustGreen(SDL_Surface* surface, int offset); 376 | 377 | //Add amount to surface 'blue' channel 378 | void AdjustBlue(SDL_Surface* surface, int offset); 379 | 380 | //Adds amounts to individual color channels within a single function 381 | void AdjustMulti(SDL_Surface* surface, int rOffset, int gOffset, int bOffset); 382 | 383 | //Adds uniform amount to all color channels 384 | void AdjustBrightness(SDL_Surface* surface, int amount); 385 | 386 | //Adjust surface dynamic range (recommended amounts between 0.0 and 100.0) 387 | void AdjustContrast(SDL_Surface* surface, double amount); 388 | 389 | //Adjust highlight amount on surface (recommended amounts between 0.0 and 100.0) 390 | void AdjustHighlights(SDL_Surface* surface, double amount); 391 | 392 | //Adjust shadow amount on surface (recommended amounts between 0.0 and -100.0) 393 | void AdjustShadows(SDL_Surface* surface, double amount); 394 | 395 | //Adjust gamma level of surface (recommended amounts between 0.0 to 8.0) 396 | void AdjustGamma(SDL_Surface* surface, double amount); 397 | 398 | //Adjust color tempurature of surface 399 | void AdjustTemperature(SDL_Surface* surface, double kelvin, double strength); 400 | 401 | //Generate temperature spectrum to SDL_Surface 402 | SDL_Surface* TemperatureSpectrum(double kelvinA, double kelvinB, int height); 403 | 404 | //Draw gradient or solid line 405 | void DrawLine(SDL_Surface* surface, Vec2i srcPos, Vec2i dstPos, SDL_Color srcCol, SDL_Color dstCol); 406 | 407 | //Draw basic circle to surface 408 | void DrawCircle(SDL_Surface* surface, Vec2i pos, double radius, SDL_Color outline, SDL_Color fill, bool filled, bool dotted); 409 | 410 | //Draw rectangle to surface 411 | void DrawRect(SDL_Surface* surface, Vec2i srcPos, Vec2i dstPos, SDL_Color outline, SDL_Color fill, bool filled); 412 | 413 | //Generate cone gradient on target surface 414 | void GradientCone(); 415 | 416 | //Generate radial gradient on target surface 417 | void GradientRadial(SDL_Surface* surface, Vec2i pos, double radius, SDL_Color srcCol, SDL_Color dstCol); 418 | 419 | //Generate linear gradient on target surface 420 | void GradientLinear(Vec2i srcPos, Vec2i dstPos, SDL_Color srcCol, SDL_Color dstCol); 421 | 422 | //Generate basic noise 423 | void GenerateNoise(SDL_Surface* surface, flag style, Uint8 rangeMin, Uint8 rangeMax); 424 | 425 | //Generate difference clouds 426 | void GenerateClouds(); 427 | 428 | //Fills in specified surface with solid color 429 | void Fill(SDL_Surface* surface, SDL_Color color); 430 | 431 | //----------------------------------------------------------------------------------------------------------------------// 432 | // Graphing & Visualization Functions: // 433 | //----------------------------------------------------------------------------------------------------------------------// 434 | 435 | void DrawPoints(SDL_Surface* surface, SDL_Color color, std::vector points); 436 | 437 | void DrawGridlines(SDL_Surface* surface, SDL_Color color, flag style, int padding, double cellsize, double maxX, double maxY); 438 | 439 | void Histogram(SDL_Surface* srcSurface, SDL_Surface* dstSurface, flag channel, flag style, int padding, int desample); 440 | 441 | void Histogram(SDL_Surface* srcSurface, SDL_Surface* dstSurface, flag channel, flag style, int padding, int desample, int maxY); 442 | 443 | void FFT(SDL_Surface* source, SDL_Surface* destination, double samples); 444 | 445 | //----------------------------------------------------------------------------------------------------------------------// 446 | // Color Conversion Functions: // 447 | //----------------------------------------------------------------------------------------------------------------------// 448 | 449 | void RGBtoHSV(float r, float g, float b, float *h, float *s, float *v); 450 | 451 | void HSVtoRGB(float *r, float *g, float *b, float h, float s, float v); 452 | 453 | //----------------------------------------------------------------------------------------------------------------------// 454 | // Color Conversion Functions: // 455 | //----------------------------------------------------------------------------------------------------------------------// 456 | 457 | #endif -------------------------------------------------------------------------------- /Util/GUI.h: -------------------------------------------------------------------------------- 1 | #ifndef GUI_H 2 | #define GUI_H 3 | 4 | struct GUI_Graphic{ 5 | SDL_Texture* texture; 6 | std::vector frames; 7 | }; 8 | 9 | struct GUI_Event{ 10 | flag type; //Specifies type of GUI_Event 11 | int id; //Specifies element ID of GUI_Event (If applicable) 12 | }; 13 | 14 | struct GUI_Element{ 15 | static int count; //Total object count 16 | 17 | //Properties 18 | int id; 19 | std::vector states; //Source rects for drawing 20 | flag status; //Current element states 21 | SDL_Rect rect; 22 | std::string title; 23 | 24 | //Controls 25 | bool isVisible; 26 | bool isActive; 27 | 28 | //Styling (if applicable) 29 | int margin, padding; 30 | int shadowAngle; 31 | int shadowDistance; 32 | int shadowAlpha; 33 | 34 | void Create(std::string Title, SDL_Rect dstRect); 35 | }; 36 | 37 | struct GUI_Button : public GUI_Element{ 38 | void Create(); 39 | }; 40 | 41 | struct GUI_RadioButton : public GUI_Element{ 42 | 43 | }; 44 | 45 | struct GUI_Checkbox : public GUI_Element{ 46 | 47 | }; 48 | 49 | struct GUI_MenuItem : public GUI_Element{ 50 | 51 | }; 52 | 53 | struct GUI_Menu : public GUI_Element{ 54 | 55 | }; 56 | 57 | struct GUI_Container : public GUI_Element{ 58 | bool isGraphical; 59 | 60 | std::vector rbuttons; 61 | std::vector cboxes; 62 | 63 | SDL_Surface* content; 64 | }; 65 | 66 | class GUI_Window{ 67 | private: 68 | static Vec2i mouse; Vec2i mouseOld; 69 | static int count; 70 | int id; 71 | SDL_Rect rect; 72 | const char* title; 73 | flag state; 74 | Uint8 alpha; 75 | 76 | int headerH; 77 | int gfxSize; //The size (in pixels) of the individual tiles that 'build' the GUI representation 78 | SDL_Color gfxKey; 79 | 80 | std::vector gfx; 81 | std::vector events; 82 | 83 | //Window elements 84 | std::vector buttons; 85 | std::vector checkboxes; 86 | std::vector containers; 87 | 88 | //Generation functions (For use with function 'Create()') 89 | SDL_Surface* gen_gfxW(); //This generates required window media if missing 90 | SDL_Surface* gen_gfxWB(); //This generates required window button media if missing 91 | SDL_Surface* gen_gfxB(); //This generates required button media if missing 92 | SDL_Surface* gen_gfxS(); //This generates required slider media if missing 93 | SDL_Surface* gen_gfxM(); //This generates required menu media if missing 94 | SDL_Surface* gen_gfxRB(); //This generates required radio button media if missing 95 | SDL_Surface* gen_gfxCB(); //This generates required checkbox media if missing 96 | 97 | public: 98 | //Constructors 99 | GUI_Window(); 100 | 101 | //Main functions 102 | void Create(const char* wTitle, SDL_Renderer* renderTarget, int X, int Y, int W, int H); //Create empty window 103 | void Retrieve(); //Get information about status changes to window 104 | void Update(SDL_Event* targetEvent); //Handle window controls 105 | void Draw(SDL_Renderer* renderTarget); //Draw window 106 | 107 | //Add elements 108 | void addMenu(); 109 | void addMenuItem(); 110 | void addButton(); 111 | void addTextbox(); 112 | void addContainer(); //Add empty container for text, radio buttons, checkboxes, etc. 113 | void addRadioButton(); //Add radio button to target container 114 | void addCheckbox(); //Add checkbox to target container 115 | void addSlider(); //Add slider to window 116 | void addSurface(); //Add surface to container 117 | 118 | //Data 119 | int getX(); int getY(); 120 | int getW(); int getH(); 121 | 122 | bool mouseHoverRegion(SDL_Rect region); //Returns true if mouse is over of specified region 123 | bool mouseHoverWindow(); //Returns true if mouse is over window and/or header 124 | bool mouseHoverHeader(); //Returns true if mouse is over header 125 | 126 | bool buttonPressed(); //Returns true if button has been pressed on window 127 | int getButton(); //Returns the id of the button that has been pressed 128 | }; 129 | 130 | #endif -------------------------------------------------------------------------------- /Util/Segment.h: -------------------------------------------------------------------------------- 1 | #ifndef SEGMENT_H 2 | #define SEGMENT_H 3 | 4 | struct Polygon{ 5 | std::vector points; 6 | }; 7 | 8 | struct Quad{ 9 | Vec2i pos; 10 | SDL_Rect rect; 11 | std::vector points; 12 | 13 | Quad(); 14 | Quad(std::vector data); 15 | Quad(std::vector data, Vec2i position); 16 | 17 | inline SDL_Rect getRect(); 18 | }; 19 | 20 | struct QuadTree{ 21 | std::vector quads; 22 | SDL_Rect rect; 23 | 24 | QuadTree(); 25 | QuadTree(std::vector data); 26 | QuadTree(std::vector data, int indices); 27 | 28 | inline std::vector* getPoints(int index); 29 | 30 | inline int getX(int index, int item); 31 | inline int getY(int index, int item); 32 | 33 | inline int getSize(); 34 | inline int getSize(int index); 35 | 36 | inline SDL_Rect getRect(); 37 | inline SDL_Rect getRect(int index); 38 | 39 | inline Vec2i getVec2i(int index, int item); 40 | }; 41 | 42 | class Segmented{ 43 | private: 44 | bool isValid; 45 | 46 | public: 47 | SDL_Surface* surface; 48 | 49 | Segmented(); 50 | Segmented(SDL_Surface* source); 51 | Segmented(SDL_Surface* source, Uint8 threshold); 52 | }; 53 | 54 | std::vector RotateData(std::vector points); 55 | 56 | inline SDL_Rect FindRect(std::vector points); 57 | 58 | inline bool PointExists(std::vector points, Vec2i point, bool useQuad); 59 | 60 | inline bool PointExists(QuadTree tree, Vec2i point); 61 | 62 | inline bool PointExists(QuadTree tree, Vec2i point, int* id); 63 | 64 | inline bool PointInRect(SDL_Rect rect, Vec2i pt); 65 | 66 | inline bool PointInPoly(std::vector points, Vec2i point, SDL_Rect boundary); 67 | 68 | void MapPNG(std::string filename, std::vector points); 69 | 70 | void MapPNG(std::string filename, QuadTree qt, int index); 71 | 72 | std::vector PointTrace(SDL_Surface* surface); 73 | 74 | void SearchPolygons(QuadTree tree, int index, Vec2i samples); 75 | 76 | //void RemoveNoise(SDL_Surface* surface, std::vector points, int distance); 77 | 78 | #endif -------------------------------------------------------------------------------- /Util/gui.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include "SDL.h" 7 | #include "SDL_image.h" 8 | #include "SDL_ttf.h" 9 | #include "../Transform.h" 10 | #include "GUI.h" 11 | 12 | using namespace std; 13 | using namespace transform; 14 | 15 | void GUI_Element::Create(string Title, SDL_Rect dstRect){ 16 | rect.x = dstRect.x; rect.y = dstRect.y; 17 | rect.w = dstRect.w; rect.h = dstRect.h; 18 | } 19 | 20 | SDL_Surface* GUI_Window::gen_gfxW(){ 21 | //Window graphical media generated to following vertical template: 22 | //1st 3x3: window title and control region style 23 | //2nd 3x3: window background style 24 | //3rd 3x3: window container style 25 | 26 | SDL_Surface* temp = EmptySurface(3 * gfxSize, 9 * gfxSize); 27 | 28 | for (int y = 0; y < 9; y++){ 29 | for (int x = 0; x < 3; x++){ 30 | for (int py = 0; py < gfxSize; py++){ 31 | for (int px = 0; px < gfxSize; px++){ 32 | if (y == 0 || y == 3 || y == 6){ //Tops 33 | if (x == 0){ //Top left 34 | switch (y){ 35 | case 0: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 36 | case 3: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 150, 150, 150, 255)); break; 37 | case 6: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 150, 150, 150, 255)); break; 38 | } 39 | } 40 | else if (x == 1){ //Top center 41 | switch (y){ 42 | case 0: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 200, 200, 200, 255)); break; 43 | case 3: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 44 | case 6: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 45 | } 46 | } 47 | else if (x == 2){ //Top right 48 | switch (y){ 49 | case 0: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 190, 190, 190, 255)); break; 50 | case 3: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 51 | case 6: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 52 | } 53 | } 54 | } 55 | else if (y == 1 || y == 4 || y == 7){ //Centers 56 | if (x == 0){ //Center left 57 | switch (y){ 58 | case 1: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 200, 200, 200, 255)); break; 59 | case 4: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 60 | case 7: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 61 | } 62 | } 63 | else if (x == 1){ //Center 64 | switch (y){ 65 | case 1: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 220, 220, 220, 255)); break; 66 | case 4: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 120, 120, 120, 255)); break; 67 | case 7: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 120, 120, 120, 255)); break; 68 | } 69 | } 70 | else if (x == 2){ //Center right 71 | switch (y){ 72 | case 1: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 200, 200, 200, 255)); break; 73 | case 4: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 74 | case 7: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 75 | } 76 | } 77 | } 78 | else{ //Bottoms 79 | if (x == 0){ //Bottom left 80 | switch (y){ 81 | case 2: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 190, 190, 190, 255)); break; 82 | case 5: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 83 | case 8: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 84 | } 85 | } 86 | else if (x == 1){ //Bottom center 87 | switch (y){ 88 | case 2: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 200, 200, 200, 255)); break; 89 | case 5: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 90 | case 8: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 180, 180, 180, 255)); break; 91 | } 92 | } 93 | else if (x == 2){ //Bottom right 94 | switch (y){ 95 | case 2: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 210, 210, 210, 255)); break; 96 | case 5: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 150, 150, 150, 255)); break; 97 | case 8: SetPixel(temp, px + (x*gfxSize), py + (y*gfxSize), SDL_MapRGBA(temp->format, 150, 150, 150, 255)); break; 98 | } 99 | } 100 | } 101 | 102 | } 103 | } 104 | } 105 | } 106 | 107 | IMG_SavePNG(temp, "gfxW.png"); 108 | 109 | return temp; 110 | } 111 | 112 | SDL_Surface* GUI_Window::gen_gfxB(){ 113 | SDL_Surface* temp = EmptySurface(1, 1); 114 | return temp; 115 | } 116 | 117 | SDL_Surface* GUI_Window::gen_gfxS(){ 118 | SDL_Surface* temp = EmptySurface(1, 1); 119 | return temp; 120 | } 121 | 122 | SDL_Surface* GUI_Window::gen_gfxM(){ 123 | SDL_Surface* temp = EmptySurface(1, 1); 124 | return temp; 125 | } 126 | 127 | SDL_Surface* GUI_Window::gen_gfxRB(){ 128 | SDL_Surface* temp = EmptySurface(1, 1); 129 | return temp; 130 | } 131 | 132 | SDL_Surface* GUI_Window::gen_gfxCB(){ 133 | SDL_Surface* temp = EmptySurface(1, 1); 134 | return temp; 135 | } 136 | 137 | GUI_Window::GUI_Window(){ 138 | rect = CreateRect(0, 0, gfxSize * 2, gfxSize * 4); 139 | id = count; count++; 140 | mouse.x = 0; 141 | mouse.y = 0; 142 | } 143 | 144 | bool GUI_Window::mouseHoverRegion(SDL_Rect region){ 145 | if (mouse.x >= region.x && mouse.y >= region.y && mouse.x <= region.x + region.w && mouse.y <= region.y + region.h) 146 | return true; 147 | else 148 | return false; 149 | } 150 | 151 | bool GUI_Window::mouseHoverHeader(){ 152 | return mouseHoverRegion(CreateRect(rect.x, rect.y, rect.w, rect.y + headerH)); 153 | } 154 | 155 | bool GUI_Window::mouseHoverWindow(){ 156 | return mouseHoverRegion(CreateRect(rect.x, rect.y, rect.w, rect.h)); 157 | } 158 | 159 | void GUI_Window::Create(const char* wTitle, SDL_Renderer* renderTarget, int X, int Y, int W, int H){ 160 | //Store defaults 161 | title = wTitle; 162 | rect.x = X; rect.y = Y; rect.w = W; rect.h = H; 163 | gfxSize = 12; gfxKey = CreateColor(0, 255, 255, 255); 164 | headerH = 24; alpha = 200; 165 | 166 | //Load media 167 | SDL_Surface* surW = IMG_Load("gfxW.png"); 168 | SDL_Surface* surB = IMG_Load("gfxB.png"); 169 | SDL_Surface* surS = IMG_Load("gfxS.png"); 170 | SDL_Surface* surM = IMG_Load("gfxM.png"); 171 | SDL_Surface* surRB = IMG_Load("gfxRB.png"); 172 | SDL_Surface* surCB = IMG_Load("gfxCB.png"); 173 | 174 | //Generate missing media 175 | if (!surW) surW = gen_gfxW(); 176 | if (!surB) surB = gen_gfxB(); 177 | if (!surS) surS = gen_gfxS(); 178 | if (!surM) surM = gen_gfxM(); 179 | if (!surRB) surRB = gen_gfxRB(); 180 | if (!surCB) surCB = gen_gfxCB(); 181 | 182 | //Convert to drawable texture 183 | GUI_Graphic gfxW, gfxB, gfxS, gfxM, gfxRB, gfxCB; 184 | gfxW.texture = SDL_CreateTextureFromSurface(renderTarget, surW); gfx.push_back(gfxW); 185 | gfxB.texture = SDL_CreateTextureFromSurface(renderTarget, surB); gfx.push_back(gfxB); 186 | gfxS.texture = SDL_CreateTextureFromSurface(renderTarget, surS); gfx.push_back(gfxS); 187 | gfxM.texture = SDL_CreateTextureFromSurface(renderTarget, surM); gfx.push_back(gfxM); 188 | gfxRB.texture = SDL_CreateTextureFromSurface(renderTarget, surRB); gfx.push_back(gfxRB); 189 | gfxCB.texture = SDL_CreateTextureFromSurface(renderTarget, surCB); gfx.push_back(gfxCB); 190 | 191 | //Store rect data 192 | for (int a = 0; a < gfx.size(); a++){ 193 | int w, h; 194 | SDL_QueryTexture(gfx[a].texture, NULL, NULL, &w, &h); 195 | 196 | for (int y = 0; y < h / gfxSize; y++){ 197 | for (int x = 0; x < w / gfxSize; x++){ 198 | gfx[a].frames.push_back(CreateRect(x*gfxSize, y*gfxSize, gfxSize, gfxSize)); 199 | } 200 | } 201 | } 202 | 203 | cout << gfx[0].frames.size() << endl; 204 | 205 | //Clean up 206 | SDL_FreeSurface(surW); 207 | SDL_FreeSurface(surB); 208 | SDL_FreeSurface(surS); 209 | SDL_FreeSurface(surM); 210 | SDL_FreeSurface(surRB); 211 | SDL_FreeSurface(surCB); 212 | } 213 | 214 | void GUI_Window::Update(SDL_Event* targetEvent){ 215 | SDL_GetMouseState(&mouse.x, &mouse.y); 216 | 217 | if (mouseHoverWindow() != false){ 218 | while (SDL_PollEvent(targetEvent) != 0){ 219 | switch (targetEvent->type){ 220 | case SDL_BUTTON_LEFT: 221 | cout << "gfa" << endl; 222 | break; 223 | } 224 | } 225 | } 226 | else if (SDL_MouseButtonEvent().state == SDL_PRESSED){ 227 | if (state == GUI_STATE_RESIZE){ 228 | rect.w = rect.w + (mouse.x - mouseOld.x); 229 | rect.h = rect.h + (mouse.y - mouseOld.y); 230 | } 231 | cout << mouse.x << " " << mouse.y << endl; 232 | } 233 | else if (SDL_MouseButtonEvent().state == SDL_RELEASED){ 234 | if (state != GUI_STATE_DEFAULT) state = GUI_STATE_DEFAULT; 235 | } 236 | } 237 | 238 | void GUI_Window::Draw(SDL_Renderer* renderTarget){ 239 | //Fix limitations 240 | if (headerH < gfxSize * 2) headerH = gfxSize * 2; 241 | if (rect.w < gfxSize * 2) rect.w = gfxSize * 2; 242 | if (rect.h < gfxSize * 4) rect.h = gfxSize * 4; 243 | 244 | //Draw window header 245 | SDL_SetTextureAlphaMod(gfx[0].texture, 255); 246 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[0], &CreateRect(rect.x, rect.y, gfxSize, gfxSize)); //Top left corner 247 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[1], &CreateRect(rect.x + gfxSize, rect.y, rect.w - (gfxSize * 2), gfxSize)); //Top center border 248 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[2], &CreateRect(rect.x + rect.w - gfxSize, rect.y, gfxSize, gfxSize)); //Top right corner 249 | if (headerH > gfxSize * 2){ 250 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[3], &CreateRect(rect.x, rect.y + gfxSize, gfxSize, headerH - (gfxSize * 2))); 251 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[4], &CreateRect(rect.x + gfxSize, rect.y + gfxSize, rect.w - (gfxSize * 2), headerH - (gfxSize * 2))); 252 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[5], &CreateRect(rect.x + rect.w - gfxSize, rect.y + gfxSize, gfxSize, headerH - (gfxSize * 2))); 253 | } 254 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[6], &CreateRect(rect.x, rect.y + headerH - gfxSize, gfxSize, gfxSize)); 255 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[7], &CreateRect(rect.x + gfxSize, rect.y + headerH - gfxSize, rect.w - (gfxSize * 2), gfxSize)); 256 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[8], &CreateRect(rect.x + rect.w - gfxSize, rect.y + headerH - gfxSize, gfxSize, gfxSize)); 257 | 258 | //Draw main window region 259 | SDL_SetTextureAlphaMod(gfx[0].texture, alpha); 260 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[9], &CreateRect(rect.x, rect.y + headerH, gfxSize, gfxSize)); //Top left corner 261 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[10], &CreateRect(rect.x + gfxSize, rect.y + headerH, rect.w - (gfxSize * 2), gfxSize)); //Top center border 262 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[11], &CreateRect(rect.x + rect.w - gfxSize, rect.y + headerH, gfxSize, gfxSize)); //Top right corner 263 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[12], &CreateRect(rect.x, rect.y + headerH + gfxSize, gfxSize, rect.h - headerH - (gfxSize * 2))); 264 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[13], &CreateRect(rect.x + gfxSize, rect.y + headerH + gfxSize, rect.w - (gfxSize)* 2, rect.h - headerH - (gfxSize * 2))); 265 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[14], &CreateRect(rect.x + rect.w - gfxSize, rect.y + headerH + gfxSize, gfxSize, rect.h - headerH - (gfxSize * 2))); 266 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[15], &CreateRect(rect.x, rect.y + rect.h - gfxSize, gfxSize, gfxSize)); 267 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[16], &CreateRect(rect.x + gfxSize, rect.y + rect.h - gfxSize, rect.w - (gfxSize * 2), gfxSize)); 268 | SDL_RenderCopy(renderTarget, gfx[0].texture, &gfx[0].frames[17], &CreateRect(rect.x + rect.w - gfxSize, rect.y + rect.h - gfxSize, gfxSize, gfxSize)); 269 | } 270 | 271 | int GUI_Window::getX(){ 272 | return rect.x; 273 | } 274 | 275 | int GUI_Window::getY(){ 276 | return rect.y; 277 | } 278 | 279 | int GUI_Window::getW(){ 280 | return rect.w; 281 | } 282 | 283 | int GUI_Window::getH(){ 284 | return rect.h; 285 | } 286 | 287 | //Init static members 288 | int GUI_Window::count = 0; 289 | 290 | Vec2i GUI_Window::mouse = CreateVec2i(0, 0); 291 | -------------------------------------------------------------------------------- /Util/kiss_fft/Makefile: -------------------------------------------------------------------------------- 1 | KFVER=130 2 | 3 | doc: 4 | @echo "Start by reading the README file. If you want to build and test lots of stuff, do a 'make testall'" 5 | @echo "but be aware that 'make testall' has dependencies that the basic kissfft software does not." 6 | @echo "It is generally unneeded to run these tests yourself, unless you plan on changing the inner workings" 7 | @echo "of kissfft and would like to make use of its regression tests." 8 | 9 | testall: 10 | # The simd and int32_t types may or may not work on your machine 11 | make -C test DATATYPE=simd CFLAGADD="$(CFLAGADD)" test 12 | make -C test DATATYPE=int32_t CFLAGADD="$(CFLAGADD)" test 13 | make -C test DATATYPE=int16_t CFLAGADD="$(CFLAGADD)" test 14 | make -C test DATATYPE=float CFLAGADD="$(CFLAGADD)" test 15 | make -C test DATATYPE=double CFLAGADD="$(CFLAGADD)" test 16 | echo "all tests passed" 17 | 18 | tarball: clean 19 | hg archive -r v$(KFVER) -t tgz kiss_fft$(KFVER).tar.gz 20 | hg archive -r v$(KFVER) -t zip kiss_fft$(KFVER).zip 21 | 22 | clean: 23 | cd test && make clean 24 | cd tools && make clean 25 | rm -f kiss_fft*.tar.gz *~ *.pyc kiss_fft*.zip 26 | 27 | asm: kiss_fft.s 28 | 29 | kiss_fft.s: kiss_fft.c kiss_fft.h _kiss_fft_guts.h 30 | [ -e kiss_fft.s ] && mv kiss_fft.s kiss_fft.s~ || true 31 | gcc -S kiss_fft.c -O3 -mtune=native -ffast-math -fomit-frame-pointer -unroll-loops -dA -fverbose-asm 32 | gcc -o kiss_fft_short.s -S kiss_fft.c -O3 -mtune=native -ffast-math -fomit-frame-pointer -dA -fverbose-asm -DFIXED_POINT 33 | [ -e kiss_fft.s~ ] && diff kiss_fft.s~ kiss_fft.s || true 34 | -------------------------------------------------------------------------------- /Util/kiss_fft/_kiss_fft_guts.h: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2010, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | /* kiss_fft.h 16 | defines kiss_fft_scalar as either short or a float type 17 | and defines 18 | typedef struct { kiss_fft_scalar r; kiss_fft_scalar i; }kiss_fft_cpx; */ 19 | #include "kiss_fft.h" 20 | #include 21 | 22 | #define MAXFACTORS 32 23 | /* e.g. an fft of length 128 has 4 factors 24 | as far as kissfft is concerned 25 | 4*4*4*2 26 | */ 27 | 28 | struct kiss_fft_state{ 29 | int nfft; 30 | int inverse; 31 | int factors[2*MAXFACTORS]; 32 | kiss_fft_cpx twiddles[1]; 33 | }; 34 | 35 | /* 36 | Explanation of macros dealing with complex math: 37 | 38 | C_MUL(m,a,b) : m = a*b 39 | C_FIXDIV( c , div ) : if a fixed point impl., c /= div. noop otherwise 40 | C_SUB( res, a,b) : res = a - b 41 | C_SUBFROM( res , a) : res -= a 42 | C_ADDTO( res , a) : res += a 43 | * */ 44 | #ifdef FIXED_POINT 45 | #if (FIXED_POINT==32) 46 | # define FRACBITS 31 47 | # define SAMPPROD int64_t 48 | #define SAMP_MAX 2147483647 49 | #else 50 | # define FRACBITS 15 51 | # define SAMPPROD int32_t 52 | #define SAMP_MAX 32767 53 | #endif 54 | 55 | #define SAMP_MIN -SAMP_MAX 56 | 57 | #if defined(CHECK_OVERFLOW) 58 | # define CHECK_OVERFLOW_OP(a,op,b) \ 59 | if ( (SAMPPROD)(a) op (SAMPPROD)(b) > SAMP_MAX || (SAMPPROD)(a) op (SAMPPROD)(b) < SAMP_MIN ) { \ 60 | fprintf(stderr,"WARNING:overflow @ " __FILE__ "(%d): (%d " #op" %d) = %ld\n",__LINE__,(a),(b),(SAMPPROD)(a) op (SAMPPROD)(b) ); } 61 | #endif 62 | 63 | 64 | # define smul(a,b) ( (SAMPPROD)(a)*(b) ) 65 | # define sround( x ) (kiss_fft_scalar)( ( (x) + (1<<(FRACBITS-1)) ) >> FRACBITS ) 66 | 67 | # define S_MUL(a,b) sround( smul(a,b) ) 68 | 69 | # define C_MUL(m,a,b) \ 70 | do{ (m).r = sround( smul((a).r,(b).r) - smul((a).i,(b).i) ); \ 71 | (m).i = sround( smul((a).r,(b).i) + smul((a).i,(b).r) ); }while(0) 72 | 73 | # define DIVSCALAR(x,k) \ 74 | (x) = sround( smul( x, SAMP_MAX/k ) ) 75 | 76 | # define C_FIXDIV(c,div) \ 77 | do { DIVSCALAR( (c).r , div); \ 78 | DIVSCALAR( (c).i , div); }while (0) 79 | 80 | # define C_MULBYSCALAR( c, s ) \ 81 | do{ (c).r = sround( smul( (c).r , s ) ) ;\ 82 | (c).i = sround( smul( (c).i , s ) ) ; }while(0) 83 | 84 | #else /* not FIXED_POINT*/ 85 | 86 | # define S_MUL(a,b) ( (a)*(b) ) 87 | #define C_MUL(m,a,b) \ 88 | do{ (m).r = (a).r*(b).r - (a).i*(b).i;\ 89 | (m).i = (a).r*(b).i + (a).i*(b).r; }while(0) 90 | # define C_FIXDIV(c,div) /* NOOP */ 91 | # define C_MULBYSCALAR( c, s ) \ 92 | do{ (c).r *= (s);\ 93 | (c).i *= (s); }while(0) 94 | #endif 95 | 96 | #ifndef CHECK_OVERFLOW_OP 97 | # define CHECK_OVERFLOW_OP(a,op,b) /* noop */ 98 | #endif 99 | 100 | #define C_ADD( res, a,b)\ 101 | do { \ 102 | CHECK_OVERFLOW_OP((a).r,+,(b).r)\ 103 | CHECK_OVERFLOW_OP((a).i,+,(b).i)\ 104 | (res).r=(a).r+(b).r; (res).i=(a).i+(b).i; \ 105 | }while(0) 106 | #define C_SUB( res, a,b)\ 107 | do { \ 108 | CHECK_OVERFLOW_OP((a).r,-,(b).r)\ 109 | CHECK_OVERFLOW_OP((a).i,-,(b).i)\ 110 | (res).r=(a).r-(b).r; (res).i=(a).i-(b).i; \ 111 | }while(0) 112 | #define C_ADDTO( res , a)\ 113 | do { \ 114 | CHECK_OVERFLOW_OP((res).r,+,(a).r)\ 115 | CHECK_OVERFLOW_OP((res).i,+,(a).i)\ 116 | (res).r += (a).r; (res).i += (a).i;\ 117 | }while(0) 118 | 119 | #define C_SUBFROM( res , a)\ 120 | do {\ 121 | CHECK_OVERFLOW_OP((res).r,-,(a).r)\ 122 | CHECK_OVERFLOW_OP((res).i,-,(a).i)\ 123 | (res).r -= (a).r; (res).i -= (a).i; \ 124 | }while(0) 125 | 126 | 127 | #ifdef FIXED_POINT 128 | # define KISS_FFT_COS(phase) floor(.5+SAMP_MAX * cos (phase)) 129 | # define KISS_FFT_SIN(phase) floor(.5+SAMP_MAX * sin (phase)) 130 | # define HALF_OF(x) ((x)>>1) 131 | #elif defined(USE_SIMD) 132 | # define KISS_FFT_COS(phase) _mm_set1_ps( cos(phase) ) 133 | # define KISS_FFT_SIN(phase) _mm_set1_ps( sin(phase) ) 134 | # define HALF_OF(x) ((x)*_mm_set1_ps(.5)) 135 | #else 136 | # define KISS_FFT_COS(phase) (kiss_fft_scalar) cos(phase) 137 | # define KISS_FFT_SIN(phase) (kiss_fft_scalar) sin(phase) 138 | # define HALF_OF(x) ((x)*.5) 139 | #endif 140 | 141 | #define kf_cexp(x,phase) \ 142 | do{ \ 143 | (x)->r = KISS_FFT_COS(phase);\ 144 | (x)->i = KISS_FFT_SIN(phase);\ 145 | }while(0) 146 | 147 | 148 | /* a debugging function */ 149 | #define pcpx(c)\ 150 | fprintf(stderr,"%g + %gi\n",(double)((c)->r),(double)((c)->i) ) 151 | 152 | 153 | #ifdef KISS_FFT_USE_ALLOCA 154 | // define this to allow use of alloca instead of malloc for temporary buffers 155 | // Temporary buffers are used in two case: 156 | // 1. FFT sizes that have "bad" factors. i.e. not 2,3 and 5 157 | // 2. "in-place" FFTs. Notice the quotes, since kissfft does not really do an in-place transform. 158 | #include 159 | #define KISS_FFT_TMP_ALLOC(nbytes) alloca(nbytes) 160 | #define KISS_FFT_TMP_FREE(ptr) 161 | #else 162 | #define KISS_FFT_TMP_ALLOC(nbytes) KISS_FFT_MALLOC(nbytes) 163 | #define KISS_FFT_TMP_FREE(ptr) KISS_FFT_FREE(ptr) 164 | #endif 165 | -------------------------------------------------------------------------------- /Util/kiss_fft/kiss_fft.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2010, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | 16 | #include "_kiss_fft_guts.h" 17 | /* The guts header contains all the multiplication and addition macros that are defined for 18 | fixed or floating point complex numbers. It also delares the kf_ internal functions. 19 | */ 20 | 21 | static void kf_bfly2( 22 | kiss_fft_cpx * Fout, 23 | const size_t fstride, 24 | const kiss_fft_cfg st, 25 | int m 26 | ) 27 | { 28 | kiss_fft_cpx * Fout2; 29 | kiss_fft_cpx * tw1 = st->twiddles; 30 | kiss_fft_cpx t; 31 | Fout2 = Fout + m; 32 | do{ 33 | C_FIXDIV(*Fout,2); C_FIXDIV(*Fout2,2); 34 | 35 | C_MUL (t, *Fout2 , *tw1); 36 | tw1 += fstride; 37 | C_SUB( *Fout2 , *Fout , t ); 38 | C_ADDTO( *Fout , t ); 39 | ++Fout2; 40 | ++Fout; 41 | }while (--m); 42 | } 43 | 44 | static void kf_bfly4( 45 | kiss_fft_cpx * Fout, 46 | const size_t fstride, 47 | const kiss_fft_cfg st, 48 | const size_t m 49 | ) 50 | { 51 | kiss_fft_cpx *tw1,*tw2,*tw3; 52 | kiss_fft_cpx scratch[6]; 53 | size_t k=m; 54 | const size_t m2=2*m; 55 | const size_t m3=3*m; 56 | 57 | 58 | tw3 = tw2 = tw1 = st->twiddles; 59 | 60 | do { 61 | C_FIXDIV(*Fout,4); C_FIXDIV(Fout[m],4); C_FIXDIV(Fout[m2],4); C_FIXDIV(Fout[m3],4); 62 | 63 | C_MUL(scratch[0],Fout[m] , *tw1 ); 64 | C_MUL(scratch[1],Fout[m2] , *tw2 ); 65 | C_MUL(scratch[2],Fout[m3] , *tw3 ); 66 | 67 | C_SUB( scratch[5] , *Fout, scratch[1] ); 68 | C_ADDTO(*Fout, scratch[1]); 69 | C_ADD( scratch[3] , scratch[0] , scratch[2] ); 70 | C_SUB( scratch[4] , scratch[0] , scratch[2] ); 71 | C_SUB( Fout[m2], *Fout, scratch[3] ); 72 | tw1 += fstride; 73 | tw2 += fstride*2; 74 | tw3 += fstride*3; 75 | C_ADDTO( *Fout , scratch[3] ); 76 | 77 | if(st->inverse) { 78 | Fout[m].r = scratch[5].r - scratch[4].i; 79 | Fout[m].i = scratch[5].i + scratch[4].r; 80 | Fout[m3].r = scratch[5].r + scratch[4].i; 81 | Fout[m3].i = scratch[5].i - scratch[4].r; 82 | }else{ 83 | Fout[m].r = scratch[5].r + scratch[4].i; 84 | Fout[m].i = scratch[5].i - scratch[4].r; 85 | Fout[m3].r = scratch[5].r - scratch[4].i; 86 | Fout[m3].i = scratch[5].i + scratch[4].r; 87 | } 88 | ++Fout; 89 | }while(--k); 90 | } 91 | 92 | static void kf_bfly3( 93 | kiss_fft_cpx * Fout, 94 | const size_t fstride, 95 | const kiss_fft_cfg st, 96 | size_t m 97 | ) 98 | { 99 | size_t k=m; 100 | const size_t m2 = 2*m; 101 | kiss_fft_cpx *tw1,*tw2; 102 | kiss_fft_cpx scratch[5]; 103 | kiss_fft_cpx epi3; 104 | epi3 = st->twiddles[fstride*m]; 105 | 106 | tw1=tw2=st->twiddles; 107 | 108 | do{ 109 | C_FIXDIV(*Fout,3); C_FIXDIV(Fout[m],3); C_FIXDIV(Fout[m2],3); 110 | 111 | C_MUL(scratch[1],Fout[m] , *tw1); 112 | C_MUL(scratch[2],Fout[m2] , *tw2); 113 | 114 | C_ADD(scratch[3],scratch[1],scratch[2]); 115 | C_SUB(scratch[0],scratch[1],scratch[2]); 116 | tw1 += fstride; 117 | tw2 += fstride*2; 118 | 119 | Fout[m].r = Fout->r - HALF_OF(scratch[3].r); 120 | Fout[m].i = Fout->i - HALF_OF(scratch[3].i); 121 | 122 | C_MULBYSCALAR( scratch[0] , epi3.i ); 123 | 124 | C_ADDTO(*Fout,scratch[3]); 125 | 126 | Fout[m2].r = Fout[m].r + scratch[0].i; 127 | Fout[m2].i = Fout[m].i - scratch[0].r; 128 | 129 | Fout[m].r -= scratch[0].i; 130 | Fout[m].i += scratch[0].r; 131 | 132 | ++Fout; 133 | }while(--k); 134 | } 135 | 136 | static void kf_bfly5( 137 | kiss_fft_cpx * Fout, 138 | const size_t fstride, 139 | const kiss_fft_cfg st, 140 | int m 141 | ) 142 | { 143 | kiss_fft_cpx *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; 144 | int u; 145 | kiss_fft_cpx scratch[13]; 146 | kiss_fft_cpx * twiddles = st->twiddles; 147 | kiss_fft_cpx *tw; 148 | kiss_fft_cpx ya,yb; 149 | ya = twiddles[fstride*m]; 150 | yb = twiddles[fstride*2*m]; 151 | 152 | Fout0=Fout; 153 | Fout1=Fout0+m; 154 | Fout2=Fout0+2*m; 155 | Fout3=Fout0+3*m; 156 | Fout4=Fout0+4*m; 157 | 158 | tw=st->twiddles; 159 | for ( u=0; ur += scratch[7].r + scratch[8].r; 174 | Fout0->i += scratch[7].i + scratch[8].i; 175 | 176 | scratch[5].r = scratch[0].r + S_MUL(scratch[7].r,ya.r) + S_MUL(scratch[8].r,yb.r); 177 | scratch[5].i = scratch[0].i + S_MUL(scratch[7].i,ya.r) + S_MUL(scratch[8].i,yb.r); 178 | 179 | scratch[6].r = S_MUL(scratch[10].i,ya.i) + S_MUL(scratch[9].i,yb.i); 180 | scratch[6].i = -S_MUL(scratch[10].r,ya.i) - S_MUL(scratch[9].r,yb.i); 181 | 182 | C_SUB(*Fout1,scratch[5],scratch[6]); 183 | C_ADD(*Fout4,scratch[5],scratch[6]); 184 | 185 | scratch[11].r = scratch[0].r + S_MUL(scratch[7].r,yb.r) + S_MUL(scratch[8].r,ya.r); 186 | scratch[11].i = scratch[0].i + S_MUL(scratch[7].i,yb.r) + S_MUL(scratch[8].i,ya.r); 187 | scratch[12].r = - S_MUL(scratch[10].i,yb.i) + S_MUL(scratch[9].i,ya.i); 188 | scratch[12].i = S_MUL(scratch[10].r,yb.i) - S_MUL(scratch[9].r,ya.i); 189 | 190 | C_ADD(*Fout2,scratch[11],scratch[12]); 191 | C_SUB(*Fout3,scratch[11],scratch[12]); 192 | 193 | ++Fout0;++Fout1;++Fout2;++Fout3;++Fout4; 194 | } 195 | } 196 | 197 | /* perform the butterfly for one stage of a mixed radix FFT */ 198 | static void kf_bfly_generic( 199 | kiss_fft_cpx * Fout, 200 | const size_t fstride, 201 | const kiss_fft_cfg st, 202 | int m, 203 | int p 204 | ) 205 | { 206 | int u,k,q1,q; 207 | kiss_fft_cpx * twiddles = st->twiddles; 208 | kiss_fft_cpx t; 209 | int Norig = st->nfft; 210 | 211 | kiss_fft_cpx * scratch = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC(sizeof(kiss_fft_cpx)*p); 212 | 213 | for ( u=0; u=Norig) twidx-=Norig; 228 | C_MUL(t,scratch[q] , twiddles[twidx] ); 229 | C_ADDTO( Fout[ k ] ,t); 230 | } 231 | k += m; 232 | } 233 | } 234 | KISS_FFT_TMP_FREE(scratch); 235 | } 236 | 237 | static 238 | void kf_work( 239 | kiss_fft_cpx * Fout, 240 | const kiss_fft_cpx * f, 241 | const size_t fstride, 242 | int in_stride, 243 | int * factors, 244 | const kiss_fft_cfg st 245 | ) 246 | { 247 | kiss_fft_cpx * Fout_beg=Fout; 248 | const int p=*factors++; /* the radix */ 249 | const int m=*factors++; /* stage's fft length/p */ 250 | const kiss_fft_cpx * Fout_end = Fout + p*m; 251 | 252 | #ifdef _OPENMP 253 | // use openmp extensions at the 254 | // top-level (not recursive) 255 | if (fstride==1 && p<=5) 256 | { 257 | int k; 258 | 259 | // execute the p different work units in different threads 260 | # pragma omp parallel for 261 | for (k=0;k floor_sqrt) 324 | p = n; /* no more factors, skip to end */ 325 | } 326 | n /= p; 327 | *facbuf++ = p; 328 | *facbuf++ = n; 329 | } while (n > 1); 330 | } 331 | 332 | /* 333 | * 334 | * User-callable function to allocate all necessary storage space for the fft. 335 | * 336 | * The return value is a contiguous block of memory, allocated with malloc. As such, 337 | * It can be freed with free(), rather than a kiss_fft-specific function. 338 | * */ 339 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem ) 340 | { 341 | kiss_fft_cfg st=NULL; 342 | size_t memneeded = sizeof(struct kiss_fft_state) 343 | + sizeof(kiss_fft_cpx)*(nfft-1); /* twiddle factors*/ 344 | 345 | if ( lenmem==NULL ) { 346 | st = ( kiss_fft_cfg)KISS_FFT_MALLOC( memneeded ); 347 | }else{ 348 | if (mem != NULL && *lenmem >= memneeded) 349 | st = (kiss_fft_cfg)mem; 350 | *lenmem = memneeded; 351 | } 352 | if (st) { 353 | int i; 354 | st->nfft=nfft; 355 | st->inverse = inverse_fft; 356 | 357 | for (i=0;iinverse) 361 | phase *= -1; 362 | kf_cexp(st->twiddles+i, phase ); 363 | } 364 | 365 | kf_factor(nfft,st->factors); 366 | } 367 | return st; 368 | } 369 | 370 | 371 | void kiss_fft_stride(kiss_fft_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int in_stride) 372 | { 373 | if (fin == fout) { 374 | //NOTE: this is not really an in-place FFT algorithm. 375 | //It just performs an out-of-place FFT into a temp buffer 376 | kiss_fft_cpx * tmpbuf = (kiss_fft_cpx*)KISS_FFT_TMP_ALLOC( sizeof(kiss_fft_cpx)*st->nfft); 377 | kf_work(tmpbuf,fin,1,in_stride, st->factors,st); 378 | memcpy(fout,tmpbuf,sizeof(kiss_fft_cpx)*st->nfft); 379 | KISS_FFT_TMP_FREE(tmpbuf); 380 | }else{ 381 | kf_work( fout, fin, 1,in_stride, st->factors,st ); 382 | } 383 | } 384 | 385 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 386 | { 387 | kiss_fft_stride(cfg,fin,fout,1); 388 | } 389 | 390 | 391 | void kiss_fft_cleanup(void) 392 | { 393 | // nothing needed any more 394 | } 395 | 396 | int kiss_fft_next_fast_size(int n) 397 | { 398 | while(1) { 399 | int m=n; 400 | while ( (m%2) == 0 ) m/=2; 401 | while ( (m%3) == 0 ) m/=3; 402 | while ( (m%5) == 0 ) m/=5; 403 | if (m<=1) 404 | break; /* n is completely factorable by twos, threes, and fives */ 405 | n++; 406 | } 407 | return n; 408 | } 409 | -------------------------------------------------------------------------------- /Util/kiss_fft/kiss_fft.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FFT_H 2 | #define KISS_FFT_H 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #ifdef __cplusplus 10 | extern "C" { 11 | #endif 12 | 13 | /* 14 | ATTENTION! 15 | If you would like a : 16 | -- a utility that will handle the caching of fft objects 17 | -- real-only (no imaginary time component ) FFT 18 | -- a multi-dimensional FFT 19 | -- a command-line utility to perform ffts 20 | -- a command-line utility to perform fast-convolution filtering 21 | 22 | Then see kfc.h kiss_fftr.h kiss_fftnd.h fftutil.c kiss_fastfir.c 23 | in the tools/ directory. 24 | */ 25 | 26 | #ifdef USE_SIMD 27 | # include 28 | # define kiss_fft_scalar __m128 29 | #define KISS_FFT_MALLOC(nbytes) _mm_malloc(nbytes,16) 30 | #define KISS_FFT_FREE _mm_free 31 | #else 32 | #define KISS_FFT_MALLOC malloc 33 | #define KISS_FFT_FREE free 34 | #endif 35 | 36 | 37 | #ifdef FIXED_POINT 38 | #include 39 | # if (FIXED_POINT == 32) 40 | # define kiss_fft_scalar int32_t 41 | # else 42 | # define kiss_fft_scalar int16_t 43 | # endif 44 | #else 45 | # ifndef kiss_fft_scalar 46 | /* default is float */ 47 | # define kiss_fft_scalar float 48 | # endif 49 | #endif 50 | 51 | typedef struct { 52 | kiss_fft_scalar r; 53 | kiss_fft_scalar i; 54 | }kiss_fft_cpx; 55 | 56 | typedef struct kiss_fft_state* kiss_fft_cfg; 57 | 58 | /* 59 | * kiss_fft_alloc 60 | * 61 | * Initialize a FFT (or IFFT) algorithm's cfg/state buffer. 62 | * 63 | * typical usage: kiss_fft_cfg mycfg=kiss_fft_alloc(1024,0,NULL,NULL); 64 | * 65 | * The return value from fft_alloc is a cfg buffer used internally 66 | * by the fft routine or NULL. 67 | * 68 | * If lenmem is NULL, then kiss_fft_alloc will allocate a cfg buffer using malloc. 69 | * The returned value should be free()d when done to avoid memory leaks. 70 | * 71 | * The state can be placed in a user supplied buffer 'mem': 72 | * If lenmem is not NULL and mem is not NULL and *lenmem is large enough, 73 | * then the function places the cfg in mem and the size used in *lenmem 74 | * and returns mem. 75 | * 76 | * If lenmem is not NULL and ( mem is NULL or *lenmem is not large enough), 77 | * then the function returns NULL and places the minimum cfg 78 | * buffer size in *lenmem. 79 | * */ 80 | 81 | kiss_fft_cfg kiss_fft_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem); 82 | 83 | /* 84 | * kiss_fft(cfg,in_out_buf) 85 | * 86 | * Perform an FFT on a complex input buffer. 87 | * for a forward FFT, 88 | * fin should be f[0] , f[1] , ... ,f[nfft-1] 89 | * fout will be F[0] , F[1] , ... ,F[nfft-1] 90 | * Note that each element is complex and can be accessed like 91 | f[k].r and f[k].i 92 | * */ 93 | void kiss_fft(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 94 | 95 | /* 96 | A more generic version of the above function. It reads its input from every Nth sample. 97 | * */ 98 | void kiss_fft_stride(kiss_fft_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout,int fin_stride); 99 | 100 | /* If kiss_fft_alloc allocated a buffer, it is one contiguous 101 | buffer and can be simply free()d when no longer needed*/ 102 | #define kiss_fft_free free 103 | 104 | /* 105 | Cleans up some memory that gets managed internally. Not necessary to call, but it might clean up 106 | your compiler output to call this before you exit. 107 | */ 108 | void kiss_fft_cleanup(void); 109 | 110 | 111 | /* 112 | * Returns the smallest integer k, such that k>=n and k has only "fast" factors (2,3,5) 113 | */ 114 | int kiss_fft_next_fast_size(int n); 115 | 116 | /* for real ffts, we need an even size */ 117 | #define kiss_fftr_next_fast_size_real(n) \ 118 | (kiss_fft_next_fast_size( ((n)+1)>>1)<<1) 119 | 120 | #ifdef __cplusplus 121 | } 122 | #endif 123 | 124 | #endif 125 | -------------------------------------------------------------------------------- /Util/kiss_fft/kissfft.hh: -------------------------------------------------------------------------------- 1 | #ifndef KISSFFT_CLASS_HH 2 | #include 3 | #include 4 | 5 | namespace kissfft_utils { 6 | 7 | template 8 | struct traits 9 | { 10 | typedef T_scalar scalar_type; 11 | typedef std::complex cpx_type; 12 | void fill_twiddles( std::complex * dst ,int nfft,bool inverse) 13 | { 14 | T_scalar phinc = (inverse?2:-2)* acos( (T_scalar) -1) / nfft; 15 | for (int i=0;i(0,i*phinc) ); 17 | } 18 | 19 | void prepare( 20 | std::vector< std::complex > & dst, 21 | int nfft,bool inverse, 22 | std::vector & stageRadix, 23 | std::vector & stageRemainder ) 24 | { 25 | _twiddles.resize(nfft); 26 | fill_twiddles( &_twiddles[0],nfft,inverse); 27 | dst = _twiddles; 28 | 29 | //factorize 30 | //start factoring out 4's, then 2's, then 3,5,7,9,... 31 | int n= nfft; 32 | int p=4; 33 | do { 34 | while (n % p) { 35 | switch (p) { 36 | case 4: p = 2; break; 37 | case 2: p = 3; break; 38 | default: p += 2; break; 39 | } 40 | if (p*p>n) 41 | p=n;// no more factors 42 | } 43 | n /= p; 44 | stageRadix.push_back(p); 45 | stageRemainder.push_back(n); 46 | }while(n>1); 47 | } 48 | std::vector _twiddles; 49 | 50 | 51 | const cpx_type twiddle(int i) { return _twiddles[i]; } 52 | }; 53 | 54 | } 55 | 56 | template 58 | > 59 | class kissfft 60 | { 61 | public: 62 | typedef T_traits traits_type; 63 | typedef typename traits_type::scalar_type scalar_type; 64 | typedef typename traits_type::cpx_type cpx_type; 65 | 66 | kissfft(int nfft,bool inverse,const traits_type & traits=traits_type() ) 67 | :_nfft(nfft),_inverse(inverse),_traits(traits) 68 | { 69 | _traits.prepare(_twiddles, _nfft,_inverse ,_stageRadix, _stageRemainder); 70 | } 71 | 72 | void transform(const cpx_type * src , cpx_type * dst) 73 | { 74 | kf_work(0, dst, src, 1,1); 75 | } 76 | 77 | private: 78 | void kf_work( int stage,cpx_type * Fout, const cpx_type * f, size_t fstride,size_t in_stride) 79 | { 80 | int p = _stageRadix[stage]; 81 | int m = _stageRemainder[stage]; 82 | cpx_type * Fout_beg = Fout; 83 | cpx_type * Fout_end = Fout + p*m; 84 | 85 | if (m==1) { 86 | do{ 87 | *Fout = *f; 88 | f += fstride*in_stride; 89 | }while(++Fout != Fout_end ); 90 | }else{ 91 | do{ 92 | // recursive call: 93 | // DFT of size m*p performed by doing 94 | // p instances of smaller DFTs of size m, 95 | // each one takes a decimated version of the input 96 | kf_work(stage+1, Fout , f, fstride*p,in_stride); 97 | f += fstride*in_stride; 98 | }while( (Fout += m) != Fout_end ); 99 | } 100 | 101 | Fout=Fout_beg; 102 | 103 | // recombine the p smaller DFTs 104 | switch (p) { 105 | case 2: kf_bfly2(Fout,fstride,m); break; 106 | case 3: kf_bfly3(Fout,fstride,m); break; 107 | case 4: kf_bfly4(Fout,fstride,m); break; 108 | case 5: kf_bfly5(Fout,fstride,m); break; 109 | default: kf_bfly_generic(Fout,fstride,m,p); break; 110 | } 111 | } 112 | 113 | // these were #define macros in the original kiss_fft 114 | void C_ADD( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a+b;} 115 | void C_MUL( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a*b;} 116 | void C_SUB( cpx_type & c,const cpx_type & a,const cpx_type & b) { c=a-b;} 117 | void C_ADDTO( cpx_type & c,const cpx_type & a) { c+=a;} 118 | void C_FIXDIV( cpx_type & ,int ) {} // NO-OP for float types 119 | scalar_type S_MUL( const scalar_type & a,const scalar_type & b) { return a*b;} 120 | scalar_type HALF_OF( const scalar_type & a) { return a*.5;} 121 | void C_MULBYSCALAR(cpx_type & c,const scalar_type & a) {c*=a;} 122 | 123 | void kf_bfly2( cpx_type * Fout, const size_t fstride, int m) 124 | { 125 | for (int k=0;kreal() - HALF_OF(scratch[3].real() ) , Fout->imag() - HALF_OF(scratch[3].imag() ) ); 177 | 178 | C_MULBYSCALAR( scratch[0] , epi3.imag() ); 179 | 180 | C_ADDTO(*Fout,scratch[3]); 181 | 182 | Fout[m2] = cpx_type( Fout[m].real() + scratch[0].imag() , Fout[m].imag() - scratch[0].real() ); 183 | 184 | C_ADDTO( Fout[m] , cpx_type( -scratch[0].imag(),scratch[0].real() ) ); 185 | ++Fout; 186 | }while(--k); 187 | } 188 | 189 | void kf_bfly5( cpx_type * Fout, const size_t fstride, const size_t m) 190 | { 191 | cpx_type *Fout0,*Fout1,*Fout2,*Fout3,*Fout4; 192 | size_t u; 193 | cpx_type scratch[13]; 194 | cpx_type * twiddles = &_twiddles[0]; 195 | cpx_type *tw; 196 | cpx_type ya,yb; 197 | ya = twiddles[fstride*m]; 198 | yb = twiddles[fstride*2*m]; 199 | 200 | Fout0=Fout; 201 | Fout1=Fout0+m; 202 | Fout2=Fout0+2*m; 203 | Fout3=Fout0+3*m; 204 | Fout4=Fout0+4*m; 205 | 206 | tw=twiddles; 207 | for ( u=0; u=Norig) twidx-=Norig; 284 | C_MUL(t,scratchbuf[q] , twiddles[twidx] ); 285 | C_ADDTO( Fout[ k ] ,t); 286 | } 287 | k += m; 288 | } 289 | } 290 | } 291 | 292 | int _nfft; 293 | bool _inverse; 294 | std::vector _twiddles; 295 | std::vector _stageRadix; 296 | std::vector _stageRemainder; 297 | traits_type _traits; 298 | }; 299 | #endif 300 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/Makefile: -------------------------------------------------------------------------------- 1 | WARNINGS=-W -Wall -Wstrict-prototypes -Wmissing-prototypes -Waggregate-return \ 2 | -Wcast-align -Wcast-qual -Wnested-externs -Wshadow -Wbad-function-cast \ 3 | -Wwrite-strings 4 | 5 | ifeq "$(DATATYPE)" "" 6 | DATATYPE=float 7 | endif 8 | 9 | ifeq "$(DATATYPE)" "int32_t" 10 | TYPEFLAGS=-DFIXED_POINT=32 11 | endif 12 | 13 | ifeq "$(DATATYPE)" "int16_t" 14 | TYPEFLAGS=-DFIXED_POINT=16 15 | endif 16 | 17 | ifeq "$(DATATYPE)" "simd" 18 | TYPEFLAGS=-DUSE_SIMD=1 -msse 19 | endif 20 | 21 | ifeq "$(TYPEFLAGS)" "" 22 | TYPEFLAGS=-Dkiss_fft_scalar=$(DATATYPE) 23 | endif 24 | 25 | ifneq ("$(KISS_FFT_USE_ALLOCA)","") 26 | CFLAGS+= -DKISS_FFT_USE_ALLOCA=1 27 | endif 28 | CFLAGS+= $(CFLAGADD) 29 | 30 | 31 | FFTUTIL=fft_$(DATATYPE) 32 | FASTFILT=fastconv_$(DATATYPE) 33 | FASTFILTREAL=fastconvr_$(DATATYPE) 34 | PSDPNG=psdpng_$(DATATYPE) 35 | DUMPHDR=dumphdr_$(DATATYPE) 36 | 37 | all: $(FFTUTIL) $(FASTFILT) $(FASTFILTREAL) 38 | # $(PSDPNG) 39 | # $(DUMPHDR) 40 | 41 | #CFLAGS=-Wall -O3 -pedantic -march=pentiumpro -ffast-math -fomit-frame-pointer $(WARNINGS) 42 | # If the above flags do not work, try the following 43 | CFLAGS=-Wall -O3 $(WARNINGS) 44 | # tip: try -openmp or -fopenmp to use multiple cores 45 | 46 | $(FASTFILTREAL): ../kiss_fft.c kiss_fastfir.c kiss_fftr.c 47 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) -DREAL_FASTFIR $+ -DFAST_FILT_UTIL -lm 48 | 49 | $(FASTFILT): ../kiss_fft.c kiss_fastfir.c 50 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -DFAST_FILT_UTIL -lm 51 | 52 | $(FFTUTIL): ../kiss_fft.c fftutil.c kiss_fftnd.c kiss_fftr.c kiss_fftndr.c 53 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lm 54 | 55 | $(PSDPNG): ../kiss_fft.c psdpng.c kiss_fftr.c 56 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lpng -lm 57 | 58 | $(DUMPHDR): ../kiss_fft.c dumphdr.c 59 | $(CC) -o $@ $(CFLAGS) -I.. $(TYPEFLAGS) $+ -lm 60 | 61 | clean: 62 | rm -f *~ fft fft_* fastconv fastconv_* fastconvr fastconvr_* psdpng psdpng_* 63 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/fftutil.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "kiss_fft.h" 22 | #include "kiss_fftndr.h" 23 | 24 | static 25 | void fft_file(FILE * fin,FILE * fout,int nfft,int isinverse) 26 | { 27 | kiss_fft_cfg st; 28 | kiss_fft_cpx * buf; 29 | kiss_fft_cpx * bufout; 30 | 31 | buf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft ); 32 | bufout = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * nfft ); 33 | st = kiss_fft_alloc( nfft ,isinverse ,0,0); 34 | 35 | while ( fread( buf , sizeof(kiss_fft_cpx) * nfft ,1, fin ) > 0 ) { 36 | kiss_fft( st , buf ,bufout); 37 | fwrite( bufout , sizeof(kiss_fft_cpx) , nfft , fout ); 38 | } 39 | free(st); 40 | free(buf); 41 | free(bufout); 42 | } 43 | 44 | static 45 | void fft_filend(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) 46 | { 47 | kiss_fftnd_cfg st; 48 | kiss_fft_cpx *buf; 49 | int dimprod=1,i; 50 | for (i=0;i 0) { 57 | kiss_fftnd (st, buf, buf); 58 | fwrite (buf, sizeof (kiss_fft_cpx), dimprod, fout); 59 | } 60 | free (st); 61 | free (buf); 62 | } 63 | 64 | 65 | 66 | static 67 | void fft_filend_real(FILE * fin,FILE * fout,int *dims,int ndims,int isinverse) 68 | { 69 | int dimprod=1,i; 70 | kiss_fftndr_cfg st; 71 | void *ibuf; 72 | void *obuf; 73 | int insize,outsize; // size in bytes 74 | 75 | for (i=0;i 0) { 91 | if (isinverse) { 92 | kiss_fftndri(st, 93 | (kiss_fft_cpx*)ibuf, 94 | (kiss_fft_scalar*)obuf); 95 | }else{ 96 | kiss_fftndr(st, 97 | (kiss_fft_scalar*)ibuf, 98 | (kiss_fft_cpx*)obuf); 99 | } 100 | fwrite (obuf, sizeof(kiss_fft_scalar), outsize,fout); 101 | } 102 | free(st); 103 | free(ibuf); 104 | free(obuf); 105 | } 106 | 107 | static 108 | void fft_file_real(FILE * fin,FILE * fout,int nfft,int isinverse) 109 | { 110 | kiss_fftr_cfg st; 111 | kiss_fft_scalar * rbuf; 112 | kiss_fft_cpx * cbuf; 113 | 114 | rbuf = (kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar) * nfft ); 115 | cbuf = (kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx) * (nfft/2+1) ); 116 | st = kiss_fftr_alloc( nfft ,isinverse ,0,0); 117 | 118 | if (isinverse==0) { 119 | while ( fread( rbuf , sizeof(kiss_fft_scalar) * nfft ,1, fin ) > 0 ) { 120 | kiss_fftr( st , rbuf ,cbuf); 121 | fwrite( cbuf , sizeof(kiss_fft_cpx) , (nfft/2 + 1) , fout ); 122 | } 123 | }else{ 124 | while ( fread( cbuf , sizeof(kiss_fft_cpx) * (nfft/2+1) ,1, fin ) > 0 ) { 125 | kiss_fftri( st , cbuf ,rbuf); 126 | fwrite( rbuf , sizeof(kiss_fft_scalar) , nfft , fout ); 127 | } 128 | } 129 | free(st); 130 | free(rbuf); 131 | free(cbuf); 132 | } 133 | 134 | static 135 | int get_dims(char * arg,int * dims) 136 | { 137 | char *p0; 138 | int ndims=0; 139 | 140 | do{ 141 | p0 = strchr(arg,','); 142 | if (p0) 143 | *p0++ = '\0'; 144 | dims[ndims++] = atoi(arg); 145 | // fprintf(stderr,"dims[%d] = %d\n",ndims-1,dims[ndims-1]); 146 | arg = p0; 147 | }while (p0); 148 | return ndims; 149 | } 150 | 151 | int main(int argc,char ** argv) 152 | { 153 | int isinverse=0; 154 | int isreal=0; 155 | FILE *fin=stdin; 156 | FILE *fout=stdout; 157 | int ndims=1; 158 | int dims[32]; 159 | dims[0] = 1024; /*default fft size*/ 160 | 161 | while (1) { 162 | int c=getopt(argc,argv,"n:iR"); 163 | if (c==-1) break; 164 | switch (c) { 165 | case 'n': 166 | ndims = get_dims(optarg,dims); 167 | break; 168 | case 'i':isinverse=1;break; 169 | case 'R':isreal=1;break; 170 | case '?': 171 | fprintf(stderr,"usage options:\n" 172 | "\t-n d1[,d2,d3...]: fft dimension(s)\n" 173 | "\t-i : inverse\n" 174 | "\t-R : real input samples, not complex\n"); 175 | exit (1); 176 | default:fprintf(stderr,"bad %c\n",c);break; 177 | } 178 | } 179 | 180 | if ( optind < argc ) { 181 | if (strcmp("-",argv[optind]) !=0) 182 | fin = fopen(argv[optind],"rb"); 183 | ++optind; 184 | } 185 | 186 | if ( optind < argc ) { 187 | if ( strcmp("-",argv[optind]) !=0 ) 188 | fout = fopen(argv[optind],"wb"); 189 | ++optind; 190 | } 191 | 192 | if (ndims==1) { 193 | if (isreal) 194 | fft_file_real(fin,fout,dims[0],isinverse); 195 | else 196 | fft_file(fin,fout,dims[0],isinverse); 197 | }else{ 198 | if (isreal) 199 | fft_filend_real(fin,fout,dims,ndims,isinverse); 200 | else 201 | fft_filend(fin,fout,dims,ndims,isinverse); 202 | } 203 | 204 | if (fout!=stdout) fclose(fout); 205 | if (fin!=stdin) fclose(fin); 206 | 207 | return 0; 208 | } 209 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kfc.c: -------------------------------------------------------------------------------- 1 | #include "kfc.h" 2 | 3 | /* 4 | Copyright (c) 2003-2004, Mark Borgerding 5 | 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 | */ 16 | 17 | 18 | typedef struct cached_fft *kfc_cfg; 19 | 20 | struct cached_fft 21 | { 22 | int nfft; 23 | int inverse; 24 | kiss_fft_cfg cfg; 25 | kfc_cfg next; 26 | }; 27 | 28 | static kfc_cfg cache_root=NULL; 29 | static int ncached=0; 30 | 31 | static kiss_fft_cfg find_cached_fft(int nfft,int inverse) 32 | { 33 | size_t len; 34 | kfc_cfg cur=cache_root; 35 | kfc_cfg prev=NULL; 36 | while ( cur ) { 37 | if ( cur->nfft == nfft && inverse == cur->inverse ) 38 | break;/*found the right node*/ 39 | prev = cur; 40 | cur = prev->next; 41 | } 42 | if (cur== NULL) { 43 | /* no cached node found, need to create a new one*/ 44 | kiss_fft_alloc(nfft,inverse,0,&len); 45 | #ifdef USE_SIMD 46 | int padding = (16-sizeof(struct cached_fft)) & 15; 47 | // make sure the cfg aligns on a 16 byte boundary 48 | len += padding; 49 | #endif 50 | cur = (kfc_cfg)KISS_FFT_MALLOC((sizeof(struct cached_fft) + len )); 51 | if (cur == NULL) 52 | return NULL; 53 | cur->cfg = (kiss_fft_cfg)(cur+1); 54 | #ifdef USE_SIMD 55 | cur->cfg = (kiss_fft_cfg) ((char*)(cur+1)+padding); 56 | #endif 57 | kiss_fft_alloc(nfft,inverse,cur->cfg,&len); 58 | cur->nfft=nfft; 59 | cur->inverse=inverse; 60 | cur->next = NULL; 61 | if ( prev ) 62 | prev->next = cur; 63 | else 64 | cache_root = cur; 65 | ++ncached; 66 | } 67 | return cur->cfg; 68 | } 69 | 70 | void kfc_cleanup(void) 71 | { 72 | kfc_cfg cur=cache_root; 73 | kfc_cfg next=NULL; 74 | while (cur){ 75 | next = cur->next; 76 | free(cur); 77 | cur=next; 78 | } 79 | ncached=0; 80 | cache_root = NULL; 81 | } 82 | void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout) 83 | { 84 | kiss_fft( find_cached_fft(nfft,0),fin,fout ); 85 | } 86 | 87 | void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout) 88 | { 89 | kiss_fft( find_cached_fft(nfft,1),fin,fout ); 90 | } 91 | 92 | #ifdef KFC_TEST 93 | static void check(int nc) 94 | { 95 | if (ncached != nc) { 96 | fprintf(stderr,"ncached should be %d,but it is %d\n",nc,ncached); 97 | exit(1); 98 | } 99 | } 100 | 101 | int main(void) 102 | { 103 | kiss_fft_cpx buf1[1024],buf2[1024]; 104 | memset(buf1,0,sizeof(buf1)); 105 | check(0); 106 | kfc_fft(512,buf1,buf2); 107 | check(1); 108 | kfc_fft(512,buf1,buf2); 109 | check(1); 110 | kfc_ifft(512,buf1,buf2); 111 | check(2); 112 | kfc_cleanup(); 113 | check(0); 114 | return 0; 115 | } 116 | #endif 117 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kfc.h: -------------------------------------------------------------------------------- 1 | #ifndef KFC_H 2 | #define KFC_H 3 | #include "kiss_fft.h" 4 | 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | /* 10 | KFC -- Kiss FFT Cache 11 | 12 | Not needing to deal with kiss_fft_alloc and a config 13 | object may be handy for a lot of programs. 14 | 15 | KFC uses the underlying KISS FFT functions, but caches the config object. 16 | The first time kfc_fft or kfc_ifft for a given FFT size, the cfg 17 | object is created for it. All subsequent calls use the cached 18 | configuration object. 19 | 20 | NOTE: 21 | You should probably not use this if your program will be using a lot 22 | of various sizes of FFTs. There is a linear search through the 23 | cached objects. If you are only using one or two FFT sizes, this 24 | will be negligible. Otherwise, you may want to use another method 25 | of managing the cfg objects. 26 | 27 | There is no automated cleanup of the cached objects. This could lead 28 | to large memory usage in a program that uses a lot of *DIFFERENT* 29 | sized FFTs. If you want to force all cached cfg objects to be freed, 30 | call kfc_cleanup. 31 | 32 | */ 33 | 34 | /*forward complex FFT */ 35 | void kfc_fft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout); 36 | /*reverse complex FFT */ 37 | void kfc_ifft(int nfft, const kiss_fft_cpx * fin,kiss_fft_cpx * fout); 38 | 39 | /*free all cached objects*/ 40 | void kfc_cleanup(void); 41 | 42 | #ifdef __cplusplus 43 | } 44 | #endif 45 | 46 | #endif 47 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fastfir.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include "_kiss_fft_guts.h" 16 | 17 | 18 | /* 19 | Some definitions that allow real or complex filtering 20 | */ 21 | #ifdef REAL_FASTFIR 22 | #define MIN_FFT_LEN 2048 23 | #include "kiss_fftr.h" 24 | typedef kiss_fft_scalar kffsamp_t; 25 | typedef kiss_fftr_cfg kfcfg_t; 26 | #define FFT_ALLOC kiss_fftr_alloc 27 | #define FFTFWD kiss_fftr 28 | #define FFTINV kiss_fftri 29 | #else 30 | #define MIN_FFT_LEN 1024 31 | typedef kiss_fft_cpx kffsamp_t; 32 | typedef kiss_fft_cfg kfcfg_t; 33 | #define FFT_ALLOC kiss_fft_alloc 34 | #define FFTFWD kiss_fft 35 | #define FFTINV kiss_fft 36 | #endif 37 | 38 | typedef struct kiss_fastfir_state *kiss_fastfir_cfg; 39 | 40 | 41 | 42 | kiss_fastfir_cfg kiss_fastfir_alloc(const kffsamp_t * imp_resp,size_t n_imp_resp, 43 | size_t * nfft,void * mem,size_t*lenmem); 44 | 45 | /* see do_file_filter for usage */ 46 | size_t kiss_fastfir( kiss_fastfir_cfg cfg, kffsamp_t * inbuf, kffsamp_t * outbuf, size_t n, size_t *offset); 47 | 48 | 49 | 50 | static int verbose=0; 51 | 52 | 53 | struct kiss_fastfir_state{ 54 | size_t nfft; 55 | size_t ngood; 56 | kfcfg_t fftcfg; 57 | kfcfg_t ifftcfg; 58 | kiss_fft_cpx * fir_freq_resp; 59 | kiss_fft_cpx * freqbuf; 60 | size_t n_freq_bins; 61 | kffsamp_t * tmpbuf; 62 | }; 63 | 64 | 65 | kiss_fastfir_cfg kiss_fastfir_alloc( 66 | const kffsamp_t * imp_resp,size_t n_imp_resp, 67 | size_t *pnfft, /* if <= 0, an appropriate size will be chosen */ 68 | void * mem,size_t*lenmem) 69 | { 70 | kiss_fastfir_cfg st = NULL; 71 | size_t len_fftcfg,len_ifftcfg; 72 | size_t memneeded = sizeof(struct kiss_fastfir_state); 73 | char * ptr; 74 | size_t i; 75 | size_t nfft=0; 76 | float scale; 77 | int n_freq_bins; 78 | if (pnfft) 79 | nfft=*pnfft; 80 | 81 | if (nfft<=0) { 82 | /* determine fft size as next power of two at least 2x 83 | the impulse response length*/ 84 | i=n_imp_resp-1; 85 | nfft=2; 86 | do{ 87 | nfft<<=1; 88 | }while (i>>=1); 89 | #ifdef MIN_FFT_LEN 90 | if ( nfft < MIN_FFT_LEN ) 91 | nfft=MIN_FFT_LEN; 92 | #endif 93 | } 94 | if (pnfft) 95 | *pnfft = nfft; 96 | 97 | #ifdef REAL_FASTFIR 98 | n_freq_bins = nfft/2 + 1; 99 | #else 100 | n_freq_bins = nfft; 101 | #endif 102 | /*fftcfg*/ 103 | FFT_ALLOC (nfft, 0, NULL, &len_fftcfg); 104 | memneeded += len_fftcfg; 105 | /*ifftcfg*/ 106 | FFT_ALLOC (nfft, 1, NULL, &len_ifftcfg); 107 | memneeded += len_ifftcfg; 108 | /* tmpbuf */ 109 | memneeded += sizeof(kffsamp_t) * nfft; 110 | /* fir_freq_resp */ 111 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; 112 | /* freqbuf */ 113 | memneeded += sizeof(kiss_fft_cpx) * n_freq_bins; 114 | 115 | if (lenmem == NULL) { 116 | st = (kiss_fastfir_cfg) malloc (memneeded); 117 | } else { 118 | if (*lenmem >= memneeded) 119 | st = (kiss_fastfir_cfg) mem; 120 | *lenmem = memneeded; 121 | } 122 | if (!st) 123 | return NULL; 124 | 125 | st->nfft = nfft; 126 | st->ngood = nfft - n_imp_resp + 1; 127 | st->n_freq_bins = n_freq_bins; 128 | ptr=(char*)(st+1); 129 | 130 | st->fftcfg = (kfcfg_t)ptr; 131 | ptr += len_fftcfg; 132 | 133 | st->ifftcfg = (kfcfg_t)ptr; 134 | ptr += len_ifftcfg; 135 | 136 | st->tmpbuf = (kffsamp_t*)ptr; 137 | ptr += sizeof(kffsamp_t) * nfft; 138 | 139 | st->freqbuf = (kiss_fft_cpx*)ptr; 140 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins; 141 | 142 | st->fir_freq_resp = (kiss_fft_cpx*)ptr; 143 | ptr += sizeof(kiss_fft_cpx) * n_freq_bins; 144 | 145 | FFT_ALLOC (nfft,0,st->fftcfg , &len_fftcfg); 146 | FFT_ALLOC (nfft,1,st->ifftcfg , &len_ifftcfg); 147 | 148 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*nfft); 149 | /*zero pad in the middle to left-rotate the impulse response 150 | This puts the scrap samples at the end of the inverse fft'd buffer */ 151 | st->tmpbuf[0] = imp_resp[ n_imp_resp - 1 ]; 152 | for (i=0;itmpbuf[ nfft - n_imp_resp + 1 + i ] = imp_resp[ i ]; 154 | } 155 | 156 | FFTFWD(st->fftcfg,st->tmpbuf,st->fir_freq_resp); 157 | 158 | /* TODO: this won't work for fixed point */ 159 | scale = 1.0 / st->nfft; 160 | 161 | for ( i=0; i < st->n_freq_bins; ++i ) { 162 | #ifdef USE_SIMD 163 | st->fir_freq_resp[i].r *= _mm_set1_ps(scale); 164 | st->fir_freq_resp[i].i *= _mm_set1_ps(scale); 165 | #else 166 | st->fir_freq_resp[i].r *= scale; 167 | st->fir_freq_resp[i].i *= scale; 168 | #endif 169 | } 170 | return st; 171 | } 172 | 173 | static void fastconv1buf(const kiss_fastfir_cfg st,const kffsamp_t * in,kffsamp_t * out) 174 | { 175 | size_t i; 176 | /* multiply the frequency response of the input signal by 177 | that of the fir filter*/ 178 | FFTFWD( st->fftcfg, in , st->freqbuf ); 179 | for ( i=0; in_freq_bins; ++i ) { 180 | kiss_fft_cpx tmpsamp; 181 | C_MUL(tmpsamp,st->freqbuf[i],st->fir_freq_resp[i]); 182 | st->freqbuf[i] = tmpsamp; 183 | } 184 | 185 | /* perform the inverse fft*/ 186 | FFTINV(st->ifftcfg,st->freqbuf,out); 187 | } 188 | 189 | /* n : the size of inbuf and outbuf in samples 190 | return value: the number of samples completely processed 191 | n-retval samples should be copied to the front of the next input buffer */ 192 | static size_t kff_nocopy( 193 | kiss_fastfir_cfg st, 194 | const kffsamp_t * inbuf, 195 | kffsamp_t * outbuf, 196 | size_t n) 197 | { 198 | size_t norig=n; 199 | while (n >= st->nfft ) { 200 | fastconv1buf(st,inbuf,outbuf); 201 | inbuf += st->ngood; 202 | outbuf += st->ngood; 203 | n -= st->ngood; 204 | } 205 | return norig - n; 206 | } 207 | 208 | static 209 | size_t kff_flush(kiss_fastfir_cfg st,const kffsamp_t * inbuf,kffsamp_t * outbuf,size_t n) 210 | { 211 | size_t zpad=0,ntmp; 212 | 213 | ntmp = kff_nocopy(st,inbuf,outbuf,n); 214 | n -= ntmp; 215 | inbuf += ntmp; 216 | outbuf += ntmp; 217 | 218 | zpad = st->nfft - n; 219 | memset(st->tmpbuf,0,sizeof(kffsamp_t)*st->nfft ); 220 | memcpy(st->tmpbuf,inbuf,sizeof(kffsamp_t)*n ); 221 | 222 | fastconv1buf(st,st->tmpbuf,st->tmpbuf); 223 | 224 | memcpy(outbuf,st->tmpbuf,sizeof(kffsamp_t)*( st->ngood - zpad )); 225 | return ntmp + st->ngood - zpad; 226 | } 227 | 228 | size_t kiss_fastfir( 229 | kiss_fastfir_cfg vst, 230 | kffsamp_t * inbuf, 231 | kffsamp_t * outbuf, 232 | size_t n_new, 233 | size_t *offset) 234 | { 235 | size_t ntot = n_new + *offset; 236 | if (n_new==0) { 237 | return kff_flush(vst,inbuf,outbuf,ntot); 238 | }else{ 239 | size_t nwritten = kff_nocopy(vst,inbuf,outbuf,ntot); 240 | *offset = ntot - nwritten; 241 | /*save the unused or underused samples at the front of the input buffer */ 242 | memcpy( inbuf , inbuf+nwritten , *offset * sizeof(kffsamp_t) ); 243 | return nwritten; 244 | } 245 | } 246 | 247 | #ifdef FAST_FILT_UTIL 248 | #include 249 | #include 250 | #include 251 | #include 252 | 253 | static 254 | void direct_file_filter( 255 | FILE * fin, 256 | FILE * fout, 257 | const kffsamp_t * imp_resp, 258 | size_t n_imp_resp) 259 | { 260 | size_t nlag = n_imp_resp - 1; 261 | 262 | const kffsamp_t *tmph; 263 | kffsamp_t *buf, *circbuf; 264 | kffsamp_t outval; 265 | size_t nread; 266 | size_t nbuf; 267 | size_t oldestlag = 0; 268 | size_t k, tap; 269 | #ifndef REAL_FASTFIR 270 | kffsamp_t tmp; 271 | #endif 272 | 273 | nbuf = 4096; 274 | buf = (kffsamp_t *) malloc ( sizeof (kffsamp_t) * nbuf); 275 | circbuf = (kffsamp_t *) malloc (sizeof (kffsamp_t) * nlag); 276 | if (!circbuf || !buf) { 277 | perror("circbuf allocation"); 278 | exit(1); 279 | } 280 | 281 | if ( fread (circbuf, sizeof (kffsamp_t), nlag, fin) != nlag ) { 282 | perror ("insufficient data to overcome transient"); 283 | exit (1); 284 | } 285 | 286 | do { 287 | nread = fread (buf, sizeof (kffsamp_t), nbuf, fin); 288 | if (nread <= 0) 289 | break; 290 | 291 | for (k = 0; k < nread; ++k) { 292 | tmph = imp_resp+nlag; 293 | #ifdef REAL_FASTFIR 294 | # ifdef USE_SIMD 295 | outval = _mm_set1_ps(0); 296 | #else 297 | outval = 0; 298 | #endif 299 | for (tap = oldestlag; tap < nlag; ++tap) 300 | outval += circbuf[tap] * *tmph--; 301 | for (tap = 0; tap < oldestlag; ++tap) 302 | outval += circbuf[tap] * *tmph--; 303 | outval += buf[k] * *tmph; 304 | #else 305 | # ifdef USE_SIMD 306 | outval.r = outval.i = _mm_set1_ps(0); 307 | #else 308 | outval.r = outval.i = 0; 309 | #endif 310 | for (tap = oldestlag; tap < nlag; ++tap){ 311 | C_MUL(tmp,circbuf[tap],*tmph); 312 | --tmph; 313 | C_ADDTO(outval,tmp); 314 | } 315 | 316 | for (tap = 0; tap < oldestlag; ++tap) { 317 | C_MUL(tmp,circbuf[tap],*tmph); 318 | --tmph; 319 | C_ADDTO(outval,tmp); 320 | } 321 | C_MUL(tmp,buf[k],*tmph); 322 | C_ADDTO(outval,tmp); 323 | #endif 324 | 325 | circbuf[oldestlag++] = buf[k]; 326 | buf[k] = outval; 327 | 328 | if (oldestlag == nlag) 329 | oldestlag = 0; 330 | } 331 | 332 | if (fwrite (buf, sizeof (buf[0]), nread, fout) != nread) { 333 | perror ("short write"); 334 | exit (1); 335 | } 336 | } while (nread); 337 | free (buf); 338 | free (circbuf); 339 | } 340 | 341 | static 342 | void do_file_filter( 343 | FILE * fin, 344 | FILE * fout, 345 | const kffsamp_t * imp_resp, 346 | size_t n_imp_resp, 347 | size_t nfft ) 348 | { 349 | int fdout; 350 | size_t n_samps_buf; 351 | 352 | kiss_fastfir_cfg cfg; 353 | kffsamp_t *inbuf,*outbuf; 354 | int nread,nwrite; 355 | size_t idx_inbuf; 356 | 357 | fdout = fileno(fout); 358 | 359 | cfg=kiss_fastfir_alloc(imp_resp,n_imp_resp,&nfft,0,0); 360 | 361 | /* use length to minimize buffer shift*/ 362 | n_samps_buf = 8*4096/sizeof(kffsamp_t); 363 | n_samps_buf = nfft + 4*(nfft-n_imp_resp+1); 364 | 365 | if (verbose) fprintf(stderr,"bufsize=%d\n",(int)(sizeof(kffsamp_t)*n_samps_buf) ); 366 | 367 | 368 | /*allocate space and initialize pointers */ 369 | inbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); 370 | outbuf = (kffsamp_t*)malloc(sizeof(kffsamp_t)*n_samps_buf); 371 | 372 | idx_inbuf=0; 373 | do{ 374 | /* start reading at inbuf[idx_inbuf] */ 375 | nread = fread( inbuf + idx_inbuf, sizeof(kffsamp_t), n_samps_buf - idx_inbuf,fin ); 376 | 377 | /* If nread==0, then this is a flush. 378 | The total number of samples in input is idx_inbuf + nread . */ 379 | nwrite = kiss_fastfir(cfg, inbuf, outbuf,nread,&idx_inbuf) * sizeof(kffsamp_t); 380 | /* kiss_fastfir moved any unused samples to the front of inbuf and updated idx_inbuf */ 381 | 382 | if ( write(fdout, outbuf, nwrite) != nwrite ) { 383 | perror("short write"); 384 | exit(1); 385 | } 386 | }while ( nread ); 387 | free(cfg); 388 | free(inbuf); 389 | free(outbuf); 390 | } 391 | 392 | int main(int argc,char**argv) 393 | { 394 | kffsamp_t * h; 395 | int use_direct=0; 396 | size_t nh,nfft=0; 397 | FILE *fin=stdin; 398 | FILE *fout=stdout; 399 | FILE *filtfile=NULL; 400 | while (1) { 401 | int c=getopt(argc,argv,"n:h:i:o:vd"); 402 | if (c==-1) break; 403 | switch (c) { 404 | case 'v': 405 | verbose=1; 406 | break; 407 | case 'n': 408 | nfft=atoi(optarg); 409 | break; 410 | case 'i': 411 | fin = fopen(optarg,"rb"); 412 | if (fin==NULL) { 413 | perror(optarg); 414 | exit(1); 415 | } 416 | break; 417 | case 'o': 418 | fout = fopen(optarg,"w+b"); 419 | if (fout==NULL) { 420 | perror(optarg); 421 | exit(1); 422 | } 423 | break; 424 | case 'h': 425 | filtfile = fopen(optarg,"rb"); 426 | if (filtfile==NULL) { 427 | perror(optarg); 428 | exit(1); 429 | } 430 | break; 431 | case 'd': 432 | use_direct=1; 433 | break; 434 | case '?': 435 | fprintf(stderr,"usage options:\n" 436 | "\t-n nfft: fft size to use\n" 437 | "\t-d : use direct FIR filtering, not fast convolution\n" 438 | "\t-i filename: input file\n" 439 | "\t-o filename: output(filtered) file\n" 440 | "\t-n nfft: fft size to use\n" 441 | "\t-h filename: impulse response\n"); 442 | exit (1); 443 | default:fprintf(stderr,"bad %c\n",c);break; 444 | } 445 | } 446 | if (filtfile==NULL) { 447 | fprintf(stderr,"You must supply the FIR coeffs via -h\n"); 448 | exit(1); 449 | } 450 | fseek(filtfile,0,SEEK_END); 451 | nh = ftell(filtfile) / sizeof(kffsamp_t); 452 | if (verbose) fprintf(stderr,"%d samples in FIR filter\n",(int)nh); 453 | h = (kffsamp_t*)malloc(sizeof(kffsamp_t)*nh); 454 | fseek(filtfile,0,SEEK_SET); 455 | if (fread(h,sizeof(kffsamp_t),nh,filtfile) != nh) 456 | fprintf(stderr,"short read on filter file\n"); 457 | 458 | fclose(filtfile); 459 | 460 | if (use_direct) 461 | direct_file_filter( fin, fout, h,nh); 462 | else 463 | do_file_filter( fin, fout, h,nh,nfft); 464 | 465 | if (fout!=stdout) fclose(fout); 466 | if (fin!=stdin) fclose(fin); 467 | 468 | return 0; 469 | } 470 | #endif 471 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftnd.c: -------------------------------------------------------------------------------- 1 | 2 | 3 | /* 4 | Copyright (c) 2003-2004, Mark Borgerding 5 | 6 | All rights reserved. 7 | 8 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 9 | 10 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 | 14 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 | */ 16 | 17 | #include "kiss_fftnd.h" 18 | #include "_kiss_fft_guts.h" 19 | 20 | struct kiss_fftnd_state{ 21 | int dimprod; /* dimsum would be mighty tasty right now */ 22 | int ndims; 23 | int *dims; 24 | kiss_fft_cfg *states; /* cfg states for each dimension */ 25 | kiss_fft_cpx * tmpbuf; /*buffer capable of hold the entire input */ 26 | }; 27 | 28 | kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem) 29 | { 30 | kiss_fftnd_cfg st = NULL; 31 | int i; 32 | int dimprod=1; 33 | size_t memneeded = sizeof(struct kiss_fftnd_state); 34 | char * ptr; 35 | 36 | for (i=0;istates[i] */ 40 | dimprod *= dims[i]; 41 | } 42 | memneeded += sizeof(int) * ndims;/* st->dims */ 43 | memneeded += sizeof(void*) * ndims;/* st->states */ 44 | memneeded += sizeof(kiss_fft_cpx) * dimprod; /* st->tmpbuf */ 45 | 46 | if (lenmem == NULL) {/* allocate for the caller*/ 47 | st = (kiss_fftnd_cfg) malloc (memneeded); 48 | } else { /* initialize supplied buffer if big enough */ 49 | if (*lenmem >= memneeded) 50 | st = (kiss_fftnd_cfg) mem; 51 | *lenmem = memneeded; /*tell caller how big struct is (or would be) */ 52 | } 53 | if (!st) 54 | return NULL; /*malloc failed or buffer too small */ 55 | 56 | st->dimprod = dimprod; 57 | st->ndims = ndims; 58 | ptr=(char*)(st+1); 59 | 60 | st->states = (kiss_fft_cfg *)ptr; 61 | ptr += sizeof(void*) * ndims; 62 | 63 | st->dims = (int*)ptr; 64 | ptr += sizeof(int) * ndims; 65 | 66 | st->tmpbuf = (kiss_fft_cpx*)ptr; 67 | ptr += sizeof(kiss_fft_cpx) * dimprod; 68 | 69 | for (i=0;idims[i] = dims[i]; 72 | kiss_fft_alloc (st->dims[i], inverse_fft, NULL, &len); 73 | st->states[i] = kiss_fft_alloc (st->dims[i], inverse_fft, ptr,&len); 74 | ptr += len; 75 | } 76 | /* 77 | Hi there! 78 | 79 | If you're looking at this particular code, it probably means you've got a brain-dead bounds checker 80 | that thinks the above code overwrites the end of the array. 81 | 82 | It doesn't. 83 | 84 | -- Mark 85 | 86 | P.S. 87 | The below code might give you some warm fuzzies and help convince you. 88 | */ 89 | if ( ptr - (char*)st != (int)memneeded ) { 90 | fprintf(stderr, 91 | "################################################################################\n" 92 | "Internal error! Memory allocation miscalculation\n" 93 | "################################################################################\n" 94 | ); 95 | } 96 | return st; 97 | } 98 | 99 | /* 100 | This works by tackling one dimension at a time. 101 | 102 | In effect, 103 | Each stage starts out by reshaping the matrix into a DixSi 2d matrix. 104 | A Di-sized fft is taken of each column, transposing the matrix as it goes. 105 | 106 | Here's a 3-d example: 107 | Take a 2x3x4 matrix, laid out in memory as a contiguous buffer 108 | [ [ [ a b c d ] [ e f g h ] [ i j k l ] ] 109 | [ [ m n o p ] [ q r s t ] [ u v w x ] ] ] 110 | 111 | Stage 0 ( D=2): treat the buffer as a 2x12 matrix 112 | [ [a b ... k l] 113 | [m n ... w x] ] 114 | 115 | FFT each column with size 2. 116 | Transpose the matrix at the same time using kiss_fft_stride. 117 | 118 | [ [ a+m a-m ] 119 | [ b+n b-n] 120 | ... 121 | [ k+w k-w ] 122 | [ l+x l-x ] ] 123 | 124 | Note fft([x y]) == [x+y x-y] 125 | 126 | Stage 1 ( D=3) treats the buffer (the output of stage D=2) as an 3x8 matrix, 127 | [ [ a+m a-m b+n b-n c+o c-o d+p d-p ] 128 | [ e+q e-q f+r f-r g+s g-s h+t h-t ] 129 | [ i+u i-u j+v j-v k+w k-w l+x l-x ] ] 130 | 131 | And perform FFTs (size=3) on each of the columns as above, transposing 132 | the matrix as it goes. The output of stage 1 is 133 | (Legend: ap = [ a+m e+q i+u ] 134 | am = [ a-m e-q i-u ] ) 135 | 136 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] ] 137 | [ sum(am) fft(am)[0] fft(am)[1] ] 138 | [ sum(bp) fft(bp)[0] fft(bp)[1] ] 139 | [ sum(bm) fft(bm)[0] fft(bm)[1] ] 140 | [ sum(cp) fft(cp)[0] fft(cp)[1] ] 141 | [ sum(cm) fft(cm)[0] fft(cm)[1] ] 142 | [ sum(dp) fft(dp)[0] fft(dp)[1] ] 143 | [ sum(dm) fft(dm)[0] fft(dm)[1] ] ] 144 | 145 | Stage 2 ( D=4) treats this buffer as a 4*6 matrix, 146 | [ [ sum(ap) fft(ap)[0] fft(ap)[1] sum(am) fft(am)[0] fft(am)[1] ] 147 | [ sum(bp) fft(bp)[0] fft(bp)[1] sum(bm) fft(bm)[0] fft(bm)[1] ] 148 | [ sum(cp) fft(cp)[0] fft(cp)[1] sum(cm) fft(cm)[0] fft(cm)[1] ] 149 | [ sum(dp) fft(dp)[0] fft(dp)[1] sum(dm) fft(dm)[0] fft(dm)[1] ] ] 150 | 151 | Then FFTs each column, transposing as it goes. 152 | 153 | The resulting matrix is the 3d FFT of the 2x3x4 input matrix. 154 | 155 | Note as a sanity check that the first element of the final 156 | stage's output (DC term) is 157 | sum( [ sum(ap) sum(bp) sum(cp) sum(dp) ] ) 158 | , i.e. the summation of all 24 input elements. 159 | 160 | */ 161 | void kiss_fftnd(kiss_fftnd_cfg st,const kiss_fft_cpx *fin,kiss_fft_cpx *fout) 162 | { 163 | int i,k; 164 | const kiss_fft_cpx * bufin=fin; 165 | kiss_fft_cpx * bufout; 166 | 167 | /*arrange it so the last bufout == fout*/ 168 | if ( st->ndims & 1 ) { 169 | bufout = fout; 170 | if (fin==fout) { 171 | memcpy( st->tmpbuf, fin, sizeof(kiss_fft_cpx) * st->dimprod ); 172 | bufin = st->tmpbuf; 173 | } 174 | }else 175 | bufout = st->tmpbuf; 176 | 177 | for ( k=0; k < st->ndims; ++k) { 178 | int curdim = st->dims[k]; 179 | int stride = st->dimprod / curdim; 180 | 181 | for ( i=0 ; istates[k], bufin+i , bufout+i*curdim, stride ); 183 | 184 | /*toggle back and forth between the two buffers*/ 185 | if (bufout == st->tmpbuf){ 186 | bufout = fout; 187 | bufin = st->tmpbuf; 188 | }else{ 189 | bufout = st->tmpbuf; 190 | bufin = fout; 191 | } 192 | } 193 | } 194 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftnd.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FFTND_H 2 | #define KISS_FFTND_H 3 | 4 | #include "kiss_fft.h" 5 | 6 | #ifdef __cplusplus 7 | extern "C" { 8 | #endif 9 | 10 | typedef struct kiss_fftnd_state * kiss_fftnd_cfg; 11 | 12 | kiss_fftnd_cfg kiss_fftnd_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem); 13 | void kiss_fftnd(kiss_fftnd_cfg cfg,const kiss_fft_cpx *fin,kiss_fft_cpx *fout); 14 | 15 | #ifdef __cplusplus 16 | } 17 | #endif 18 | #endif 19 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftndr.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include "kiss_fftndr.h" 16 | #include "_kiss_fft_guts.h" 17 | #define MAX(x,y) ( ( (x)<(y) )?(y):(x) ) 18 | 19 | struct kiss_fftndr_state 20 | { 21 | int dimReal; 22 | int dimOther; 23 | kiss_fftr_cfg cfg_r; 24 | kiss_fftnd_cfg cfg_nd; 25 | void * tmpbuf; 26 | }; 27 | 28 | static int prod(const int *dims, int ndims) 29 | { 30 | int x=1; 31 | while (ndims--) 32 | x *= *dims++; 33 | return x; 34 | } 35 | 36 | kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem) 37 | { 38 | kiss_fftndr_cfg st = NULL; 39 | size_t nr=0 , nd=0,ntmp=0; 40 | int dimReal = dims[ndims-1]; 41 | int dimOther = prod(dims,ndims-1); 42 | size_t memneeded; 43 | 44 | (void)kiss_fftr_alloc(dimReal,inverse_fft,NULL,&nr); 45 | (void)kiss_fftnd_alloc(dims,ndims-1,inverse_fft,NULL,&nd); 46 | ntmp = 47 | MAX( 2*dimOther , dimReal+2) * sizeof(kiss_fft_scalar) // freq buffer for one pass 48 | + dimOther*(dimReal+2) * sizeof(kiss_fft_scalar); // large enough to hold entire input in case of in-place 49 | 50 | memneeded = sizeof( struct kiss_fftndr_state ) + nr + nd + ntmp; 51 | 52 | if (lenmem==NULL) { 53 | st = (kiss_fftndr_cfg) malloc(memneeded); 54 | }else{ 55 | if (*lenmem >= memneeded) 56 | st = (kiss_fftndr_cfg)mem; 57 | *lenmem = memneeded; 58 | } 59 | if (st==NULL) 60 | return NULL; 61 | memset( st , 0 , memneeded); 62 | 63 | st->dimReal = dimReal; 64 | st->dimOther = dimOther; 65 | st->cfg_r = kiss_fftr_alloc( dimReal,inverse_fft,st+1,&nr); 66 | st->cfg_nd = kiss_fftnd_alloc(dims,ndims-1,inverse_fft, ((char*) st->cfg_r)+nr,&nd); 67 | st->tmpbuf = (char*)st->cfg_nd + nd; 68 | 69 | return st; 70 | } 71 | 72 | void kiss_fftndr(kiss_fftndr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 73 | { 74 | int k1,k2; 75 | int dimReal = st->dimReal; 76 | int dimOther = st->dimOther; 77 | int nrbins = dimReal/2+1; 78 | 79 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 80 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther); 81 | 82 | // timedata is N0 x N1 x ... x Nk real 83 | 84 | // take a real chunk of data, fft it and place the output at correct intervals 85 | for (k1=0;k1cfg_r, timedata + k1*dimReal , tmp1 ); // tmp1 now holds nrbins complex points 87 | for (k2=0;k2cfg_nd, tmp2+k2*dimOther, tmp1); // tmp1 now holds dimOther complex points 93 | for (k1=0;k1dimReal; 102 | int dimOther = st->dimOther; 103 | int nrbins = dimReal/2+1; 104 | kiss_fft_cpx * tmp1 = (kiss_fft_cpx*)st->tmpbuf; 105 | kiss_fft_cpx * tmp2 = tmp1 + MAX(nrbins,dimOther); 106 | 107 | for (k2=0;k2cfg_nd, tmp1, tmp2+k2*dimOther); 111 | } 112 | 113 | for (k1=0;k1cfg_r,tmp1,timedata + k1*dimReal); 117 | } 118 | } 119 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftndr.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_NDR_H 2 | #define KISS_NDR_H 3 | 4 | #include "kiss_fft.h" 5 | #include "kiss_fftr.h" 6 | #include "kiss_fftnd.h" 7 | 8 | #ifdef __cplusplus 9 | extern "C" { 10 | #endif 11 | 12 | typedef struct kiss_fftndr_state *kiss_fftndr_cfg; 13 | 14 | 15 | kiss_fftndr_cfg kiss_fftndr_alloc(const int *dims,int ndims,int inverse_fft,void*mem,size_t*lenmem); 16 | /* 17 | dims[0] must be even 18 | 19 | If you don't care to allocate space, use mem = lenmem = NULL 20 | */ 21 | 22 | 23 | void kiss_fftndr( 24 | kiss_fftndr_cfg cfg, 25 | const kiss_fft_scalar *timedata, 26 | kiss_fft_cpx *freqdata); 27 | /* 28 | input timedata has dims[0] X dims[1] X ... X dims[ndims-1] scalar points 29 | output freqdata has dims[0] X dims[1] X ... X dims[ndims-1]/2+1 complex points 30 | */ 31 | 32 | void kiss_fftndri( 33 | kiss_fftndr_cfg cfg, 34 | const kiss_fft_cpx *freqdata, 35 | kiss_fft_scalar *timedata); 36 | /* 37 | input and output dimensions are the exact opposite of kiss_fftndr 38 | */ 39 | 40 | 41 | #define kiss_fftr_free free 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | 47 | #endif 48 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftr.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include "kiss_fftr.h" 16 | #include "_kiss_fft_guts.h" 17 | 18 | struct kiss_fftr_state{ 19 | kiss_fft_cfg substate; 20 | kiss_fft_cpx * tmpbuf; 21 | kiss_fft_cpx * super_twiddles; 22 | #ifdef USE_SIMD 23 | void * pad; 24 | #endif 25 | }; 26 | 27 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem,size_t * lenmem) 28 | { 29 | int i; 30 | kiss_fftr_cfg st = NULL; 31 | size_t subsize, memneeded; 32 | 33 | if (nfft & 1) { 34 | fprintf(stderr,"Real FFT optimization must be even.\n"); 35 | return NULL; 36 | } 37 | nfft >>= 1; 38 | 39 | kiss_fft_alloc (nfft, inverse_fft, NULL, &subsize); 40 | memneeded = sizeof(struct kiss_fftr_state) + subsize + sizeof(kiss_fft_cpx) * ( nfft * 3 / 2); 41 | 42 | if (lenmem == NULL) { 43 | st = (kiss_fftr_cfg) KISS_FFT_MALLOC (memneeded); 44 | } else { 45 | if (*lenmem >= memneeded) 46 | st = (kiss_fftr_cfg) mem; 47 | *lenmem = memneeded; 48 | } 49 | if (!st) 50 | return NULL; 51 | 52 | st->substate = (kiss_fft_cfg) (st + 1); /*just beyond kiss_fftr_state struct */ 53 | st->tmpbuf = (kiss_fft_cpx *) (((char *) st->substate) + subsize); 54 | st->super_twiddles = st->tmpbuf + nfft; 55 | kiss_fft_alloc(nfft, inverse_fft, st->substate, &subsize); 56 | 57 | for (i = 0; i < nfft/2; ++i) { 58 | double phase = 59 | -3.14159265358979323846264338327 * ((double) (i+1) / nfft + .5); 60 | if (inverse_fft) 61 | phase *= -1; 62 | kf_cexp (st->super_twiddles+i,phase); 63 | } 64 | return st; 65 | } 66 | 67 | void kiss_fftr(kiss_fftr_cfg st,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata) 68 | { 69 | /* input buffer timedata is stored row-wise */ 70 | int k,ncfft; 71 | kiss_fft_cpx fpnk,fpk,f1k,f2k,tw,tdc; 72 | 73 | if ( st->substate->inverse) { 74 | fprintf(stderr,"kiss fft usage error: improper alloc\n"); 75 | exit(1); 76 | } 77 | 78 | ncfft = st->substate->nfft; 79 | 80 | /*perform the parallel fft of two real signals packed in real,imag*/ 81 | kiss_fft( st->substate , (const kiss_fft_cpx*)timedata, st->tmpbuf ); 82 | /* The real part of the DC element of the frequency spectrum in st->tmpbuf 83 | * contains the sum of the even-numbered elements of the input time sequence 84 | * The imag part is the sum of the odd-numbered elements 85 | * 86 | * The sum of tdc.r and tdc.i is the sum of the input time sequence. 87 | * yielding DC of input time sequence 88 | * The difference of tdc.r - tdc.i is the sum of the input (dot product) [1,-1,1,-1... 89 | * yielding Nyquist bin of input time sequence 90 | */ 91 | 92 | tdc.r = st->tmpbuf[0].r; 93 | tdc.i = st->tmpbuf[0].i; 94 | C_FIXDIV(tdc,2); 95 | CHECK_OVERFLOW_OP(tdc.r ,+, tdc.i); 96 | CHECK_OVERFLOW_OP(tdc.r ,-, tdc.i); 97 | freqdata[0].r = tdc.r + tdc.i; 98 | freqdata[ncfft].r = tdc.r - tdc.i; 99 | #ifdef USE_SIMD 100 | freqdata[ncfft].i = freqdata[0].i = _mm_set1_ps(0); 101 | #else 102 | freqdata[ncfft].i = freqdata[0].i = 0; 103 | #endif 104 | 105 | for ( k=1;k <= ncfft/2 ; ++k ) { 106 | fpk = st->tmpbuf[k]; 107 | fpnk.r = st->tmpbuf[ncfft-k].r; 108 | fpnk.i = - st->tmpbuf[ncfft-k].i; 109 | C_FIXDIV(fpk,2); 110 | C_FIXDIV(fpnk,2); 111 | 112 | C_ADD( f1k, fpk , fpnk ); 113 | C_SUB( f2k, fpk , fpnk ); 114 | C_MUL( tw , f2k , st->super_twiddles[k-1]); 115 | 116 | freqdata[k].r = HALF_OF(f1k.r + tw.r); 117 | freqdata[k].i = HALF_OF(f1k.i + tw.i); 118 | freqdata[ncfft-k].r = HALF_OF(f1k.r - tw.r); 119 | freqdata[ncfft-k].i = HALF_OF(tw.i - f1k.i); 120 | } 121 | } 122 | 123 | void kiss_fftri(kiss_fftr_cfg st,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata) 124 | { 125 | /* input buffer timedata is stored row-wise */ 126 | int k, ncfft; 127 | 128 | if (st->substate->inverse == 0) { 129 | fprintf (stderr, "kiss fft usage error: improper alloc\n"); 130 | exit (1); 131 | } 132 | 133 | ncfft = st->substate->nfft; 134 | 135 | st->tmpbuf[0].r = freqdata[0].r + freqdata[ncfft].r; 136 | st->tmpbuf[0].i = freqdata[0].r - freqdata[ncfft].r; 137 | C_FIXDIV(st->tmpbuf[0],2); 138 | 139 | for (k = 1; k <= ncfft / 2; ++k) { 140 | kiss_fft_cpx fk, fnkc, fek, fok, tmp; 141 | fk = freqdata[k]; 142 | fnkc.r = freqdata[ncfft - k].r; 143 | fnkc.i = -freqdata[ncfft - k].i; 144 | C_FIXDIV( fk , 2 ); 145 | C_FIXDIV( fnkc , 2 ); 146 | 147 | C_ADD (fek, fk, fnkc); 148 | C_SUB (tmp, fk, fnkc); 149 | C_MUL (fok, tmp, st->super_twiddles[k-1]); 150 | C_ADD (st->tmpbuf[k], fek, fok); 151 | C_SUB (st->tmpbuf[ncfft - k], fek, fok); 152 | #ifdef USE_SIMD 153 | st->tmpbuf[ncfft - k].i *= _mm_set1_ps(-1.0); 154 | #else 155 | st->tmpbuf[ncfft - k].i *= -1; 156 | #endif 157 | } 158 | kiss_fft (st->substate, st->tmpbuf, (kiss_fft_cpx *) timedata); 159 | } 160 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/kiss_fftr.h: -------------------------------------------------------------------------------- 1 | #ifndef KISS_FTR_H 2 | #define KISS_FTR_H 3 | 4 | #include "kiss_fft.h" 5 | #ifdef __cplusplus 6 | extern "C" { 7 | #endif 8 | 9 | 10 | /* 11 | 12 | Real optimized version can save about 45% cpu time vs. complex fft of a real seq. 13 | 14 | 15 | 16 | */ 17 | 18 | typedef struct kiss_fftr_state *kiss_fftr_cfg; 19 | 20 | 21 | kiss_fftr_cfg kiss_fftr_alloc(int nfft,int inverse_fft,void * mem, size_t * lenmem); 22 | /* 23 | nfft must be even 24 | 25 | If you don't care to allocate space, use mem = lenmem = NULL 26 | */ 27 | 28 | 29 | void kiss_fftr(kiss_fftr_cfg cfg,const kiss_fft_scalar *timedata,kiss_fft_cpx *freqdata); 30 | /* 31 | input timedata has nfft scalar points 32 | output freqdata has nfft/2+1 complex points 33 | */ 34 | 35 | void kiss_fftri(kiss_fftr_cfg cfg,const kiss_fft_cpx *freqdata,kiss_fft_scalar *timedata); 36 | /* 37 | input freqdata has nfft/2+1 complex points 38 | output timedata has nfft scalar points 39 | */ 40 | 41 | #define kiss_fftr_free free 42 | 43 | #ifdef __cplusplus 44 | } 45 | #endif 46 | #endif 47 | -------------------------------------------------------------------------------- /Util/kiss_fft/tools/psdpng.c: -------------------------------------------------------------------------------- 1 | /* 2 | Copyright (c) 2003-2004, Mark Borgerding 3 | 4 | All rights reserved. 5 | 6 | Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 9 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 10 | * Neither the author nor the names of any contributors may be used to endorse or promote products derived from this software without specific prior written permission. 11 | 12 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "kiss_fft.h" 23 | #include "kiss_fftr.h" 24 | 25 | int nfft=1024; 26 | FILE * fin=NULL; 27 | FILE * fout=NULL; 28 | 29 | int navg=20; 30 | int remove_dc=0; 31 | int nrows=0; 32 | float * vals=NULL; 33 | int stereo=0; 34 | 35 | static 36 | void config(int argc,char** argv) 37 | { 38 | while (1) { 39 | int c = getopt (argc, argv, "n:r:as"); 40 | if (c == -1) 41 | break; 42 | switch (c) { 43 | case 'n': nfft=(int)atoi(optarg);break; 44 | case 'r': navg=(int)atoi(optarg);break; 45 | case 'a': remove_dc=1;break; 46 | case 's': stereo=1;break; 47 | case '?': 48 | fprintf (stderr, "usage options:\n" 49 | "\t-n d: fft dimension(s) [1024]\n" 50 | "\t-r d: number of rows to average [20]\n" 51 | "\t-a : remove average from each fft buffer\n" 52 | "\t-s : input is stereo, channels will be combined before fft\n" 53 | "16 bit machine format real input is assumed\n" 54 | ); 55 | default: 56 | fprintf (stderr, "bad %c\n", c); 57 | exit (1); 58 | break; 59 | } 60 | } 61 | if ( optind < argc ) { 62 | if (strcmp("-",argv[optind]) !=0) 63 | fin = fopen(argv[optind],"rb"); 64 | ++optind; 65 | } 66 | 67 | if ( optind < argc ) { 68 | if ( strcmp("-",argv[optind]) !=0 ) 69 | fout = fopen(argv[optind],"wb"); 70 | ++optind; 71 | } 72 | if (fin==NULL) 73 | fin=stdin; 74 | if (fout==NULL) 75 | fout=stdout; 76 | } 77 | 78 | #define CHECKNULL(p) if ( (p)==NULL ) do { fprintf(stderr,"CHECKNULL failed @ %s(%d): %s\n",__FILE__,__LINE__,#p );exit(1);} while(0) 79 | 80 | typedef struct 81 | { 82 | png_byte r; 83 | png_byte g; 84 | png_byte b; 85 | } rgb_t; 86 | 87 | static 88 | void val2rgb(float x,rgb_t *p) 89 | { 90 | const double pi = 3.14159265358979; 91 | p->g = (int)(255*sin(x*pi)); 92 | p->r = (int)(255*abs(sin(x*pi*3/2))); 93 | p->b = (int)(255*abs(sin(x*pi*5/2))); 94 | //fprintf(stderr,"%.2f : %d,%d,%d\n",x,(int)p->r,(int)p->g,(int)p->b); 95 | } 96 | 97 | static 98 | void cpx2pixels(rgb_t * res,const float * fbuf,size_t n) 99 | { 100 | size_t i; 101 | float minval,maxval,valrange; 102 | minval=maxval=fbuf[0]; 103 | 104 | for (i = 0; i < n; ++i) { 105 | if (fbuf[i] > maxval) maxval = fbuf[i]; 106 | if (fbuf[i] < minval) minval = fbuf[i]; 107 | } 108 | 109 | fprintf(stderr,"min ==%f,max=%f\n",minval,maxval); 110 | valrange = maxval-minval; 111 | if (valrange == 0) { 112 | fprintf(stderr,"min == max == %f\n",minval); 113 | exit (1); 114 | } 115 | 116 | for (i = 0; i < n; ++i) 117 | val2rgb( (fbuf[i] - minval)/valrange , res+i ); 118 | } 119 | 120 | static 121 | void transform_signal(void) 122 | { 123 | short *inbuf; 124 | kiss_fftr_cfg cfg=NULL; 125 | kiss_fft_scalar *tbuf; 126 | kiss_fft_cpx *fbuf; 127 | float *mag2buf; 128 | int i; 129 | int n; 130 | int avgctr=0; 131 | 132 | int nfreqs=nfft/2+1; 133 | 134 | CHECKNULL( cfg=kiss_fftr_alloc(nfft,0,0,0) ); 135 | CHECKNULL( inbuf=(short*)malloc(sizeof(short)*2*nfft ) ); 136 | CHECKNULL( tbuf=(kiss_fft_scalar*)malloc(sizeof(kiss_fft_scalar)*nfft ) ); 137 | CHECKNULL( fbuf=(kiss_fft_cpx*)malloc(sizeof(kiss_fft_cpx)*nfreqs ) ); 138 | CHECKNULL( mag2buf=(float*)malloc(sizeof(float)*nfreqs ) ); 139 | 140 | memset(mag2buf,0,sizeof(mag2buf)*nfreqs); 141 | 142 | while (1) { 143 | if (stereo) { 144 | n = fread(inbuf,sizeof(short)*2,nfft,fin); 145 | if (n != nfft ) 146 | break; 147 | for (i=0;i points = PointTrace(image); // 21 | // // 22 | // 5.) Create a QuadTree to store points array in (for much faster // 23 | // search algorithms.) // 24 | // *Recommendation: Use an even number for QuadTree indices. I typically // 25 | // use values between 8 and 16. // 26 | // *Example: // 27 | // QuadTree tree(points, 8); // 28 | // // 29 | // 6.) Pass function SearchPolygons() to filtering surface. 'Samples' // 30 | // describes the number of points used to 'sample' each slice of the QuadTree // 31 | // to search the points array for polygons. Use a higher number for increased // 32 | // accuracy at the expense of performance. // 33 | // *Example: // 34 | // SearchPolygons(tree, index, Vec2i( 8, 2 )); // 35 | //__________________________________________________________________________________________// 36 | 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include "SDL.h" 43 | #include "SDL_image.h" 44 | #include "../Transform.h" 45 | #include "Segment.h" 46 | 47 | using namespace std; 48 | using namespace transform; 49 | 50 | Quad::Quad(){ 51 | pos = CreateVec2i(0, 0); 52 | points.clear(); 53 | points.resize(0); 54 | rect = CreateRect(0, 0, 0, 0); 55 | } 56 | 57 | Quad::Quad(vector data){ 58 | pos = CreateVec2i(0, 0); 59 | points = data; 60 | rect = getRect(); 61 | } 62 | 63 | Quad::Quad(vector data, Vec2i position){ 64 | pos = position; 65 | points = data; 66 | rect = getRect(); 67 | } 68 | 69 | inline SDL_Rect Quad::getRect(){ 70 | Vec2i iter; 71 | int minX = points[0].x, minY = points[0].y, 72 | maxX = minX, maxY = minY; 73 | 74 | for (int a = 0; a < points.size(); a++){ 75 | iter = CreateVec2i(points[a].x, points[a].y); 76 | 77 | if (iter.x < minX) 78 | minX = iter.x; 79 | 80 | if (iter.x > maxX) 81 | maxX = iter.x; 82 | 83 | if (iter.y < minY) 84 | minY = iter.y; 85 | 86 | if (iter.y > maxY) 87 | maxY = iter.y; 88 | } 89 | 90 | return CreateRect(minX, minY, maxX, maxY); 91 | } 92 | 93 | QuadTree::QuadTree(){ 94 | quads.clear(); 95 | quads.resize(0); 96 | } 97 | 98 | QuadTree::QuadTree(vector data){ 99 | if (data.size() < 4) data.resize(4); 100 | 101 | int step = data.size() / 4; 102 | Quad temp; 103 | 104 | for (int a = 0; a < 4; a++){ 105 | //Clear temporary Quad array 106 | temp.points.clear(); temp.points.resize(0); 107 | 108 | //Add point data per step size 109 | for (int b = 0; b < step; b++){ 110 | int id = b + (step*a); 111 | 112 | if (data.size() > id) 113 | temp.points.push_back(data[b + (step*a)]); 114 | } 115 | 116 | //Update temporary Quad index 117 | temp.pos = CreateVec2i(a, 0); 118 | temp.getRect(); 119 | quads.push_back(temp); 120 | } 121 | 122 | rect = getRect(); 123 | 124 | for (int b = 0; b < quads.size(); b++){ 125 | quads[b].rect = quads[b].getRect(); 126 | } 127 | } 128 | 129 | QuadTree::QuadTree(vector data, int indices){ 130 | if (data.size() < indices) data.resize(indices); 131 | 132 | int step = data.size() / indices; 133 | Quad temp; 134 | 135 | for (int a = 0; a < indices; a++){ 136 | //Clear temporary Quad array 137 | temp.points.clear(); temp.points.resize(0); 138 | 139 | //Add point data per step size 140 | for (int b = 0; b < step; b++){ 141 | int id = b + (step*a); 142 | 143 | if (data.size() > id) 144 | temp.points.push_back(data[id]); 145 | } 146 | 147 | //Update temporary Quad index 148 | temp.pos = CreateVec2i(a, 0); 149 | quads.push_back(temp); 150 | } 151 | 152 | rect = getRect(); 153 | 154 | for (int b = 0; b < quads.size(); b++){ 155 | quads[b].rect = quads[b].getRect(); 156 | } 157 | } 158 | 159 | inline vector* QuadTree::getPoints(int index){ 160 | if (index < quads.size()) 161 | return &quads[index].points; 162 | else 163 | return NULL; 164 | } 165 | 166 | inline int QuadTree::getSize(){ 167 | int sum; 168 | 169 | for (int a = 0; a < quads.size(); a++){ 170 | sum += quads[a].points.size(); 171 | } 172 | 173 | return sum; 174 | } 175 | 176 | inline int QuadTree::getSize(int index){ 177 | if (index < quads.size()) 178 | return quads[index].points.size(); 179 | else 180 | return 0; 181 | } 182 | 183 | inline int QuadTree::getX(int index, int item){ 184 | if (index < quads.size()){ 185 | if (item < quads[index].points.size()) 186 | return quads[index].points[item].x; 187 | else{ 188 | cout << "Failed to return X value for item " << item << " at index " << index << endl; 189 | return 0; 190 | } 191 | } 192 | else{ 193 | cout << "Failed to return X value at index " << index << endl; 194 | return 0; 195 | } 196 | } 197 | 198 | inline int QuadTree::getY(int index, int item){ 199 | if (index < quads.size()){ 200 | if (item < quads[index].points.size()) 201 | return quads[index].points[item].y; 202 | else{ 203 | cout << "Failed to return Y value for item " << item << " at index " << index << endl; 204 | return 0; 205 | } 206 | } 207 | else{ 208 | cout << "Failed to return Y value at index " << index << endl; 209 | return 0; 210 | } 211 | } 212 | 213 | inline Vec2i QuadTree::getVec2i(int index, int item){ 214 | if (index < quads.size()){ 215 | if (item < quads[index].points.size()) 216 | return quads[index].points[item]; 217 | else{ 218 | cout << "Failed to return item " << item << " at index " << index << endl; 219 | return Vec2i(0, 0); 220 | } 221 | } 222 | else{ 223 | cout << "Failed to return item at index " << index << endl; 224 | return Vec2i(0, 0); 225 | } 226 | } 227 | 228 | inline SDL_Rect QuadTree::getRect(){ 229 | Vec2i iter; 230 | int minX = getX(0, 0), minY = getY(0, 0), 231 | maxX = minX, maxY = minY; 232 | 233 | for (int index = 0; index < quads.size(); index++){ 234 | for (int a = 0; a < getSize(index); a++){ 235 | iter = CreateVec2i(getX(index, a), getY(index, a)); 236 | 237 | if (iter.x < minX) 238 | minX = iter.x; 239 | 240 | if (iter.x > maxX) 241 | maxX = iter.x; 242 | 243 | if (iter.y < minY) 244 | minY = iter.y; 245 | 246 | if (iter.y > maxY) 247 | maxY = iter.y; 248 | } 249 | } 250 | 251 | return CreateRect(minX, minY, maxX, maxY); 252 | } 253 | 254 | inline SDL_Rect QuadTree::getRect(int index){ 255 | Vec2i iter; 256 | int minX = getX(index, 0), minY = getY(index, 0), 257 | maxX = minX, maxY = minY; 258 | 259 | for (int a = 0; a < getSize(index); a++){ 260 | iter = CreateVec2i(getX(index, a), getY(index, a)); 261 | 262 | if (iter.x < minX) 263 | minX = iter.x; 264 | 265 | if (iter.x > maxX) 266 | maxX = iter.x; 267 | 268 | if (iter.y < minY) 269 | minY = iter.y; 270 | 271 | if (iter.y > maxY) 272 | maxY = iter.y; 273 | } 274 | 275 | return CreateRect(minX, minY, maxX, maxY); 276 | } 277 | 278 | Segmented::Segmented(){ 279 | surface = EmptySurface(64, 64); 280 | isValid = false; 281 | } 282 | 283 | Segmented::Segmented(SDL_Surface* source){ 284 | surface = CopySurface(source); 285 | 286 | //Grayscale(surface, GRAY_AVERAGE); 287 | //Threshold(surface, CHANNEL_RED, 150); 288 | 289 | isValid = true; 290 | } 291 | 292 | Segmented::Segmented(SDL_Surface* source, Uint8 threshold){ 293 | surface = CopySurface(source); 294 | 295 | Grayscale(surface, GRAY_AVERAGE); 296 | Threshold(surface, CHANNEL_RED, threshold); 297 | 298 | isValid = true; 299 | } 300 | 301 | vector RotateData(vector points){ 302 | 303 | vector out; 304 | return out; 305 | } 306 | 307 | inline SDL_Rect FindRect(vector points){ 308 | Vec2i iter; 309 | int minX = points[0].x, minY = points[0].y, 310 | maxX = minX, maxY = minY; 311 | 312 | for (int a = 0; a < points.size(); a++){ 313 | iter = CreateVec2i(points[0].x, points[0].y); 314 | 315 | if (iter.x < minX) 316 | minX = iter.x; 317 | 318 | if (iter.x > maxX) 319 | maxX = iter.x; 320 | 321 | if (iter.y < minY) 322 | minY = iter.y; 323 | 324 | if (iter.y > maxY) 325 | maxY = iter.y; 326 | } 327 | 328 | return CreateRect(minX, minY, maxX, maxY); 329 | } 330 | 331 | inline int FindIndex(QuadTree tree, int y){ 332 | int index = -1; 333 | 334 | for (int b = 0; b < tree.quads.size(); b++){ 335 | if (y >= tree.quads[b].rect.y && y <= tree.quads[b].rect.h){ 336 | index = b; 337 | break; 338 | } 339 | } 340 | 341 | return index; 342 | } 343 | 344 | inline bool PointExists(vector points, Vec2i point, bool useQuad){ 345 | if (useQuad != false){ 346 | int segments = 8; 347 | 348 | if (points.size() < segments) points.resize(segments); 349 | QuadTree sub(points, segments); 350 | 351 | int index = -1; 352 | 353 | for (int b = 0; b < sub.quads.size(); b++){ 354 | if (point.y >= sub.quads[b].rect.y && point.y <= sub.quads[b].rect.h){ 355 | index = b; 356 | break; 357 | } 358 | } 359 | 360 | if (index > -1){ 361 | for (int a = 0; a < sub.quads[index].points.size(); a++){ 362 | if (sub.getX(index, a) == point.x && sub.getY(index, a) == point.y){ 363 | return true; 364 | break; 365 | } 366 | } 367 | return false; 368 | } 369 | else 370 | return false; 371 | } 372 | else{ 373 | for (int a = 0; a < points.size(); a++){ 374 | if (point.x == points[a].x && point.y == points[a].y){ 375 | return true; 376 | break; 377 | } 378 | } 379 | return false; 380 | } 381 | } 382 | 383 | inline bool PointExists(QuadTree tree, Vec2i point){ 384 | int index = -1; 385 | 386 | for (int b = 0; b < tree.quads.size(); b++){ 387 | if (point.y >= tree.quads[b].rect.y && point.y <= tree.quads[b].rect.h){ 388 | index = b; 389 | break; 390 | } 391 | } 392 | 393 | if (index > -1){ 394 | for (int a = 0; a < tree.quads[index].points.size(); a++){ 395 | if (tree.getX(index, a) == point.x && tree.getY(index, a) == point.y){ 396 | return true; 397 | } 398 | } 399 | return false; 400 | } 401 | else 402 | return false; 403 | } 404 | 405 | inline bool PointExists(QuadTree tree, Vec2i point, int* id){ 406 | int index = -1; 407 | 408 | for (int b = 0; b < tree.quads.size(); b++){ 409 | if (point.y >= tree.quads[b].rect.y && point.y <= tree.quads[b].rect.h){ 410 | index = b; 411 | break; 412 | } 413 | } 414 | 415 | if (index > -1){ 416 | for (int a = 0; a < tree.quads[index].points.size(); a++){ 417 | if (tree.getX(index, a) == point.x && tree.getY(index, a) == point.y){ 418 | *id = index; 419 | return true; 420 | } 421 | } 422 | return false; 423 | } 424 | else 425 | return false; 426 | } 427 | 428 | inline bool PointInRect(SDL_Rect rect, Vec2i pt){ 429 | //Notes: I might not need to add rect.x to rect.w & rect.y to rect.h 430 | //Need to test more. 431 | 432 | if (pt.x >= rect.x && pt.y >= rect.y && 433 | pt.x <= rect.w && pt.y <= rect.h) 434 | return true; 435 | else 436 | return false; 437 | } 438 | 439 | inline bool PointInPoly(vector points, Vec2i point, SDL_Rect boundary){ 440 | int count = 0; 441 | 442 | for (int x = point.x; x < boundary.x + boundary.w; x++){ 443 | if (PointExists(points, CreateVec2i(point.x + x, point.y), true) != false){ 444 | count++; 445 | } 446 | } 447 | 448 | if (count % 2 == 0) 449 | return false; 450 | else 451 | return true; 452 | } 453 | 454 | inline bool PointInPoly(QuadTree tree, Vec2i point){ 455 | int count = 0; int id = FindIndex(tree, point.y); 456 | 457 | if (id > -1){ 458 | QuadTree sub(tree.quads[id].points, 8); 459 | id = FindIndex(sub, point.y); 460 | 461 | for (int x = point.x; x < tree.rect.w; x++){ 462 | if (PointExists(sub.quads[id].points, CreateVec2i(point.x + x, point.y), true) != false){ 463 | count++; 464 | } 465 | } 466 | 467 | if (count % 2 == 0) 468 | return false; 469 | else 470 | return true; 471 | } 472 | else 473 | return false; 474 | } 475 | 476 | void MapPNG(string filename, vector points){ 477 | 478 | } 479 | 480 | void MapPNG(string filename, QuadTree tree){ 481 | 482 | } 483 | 484 | void MapPNG(string filename, QuadTree qt, int index){ 485 | SDL_Rect rect = qt.getRect(index); 486 | SDL_Surface* temp = EmptySurface(rect.w - rect.x, rect.h - rect.y); 487 | 488 | for (int a = 0; a < qt.getSize(index); a++){ 489 | if (hasPoint(temp, CreateVec2i(qt.getX(index,a), qt.getY(index,a))) != false) 490 | SetPixel(temp, qt.getX(index, a), qt.getY(index, a), SDL_MapRGBA(temp->format, 255, 255, 255, 255)); 491 | } 492 | 493 | IMG_SavePNG(temp, filename.c_str()); 494 | } 495 | 496 | vector PointTrace(SDL_Surface* surface){ 497 | vector points; 498 | points.reserve(int(surface->w * surface->h)); 499 | 500 | for (int y = 0; y < surface->h; y++){ 501 | for (int x = 0; x < surface->w; x++){ 502 | UMatrix3x3 neighbors; 503 | 504 | //Fill matrix with local color values 505 | for (int col = 0; col < 3; col++){ 506 | for (int row = 0; row < 3; row++){ 507 | int locX = x - 1 + row; 508 | int locY = y - 1 + col; 509 | 510 | Uint32 color; Uint8 g, b; 511 | 512 | if (locX < 0 || locX > surface->w - 1 || locY < 0 || locY > surface->h - 1) 513 | color = SDL_MapRGBA(surface->format, 1, 1, 1, 255); 514 | else 515 | color = GetPixel(surface, locX, locY); 516 | 517 | SDL_GetRGB(color, surface->format, &neighbors.val[col][row], &g, &b); 518 | } 519 | } 520 | 521 | //Check center value. If pixel isn't black, 522 | if (neighbors.val[1][1] != 0){ 523 | //Check pixel values in all directions. If any neighbors are black, fill [1][1] with color 524 | if (int(neighbors.val[0][1]) == 0 || neighbors.val[1][0] == 0 || neighbors.val[1][2] == 0 || neighbors.val[2][1] == 0){ 525 | points.push_back(CreateVec2i(x,y)); 526 | } 527 | } 528 | } 529 | } 530 | 531 | return points; 532 | } 533 | 534 | void SearchPolygons(QuadTree tree, int index, Vec2i samples){ 535 | SDL_Surface* temp = EmptySurface(tree.rect.w, tree.rect.h); 536 | SDL_Rect rect = tree.quads[index].rect; 537 | 538 | int w = rect.w - rect.x; 539 | int h = rect.h - rect.y; 540 | 541 | double dx = w / samples.x; 542 | double dy = h / samples.y; 543 | 544 | Vec2i loc; 545 | 546 | for (int y = 0; y < samples.y; y++){ 547 | for (int x = 0; x < samples.x; x++){ 548 | loc = CreateVec2i(rect.x + int(x * dx), rect.y + int(y * dy)); 549 | 550 | if (PointInPoly(tree.quads[index].points, loc) != false){ 551 | SetPixel(temp, loc.x, loc.y, SDL_MapRGBA(temp->format, 0, 0, 255, 255)); 552 | } 553 | else 554 | SetPixel(temp, loc.x, loc.y, SDL_MapRGBA(temp->format, 255, 0, 0, 255)); 555 | } 556 | } 557 | 558 | IMG_SavePNG(temp, "polygons.png"); 559 | } 560 | 561 | 562 | 563 | 564 | //======================================================================================================================// 565 | // Current bugs, fixes, and feature ideas: // 566 | //----------------------------------------------------------------------------------------------------------------------// 567 | // Tasks: // 568 | // # To ensure highest accuracy, QuadTree input points in constructor should be divisible by total step size. // 569 | // Implement a strategy to avoid losing data. // 570 | // # 571 | //______________________________________________________________________________________________________________________// -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include "SDL.h" 4 | #include "SDL_image.h" 5 | #include "SDL_ttf.h" 6 | #include "Transform.h" 7 | //#include "GUI.h" 8 | 9 | using namespace std; 10 | using namespace transform; 11 | 12 | const int SCREEN_WID = 1280; 13 | const int SCREEN_HEI = 960; 14 | const int WINDOW_WID = SCREEN_WID; 15 | const int WINDOW_HEI = SCREEN_HEI; 16 | 17 | int main(int argc, char* argv[]){ 18 | if (SDL_Init(SDL_INIT_EVERYTHING) >0){ 19 | cout << "Error: SDL failed to initialize." << endl; 20 | return 0; 21 | } 22 | 23 | SDL_Window* window = SDL_CreateWindow("SDL Image Manipulation", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WINDOW_WID, WINDOW_HEI, NULL); 24 | SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 25 | SDL_Event event; 26 | 27 | //Load and modify image 28 | SDL_Surface* image = IMG_Load("Resources/test.jpg"); //Load source image 29 | SDL_Surface* filtered = EmptySurface(image); //Copy source image's pixel format, pixel data, and dimensions 30 | SDL_Surface* bg = EmptySurface(image, SCREEN_WID, SCREEN_HEI); 31 | 32 | //Generate background 33 | Fill(bg, CreateColor(50,50,50,255)); 34 | 35 | SDL_Texture* tImage = SDL_CreateTextureFromSurface(renderer, image); 36 | SDL_Texture* tPost = SDL_CreateTextureFromSurface(renderer, filtered); 37 | SDL_Texture* back = SDL_CreateTextureFromSurface(renderer, bg); 38 | 39 | //Clean Up 40 | SDL_FreeSurface(image); 41 | SDL_FreeSurface(filtered); 42 | 43 | //Main loop 44 | bool quit = false; 45 | 46 | while (quit != true){ 47 | while (SDL_PollEvent(&event) != 0){ 48 | switch (event.key.keysym.sym){ 49 | case SDLK_ESCAPE: 50 | quit = true; 51 | break; 52 | } 53 | } 54 | 55 | //Render media 56 | SDL_RenderClear(renderer); 57 | SDL_RenderCopy(renderer, back, NULL, NULL); 58 | SDL_RenderCopy(renderer, tImage, NULL, &CreateRect(0,SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2)); 59 | SDL_RenderCopy(renderer, tPost, NULL, &CreateRect(SCREEN_WID/2, SCREEN_HEI/4, SCREEN_WID/2, SCREEN_HEI/2)); 60 | SDL_RenderPresent(renderer); 61 | } 62 | 63 | //Clean up 64 | SDL_Quit(); 65 | 66 | return 0; 67 | } --------------------------------------------------------------------------------