├── .gitignore ├── assets ├── .DS_Store ├── back.png ├── down.png ├── front.png ├── left.png ├── right.png └── top.png ├── Makefile ├── LICENSE ├── example ├── example.cpp └── include │ ├── LICENSE │ ├── stb_image_write.h │ └── std_image_resize.h ├── README.md └── src └── converter.hpp /.gitignore: -------------------------------------------------------------------------------- 1 | example/example 2 | out -------------------------------------------------------------------------------- /assets/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/.DS_Store -------------------------------------------------------------------------------- /assets/back.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/back.png -------------------------------------------------------------------------------- /assets/down.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/down.png -------------------------------------------------------------------------------- /assets/front.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/front.png -------------------------------------------------------------------------------- /assets/left.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/left.png -------------------------------------------------------------------------------- /assets/right.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/right.png -------------------------------------------------------------------------------- /assets/top.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chinhsuanwu/360-converter/HEAD/assets/top.png -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | OUTPUT_DIR = out 2 | 3 | all: 4 | @clang++ example/example.cpp -o example/example 5 | 6 | run: 7 | @if [ ! -d $(OUTPUT_DIR) ]; then mkdir $(OUTPUT_DIR); fi 8 | @./example/example assets/front.png assets/right.png assets/back.png assets/left.png assets/top.png assets/down.png 9 | 10 | .PHONY: clean 11 | clean: 12 | @rm example/example 13 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2020 Chin-Hsuan Wu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /example/example.cpp: -------------------------------------------------------------------------------- 1 | #define STB_IMAGE_IMPLEMENTATION 2 | #include "include/stb_image.h" 3 | 4 | #define STB_IMAGE_WRITE_IMPLEMENTATION 5 | #include "include/stb_image_write.h" 6 | 7 | #define CONVERTER_IMPLEMENTATION 8 | #include "../src/converter.hpp" 9 | 10 | #ifndef CHANNEL_NUM 11 | #define CHANNEL_NUM 4 12 | #endif 13 | 14 | int main(int argc, char **argv) 15 | { 16 | Converter::Image img; 17 | Converter::Image faces[Converter::FACE_NUM]; 18 | 19 | int w, h, bpp; 20 | for (int i = 0; i < Converter::FACE_NUM; ++i) 21 | { 22 | faces[i].img = stbi_load(argv[i + 1], &w, &h, &bpp, CHANNEL_NUM); 23 | faces[i].h = h, faces[i].w = w; 24 | } 25 | 26 | Converter::Face face = Converter::Face(faces); 27 | 28 | img = face.getFace(Converter::FRONT); 29 | stbi_write_png("out/front.png", img.w, img.h, CHANNEL_NUM, img.img, img.w * CHANNEL_NUM); 30 | 31 | Converter::Cube cube = face.toCube(); 32 | img = cube.getCubeMap(); 33 | stbi_write_png("out/cubemap.png", img.w, img.h, CHANNEL_NUM, img.img, img.w * CHANNEL_NUM); 34 | 35 | Converter::Equi equi = face.toEqui(); 36 | img = equi.getEqui(); 37 | stbi_write_png("out/equi.png", img.w, img.h, CHANNEL_NUM, img.img, img.w * CHANNEL_NUM); 38 | 39 | img = equi.toStereo().getStereo(); 40 | stbi_write_png("out/stereo.png", img.w, img.h, CHANNEL_NUM, img.img, img.w * CHANNEL_NUM); 41 | 42 | img = equi.toStereo(Converter::TOP).toEqui().getEqui(); 43 | stbi_write_png("out/equi.png", img.w, img.h, CHANNEL_NUM, img.img, img.w * CHANNEL_NUM); 44 | 45 | 46 | return 0; 47 | } -------------------------------------------------------------------------------- /example/include/LICENSE: -------------------------------------------------------------------------------- 1 | This software is available under 2 licenses -- choose whichever you prefer. 2 | ------------------------------------------------------------------------------ 3 | ALTERNATIVE A - MIT License 4 | Copyright (c) 2017 Sean Barrett 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 9 | of the Software, and to permit persons to whom the Software is furnished to do 10 | so, subject to the following conditions: 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | ------------------------------------------------------------------------------ 21 | ALTERNATIVE B - Public Domain (www.unlicense.org) 22 | This is free and unencumbered software released into the public domain. 23 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 24 | software, either in source code form or as a compiled binary, for any purpose, 25 | commercial or non-commercial, and by any means. 26 | In jurisdictions that recognize copyright laws, the author or authors of this 27 | software dedicate any and all copyright interest in the software to the public 28 | domain. We make this dedication for the benefit of the public at large and to 29 | the detriment of our heirs and successors. We intend this dedication to be an 30 | overt act of relinquishment in perpetuity of all present and future rights to 31 | this software under copyright law. 32 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 33 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 34 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 35 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 36 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 37 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 360-converter 2 | 3 | ## Overview 4 | Input one of the following type 360 image and convert it to another one. 5 | | Faces | Cubemap | Equirectangular | Stereographic | 6 | | :---: | :---: | :---:| :---:| 7 | | ![](https://user-images.githubusercontent.com/67839539/105892418-cdb67580-604c-11eb-95b9-877cafa2dfca.png) | ![](https://user-images.githubusercontent.com/67839539/105892517-efaff800-604c-11eb-8963-0a4ea77752ac.png) | ![](https://user-images.githubusercontent.com/67839539/105892651-1c640f80-604d-11eb-9d1d-d01e9ff5b02b.png) | ![](https://user-images.githubusercontent.com/67839539/105892704-2f76df80-604d-11eb-99f2-0f9dd3a20a22.png) | 8 | 9 | ## Prerequisites 10 | This project has been tested on: 11 | - Ubuntu 18.04 12 | - G++ 7.5.0 13 | - Clang 6.0.0 14 | 15 | The only thing you need is a C/C++ compiler, no other package do you need to install. 16 | 17 | ## Run example 18 | ``` 19 | git clone https://github.com/chinhsuanwu/360-converter.git 20 | cd 360-converter 21 | make 22 | make run 23 | ``` 24 | 25 | ## Usage 26 | Just directly include the [`converter.hpp`](https://github.com/chinhsuanwu/360-converter/blob/master/src/converter.hpp) in folder `src`. 27 | ```c++ 28 | #include "src/converter.hpp" 29 | ``` 30 | For example, the conversion from `Face` to `Cube`, `Equi` and `Stereo` can be shown as below, other conversions can be deduced: 31 |

32 | 33 | - To convert from `Face` to `Equi` 34 | ```c++ 35 | Converter::Equi equi = face.toEqui(); 36 | ``` 37 | - To get image 38 | ```c++ 39 | Converter::Image img = equi.getEqui(); 40 | ``` 41 | - For `Stereo` images, you can assign `TOP` or `DOWN`, while the default will be `DOWN` 42 | ```c++ 43 | Converter::Image img = cube.toStereo(Converter::TOP).getStereo(); 44 | ``` 45 | | `TOP` | `DOWN` | 46 | | :---: | :---: | 47 | |

|

| 48 | 49 | - To load image 50 | ```c++ 51 | img.img = stbi_load("assets/equi.png", &w, &h, &bpp, CHANNEL_NUM); 52 | img.w = w, img.h = h; 53 | ``` 54 | - To write image 55 | ```c++ 56 | stbi_write_png("out/equi.png", img.w, img.h, CHANNEL_NUM, img.img, img.w*CHANNEL_NUM); 57 | ``` 58 | 59 | Find out more at [example.cpp](https://github.com/chinhsuanwu/360-converter/blob/master/example/example.cpp). 60 | 61 | ## Acknowledgement 62 | This project is using [stb](https://github.com/nothings/stb) library for image I/O in [example.cpp](https://github.com/chinhsuanwu/360-converter/blob/master/example/example.cpp), great thanks to their excellent work. -------------------------------------------------------------------------------- /src/converter.hpp: -------------------------------------------------------------------------------- 1 | #ifndef CONVERTER_HPP 2 | #define CONVERTER_HPP 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | #ifndef CHANNEL_NUM 9 | #define CHANNEL_NUM 4 10 | #endif 11 | 12 | namespace Converter 13 | { 14 | enum FaceID 15 | { 16 | FRONT = 0, 17 | RIGHT, 18 | BACK, 19 | LEFT, 20 | TOP, 21 | DOWN, 22 | FACE_NUM 23 | }; 24 | 25 | struct Coord 26 | { 27 | FaceID face; // the face of the face 28 | double x; // the x Coordinate 29 | double y; // the y Coordinate 30 | }; 31 | 32 | struct Image 33 | { 34 | unsigned int h; 35 | unsigned int w; 36 | uint8_t *img; 37 | }; 38 | 39 | class Converter 40 | { 41 | public: 42 | Converter() 43 | { 44 | FE_map = FS_map = NULL; 45 | return; 46 | } 47 | 48 | inline bool isInRange(const double l, const double theta, const double u) 49 | { 50 | return (theta >= l && theta < u); 51 | } 52 | 53 | const FaceID getFaceID(const double theta, const double phi) 54 | { 55 | FaceID faceID; 56 | double n_theta; 57 | 58 | if (isInRange(-M_PI_4, theta, M_PI_4)) 59 | { 60 | faceID = FRONT; 61 | n_theta = theta; 62 | } 63 | else if (isInRange(M_PI_4, theta, M_PI_2 + M_PI_4)) 64 | { 65 | faceID = RIGHT; 66 | n_theta = theta - M_PI_2; 67 | } 68 | else if (isInRange(-(M_PI_2 + M_PI_4), theta, -M_PI_4)) 69 | { 70 | faceID = LEFT; 71 | n_theta = theta + M_PI_2; 72 | } 73 | else 74 | { 75 | faceID = BACK; 76 | n_theta = (theta > 0.0 ? theta - M_PI : theta + M_PI); 77 | } 78 | 79 | double phiThr = atan2(1.0, 1.0 / cos(n_theta)); 80 | if (phi > phiThr) 81 | { 82 | faceID = DOWN; 83 | } 84 | else if (phi < -phiThr) 85 | { 86 | faceID = TOP; 87 | } 88 | return faceID; 89 | } 90 | 91 | void transform(const double axis, const double px, const double py, const double radian, double *coord_x, double *coord_y) 92 | { 93 | double radius = (double)face_h / 2.0; 94 | double ratio = radius / axis; 95 | 96 | *coord_x = ratio * px; 97 | *coord_y = ratio * py; 98 | 99 | // Rotation 100 | double tmp = *coord_x; 101 | *coord_x = *coord_x * cos(radian) - *coord_y * sin(radian); 102 | *coord_y = tmp * sin(radian) + *coord_y * cos(radian); 103 | 104 | // Translation 105 | *coord_x += radius; 106 | *coord_y += radius; 107 | 108 | return; 109 | } 110 | 111 | void setFEMap(Coord *t_FE_map = NULL) 112 | { 113 | if (t_FE_map) 114 | { 115 | FE_map = t_FE_map; 116 | return; 117 | } 118 | 119 | assert(face_h == face_w); 120 | equi_h = face_h * 2; 121 | equi_w = face_w * 4; 122 | 123 | FE_map = new Coord[equi_h * equi_w]; 124 | 125 | unsigned int pix = 0; 126 | for (int i = 0; i < equi_h; ++i) 127 | { 128 | for (int j = 0; j < equi_w; ++j) 129 | { 130 | double u = ((2.0 * j) / equi_w - 1.0); 131 | double v = ((2.0 * i) / equi_h - 1.0); 132 | double theta = u * M_PI, phi = v * M_PI_2; 133 | double x = cos(phi) * cos(theta), y = sin(phi), z = cos(phi) * sin(theta); 134 | 135 | FaceID faceID = getFaceID(theta, phi); 136 | 137 | double coord_x, coord_y; 138 | switch (faceID) 139 | { 140 | case FRONT: 141 | transform(x, z, y, 0.0, &coord_x, &coord_y); 142 | break; 143 | case RIGHT: 144 | transform(z, y, x, M_PI_2, &coord_x, &coord_y); 145 | break; 146 | case BACK: 147 | transform(x, y, z, -M_PI_2, &coord_x, &coord_y); 148 | break; 149 | case LEFT: 150 | transform(z, x, y, M_PI, &coord_x, &coord_y); 151 | break; 152 | case TOP: 153 | transform(y, z, x, M_PI, &coord_x, &coord_y); 154 | break; 155 | case DOWN: 156 | transform(y, x, z, -M_PI_2, &coord_x, &coord_y); 157 | break; 158 | default: 159 | break; 160 | } 161 | 162 | FE_map[pix].face = faceID; 163 | FE_map[pix].x = coord_x; 164 | FE_map[pix++].y = coord_y; 165 | } 166 | } 167 | return; 168 | } 169 | 170 | Coord getFECoord(int i, int j) 171 | { 172 | return FE_map[i * equi_w + j]; 173 | } 174 | 175 | Coord *getFEMap() 176 | { 177 | return FE_map; 178 | } 179 | 180 | void cart2pol(const int x, const int y, double &rho, double &theta) 181 | { 182 | const double deg = 180.0 / M_PI; 183 | rho = sqrt(x * x + y * y); 184 | theta = atan2(y, x) * deg; 185 | 186 | return; 187 | } 188 | 189 | void pol2cart(int &x, int &y, double rho, double theta) 190 | { 191 | const double deg = 180.0 / M_PI; 192 | x = rho * cos(theta / deg); 193 | y = rho * sin(theta / deg); 194 | return; 195 | } 196 | 197 | void setFSMap(Coord *t_FS_map = NULL) 198 | { 199 | if (!FE_map) 200 | setFEMap(); 201 | 202 | stereo_h = equi_h; 203 | stereo_w = equi_w / 2; 204 | assert(stereo_w == stereo_h); 205 | 206 | FS_map = new Coord[stereo_h * stereo_w]; 207 | 208 | unsigned int org_x = stereo_w / 2; 209 | unsigned int org_y = stereo_h / 2; 210 | 211 | unsigned int pix = 0; 212 | for (int i = 0; i < stereo_h; ++i) 213 | { 214 | for (int j = 0; j < stereo_w; ++j) 215 | { 216 | double rho, theta; 217 | cart2pol(j - org_x, i - org_y, rho, theta); 218 | 219 | if (rho <= stereo_w / 2) 220 | { 221 | Coord coord = getFECoord(int(rho * 2.0), int(theta / 360.0 * equi_w)); 222 | FS_map[pix].face = coord.face; 223 | FS_map[pix].x = coord.x; 224 | FS_map[pix++].y = coord.y; 225 | } 226 | } 227 | } 228 | return; 229 | } 230 | 231 | Coord getFSCoord(int i, int j) 232 | { 233 | return FS_map[i * stereo_w + j]; 234 | } 235 | 236 | Coord *getFSMap() 237 | { 238 | return FS_map; 239 | } 240 | 241 | template 242 | void swap(T &a, T &b) 243 | { 244 | T c(a); 245 | a = b; 246 | b = c; 247 | } 248 | 249 | Image rotate(Image t_img, double theta = M_PI_2) 250 | { 251 | Image img = (Image){((theta == M_PI_2 || theta == M_PI + M_PI_2) ? t_img.w : t_img.h), 252 | ((theta == M_PI_2 || theta == M_PI + M_PI_2) ? t_img.h : t_img.w), 253 | new uint8_t[t_img.w * t_img.h * CHANNEL_NUM]}; 254 | 255 | for (int i = 0; i < t_img.h; ++i) 256 | { 257 | for (int j = 0; j < t_img.w; ++j) 258 | { 259 | double x = j * cos(theta) - i * sin(theta); 260 | double y = j * sin(theta) + i * cos(theta); 261 | 262 | if (x < 0.0) 263 | x += img.w; 264 | if (y < 0.0) 265 | y += img.h; 266 | 267 | for (int k = 0; k < CHANNEL_NUM; ++k) 268 | { 269 | img.img[CHANNEL_NUM * int(y * img.w + x) + k] = t_img.img[CHANNEL_NUM * (i * t_img.w + j) + k]; 270 | } 271 | } 272 | } 273 | return img; 274 | } 275 | 276 | protected: 277 | unsigned int face_h, face_w; 278 | unsigned int cube_h, cube_w; 279 | unsigned int equi_h, equi_w; 280 | unsigned int stereo_h, stereo_w; 281 | Coord *FE_map, *FS_map; 282 | }; 283 | 284 | class Face; 285 | class Cube; 286 | class Equi; 287 | class Stereo; 288 | 289 | class Face : public Converter 290 | { 291 | public: 292 | Face(Image t_faces[FACE_NUM]) 293 | { 294 | for (int i = 0; i < FACE_NUM; ++i) 295 | { 296 | assert(t_faces[i].h == t_faces[i].w); 297 | faces[i] = t_faces[i]; 298 | } 299 | 300 | face_h = faces[0].h; 301 | face_w = faces[0].w; 302 | }; 303 | 304 | Image getFace(int faceID) 305 | { 306 | return faces[faceID]; 307 | } 308 | 309 | void copy(Image src, Image dst, unsigned int start_h, unsigned int start_w) 310 | { 311 | for (unsigned int i = start_h; i < start_h + src.h; ++i) 312 | for (unsigned int j = start_w; j < start_w + src.w; ++j) 313 | for (int k = 0; k < CHANNEL_NUM; ++k) 314 | dst.img[CHANNEL_NUM * (i * dst.w + j) + k] = src.img[CHANNEL_NUM * ((i - start_h) * src.w + (j - start_w)) + k]; 315 | } 316 | 317 | Cube toCube(); 318 | Equi toEqui(); 319 | 320 | private: 321 | Image faces[FACE_NUM]; 322 | }; 323 | 324 | class Cube : public Converter 325 | { 326 | public: 327 | Cube(Image t_cubemap) : cubemap(t_cubemap) 328 | { 329 | cube_h = cubemap.h; 330 | cube_w = cubemap.w; 331 | }; 332 | 333 | Image getCubeMap() 334 | { 335 | return cubemap; 336 | } 337 | 338 | void crop(Image src, Image dst, unsigned int start_h, unsigned int start_w) 339 | { 340 | for (unsigned int i = start_h; i < start_h + dst.h; ++i) 341 | for (unsigned int j = start_w; j < start_w + dst.w; ++j) 342 | for (int k = 0; k < CHANNEL_NUM; ++k) 343 | dst.img[CHANNEL_NUM * ((i - start_h) * dst.w + (j - start_w)) + k] = src.img[CHANNEL_NUM * (i * src.w + j) + k]; 344 | } 345 | 346 | Face toFace(); 347 | Equi toEqui(); 348 | 349 | private: 350 | Image cubemap; 351 | }; 352 | 353 | class Equi : public Converter 354 | { 355 | public: 356 | Equi(Image t_equi) : equi(t_equi) 357 | { 358 | equi_h = equi.h; 359 | equi_w = equi.w; 360 | }; 361 | 362 | Image getEqui() 363 | { 364 | return equi; 365 | } 366 | 367 | Face toFace(); 368 | Cube toCube(); 369 | Stereo toStereo(FaceID faceID); 370 | 371 | private: 372 | Image equi; 373 | }; 374 | 375 | class Stereo : public Converter 376 | { 377 | public: 378 | Stereo(Image t_stereo, FaceID t_faceID) : stereo(t_stereo), faceID(t_faceID) 379 | { 380 | stereo_h = stereo.h; 381 | stereo_w = stereo.w; 382 | }; 383 | 384 | Image getStereo() 385 | { 386 | return stereo; 387 | } 388 | 389 | Equi toEqui(); 390 | 391 | private: 392 | FaceID faceID; 393 | Image stereo; 394 | }; 395 | 396 | Cube Face::toCube() 397 | { 398 | Image img = {face_h * 3, face_w * 4, new uint8_t[img.w * img.h * CHANNEL_NUM]}; 399 | copy(faces[FRONT], img, face_h, face_w); 400 | copy(faces[RIGHT], img, face_h, 2 * face_w); 401 | copy(faces[BACK], img, face_h, 3 * face_w); 402 | copy(faces[LEFT], img, face_h, 0); 403 | copy(faces[TOP], img, 0, face_w); 404 | copy(faces[DOWN], img, 2 * face_h, face_w); 405 | 406 | return Cube(img); 407 | } 408 | 409 | Equi Face::toEqui() 410 | { 411 | if (!FE_map) 412 | setFEMap(); 413 | 414 | equi_h = face_h * 2; 415 | equi_w = face_w * 4; 416 | 417 | Image img = {equi_h, equi_w, new uint8_t[equi_w * equi_h * CHANNEL_NUM]}; 418 | 419 | for (int i = 0; i < equi_h; ++i) 420 | { 421 | for (int j = 0; j < equi_w; ++j) 422 | { 423 | Coord coord = getFECoord(i, j); 424 | for (int k = 0; k < CHANNEL_NUM; ++k) 425 | { 426 | img.img[CHANNEL_NUM * (i * img.w + j) + k] = 427 | faces[coord.face].img[CHANNEL_NUM * ((unsigned long)coord.y * face_w + (unsigned long)coord.x) + k]; 428 | } 429 | } 430 | } 431 | 432 | Equi equi = Equi(img); 433 | equi.setFEMap(FE_map); 434 | 435 | return equi; 436 | } 437 | 438 | Face Cube::toFace() 439 | { 440 | face_h = cubemap.h / 3; 441 | face_w = cubemap.w / 4; 442 | 443 | Image faces[FACE_NUM]; 444 | for (int i = 0; i < FACE_NUM; ++i) 445 | faces[i] = (Image){face_h, face_w, new uint8_t[face_w * face_h * CHANNEL_NUM]}; 446 | 447 | crop(cubemap, faces[FRONT], face_h, face_w); 448 | crop(cubemap, faces[RIGHT], face_h, 2 * face_w); 449 | crop(cubemap, faces[BACK], face_h, 3 * face_w); 450 | crop(cubemap, faces[LEFT], face_h, 0); 451 | crop(cubemap, faces[TOP], 0, face_w); 452 | crop(cubemap, faces[DOWN], 2 * face_h, face_w); 453 | 454 | return Face(faces); 455 | } 456 | 457 | Equi Cube::toEqui() 458 | { 459 | return toFace().toEqui(); 460 | } 461 | 462 | Face Equi::toFace() 463 | { 464 | if (!FE_map) 465 | setFEMap(); 466 | 467 | face_h = equi.h / 2; 468 | face_w = equi.w / 4; 469 | 470 | Image faces[FACE_NUM]; 471 | for (int i = 0; i < FACE_NUM; ++i) 472 | faces[i] = (Image){face_h, face_w, new uint8_t[face_w * face_h * CHANNEL_NUM]}; 473 | 474 | for (int i = 0; i < equi.h; ++i) 475 | { 476 | for (int j = 0; j < equi.w; ++j) 477 | { 478 | Coord coord = getFECoord(i, j); 479 | for (int k = 0; k < CHANNEL_NUM; ++k) 480 | { 481 | faces[coord.face].img[CHANNEL_NUM * ((unsigned long)coord.y * face_w + (unsigned long)coord.x) + k] = 482 | equi.img[CHANNEL_NUM * (i * equi.w + j) + k]; 483 | } 484 | } 485 | } 486 | 487 | Face face = Face(faces); 488 | face.setFEMap(FE_map); 489 | 490 | return face; 491 | } 492 | 493 | Cube Equi::toCube() 494 | { 495 | return toFace().toCube(); 496 | } 497 | 498 | Stereo Equi::toStereo(FaceID t_faceID = DOWN) 499 | { 500 | FaceID faceID = t_faceID; 501 | stereo_h = equi_h; 502 | stereo_w = equi_w / 2; 503 | 504 | Image img = {stereo_h, stereo_w, new uint8_t[stereo_w * stereo_h * CHANNEL_NUM]}; 505 | 506 | unsigned int org_x = stereo_w / 2; 507 | unsigned int org_y = stereo_h / 2; 508 | 509 | for (int i = 0; i < stereo_h; ++i) 510 | { 511 | for (int j = 0; j < stereo_w; ++j) 512 | { 513 | double rho, theta; 514 | cart2pol(j - org_x, i - org_y, rho, theta); 515 | 516 | if (rho <= stereo_w / 2) 517 | { 518 | if (theta < 0.0) 519 | theta += 360.0; 520 | 521 | int x, y; 522 | x = (faceID == DOWN ? theta / 360 * equi.w : (1 - theta / 360) * equi.w); 523 | y = (faceID == DOWN ? equi.h - int(rho * 2.0) : int(rho * 2.0)); 524 | 525 | for (int k = 0; k < CHANNEL_NUM; ++k) 526 | { 527 | img.img[CHANNEL_NUM * (i * img.w + j) + k] = 528 | equi.img[CHANNEL_NUM * (y * equi.w + x) + k]; 529 | } 530 | } 531 | else 532 | { 533 | for (int k = 0; k < CHANNEL_NUM; ++k) 534 | img.img[CHANNEL_NUM * (i * img.w + j) + k] = 0; 535 | } 536 | 537 | } 538 | } 539 | 540 | if (faceID == DOWN) 541 | img = rotate(img); 542 | else 543 | img = rotate(img, -M_PI_2); 544 | 545 | return Stereo(img, faceID); 546 | } 547 | 548 | Equi Stereo::toEqui() 549 | { 550 | equi_h = stereo.h; 551 | equi_w = stereo.w * 2; 552 | 553 | Image img = {equi_h, equi_w, new uint8_t[equi_w * equi_h * CHANNEL_NUM]}; 554 | Image stereo_tmp = stereo; 555 | 556 | unsigned int org_x = stereo_w / 2; 557 | unsigned int org_y = stereo_h / 2; 558 | 559 | if (faceID == DOWN) 560 | stereo_tmp = rotate(stereo_tmp, -M_PI_2); 561 | else 562 | stereo_tmp = rotate(stereo_tmp); 563 | 564 | for (int i = 0; i < stereo_tmp.h; ++i) 565 | { 566 | for (int j = 0; j < stereo_tmp.w; ++j) 567 | { 568 | double rho, theta; 569 | cart2pol(j - org_x, i - org_y, rho, theta); 570 | 571 | if (rho <= stereo_tmp.w / 2) 572 | { 573 | if (theta < 0.0) 574 | theta += 360.0; 575 | 576 | int x, y; 577 | x = (faceID == DOWN ? theta / 360 * equi_w : (1 - theta / 360) * equi_w); 578 | y = (faceID == DOWN ? equi_h - int(rho * 2.0) : int(rho * 2.0)); 579 | 580 | for (int k = 0; k < CHANNEL_NUM; ++k) 581 | { 582 | img.img[CHANNEL_NUM * (y * img.w + x) + k] = stereo_tmp.img[CHANNEL_NUM * (i * stereo_tmp.w + j) + k]; 583 | } 584 | } 585 | } 586 | } 587 | return Equi(img); 588 | } 589 | 590 | } // namespace Converter 591 | #endif -------------------------------------------------------------------------------- /example/include/stb_image_write.h: -------------------------------------------------------------------------------- 1 | /* stb_image_write - v1.15 - public domain - http://nothings.org/stb 2 | writes out PNG/BMP/TGA/JPEG/HDR images to C stdio - Sean Barrett 2010-2015 3 | no warranty implied; use at your own risk 4 | 5 | Before #including, 6 | 7 | #define STB_IMAGE_WRITE_IMPLEMENTATION 8 | 9 | in the file that you want to have the implementation. 10 | 11 | Will probably not work correctly with strict-aliasing optimizations. 12 | 13 | ABOUT: 14 | 15 | This header file is a library for writing images to C stdio or a callback. 16 | 17 | The PNG output is not optimal; it is 20-50% larger than the file 18 | written by a decent optimizing implementation; though providing a custom 19 | zlib compress function (see STBIW_ZLIB_COMPRESS) can mitigate that. 20 | This library is designed for source code compactness and simplicity, 21 | not optimal image file size or run-time performance. 22 | 23 | BUILDING: 24 | 25 | You can #define STBIW_ASSERT(x) before the #include to avoid using assert.h. 26 | You can #define STBIW_MALLOC(), STBIW_REALLOC(), and STBIW_FREE() to replace 27 | malloc,realloc,free. 28 | You can #define STBIW_MEMMOVE() to replace memmove() 29 | You can #define STBIW_ZLIB_COMPRESS to use a custom zlib-style compress function 30 | for PNG compression (instead of the builtin one), it must have the following signature: 31 | unsigned char * my_compress(unsigned char *data, int data_len, int *out_len, int quality); 32 | The returned data will be freed with STBIW_FREE() (free() by default), 33 | so it must be heap allocated with STBIW_MALLOC() (malloc() by default), 34 | 35 | UNICODE: 36 | 37 | If compiling for Windows and you wish to use Unicode filenames, compile 38 | with 39 | #define STBIW_WINDOWS_UTF8 40 | and pass utf8-encoded filenames. Call stbiw_convert_wchar_to_utf8 to convert 41 | Windows wchar_t filenames to utf8. 42 | 43 | USAGE: 44 | 45 | There are five functions, one for each image file format: 46 | 47 | int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 48 | int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 49 | int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 50 | int stbi_write_jpg(char const *filename, int w, int h, int comp, const void *data, int quality); 51 | int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 52 | 53 | void stbi_flip_vertically_on_write(int flag); // flag is non-zero to flip data vertically 54 | 55 | There are also five equivalent functions that use an arbitrary write function. You are 56 | expected to open/close your file-equivalent before and after calling these: 57 | 58 | int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 59 | int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 60 | int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 61 | int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 62 | int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 63 | 64 | where the callback is: 65 | void stbi_write_func(void *context, void *data, int size); 66 | 67 | You can configure it with these global variables: 68 | int stbi_write_tga_with_rle; // defaults to true; set to 0 to disable RLE 69 | int stbi_write_png_compression_level; // defaults to 8; set to higher for more compression 70 | int stbi_write_force_png_filter; // defaults to -1; set to 0..5 to force a filter mode 71 | 72 | 73 | You can define STBI_WRITE_NO_STDIO to disable the file variant of these 74 | functions, so the library will not use stdio.h at all. However, this will 75 | also disable HDR writing, because it requires stdio for formatted output. 76 | 77 | Each function returns 0 on failure and non-0 on success. 78 | 79 | The functions create an image file defined by the parameters. The image 80 | is a rectangle of pixels stored from left-to-right, top-to-bottom. 81 | Each pixel contains 'comp' channels of data stored interleaved with 8-bits 82 | per channel, in the following order: 1=Y, 2=YA, 3=RGB, 4=RGBA. (Y is 83 | monochrome color.) The rectangle is 'w' pixels wide and 'h' pixels tall. 84 | The *data pointer points to the first byte of the top-left-most pixel. 85 | For PNG, "stride_in_bytes" is the distance in bytes from the first byte of 86 | a row of pixels to the first byte of the next row of pixels. 87 | 88 | PNG creates output files with the same number of components as the input. 89 | The BMP format expands Y to RGB in the file format and does not 90 | output alpha. 91 | 92 | PNG supports writing rectangles of data even when the bytes storing rows of 93 | data are not consecutive in memory (e.g. sub-rectangles of a larger image), 94 | by supplying the stride between the beginning of adjacent rows. The other 95 | formats do not. (Thus you cannot write a native-format BMP through the BMP 96 | writer, both because it is in BGR order and because it may have padding 97 | at the end of the line.) 98 | 99 | PNG allows you to set the deflate compression level by setting the global 100 | variable 'stbi_write_png_compression_level' (it defaults to 8). 101 | 102 | HDR expects linear float data. Since the format is always 32-bit rgb(e) 103 | data, alpha (if provided) is discarded, and for monochrome data it is 104 | replicated across all three channels. 105 | 106 | TGA supports RLE or non-RLE compressed data. To use non-RLE-compressed 107 | data, set the global variable 'stbi_write_tga_with_rle' to 0. 108 | 109 | JPEG does ignore alpha channels in input data; quality is between 1 and 100. 110 | Higher quality looks better but results in a bigger image. 111 | JPEG baseline (no JPEG progressive). 112 | 113 | CREDITS: 114 | 115 | 116 | Sean Barrett - PNG/BMP/TGA 117 | Baldur Karlsson - HDR 118 | Jean-Sebastien Guay - TGA monochrome 119 | Tim Kelsey - misc enhancements 120 | Alan Hickman - TGA RLE 121 | Emmanuel Julien - initial file IO callback implementation 122 | Jon Olick - original jo_jpeg.cpp code 123 | Daniel Gibson - integrate JPEG, allow external zlib 124 | Aarni Koskela - allow choosing PNG filter 125 | 126 | bugfixes: 127 | github:Chribba 128 | Guillaume Chereau 129 | github:jry2 130 | github:romigrou 131 | Sergio Gonzalez 132 | Jonas Karlsson 133 | Filip Wasil 134 | Thatcher Ulrich 135 | github:poppolopoppo 136 | Patrick Boettcher 137 | github:xeekworx 138 | Cap Petschulat 139 | Simon Rodriguez 140 | Ivan Tikhonov 141 | github:ignotion 142 | Adam Schackart 143 | 144 | LICENSE 145 | 146 | See end of file for license information. 147 | 148 | */ 149 | 150 | #ifndef INCLUDE_STB_IMAGE_WRITE_H 151 | #define INCLUDE_STB_IMAGE_WRITE_H 152 | 153 | #include 154 | 155 | // if STB_IMAGE_WRITE_STATIC causes problems, try defining STBIWDEF to 'inline' or 'static inline' 156 | #ifndef STBIWDEF 157 | #ifdef STB_IMAGE_WRITE_STATIC 158 | #define STBIWDEF static 159 | #else 160 | #ifdef __cplusplus 161 | #define STBIWDEF extern "C" 162 | #else 163 | #define STBIWDEF extern 164 | #endif 165 | #endif 166 | #endif 167 | 168 | #ifndef STB_IMAGE_WRITE_STATIC // C++ forbids static forward declarations 169 | extern int stbi_write_tga_with_rle; 170 | extern int stbi_write_png_compression_level; 171 | extern int stbi_write_force_png_filter; 172 | #endif 173 | 174 | #ifndef STBI_WRITE_NO_STDIO 175 | STBIWDEF int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes); 176 | STBIWDEF int stbi_write_bmp(char const *filename, int w, int h, int comp, const void *data); 177 | STBIWDEF int stbi_write_tga(char const *filename, int w, int h, int comp, const void *data); 178 | STBIWDEF int stbi_write_hdr(char const *filename, int w, int h, int comp, const float *data); 179 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality); 180 | 181 | #ifdef STBI_WINDOWS_UTF8 182 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input); 183 | #endif 184 | #endif 185 | 186 | typedef void stbi_write_func(void *context, void *data, int size); 187 | 188 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data, int stride_in_bytes); 189 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 190 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const void *data); 191 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int w, int h, int comp, const float *data); 192 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); 193 | 194 | STBIWDEF void stbi_flip_vertically_on_write(int flip_boolean); 195 | 196 | #endif//INCLUDE_STB_IMAGE_WRITE_H 197 | 198 | #ifdef STB_IMAGE_WRITE_IMPLEMENTATION 199 | 200 | #ifdef _WIN32 201 | #ifndef _CRT_SECURE_NO_WARNINGS 202 | #define _CRT_SECURE_NO_WARNINGS 203 | #endif 204 | #ifndef _CRT_NONSTDC_NO_DEPRECATE 205 | #define _CRT_NONSTDC_NO_DEPRECATE 206 | #endif 207 | #endif 208 | 209 | #ifndef STBI_WRITE_NO_STDIO 210 | #include 211 | #endif // STBI_WRITE_NO_STDIO 212 | 213 | #include 214 | #include 215 | #include 216 | #include 217 | 218 | #if defined(STBIW_MALLOC) && defined(STBIW_FREE) && (defined(STBIW_REALLOC) || defined(STBIW_REALLOC_SIZED)) 219 | // ok 220 | #elif !defined(STBIW_MALLOC) && !defined(STBIW_FREE) && !defined(STBIW_REALLOC) && !defined(STBIW_REALLOC_SIZED) 221 | // ok 222 | #else 223 | #error "Must define all or none of STBIW_MALLOC, STBIW_FREE, and STBIW_REALLOC (or STBIW_REALLOC_SIZED)." 224 | #endif 225 | 226 | #ifndef STBIW_MALLOC 227 | #define STBIW_MALLOC(sz) malloc(sz) 228 | #define STBIW_REALLOC(p,newsz) realloc(p,newsz) 229 | #define STBIW_FREE(p) free(p) 230 | #endif 231 | 232 | #ifndef STBIW_REALLOC_SIZED 233 | #define STBIW_REALLOC_SIZED(p,oldsz,newsz) STBIW_REALLOC(p,newsz) 234 | #endif 235 | 236 | 237 | #ifndef STBIW_MEMMOVE 238 | #define STBIW_MEMMOVE(a,b,sz) memmove(a,b,sz) 239 | #endif 240 | 241 | 242 | #ifndef STBIW_ASSERT 243 | #include 244 | #define STBIW_ASSERT(x) assert(x) 245 | #endif 246 | 247 | #define STBIW_UCHAR(x) (unsigned char) ((x) & 0xff) 248 | 249 | #ifdef STB_IMAGE_WRITE_STATIC 250 | static int stbi_write_png_compression_level = 8; 251 | static int stbi_write_tga_with_rle = 1; 252 | static int stbi_write_force_png_filter = -1; 253 | #else 254 | int stbi_write_png_compression_level = 8; 255 | int stbi_write_tga_with_rle = 1; 256 | int stbi_write_force_png_filter = -1; 257 | #endif 258 | 259 | static int stbi__flip_vertically_on_write = 0; 260 | 261 | STBIWDEF void stbi_flip_vertically_on_write(int flag) 262 | { 263 | stbi__flip_vertically_on_write = flag; 264 | } 265 | 266 | typedef struct 267 | { 268 | stbi_write_func *func; 269 | void *context; 270 | unsigned char buffer[64]; 271 | int buf_used; 272 | } stbi__write_context; 273 | 274 | // initialize a callback-based context 275 | static void stbi__start_write_callbacks(stbi__write_context *s, stbi_write_func *c, void *context) 276 | { 277 | s->func = c; 278 | s->context = context; 279 | } 280 | 281 | #ifndef STBI_WRITE_NO_STDIO 282 | 283 | static void stbi__stdio_write(void *context, void *data, int size) 284 | { 285 | fwrite(data,1,size,(FILE*) context); 286 | } 287 | 288 | #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 289 | #ifdef __cplusplus 290 | #define STBIW_EXTERN extern "C" 291 | #else 292 | #define STBIW_EXTERN extern 293 | #endif 294 | STBIW_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide); 295 | STBIW_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default); 296 | 297 | STBIWDEF int stbiw_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t* input) 298 | { 299 | return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, (int) bufferlen, NULL, NULL); 300 | } 301 | #endif 302 | 303 | static FILE *stbiw__fopen(char const *filename, char const *mode) 304 | { 305 | FILE *f; 306 | #if defined(_MSC_VER) && defined(STBI_WINDOWS_UTF8) 307 | wchar_t wMode[64]; 308 | wchar_t wFilename[1024]; 309 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, sizeof(wFilename))) 310 | return 0; 311 | 312 | if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, sizeof(wMode))) 313 | return 0; 314 | 315 | #if _MSC_VER >= 1400 316 | if (0 != _wfopen_s(&f, wFilename, wMode)) 317 | f = 0; 318 | #else 319 | f = _wfopen(wFilename, wMode); 320 | #endif 321 | 322 | #elif defined(_MSC_VER) && _MSC_VER >= 1400 323 | if (0 != fopen_s(&f, filename, mode)) 324 | f=0; 325 | #else 326 | f = fopen(filename, mode); 327 | #endif 328 | return f; 329 | } 330 | 331 | static int stbi__start_write_file(stbi__write_context *s, const char *filename) 332 | { 333 | FILE *f = stbiw__fopen(filename, "wb"); 334 | stbi__start_write_callbacks(s, stbi__stdio_write, (void *) f); 335 | return f != NULL; 336 | } 337 | 338 | static void stbi__end_write_file(stbi__write_context *s) 339 | { 340 | fclose((FILE *)s->context); 341 | } 342 | 343 | #endif // !STBI_WRITE_NO_STDIO 344 | 345 | typedef unsigned int stbiw_uint32; 346 | typedef int stb_image_write_test[sizeof(stbiw_uint32)==4 ? 1 : -1]; 347 | 348 | static void stbiw__writefv(stbi__write_context *s, const char *fmt, va_list v) 349 | { 350 | while (*fmt) { 351 | switch (*fmt++) { 352 | case ' ': break; 353 | case '1': { unsigned char x = STBIW_UCHAR(va_arg(v, int)); 354 | s->func(s->context,&x,1); 355 | break; } 356 | case '2': { int x = va_arg(v,int); 357 | unsigned char b[2]; 358 | b[0] = STBIW_UCHAR(x); 359 | b[1] = STBIW_UCHAR(x>>8); 360 | s->func(s->context,b,2); 361 | break; } 362 | case '4': { stbiw_uint32 x = va_arg(v,int); 363 | unsigned char b[4]; 364 | b[0]=STBIW_UCHAR(x); 365 | b[1]=STBIW_UCHAR(x>>8); 366 | b[2]=STBIW_UCHAR(x>>16); 367 | b[3]=STBIW_UCHAR(x>>24); 368 | s->func(s->context,b,4); 369 | break; } 370 | default: 371 | STBIW_ASSERT(0); 372 | return; 373 | } 374 | } 375 | } 376 | 377 | static void stbiw__writef(stbi__write_context *s, const char *fmt, ...) 378 | { 379 | va_list v; 380 | va_start(v, fmt); 381 | stbiw__writefv(s, fmt, v); 382 | va_end(v); 383 | } 384 | 385 | static void stbiw__write_flush(stbi__write_context *s) 386 | { 387 | if (s->buf_used) { 388 | s->func(s->context, &s->buffer, s->buf_used); 389 | s->buf_used = 0; 390 | } 391 | } 392 | 393 | static void stbiw__putc(stbi__write_context *s, unsigned char c) 394 | { 395 | s->func(s->context, &c, 1); 396 | } 397 | 398 | static void stbiw__write1(stbi__write_context *s, unsigned char a) 399 | { 400 | if (s->buf_used + 1 > sizeof(s->buffer)) 401 | stbiw__write_flush(s); 402 | s->buffer[s->buf_used++] = a; 403 | } 404 | 405 | static void stbiw__write3(stbi__write_context *s, unsigned char a, unsigned char b, unsigned char c) 406 | { 407 | int n; 408 | if (s->buf_used + 3 > sizeof(s->buffer)) 409 | stbiw__write_flush(s); 410 | n = s->buf_used; 411 | s->buf_used = n+3; 412 | s->buffer[n+0] = a; 413 | s->buffer[n+1] = b; 414 | s->buffer[n+2] = c; 415 | } 416 | 417 | static void stbiw__write_pixel(stbi__write_context *s, int rgb_dir, int comp, int write_alpha, int expand_mono, unsigned char *d) 418 | { 419 | unsigned char bg[3] = { 255, 0, 255}, px[3]; 420 | int k; 421 | 422 | if (write_alpha < 0) 423 | stbiw__write1(s, d[comp - 1]); 424 | 425 | switch (comp) { 426 | case 2: // 2 pixels = mono + alpha, alpha is written separately, so same as 1-channel case 427 | case 1: 428 | if (expand_mono) 429 | stbiw__write3(s, d[0], d[0], d[0]); // monochrome bmp 430 | else 431 | stbiw__write1(s, d[0]); // monochrome TGA 432 | break; 433 | case 4: 434 | if (!write_alpha) { 435 | // composite against pink background 436 | for (k = 0; k < 3; ++k) 437 | px[k] = bg[k] + ((d[k] - bg[k]) * d[3]) / 255; 438 | stbiw__write3(s, px[1 - rgb_dir], px[1], px[1 + rgb_dir]); 439 | break; 440 | } 441 | /* FALLTHROUGH */ 442 | case 3: 443 | stbiw__write3(s, d[1 - rgb_dir], d[1], d[1 + rgb_dir]); 444 | break; 445 | } 446 | if (write_alpha > 0) 447 | stbiw__write1(s, d[comp - 1]); 448 | } 449 | 450 | static void stbiw__write_pixels(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, void *data, int write_alpha, int scanline_pad, int expand_mono) 451 | { 452 | stbiw_uint32 zero = 0; 453 | int i,j, j_end; 454 | 455 | if (y <= 0) 456 | return; 457 | 458 | if (stbi__flip_vertically_on_write) 459 | vdir *= -1; 460 | 461 | if (vdir < 0) { 462 | j_end = -1; j = y-1; 463 | } else { 464 | j_end = y; j = 0; 465 | } 466 | 467 | for (; j != j_end; j += vdir) { 468 | for (i=0; i < x; ++i) { 469 | unsigned char *d = (unsigned char *) data + (j*x+i)*comp; 470 | stbiw__write_pixel(s, rgb_dir, comp, write_alpha, expand_mono, d); 471 | } 472 | stbiw__write_flush(s); 473 | s->func(s->context, &zero, scanline_pad); 474 | } 475 | } 476 | 477 | static int stbiw__outfile(stbi__write_context *s, int rgb_dir, int vdir, int x, int y, int comp, int expand_mono, void *data, int alpha, int pad, const char *fmt, ...) 478 | { 479 | if (y < 0 || x < 0) { 480 | return 0; 481 | } else { 482 | va_list v; 483 | va_start(v, fmt); 484 | stbiw__writefv(s, fmt, v); 485 | va_end(v); 486 | stbiw__write_pixels(s,rgb_dir,vdir,x,y,comp,data,alpha,pad, expand_mono); 487 | return 1; 488 | } 489 | } 490 | 491 | static int stbi_write_bmp_core(stbi__write_context *s, int x, int y, int comp, const void *data) 492 | { 493 | int pad = (-x*3) & 3; 494 | return stbiw__outfile(s,-1,-1,x,y,comp,1,(void *) data,0,pad, 495 | "11 4 22 4" "4 44 22 444444", 496 | 'B', 'M', 14+40+(x*3+pad)*y, 0,0, 14+40, // file header 497 | 40, x,y, 1,24, 0,0,0,0,0,0); // bitmap header 498 | } 499 | 500 | STBIWDEF int stbi_write_bmp_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 501 | { 502 | stbi__write_context s = { 0 }; 503 | stbi__start_write_callbacks(&s, func, context); 504 | return stbi_write_bmp_core(&s, x, y, comp, data); 505 | } 506 | 507 | #ifndef STBI_WRITE_NO_STDIO 508 | STBIWDEF int stbi_write_bmp(char const *filename, int x, int y, int comp, const void *data) 509 | { 510 | stbi__write_context s = { 0 }; 511 | if (stbi__start_write_file(&s,filename)) { 512 | int r = stbi_write_bmp_core(&s, x, y, comp, data); 513 | stbi__end_write_file(&s); 514 | return r; 515 | } else 516 | return 0; 517 | } 518 | #endif //!STBI_WRITE_NO_STDIO 519 | 520 | static int stbi_write_tga_core(stbi__write_context *s, int x, int y, int comp, void *data) 521 | { 522 | int has_alpha = (comp == 2 || comp == 4); 523 | int colorbytes = has_alpha ? comp-1 : comp; 524 | int format = colorbytes < 2 ? 3 : 2; // 3 color channels (RGB/RGBA) = 2, 1 color channel (Y/YA) = 3 525 | 526 | if (y < 0 || x < 0) 527 | return 0; 528 | 529 | if (!stbi_write_tga_with_rle) { 530 | return stbiw__outfile(s, -1, -1, x, y, comp, 0, (void *) data, has_alpha, 0, 531 | "111 221 2222 11", 0, 0, format, 0, 0, 0, 0, 0, x, y, (colorbytes + has_alpha) * 8, has_alpha * 8); 532 | } else { 533 | int i,j,k; 534 | int jend, jdir; 535 | 536 | stbiw__writef(s, "111 221 2222 11", 0,0,format+8, 0,0,0, 0,0,x,y, (colorbytes + has_alpha) * 8, has_alpha * 8); 537 | 538 | if (stbi__flip_vertically_on_write) { 539 | j = 0; 540 | jend = y; 541 | jdir = 1; 542 | } else { 543 | j = y-1; 544 | jend = -1; 545 | jdir = -1; 546 | } 547 | for (; j != jend; j += jdir) { 548 | unsigned char *row = (unsigned char *) data + j * x * comp; 549 | int len; 550 | 551 | for (i = 0; i < x; i += len) { 552 | unsigned char *begin = row + i * comp; 553 | int diff = 1; 554 | len = 1; 555 | 556 | if (i < x - 1) { 557 | ++len; 558 | diff = memcmp(begin, row + (i + 1) * comp, comp); 559 | if (diff) { 560 | const unsigned char *prev = begin; 561 | for (k = i + 2; k < x && len < 128; ++k) { 562 | if (memcmp(prev, row + k * comp, comp)) { 563 | prev += comp; 564 | ++len; 565 | } else { 566 | --len; 567 | break; 568 | } 569 | } 570 | } else { 571 | for (k = i + 2; k < x && len < 128; ++k) { 572 | if (!memcmp(begin, row + k * comp, comp)) { 573 | ++len; 574 | } else { 575 | break; 576 | } 577 | } 578 | } 579 | } 580 | 581 | if (diff) { 582 | unsigned char header = STBIW_UCHAR(len - 1); 583 | stbiw__write1(s, header); 584 | for (k = 0; k < len; ++k) { 585 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin + k * comp); 586 | } 587 | } else { 588 | unsigned char header = STBIW_UCHAR(len - 129); 589 | stbiw__write1(s, header); 590 | stbiw__write_pixel(s, -1, comp, has_alpha, 0, begin); 591 | } 592 | } 593 | } 594 | stbiw__write_flush(s); 595 | } 596 | return 1; 597 | } 598 | 599 | STBIWDEF int stbi_write_tga_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data) 600 | { 601 | stbi__write_context s = { 0 }; 602 | stbi__start_write_callbacks(&s, func, context); 603 | return stbi_write_tga_core(&s, x, y, comp, (void *) data); 604 | } 605 | 606 | #ifndef STBI_WRITE_NO_STDIO 607 | STBIWDEF int stbi_write_tga(char const *filename, int x, int y, int comp, const void *data) 608 | { 609 | stbi__write_context s = { 0 }; 610 | if (stbi__start_write_file(&s,filename)) { 611 | int r = stbi_write_tga_core(&s, x, y, comp, (void *) data); 612 | stbi__end_write_file(&s); 613 | return r; 614 | } else 615 | return 0; 616 | } 617 | #endif 618 | 619 | // ************************************************************************************************* 620 | // Radiance RGBE HDR writer 621 | // by Baldur Karlsson 622 | 623 | #define stbiw__max(a, b) ((a) > (b) ? (a) : (b)) 624 | 625 | static void stbiw__linear_to_rgbe(unsigned char *rgbe, float *linear) 626 | { 627 | int exponent; 628 | float maxcomp = stbiw__max(linear[0], stbiw__max(linear[1], linear[2])); 629 | 630 | if (maxcomp < 1e-32f) { 631 | rgbe[0] = rgbe[1] = rgbe[2] = rgbe[3] = 0; 632 | } else { 633 | float normalize = (float) frexp(maxcomp, &exponent) * 256.0f/maxcomp; 634 | 635 | rgbe[0] = (unsigned char)(linear[0] * normalize); 636 | rgbe[1] = (unsigned char)(linear[1] * normalize); 637 | rgbe[2] = (unsigned char)(linear[2] * normalize); 638 | rgbe[3] = (unsigned char)(exponent + 128); 639 | } 640 | } 641 | 642 | static void stbiw__write_run_data(stbi__write_context *s, int length, unsigned char databyte) 643 | { 644 | unsigned char lengthbyte = STBIW_UCHAR(length+128); 645 | STBIW_ASSERT(length+128 <= 255); 646 | s->func(s->context, &lengthbyte, 1); 647 | s->func(s->context, &databyte, 1); 648 | } 649 | 650 | static void stbiw__write_dump_data(stbi__write_context *s, int length, unsigned char *data) 651 | { 652 | unsigned char lengthbyte = STBIW_UCHAR(length); 653 | STBIW_ASSERT(length <= 128); // inconsistent with spec but consistent with official code 654 | s->func(s->context, &lengthbyte, 1); 655 | s->func(s->context, data, length); 656 | } 657 | 658 | static void stbiw__write_hdr_scanline(stbi__write_context *s, int width, int ncomp, unsigned char *scratch, float *scanline) 659 | { 660 | unsigned char scanlineheader[4] = { 2, 2, 0, 0 }; 661 | unsigned char rgbe[4]; 662 | float linear[3]; 663 | int x; 664 | 665 | scanlineheader[2] = (width&0xff00)>>8; 666 | scanlineheader[3] = (width&0x00ff); 667 | 668 | /* skip RLE for images too small or large */ 669 | if (width < 8 || width >= 32768) { 670 | for (x=0; x < width; x++) { 671 | switch (ncomp) { 672 | case 4: /* fallthrough */ 673 | case 3: linear[2] = scanline[x*ncomp + 2]; 674 | linear[1] = scanline[x*ncomp + 1]; 675 | linear[0] = scanline[x*ncomp + 0]; 676 | break; 677 | default: 678 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 679 | break; 680 | } 681 | stbiw__linear_to_rgbe(rgbe, linear); 682 | s->func(s->context, rgbe, 4); 683 | } 684 | } else { 685 | int c,r; 686 | /* encode into scratch buffer */ 687 | for (x=0; x < width; x++) { 688 | switch(ncomp) { 689 | case 4: /* fallthrough */ 690 | case 3: linear[2] = scanline[x*ncomp + 2]; 691 | linear[1] = scanline[x*ncomp + 1]; 692 | linear[0] = scanline[x*ncomp + 0]; 693 | break; 694 | default: 695 | linear[0] = linear[1] = linear[2] = scanline[x*ncomp + 0]; 696 | break; 697 | } 698 | stbiw__linear_to_rgbe(rgbe, linear); 699 | scratch[x + width*0] = rgbe[0]; 700 | scratch[x + width*1] = rgbe[1]; 701 | scratch[x + width*2] = rgbe[2]; 702 | scratch[x + width*3] = rgbe[3]; 703 | } 704 | 705 | s->func(s->context, scanlineheader, 4); 706 | 707 | /* RLE each component separately */ 708 | for (c=0; c < 4; c++) { 709 | unsigned char *comp = &scratch[width*c]; 710 | 711 | x = 0; 712 | while (x < width) { 713 | // find first run 714 | r = x; 715 | while (r+2 < width) { 716 | if (comp[r] == comp[r+1] && comp[r] == comp[r+2]) 717 | break; 718 | ++r; 719 | } 720 | if (r+2 >= width) 721 | r = width; 722 | // dump up to first run 723 | while (x < r) { 724 | int len = r-x; 725 | if (len > 128) len = 128; 726 | stbiw__write_dump_data(s, len, &comp[x]); 727 | x += len; 728 | } 729 | // if there's a run, output it 730 | if (r+2 < width) { // same test as what we break out of in search loop, so only true if we break'd 731 | // find next byte after run 732 | while (r < width && comp[r] == comp[x]) 733 | ++r; 734 | // output run up to r 735 | while (x < r) { 736 | int len = r-x; 737 | if (len > 127) len = 127; 738 | stbiw__write_run_data(s, len, comp[x]); 739 | x += len; 740 | } 741 | } 742 | } 743 | } 744 | } 745 | } 746 | 747 | static int stbi_write_hdr_core(stbi__write_context *s, int x, int y, int comp, float *data) 748 | { 749 | if (y <= 0 || x <= 0 || data == NULL) 750 | return 0; 751 | else { 752 | // Each component is stored separately. Allocate scratch space for full output scanline. 753 | unsigned char *scratch = (unsigned char *) STBIW_MALLOC(x*4); 754 | int i, len; 755 | char buffer[128]; 756 | char header[] = "#?RADIANCE\n# Written by stb_image_write.h\nFORMAT=32-bit_rle_rgbe\n"; 757 | s->func(s->context, header, sizeof(header)-1); 758 | 759 | #ifdef __STDC_WANT_SECURE_LIB__ 760 | len = sprintf_s(buffer, sizeof(buffer), "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 761 | #else 762 | len = sprintf(buffer, "EXPOSURE= 1.0000000000000\n\n-Y %d +X %d\n", y, x); 763 | #endif 764 | s->func(s->context, buffer, len); 765 | 766 | for(i=0; i < y; i++) 767 | stbiw__write_hdr_scanline(s, x, comp, scratch, data + comp*x*(stbi__flip_vertically_on_write ? y-1-i : i)); 768 | STBIW_FREE(scratch); 769 | return 1; 770 | } 771 | } 772 | 773 | STBIWDEF int stbi_write_hdr_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const float *data) 774 | { 775 | stbi__write_context s = { 0 }; 776 | stbi__start_write_callbacks(&s, func, context); 777 | return stbi_write_hdr_core(&s, x, y, comp, (float *) data); 778 | } 779 | 780 | #ifndef STBI_WRITE_NO_STDIO 781 | STBIWDEF int stbi_write_hdr(char const *filename, int x, int y, int comp, const float *data) 782 | { 783 | stbi__write_context s = { 0 }; 784 | if (stbi__start_write_file(&s,filename)) { 785 | int r = stbi_write_hdr_core(&s, x, y, comp, (float *) data); 786 | stbi__end_write_file(&s); 787 | return r; 788 | } else 789 | return 0; 790 | } 791 | #endif // STBI_WRITE_NO_STDIO 792 | 793 | 794 | ////////////////////////////////////////////////////////////////////////////// 795 | // 796 | // PNG writer 797 | // 798 | 799 | #ifndef STBIW_ZLIB_COMPRESS 800 | // stretchy buffer; stbiw__sbpush() == vector<>::push_back() -- stbiw__sbcount() == vector<>::size() 801 | #define stbiw__sbraw(a) ((int *) (void *) (a) - 2) 802 | #define stbiw__sbm(a) stbiw__sbraw(a)[0] 803 | #define stbiw__sbn(a) stbiw__sbraw(a)[1] 804 | 805 | #define stbiw__sbneedgrow(a,n) ((a)==0 || stbiw__sbn(a)+n >= stbiw__sbm(a)) 806 | #define stbiw__sbmaybegrow(a,n) (stbiw__sbneedgrow(a,(n)) ? stbiw__sbgrow(a,n) : 0) 807 | #define stbiw__sbgrow(a,n) stbiw__sbgrowf((void **) &(a), (n), sizeof(*(a))) 808 | 809 | #define stbiw__sbpush(a, v) (stbiw__sbmaybegrow(a,1), (a)[stbiw__sbn(a)++] = (v)) 810 | #define stbiw__sbcount(a) ((a) ? stbiw__sbn(a) : 0) 811 | #define stbiw__sbfree(a) ((a) ? STBIW_FREE(stbiw__sbraw(a)),0 : 0) 812 | 813 | static void *stbiw__sbgrowf(void **arr, int increment, int itemsize) 814 | { 815 | int m = *arr ? 2*stbiw__sbm(*arr)+increment : increment+1; 816 | void *p = STBIW_REALLOC_SIZED(*arr ? stbiw__sbraw(*arr) : 0, *arr ? (stbiw__sbm(*arr)*itemsize + sizeof(int)*2) : 0, itemsize * m + sizeof(int)*2); 817 | STBIW_ASSERT(p); 818 | if (p) { 819 | if (!*arr) ((int *) p)[1] = 0; 820 | *arr = (void *) ((int *) p + 2); 821 | stbiw__sbm(*arr) = m; 822 | } 823 | return *arr; 824 | } 825 | 826 | static unsigned char *stbiw__zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) 827 | { 828 | while (*bitcount >= 8) { 829 | stbiw__sbpush(data, STBIW_UCHAR(*bitbuffer)); 830 | *bitbuffer >>= 8; 831 | *bitcount -= 8; 832 | } 833 | return data; 834 | } 835 | 836 | static int stbiw__zlib_bitrev(int code, int codebits) 837 | { 838 | int res=0; 839 | while (codebits--) { 840 | res = (res << 1) | (code & 1); 841 | code >>= 1; 842 | } 843 | return res; 844 | } 845 | 846 | static unsigned int stbiw__zlib_countm(unsigned char *a, unsigned char *b, int limit) 847 | { 848 | int i; 849 | for (i=0; i < limit && i < 258; ++i) 850 | if (a[i] != b[i]) break; 851 | return i; 852 | } 853 | 854 | static unsigned int stbiw__zhash(unsigned char *data) 855 | { 856 | stbiw_uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16); 857 | hash ^= hash << 3; 858 | hash += hash >> 5; 859 | hash ^= hash << 4; 860 | hash += hash >> 17; 861 | hash ^= hash << 25; 862 | hash += hash >> 6; 863 | return hash; 864 | } 865 | 866 | #define stbiw__zlib_flush() (out = stbiw__zlib_flushf(out, &bitbuf, &bitcount)) 867 | #define stbiw__zlib_add(code,codebits) \ 868 | (bitbuf |= (code) << bitcount, bitcount += (codebits), stbiw__zlib_flush()) 869 | #define stbiw__zlib_huffa(b,c) stbiw__zlib_add(stbiw__zlib_bitrev(b,c),c) 870 | // default huffman tables 871 | #define stbiw__zlib_huff1(n) stbiw__zlib_huffa(0x30 + (n), 8) 872 | #define stbiw__zlib_huff2(n) stbiw__zlib_huffa(0x190 + (n)-144, 9) 873 | #define stbiw__zlib_huff3(n) stbiw__zlib_huffa(0 + (n)-256,7) 874 | #define stbiw__zlib_huff4(n) stbiw__zlib_huffa(0xc0 + (n)-280,8) 875 | #define stbiw__zlib_huff(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : (n) <= 255 ? stbiw__zlib_huff2(n) : (n) <= 279 ? stbiw__zlib_huff3(n) : stbiw__zlib_huff4(n)) 876 | #define stbiw__zlib_huffb(n) ((n) <= 143 ? stbiw__zlib_huff1(n) : stbiw__zlib_huff2(n)) 877 | 878 | #define stbiw__ZHASH 16384 879 | 880 | #endif // STBIW_ZLIB_COMPRESS 881 | 882 | STBIWDEF unsigned char * stbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) 883 | { 884 | #ifdef STBIW_ZLIB_COMPRESS 885 | // user provided a zlib compress implementation, use that 886 | return STBIW_ZLIB_COMPRESS(data, data_len, out_len, quality); 887 | #else // use builtin 888 | static unsigned short lengthc[] = { 3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258, 259 }; 889 | static unsigned char lengtheb[]= { 0,0,0,0,0,0,0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 }; 890 | static unsigned short distc[] = { 1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577, 32768 }; 891 | static unsigned char disteb[] = { 0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13 }; 892 | unsigned int bitbuf=0; 893 | int i,j, bitcount=0; 894 | unsigned char *out = NULL; 895 | unsigned char ***hash_table = (unsigned char***) STBIW_MALLOC(stbiw__ZHASH * sizeof(unsigned char**)); 896 | if (hash_table == NULL) 897 | return NULL; 898 | if (quality < 5) quality = 5; 899 | 900 | stbiw__sbpush(out, 0x78); // DEFLATE 32K window 901 | stbiw__sbpush(out, 0x5e); // FLEVEL = 1 902 | stbiw__zlib_add(1,1); // BFINAL = 1 903 | stbiw__zlib_add(1,2); // BTYPE = 1 -- fixed huffman 904 | 905 | for (i=0; i < stbiw__ZHASH; ++i) 906 | hash_table[i] = NULL; 907 | 908 | i=0; 909 | while (i < data_len-3) { 910 | // hash next 3 bytes of data to be compressed 911 | int h = stbiw__zhash(data+i)&(stbiw__ZHASH-1), best=3; 912 | unsigned char *bestloc = 0; 913 | unsigned char **hlist = hash_table[h]; 914 | int n = stbiw__sbcount(hlist); 915 | for (j=0; j < n; ++j) { 916 | if (hlist[j]-data > i-32768) { // if entry lies within window 917 | int d = stbiw__zlib_countm(hlist[j], data+i, data_len-i); 918 | if (d >= best) { best=d; bestloc=hlist[j]; } 919 | } 920 | } 921 | // when hash table entry is too long, delete half the entries 922 | if (hash_table[h] && stbiw__sbn(hash_table[h]) == 2*quality) { 923 | STBIW_MEMMOVE(hash_table[h], hash_table[h]+quality, sizeof(hash_table[h][0])*quality); 924 | stbiw__sbn(hash_table[h]) = quality; 925 | } 926 | stbiw__sbpush(hash_table[h],data+i); 927 | 928 | if (bestloc) { 929 | // "lazy matching" - check match at *next* byte, and if it's better, do cur byte as literal 930 | h = stbiw__zhash(data+i+1)&(stbiw__ZHASH-1); 931 | hlist = hash_table[h]; 932 | n = stbiw__sbcount(hlist); 933 | for (j=0; j < n; ++j) { 934 | if (hlist[j]-data > i-32767) { 935 | int e = stbiw__zlib_countm(hlist[j], data+i+1, data_len-i-1); 936 | if (e > best) { // if next match is better, bail on current match 937 | bestloc = NULL; 938 | break; 939 | } 940 | } 941 | } 942 | } 943 | 944 | if (bestloc) { 945 | int d = (int) (data+i - bestloc); // distance back 946 | STBIW_ASSERT(d <= 32767 && best <= 258); 947 | for (j=0; best > lengthc[j+1]-1; ++j); 948 | stbiw__zlib_huff(j+257); 949 | if (lengtheb[j]) stbiw__zlib_add(best - lengthc[j], lengtheb[j]); 950 | for (j=0; d > distc[j+1]-1; ++j); 951 | stbiw__zlib_add(stbiw__zlib_bitrev(j,5),5); 952 | if (disteb[j]) stbiw__zlib_add(d - distc[j], disteb[j]); 953 | i += best; 954 | } else { 955 | stbiw__zlib_huffb(data[i]); 956 | ++i; 957 | } 958 | } 959 | // write out final bytes 960 | for (;i < data_len; ++i) 961 | stbiw__zlib_huffb(data[i]); 962 | stbiw__zlib_huff(256); // end of block 963 | // pad with 0 bits to byte boundary 964 | while (bitcount) 965 | stbiw__zlib_add(0,1); 966 | 967 | for (i=0; i < stbiw__ZHASH; ++i) 968 | (void) stbiw__sbfree(hash_table[i]); 969 | STBIW_FREE(hash_table); 970 | 971 | { 972 | // compute adler32 on input 973 | unsigned int s1=1, s2=0; 974 | int blocklen = (int) (data_len % 5552); 975 | j=0; 976 | while (j < data_len) { 977 | for (i=0; i < blocklen; ++i) { s1 += data[j+i]; s2 += s1; } 978 | s1 %= 65521; s2 %= 65521; 979 | j += blocklen; 980 | blocklen = 5552; 981 | } 982 | stbiw__sbpush(out, STBIW_UCHAR(s2 >> 8)); 983 | stbiw__sbpush(out, STBIW_UCHAR(s2)); 984 | stbiw__sbpush(out, STBIW_UCHAR(s1 >> 8)); 985 | stbiw__sbpush(out, STBIW_UCHAR(s1)); 986 | } 987 | *out_len = stbiw__sbn(out); 988 | // make returned pointer freeable 989 | STBIW_MEMMOVE(stbiw__sbraw(out), out, *out_len); 990 | return (unsigned char *) stbiw__sbraw(out); 991 | #endif // STBIW_ZLIB_COMPRESS 992 | } 993 | 994 | static unsigned int stbiw__crc32(unsigned char *buffer, int len) 995 | { 996 | #ifdef STBIW_CRC32 997 | return STBIW_CRC32(buffer, len); 998 | #else 999 | static unsigned int crc_table[256] = 1000 | { 1001 | 0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F, 0xE963A535, 0x9E6495A3, 1002 | 0x0eDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988, 0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 1003 | 0x1DB71064, 0x6AB020F2, 0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7, 1004 | 0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9, 0xFA0F3D63, 0x8D080DF5, 1005 | 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172, 0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 1006 | 0x35B5A8FA, 0x42B2986C, 0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59, 1007 | 0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423, 0xCFBA9599, 0xB8BDA50F, 1008 | 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924, 0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 1009 | 0x76DC4190, 0x01DB7106, 0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433, 1010 | 0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D, 0x91646C97, 0xE6635C01, 1011 | 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E, 0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 1012 | 0x65B0D9C6, 0x12B7E950, 0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65, 1013 | 0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7, 0xA4D1C46D, 0xD3D6F4FB, 1014 | 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0, 0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 1015 | 0x5005713C, 0x270241AA, 0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F, 1016 | 0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81, 0xB7BD5C3B, 0xC0BA6CAD, 1017 | 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A, 0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 1018 | 0xE3630B12, 0x94643B84, 0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1, 1019 | 0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB, 0x196C3671, 0x6E6B06E7, 1020 | 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC, 0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 1021 | 0xD6D6A3E8, 0xA1D1937E, 0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B, 1022 | 0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55, 0x316E8EEF, 0x4669BE79, 1023 | 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236, 0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 1024 | 0xC5BA3BBE, 0xB2BD0B28, 0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D, 1025 | 0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F, 0x72076785, 0x05005713, 1026 | 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38, 0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 1027 | 0x86D3D2D4, 0xF1D4E242, 0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777, 1028 | 0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69, 0x616BFFD3, 0x166CCF45, 1029 | 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2, 0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 1030 | 0xAED16A4A, 0xD9D65ADC, 0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9, 1031 | 0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693, 0x54DE5729, 0x23D967BF, 1032 | 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94, 0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D 1033 | }; 1034 | 1035 | unsigned int crc = ~0u; 1036 | int i; 1037 | for (i=0; i < len; ++i) 1038 | crc = (crc >> 8) ^ crc_table[buffer[i] ^ (crc & 0xff)]; 1039 | return ~crc; 1040 | #endif 1041 | } 1042 | 1043 | #define stbiw__wpng4(o,a,b,c,d) ((o)[0]=STBIW_UCHAR(a),(o)[1]=STBIW_UCHAR(b),(o)[2]=STBIW_UCHAR(c),(o)[3]=STBIW_UCHAR(d),(o)+=4) 1044 | #define stbiw__wp32(data,v) stbiw__wpng4(data, (v)>>24,(v)>>16,(v)>>8,(v)); 1045 | #define stbiw__wptag(data,s) stbiw__wpng4(data, s[0],s[1],s[2],s[3]) 1046 | 1047 | static void stbiw__wpcrc(unsigned char **data, int len) 1048 | { 1049 | unsigned int crc = stbiw__crc32(*data - len - 4, len+4); 1050 | stbiw__wp32(*data, crc); 1051 | } 1052 | 1053 | static unsigned char stbiw__paeth(int a, int b, int c) 1054 | { 1055 | int p = a + b - c, pa = abs(p-a), pb = abs(p-b), pc = abs(p-c); 1056 | if (pa <= pb && pa <= pc) return STBIW_UCHAR(a); 1057 | if (pb <= pc) return STBIW_UCHAR(b); 1058 | return STBIW_UCHAR(c); 1059 | } 1060 | 1061 | // @OPTIMIZE: provide an option that always forces left-predict or paeth predict 1062 | static void stbiw__encode_png_line(unsigned char *pixels, int stride_bytes, int width, int height, int y, int n, int filter_type, signed char *line_buffer) 1063 | { 1064 | static int mapping[] = { 0,1,2,3,4 }; 1065 | static int firstmap[] = { 0,1,0,5,6 }; 1066 | int *mymap = (y != 0) ? mapping : firstmap; 1067 | int i; 1068 | int type = mymap[filter_type]; 1069 | unsigned char *z = pixels + stride_bytes * (stbi__flip_vertically_on_write ? height-1-y : y); 1070 | int signed_stride = stbi__flip_vertically_on_write ? -stride_bytes : stride_bytes; 1071 | 1072 | if (type==0) { 1073 | memcpy(line_buffer, z, width*n); 1074 | return; 1075 | } 1076 | 1077 | // first loop isn't optimized since it's just one pixel 1078 | for (i = 0; i < n; ++i) { 1079 | switch (type) { 1080 | case 1: line_buffer[i] = z[i]; break; 1081 | case 2: line_buffer[i] = z[i] - z[i-signed_stride]; break; 1082 | case 3: line_buffer[i] = z[i] - (z[i-signed_stride]>>1); break; 1083 | case 4: line_buffer[i] = (signed char) (z[i] - stbiw__paeth(0,z[i-signed_stride],0)); break; 1084 | case 5: line_buffer[i] = z[i]; break; 1085 | case 6: line_buffer[i] = z[i]; break; 1086 | } 1087 | } 1088 | switch (type) { 1089 | case 1: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-n]; break; 1090 | case 2: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - z[i-signed_stride]; break; 1091 | case 3: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - ((z[i-n] + z[i-signed_stride])>>1); break; 1092 | case 4: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], z[i-signed_stride], z[i-signed_stride-n]); break; 1093 | case 5: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - (z[i-n]>>1); break; 1094 | case 6: for (i=n; i < width*n; ++i) line_buffer[i] = z[i] - stbiw__paeth(z[i-n], 0,0); break; 1095 | } 1096 | } 1097 | 1098 | STBIWDEF unsigned char *stbi_write_png_to_mem(const unsigned char *pixels, int stride_bytes, int x, int y, int n, int *out_len) 1099 | { 1100 | int force_filter = stbi_write_force_png_filter; 1101 | int ctype[5] = { -1, 0, 4, 2, 6 }; 1102 | unsigned char sig[8] = { 137,80,78,71,13,10,26,10 }; 1103 | unsigned char *out,*o, *filt, *zlib; 1104 | signed char *line_buffer; 1105 | int j,zlen; 1106 | 1107 | if (stride_bytes == 0) 1108 | stride_bytes = x * n; 1109 | 1110 | if (force_filter >= 5) { 1111 | force_filter = -1; 1112 | } 1113 | 1114 | filt = (unsigned char *) STBIW_MALLOC((x*n+1) * y); if (!filt) return 0; 1115 | line_buffer = (signed char *) STBIW_MALLOC(x * n); if (!line_buffer) { STBIW_FREE(filt); return 0; } 1116 | for (j=0; j < y; ++j) { 1117 | int filter_type; 1118 | if (force_filter > -1) { 1119 | filter_type = force_filter; 1120 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, force_filter, line_buffer); 1121 | } else { // Estimate the best filter by running through all of them: 1122 | int best_filter = 0, best_filter_val = 0x7fffffff, est, i; 1123 | for (filter_type = 0; filter_type < 5; filter_type++) { 1124 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, filter_type, line_buffer); 1125 | 1126 | // Estimate the entropy of the line using this filter; the less, the better. 1127 | est = 0; 1128 | for (i = 0; i < x*n; ++i) { 1129 | est += abs((signed char) line_buffer[i]); 1130 | } 1131 | if (est < best_filter_val) { 1132 | best_filter_val = est; 1133 | best_filter = filter_type; 1134 | } 1135 | } 1136 | if (filter_type != best_filter) { // If the last iteration already got us the best filter, don't redo it 1137 | stbiw__encode_png_line((unsigned char*)(pixels), stride_bytes, x, y, j, n, best_filter, line_buffer); 1138 | filter_type = best_filter; 1139 | } 1140 | } 1141 | // when we get here, filter_type contains the filter type, and line_buffer contains the data 1142 | filt[j*(x*n+1)] = (unsigned char) filter_type; 1143 | STBIW_MEMMOVE(filt+j*(x*n+1)+1, line_buffer, x*n); 1144 | } 1145 | STBIW_FREE(line_buffer); 1146 | zlib = stbi_zlib_compress(filt, y*( x*n+1), &zlen, stbi_write_png_compression_level); 1147 | STBIW_FREE(filt); 1148 | if (!zlib) return 0; 1149 | 1150 | // each tag requires 12 bytes of overhead 1151 | out = (unsigned char *) STBIW_MALLOC(8 + 12+13 + 12+zlen + 12); 1152 | if (!out) return 0; 1153 | *out_len = 8 + 12+13 + 12+zlen + 12; 1154 | 1155 | o=out; 1156 | STBIW_MEMMOVE(o,sig,8); o+= 8; 1157 | stbiw__wp32(o, 13); // header length 1158 | stbiw__wptag(o, "IHDR"); 1159 | stbiw__wp32(o, x); 1160 | stbiw__wp32(o, y); 1161 | *o++ = 8; 1162 | *o++ = STBIW_UCHAR(ctype[n]); 1163 | *o++ = 0; 1164 | *o++ = 0; 1165 | *o++ = 0; 1166 | stbiw__wpcrc(&o,13); 1167 | 1168 | stbiw__wp32(o, zlen); 1169 | stbiw__wptag(o, "IDAT"); 1170 | STBIW_MEMMOVE(o, zlib, zlen); 1171 | o += zlen; 1172 | STBIW_FREE(zlib); 1173 | stbiw__wpcrc(&o, zlen); 1174 | 1175 | stbiw__wp32(o,0); 1176 | stbiw__wptag(o, "IEND"); 1177 | stbiw__wpcrc(&o,0); 1178 | 1179 | STBIW_ASSERT(o == out + *out_len); 1180 | 1181 | return out; 1182 | } 1183 | 1184 | #ifndef STBI_WRITE_NO_STDIO 1185 | STBIWDEF int stbi_write_png(char const *filename, int x, int y, int comp, const void *data, int stride_bytes) 1186 | { 1187 | FILE *f; 1188 | int len; 1189 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1190 | if (png == NULL) return 0; 1191 | 1192 | f = stbiw__fopen(filename, "wb"); 1193 | if (!f) { STBIW_FREE(png); return 0; } 1194 | fwrite(png, 1, len, f); 1195 | fclose(f); 1196 | STBIW_FREE(png); 1197 | return 1; 1198 | } 1199 | #endif 1200 | 1201 | STBIWDEF int stbi_write_png_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int stride_bytes) 1202 | { 1203 | int len; 1204 | unsigned char *png = stbi_write_png_to_mem((const unsigned char *) data, stride_bytes, x, y, comp, &len); 1205 | if (png == NULL) return 0; 1206 | func(context, png, len); 1207 | STBIW_FREE(png); 1208 | return 1; 1209 | } 1210 | 1211 | 1212 | /* *************************************************************************** 1213 | * 1214 | * JPEG writer 1215 | * 1216 | * This is based on Jon Olick's jo_jpeg.cpp: 1217 | * public domain Simple, Minimalistic JPEG writer - http://www.jonolick.com/code.html 1218 | */ 1219 | 1220 | static const unsigned char stbiw__jpg_ZigZag[] = { 0,1,5,6,14,15,27,28,2,4,7,13,16,26,29,42,3,8,12,17,25,30,41,43,9,11,18, 1221 | 24,31,40,44,53,10,19,23,32,39,45,52,54,20,22,33,38,46,51,55,60,21,34,37,47,50,56,59,61,35,36,48,49,57,58,62,63 }; 1222 | 1223 | static void stbiw__jpg_writeBits(stbi__write_context *s, int *bitBufP, int *bitCntP, const unsigned short *bs) { 1224 | int bitBuf = *bitBufP, bitCnt = *bitCntP; 1225 | bitCnt += bs[1]; 1226 | bitBuf |= bs[0] << (24 - bitCnt); 1227 | while(bitCnt >= 8) { 1228 | unsigned char c = (bitBuf >> 16) & 255; 1229 | stbiw__putc(s, c); 1230 | if(c == 255) { 1231 | stbiw__putc(s, 0); 1232 | } 1233 | bitBuf <<= 8; 1234 | bitCnt -= 8; 1235 | } 1236 | *bitBufP = bitBuf; 1237 | *bitCntP = bitCnt; 1238 | } 1239 | 1240 | static void stbiw__jpg_DCT(float *d0p, float *d1p, float *d2p, float *d3p, float *d4p, float *d5p, float *d6p, float *d7p) { 1241 | float d0 = *d0p, d1 = *d1p, d2 = *d2p, d3 = *d3p, d4 = *d4p, d5 = *d5p, d6 = *d6p, d7 = *d7p; 1242 | float z1, z2, z3, z4, z5, z11, z13; 1243 | 1244 | float tmp0 = d0 + d7; 1245 | float tmp7 = d0 - d7; 1246 | float tmp1 = d1 + d6; 1247 | float tmp6 = d1 - d6; 1248 | float tmp2 = d2 + d5; 1249 | float tmp5 = d2 - d5; 1250 | float tmp3 = d3 + d4; 1251 | float tmp4 = d3 - d4; 1252 | 1253 | // Even part 1254 | float tmp10 = tmp0 + tmp3; // phase 2 1255 | float tmp13 = tmp0 - tmp3; 1256 | float tmp11 = tmp1 + tmp2; 1257 | float tmp12 = tmp1 - tmp2; 1258 | 1259 | d0 = tmp10 + tmp11; // phase 3 1260 | d4 = tmp10 - tmp11; 1261 | 1262 | z1 = (tmp12 + tmp13) * 0.707106781f; // c4 1263 | d2 = tmp13 + z1; // phase 5 1264 | d6 = tmp13 - z1; 1265 | 1266 | // Odd part 1267 | tmp10 = tmp4 + tmp5; // phase 2 1268 | tmp11 = tmp5 + tmp6; 1269 | tmp12 = tmp6 + tmp7; 1270 | 1271 | // The rotator is modified from fig 4-8 to avoid extra negations. 1272 | z5 = (tmp10 - tmp12) * 0.382683433f; // c6 1273 | z2 = tmp10 * 0.541196100f + z5; // c2-c6 1274 | z4 = tmp12 * 1.306562965f + z5; // c2+c6 1275 | z3 = tmp11 * 0.707106781f; // c4 1276 | 1277 | z11 = tmp7 + z3; // phase 5 1278 | z13 = tmp7 - z3; 1279 | 1280 | *d5p = z13 + z2; // phase 6 1281 | *d3p = z13 - z2; 1282 | *d1p = z11 + z4; 1283 | *d7p = z11 - z4; 1284 | 1285 | *d0p = d0; *d2p = d2; *d4p = d4; *d6p = d6; 1286 | } 1287 | 1288 | static void stbiw__jpg_calcBits(int val, unsigned short bits[2]) { 1289 | int tmp1 = val < 0 ? -val : val; 1290 | val = val < 0 ? val-1 : val; 1291 | bits[1] = 1; 1292 | while(tmp1 >>= 1) { 1293 | ++bits[1]; 1294 | } 1295 | bits[0] = val & ((1<0)&&(DU[end0pos]==0); --end0pos) { 1338 | } 1339 | // end0pos = first element in reverse order !=0 1340 | if(end0pos == 0) { 1341 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1342 | return DU[0]; 1343 | } 1344 | for(i = 1; i <= end0pos; ++i) { 1345 | int startpos = i; 1346 | int nrzeroes; 1347 | unsigned short bits[2]; 1348 | for (; DU[i]==0 && i<=end0pos; ++i) { 1349 | } 1350 | nrzeroes = i-startpos; 1351 | if ( nrzeroes >= 16 ) { 1352 | int lng = nrzeroes>>4; 1353 | int nrmarker; 1354 | for (nrmarker=1; nrmarker <= lng; ++nrmarker) 1355 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, M16zeroes); 1356 | nrzeroes &= 15; 1357 | } 1358 | stbiw__jpg_calcBits(DU[i], bits); 1359 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, HTAC[(nrzeroes<<4)+bits[1]]); 1360 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, bits); 1361 | } 1362 | if(end0pos != 63) { 1363 | stbiw__jpg_writeBits(s, bitBuf, bitCnt, EOB); 1364 | } 1365 | return DU[0]; 1366 | } 1367 | 1368 | static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, int comp, const void* data, int quality) { 1369 | // Constants that don't pollute global namespace 1370 | static const unsigned char std_dc_luminance_nrcodes[] = {0,0,1,5,1,1,1,1,1,1,0,0,0,0,0,0,0}; 1371 | static const unsigned char std_dc_luminance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1372 | static const unsigned char std_ac_luminance_nrcodes[] = {0,0,2,1,3,3,2,4,3,5,5,4,4,0,0,1,0x7d}; 1373 | static const unsigned char std_ac_luminance_values[] = { 1374 | 0x01,0x02,0x03,0x00,0x04,0x11,0x05,0x12,0x21,0x31,0x41,0x06,0x13,0x51,0x61,0x07,0x22,0x71,0x14,0x32,0x81,0x91,0xa1,0x08, 1375 | 0x23,0x42,0xb1,0xc1,0x15,0x52,0xd1,0xf0,0x24,0x33,0x62,0x72,0x82,0x09,0x0a,0x16,0x17,0x18,0x19,0x1a,0x25,0x26,0x27,0x28, 1376 | 0x29,0x2a,0x34,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58,0x59, 1377 | 0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x83,0x84,0x85,0x86,0x87,0x88,0x89, 1378 | 0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4,0xb5,0xb6, 1379 | 0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda,0xe1,0xe2, 1380 | 0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf1,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1381 | }; 1382 | static const unsigned char std_dc_chrominance_nrcodes[] = {0,0,3,1,1,1,1,1,1,1,1,1,0,0,0,0,0}; 1383 | static const unsigned char std_dc_chrominance_values[] = {0,1,2,3,4,5,6,7,8,9,10,11}; 1384 | static const unsigned char std_ac_chrominance_nrcodes[] = {0,0,2,1,2,4,4,3,4,7,5,4,4,0,1,2,0x77}; 1385 | static const unsigned char std_ac_chrominance_values[] = { 1386 | 0x00,0x01,0x02,0x03,0x11,0x04,0x05,0x21,0x31,0x06,0x12,0x41,0x51,0x07,0x61,0x71,0x13,0x22,0x32,0x81,0x08,0x14,0x42,0x91, 1387 | 0xa1,0xb1,0xc1,0x09,0x23,0x33,0x52,0xf0,0x15,0x62,0x72,0xd1,0x0a,0x16,0x24,0x34,0xe1,0x25,0xf1,0x17,0x18,0x19,0x1a,0x26, 1388 | 0x27,0x28,0x29,0x2a,0x35,0x36,0x37,0x38,0x39,0x3a,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4a,0x53,0x54,0x55,0x56,0x57,0x58, 1389 | 0x59,0x5a,0x63,0x64,0x65,0x66,0x67,0x68,0x69,0x6a,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7a,0x82,0x83,0x84,0x85,0x86,0x87, 1390 | 0x88,0x89,0x8a,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9a,0xa2,0xa3,0xa4,0xa5,0xa6,0xa7,0xa8,0xa9,0xaa,0xb2,0xb3,0xb4, 1391 | 0xb5,0xb6,0xb7,0xb8,0xb9,0xba,0xc2,0xc3,0xc4,0xc5,0xc6,0xc7,0xc8,0xc9,0xca,0xd2,0xd3,0xd4,0xd5,0xd6,0xd7,0xd8,0xd9,0xda, 1392 | 0xe2,0xe3,0xe4,0xe5,0xe6,0xe7,0xe8,0xe9,0xea,0xf2,0xf3,0xf4,0xf5,0xf6,0xf7,0xf8,0xf9,0xfa 1393 | }; 1394 | // Huffman tables 1395 | static const unsigned short YDC_HT[256][2] = { {0,2},{2,3},{3,3},{4,3},{5,3},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9}}; 1396 | static const unsigned short UVDC_HT[256][2] = { {0,2},{1,2},{2,2},{6,3},{14,4},{30,5},{62,6},{126,7},{254,8},{510,9},{1022,10},{2046,11}}; 1397 | static const unsigned short YAC_HT[256][2] = { 1398 | {10,4},{0,2},{1,2},{4,3},{11,4},{26,5},{120,7},{248,8},{1014,10},{65410,16},{65411,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1399 | {12,4},{27,5},{121,7},{502,9},{2038,11},{65412,16},{65413,16},{65414,16},{65415,16},{65416,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1400 | {28,5},{249,8},{1015,10},{4084,12},{65417,16},{65418,16},{65419,16},{65420,16},{65421,16},{65422,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1401 | {58,6},{503,9},{4085,12},{65423,16},{65424,16},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1402 | {59,6},{1016,10},{65430,16},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1403 | {122,7},{2039,11},{65438,16},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1404 | {123,7},{4086,12},{65446,16},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1405 | {250,8},{4087,12},{65454,16},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1406 | {504,9},{32704,15},{65462,16},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1407 | {505,9},{65470,16},{65471,16},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1408 | {506,9},{65479,16},{65480,16},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1409 | {1017,10},{65488,16},{65489,16},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1410 | {1018,10},{65497,16},{65498,16},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1411 | {2040,11},{65506,16},{65507,16},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1412 | {65515,16},{65516,16},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1413 | {2041,11},{65525,16},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1414 | }; 1415 | static const unsigned short UVAC_HT[256][2] = { 1416 | {0,2},{1,2},{4,3},{10,4},{24,5},{25,5},{56,6},{120,7},{500,9},{1014,10},{4084,12},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1417 | {11,4},{57,6},{246,8},{501,9},{2038,11},{4085,12},{65416,16},{65417,16},{65418,16},{65419,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1418 | {26,5},{247,8},{1015,10},{4086,12},{32706,15},{65420,16},{65421,16},{65422,16},{65423,16},{65424,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1419 | {27,5},{248,8},{1016,10},{4087,12},{65425,16},{65426,16},{65427,16},{65428,16},{65429,16},{65430,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1420 | {58,6},{502,9},{65431,16},{65432,16},{65433,16},{65434,16},{65435,16},{65436,16},{65437,16},{65438,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1421 | {59,6},{1017,10},{65439,16},{65440,16},{65441,16},{65442,16},{65443,16},{65444,16},{65445,16},{65446,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1422 | {121,7},{2039,11},{65447,16},{65448,16},{65449,16},{65450,16},{65451,16},{65452,16},{65453,16},{65454,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1423 | {122,7},{2040,11},{65455,16},{65456,16},{65457,16},{65458,16},{65459,16},{65460,16},{65461,16},{65462,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1424 | {249,8},{65463,16},{65464,16},{65465,16},{65466,16},{65467,16},{65468,16},{65469,16},{65470,16},{65471,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1425 | {503,9},{65472,16},{65473,16},{65474,16},{65475,16},{65476,16},{65477,16},{65478,16},{65479,16},{65480,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1426 | {504,9},{65481,16},{65482,16},{65483,16},{65484,16},{65485,16},{65486,16},{65487,16},{65488,16},{65489,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1427 | {505,9},{65490,16},{65491,16},{65492,16},{65493,16},{65494,16},{65495,16},{65496,16},{65497,16},{65498,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1428 | {506,9},{65499,16},{65500,16},{65501,16},{65502,16},{65503,16},{65504,16},{65505,16},{65506,16},{65507,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1429 | {2041,11},{65508,16},{65509,16},{65510,16},{65511,16},{65512,16},{65513,16},{65514,16},{65515,16},{65516,16},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}, 1430 | {16352,14},{65517,16},{65518,16},{65519,16},{65520,16},{65521,16},{65522,16},{65523,16},{65524,16},{65525,16},{0,0},{0,0},{0,0},{0,0},{0,0}, 1431 | {1018,10},{32707,15},{65526,16},{65527,16},{65528,16},{65529,16},{65530,16},{65531,16},{65532,16},{65533,16},{65534,16},{0,0},{0,0},{0,0},{0,0},{0,0} 1432 | }; 1433 | static const int YQT[] = {16,11,10,16,24,40,51,61,12,12,14,19,26,58,60,55,14,13,16,24,40,57,69,56,14,17,22,29,51,87,80,62,18,22, 1434 | 37,56,68,109,103,77,24,35,55,64,81,104,113,92,49,64,78,87,103,121,120,101,72,92,95,98,112,100,103,99}; 1435 | static const int UVQT[] = {17,18,24,47,99,99,99,99,18,21,26,66,99,99,99,99,24,26,56,99,99,99,99,99,47,66,99,99,99,99,99,99, 1436 | 99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99}; 1437 | static const float aasf[] = { 1.0f * 2.828427125f, 1.387039845f * 2.828427125f, 1.306562965f * 2.828427125f, 1.175875602f * 2.828427125f, 1438 | 1.0f * 2.828427125f, 0.785694958f * 2.828427125f, 0.541196100f * 2.828427125f, 0.275899379f * 2.828427125f }; 1439 | 1440 | int row, col, i, k, subsample; 1441 | float fdtbl_Y[64], fdtbl_UV[64]; 1442 | unsigned char YTable[64], UVTable[64]; 1443 | 1444 | if(!data || !width || !height || comp > 4 || comp < 1) { 1445 | return 0; 1446 | } 1447 | 1448 | quality = quality ? quality : 90; 1449 | subsample = quality <= 90 ? 1 : 0; 1450 | quality = quality < 1 ? 1 : quality > 100 ? 100 : quality; 1451 | quality = quality < 50 ? 5000 / quality : 200 - quality * 2; 1452 | 1453 | for(i = 0; i < 64; ++i) { 1454 | int uvti, yti = (YQT[i]*quality+50)/100; 1455 | YTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (yti < 1 ? 1 : yti > 255 ? 255 : yti); 1456 | uvti = (UVQT[i]*quality+50)/100; 1457 | UVTable[stbiw__jpg_ZigZag[i]] = (unsigned char) (uvti < 1 ? 1 : uvti > 255 ? 255 : uvti); 1458 | } 1459 | 1460 | for(row = 0, k = 0; row < 8; ++row) { 1461 | for(col = 0; col < 8; ++col, ++k) { 1462 | fdtbl_Y[k] = 1 / (YTable [stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1463 | fdtbl_UV[k] = 1 / (UVTable[stbiw__jpg_ZigZag[k]] * aasf[row] * aasf[col]); 1464 | } 1465 | } 1466 | 1467 | // Write Headers 1468 | { 1469 | static const unsigned char head0[] = { 0xFF,0xD8,0xFF,0xE0,0,0x10,'J','F','I','F',0,1,1,0,0,1,0,1,0,0,0xFF,0xDB,0,0x84,0 }; 1470 | static const unsigned char head2[] = { 0xFF,0xDA,0,0xC,3,1,0,2,0x11,3,0x11,0,0x3F,0 }; 1471 | const unsigned char head1[] = { 0xFF,0xC0,0,0x11,8,(unsigned char)(height>>8),STBIW_UCHAR(height),(unsigned char)(width>>8),STBIW_UCHAR(width), 1472 | 3,1,(unsigned char)(subsample?0x22:0x11),0,2,0x11,1,3,0x11,1,0xFF,0xC4,0x01,0xA2,0 }; 1473 | s->func(s->context, (void*)head0, sizeof(head0)); 1474 | s->func(s->context, (void*)YTable, sizeof(YTable)); 1475 | stbiw__putc(s, 1); 1476 | s->func(s->context, UVTable, sizeof(UVTable)); 1477 | s->func(s->context, (void*)head1, sizeof(head1)); 1478 | s->func(s->context, (void*)(std_dc_luminance_nrcodes+1), sizeof(std_dc_luminance_nrcodes)-1); 1479 | s->func(s->context, (void*)std_dc_luminance_values, sizeof(std_dc_luminance_values)); 1480 | stbiw__putc(s, 0x10); // HTYACinfo 1481 | s->func(s->context, (void*)(std_ac_luminance_nrcodes+1), sizeof(std_ac_luminance_nrcodes)-1); 1482 | s->func(s->context, (void*)std_ac_luminance_values, sizeof(std_ac_luminance_values)); 1483 | stbiw__putc(s, 1); // HTUDCinfo 1484 | s->func(s->context, (void*)(std_dc_chrominance_nrcodes+1), sizeof(std_dc_chrominance_nrcodes)-1); 1485 | s->func(s->context, (void*)std_dc_chrominance_values, sizeof(std_dc_chrominance_values)); 1486 | stbiw__putc(s, 0x11); // HTUACinfo 1487 | s->func(s->context, (void*)(std_ac_chrominance_nrcodes+1), sizeof(std_ac_chrominance_nrcodes)-1); 1488 | s->func(s->context, (void*)std_ac_chrominance_values, sizeof(std_ac_chrominance_values)); 1489 | s->func(s->context, (void*)head2, sizeof(head2)); 1490 | } 1491 | 1492 | // Encode 8x8 macroblocks 1493 | { 1494 | static const unsigned short fillBits[] = {0x7F, 7}; 1495 | int DCY=0, DCU=0, DCV=0; 1496 | int bitBuf=0, bitCnt=0; 1497 | // comp == 2 is grey+alpha (alpha is ignored) 1498 | int ofsG = comp > 2 ? 1 : 0, ofsB = comp > 2 ? 2 : 0; 1499 | const unsigned char *dataR = (const unsigned char *)data; 1500 | const unsigned char *dataG = dataR + ofsG; 1501 | const unsigned char *dataB = dataR + ofsB; 1502 | int x, y, pos; 1503 | if(subsample) { 1504 | for(y = 0; y < height; y += 16) { 1505 | for(x = 0; x < width; x += 16) { 1506 | float Y[256], U[256], V[256]; 1507 | for(row = y, pos = 0; row < y+16; ++row) { 1508 | // row >= height => use last input row 1509 | int clamped_row = (row < height) ? row : height - 1; 1510 | int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; 1511 | for(col = x; col < x+16; ++col, ++pos) { 1512 | // if col >= width => use pixel from last input column 1513 | int p = base_p + ((col < width) ? col : (width-1))*comp; 1514 | float r = dataR[p], g = dataG[p], b = dataB[p]; 1515 | Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; 1516 | U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; 1517 | V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; 1518 | } 1519 | } 1520 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+0, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1521 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+8, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1522 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+128, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1523 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y+136, 16, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1524 | 1525 | // subsample U,V 1526 | { 1527 | float subU[64], subV[64]; 1528 | int yy, xx; 1529 | for(yy = 0, pos = 0; yy < 8; ++yy) { 1530 | for(xx = 0; xx < 8; ++xx, ++pos) { 1531 | int j = yy*32+xx*2; 1532 | subU[pos] = (U[j+0] + U[j+1] + U[j+16] + U[j+17]) * 0.25f; 1533 | subV[pos] = (V[j+0] + V[j+1] + V[j+16] + V[j+17]) * 0.25f; 1534 | } 1535 | } 1536 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subU, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1537 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, subV, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1538 | } 1539 | } 1540 | } 1541 | } else { 1542 | for(y = 0; y < height; y += 8) { 1543 | for(x = 0; x < width; x += 8) { 1544 | float Y[64], U[64], V[64]; 1545 | for(row = y, pos = 0; row < y+8; ++row) { 1546 | // row >= height => use last input row 1547 | int clamped_row = (row < height) ? row : height - 1; 1548 | int base_p = (stbi__flip_vertically_on_write ? (height-1-clamped_row) : clamped_row)*width*comp; 1549 | for(col = x; col < x+8; ++col, ++pos) { 1550 | // if col >= width => use pixel from last input column 1551 | int p = base_p + ((col < width) ? col : (width-1))*comp; 1552 | float r = dataR[p], g = dataG[p], b = dataB[p]; 1553 | Y[pos]= +0.29900f*r + 0.58700f*g + 0.11400f*b - 128; 1554 | U[pos]= -0.16874f*r - 0.33126f*g + 0.50000f*b; 1555 | V[pos]= +0.50000f*r - 0.41869f*g - 0.08131f*b; 1556 | } 1557 | } 1558 | 1559 | DCY = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, Y, 8, fdtbl_Y, DCY, YDC_HT, YAC_HT); 1560 | DCU = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, U, 8, fdtbl_UV, DCU, UVDC_HT, UVAC_HT); 1561 | DCV = stbiw__jpg_processDU(s, &bitBuf, &bitCnt, V, 8, fdtbl_UV, DCV, UVDC_HT, UVAC_HT); 1562 | } 1563 | } 1564 | } 1565 | 1566 | // Do the bit alignment of the EOI marker 1567 | stbiw__jpg_writeBits(s, &bitBuf, &bitCnt, fillBits); 1568 | } 1569 | 1570 | // EOI 1571 | stbiw__putc(s, 0xFF); 1572 | stbiw__putc(s, 0xD9); 1573 | 1574 | return 1; 1575 | } 1576 | 1577 | STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) 1578 | { 1579 | stbi__write_context s = { 0 }; 1580 | stbi__start_write_callbacks(&s, func, context); 1581 | return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); 1582 | } 1583 | 1584 | 1585 | #ifndef STBI_WRITE_NO_STDIO 1586 | STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const void *data, int quality) 1587 | { 1588 | stbi__write_context s = { 0 }; 1589 | if (stbi__start_write_file(&s,filename)) { 1590 | int r = stbi_write_jpg_core(&s, x, y, comp, data, quality); 1591 | stbi__end_write_file(&s); 1592 | return r; 1593 | } else 1594 | return 0; 1595 | } 1596 | #endif 1597 | 1598 | #endif // STB_IMAGE_WRITE_IMPLEMENTATION 1599 | 1600 | /* Revision history 1601 | 1.14 (2020-02-02) updated JPEG writer to downsample chroma channels 1602 | 1.13 1603 | 1.12 1604 | 1.11 (2019-08-11) 1605 | 1606 | 1.10 (2019-02-07) 1607 | support utf8 filenames in Windows; fix warnings and platform ifdefs 1608 | 1.09 (2018-02-11) 1609 | fix typo in zlib quality API, improve STB_I_W_STATIC in C++ 1610 | 1.08 (2018-01-29) 1611 | add stbi__flip_vertically_on_write, external zlib, zlib quality, choose PNG filter 1612 | 1.07 (2017-07-24) 1613 | doc fix 1614 | 1.06 (2017-07-23) 1615 | writing JPEG (using Jon Olick's code) 1616 | 1.05 ??? 1617 | 1.04 (2017-03-03) 1618 | monochrome BMP expansion 1619 | 1.03 ??? 1620 | 1.02 (2016-04-02) 1621 | avoid allocating large structures on the stack 1622 | 1.01 (2016-01-16) 1623 | STBIW_REALLOC_SIZED: support allocators with no realloc support 1624 | avoid race-condition in crc initialization 1625 | minor compile issues 1626 | 1.00 (2015-09-14) 1627 | installable file IO function 1628 | 0.99 (2015-09-13) 1629 | warning fixes; TGA rle support 1630 | 0.98 (2015-04-08) 1631 | added STBIW_MALLOC, STBIW_ASSERT etc 1632 | 0.97 (2015-01-18) 1633 | fixed HDR asserts, rewrote HDR rle logic 1634 | 0.96 (2015-01-17) 1635 | add HDR output 1636 | fix monochrome BMP 1637 | 0.95 (2014-08-17) 1638 | add monochrome TGA output 1639 | 0.94 (2014-05-31) 1640 | rename private functions to avoid conflicts with stb_image.h 1641 | 0.93 (2014-05-27) 1642 | warning fixes 1643 | 0.92 (2010-08-01) 1644 | casts to unsigned char to fix warnings 1645 | 0.91 (2010-07-17) 1646 | first public release 1647 | 0.90 first internal release 1648 | */ 1649 | 1650 | /* 1651 | ------------------------------------------------------------------------------ 1652 | This software is available under 2 licenses -- choose whichever you prefer. 1653 | ------------------------------------------------------------------------------ 1654 | ALTERNATIVE A - MIT License 1655 | Copyright (c) 2017 Sean Barrett 1656 | Permission is hereby granted, free of charge, to any person obtaining a copy of 1657 | this software and associated documentation files (the "Software"), to deal in 1658 | the Software without restriction, including without limitation the rights to 1659 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 1660 | of the Software, and to permit persons to whom the Software is furnished to do 1661 | so, subject to the following conditions: 1662 | The above copyright notice and this permission notice shall be included in all 1663 | copies or substantial portions of the Software. 1664 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1665 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1666 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1667 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 1668 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 1669 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 1670 | SOFTWARE. 1671 | ------------------------------------------------------------------------------ 1672 | ALTERNATIVE B - Public Domain (www.unlicense.org) 1673 | This is free and unencumbered software released into the public domain. 1674 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 1675 | software, either in source code form or as a compiled binary, for any purpose, 1676 | commercial or non-commercial, and by any means. 1677 | In jurisdictions that recognize copyright laws, the author or authors of this 1678 | software dedicate any and all copyright interest in the software to the public 1679 | domain. We make this dedication for the benefit of the public at large and to 1680 | the detriment of our heirs and successors. We intend this dedication to be an 1681 | overt act of relinquishment in perpetuity of all present and future rights to 1682 | this software under copyright law. 1683 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 1684 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 1685 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 1686 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 1687 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 1688 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 1689 | ------------------------------------------------------------------------------ 1690 | */ -------------------------------------------------------------------------------- /example/include/std_image_resize.h: -------------------------------------------------------------------------------- 1 | /* stb_image_resize - v0.96 - public domain image resizing 2 | by Jorge L Rodriguez (@VinoBS) - 2014 3 | http://github.com/nothings/stb 4 | 5 | Written with emphasis on usability, portability, and efficiency. (No 6 | SIMD or threads, so it be easily outperformed by libs that use those.) 7 | Only scaling and translation is supported, no rotations or shears. 8 | Easy API downsamples w/Mitchell filter, upsamples w/cubic interpolation. 9 | 10 | COMPILING & LINKING 11 | In one C/C++ file that #includes this file, do this: 12 | #define STB_IMAGE_RESIZE_IMPLEMENTATION 13 | before the #include. That will create the implementation in that file. 14 | 15 | QUICKSTART 16 | stbir_resize_uint8( input_pixels , in_w , in_h , 0, 17 | output_pixels, out_w, out_h, 0, num_channels) 18 | stbir_resize_float(...) 19 | stbir_resize_uint8_srgb( input_pixels , in_w , in_h , 0, 20 | output_pixels, out_w, out_h, 0, 21 | num_channels , alpha_chan , 0) 22 | stbir_resize_uint8_srgb_edgemode( 23 | input_pixels , in_w , in_h , 0, 24 | output_pixels, out_w, out_h, 0, 25 | num_channels , alpha_chan , 0, STBIR_EDGE_CLAMP) 26 | // WRAP/REFLECT/ZERO 27 | 28 | FULL API 29 | See the "header file" section of the source for API documentation. 30 | 31 | ADDITIONAL DOCUMENTATION 32 | 33 | SRGB & FLOATING POINT REPRESENTATION 34 | The sRGB functions presume IEEE floating point. If you do not have 35 | IEEE floating point, define STBIR_NON_IEEE_FLOAT. This will use 36 | a slower implementation. 37 | 38 | MEMORY ALLOCATION 39 | The resize functions here perform a single memory allocation using 40 | malloc. To control the memory allocation, before the #include that 41 | triggers the implementation, do: 42 | 43 | #define STBIR_MALLOC(size,context) ... 44 | #define STBIR_FREE(ptr,context) ... 45 | 46 | Each resize function makes exactly one call to malloc/free, so to use 47 | temp memory, store the temp memory in the context and return that. 48 | 49 | ASSERT 50 | Define STBIR_ASSERT(boolval) to override assert() and not use assert.h 51 | 52 | OPTIMIZATION 53 | Define STBIR_SATURATE_INT to compute clamp values in-range using 54 | integer operations instead of float operations. This may be faster 55 | on some platforms. 56 | 57 | DEFAULT FILTERS 58 | For functions which don't provide explicit control over what filters 59 | to use, you can change the compile-time defaults with 60 | 61 | #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_something 62 | #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_something 63 | 64 | See stbir_filter in the header-file section for the list of filters. 65 | 66 | NEW FILTERS 67 | A number of 1D filter kernels are used. For a list of 68 | supported filters see the stbir_filter enum. To add a new filter, 69 | write a filter function and add it to stbir__filter_info_table. 70 | 71 | PROGRESS 72 | For interactive use with slow resize operations, you can install 73 | a progress-report callback: 74 | 75 | #define STBIR_PROGRESS_REPORT(val) some_func(val) 76 | 77 | The parameter val is a float which goes from 0 to 1 as progress is made. 78 | 79 | For example: 80 | 81 | static void my_progress_report(float progress); 82 | #define STBIR_PROGRESS_REPORT(val) my_progress_report(val) 83 | 84 | #define STB_IMAGE_RESIZE_IMPLEMENTATION 85 | #include "stb_image_resize.h" 86 | 87 | static void my_progress_report(float progress) 88 | { 89 | printf("Progress: %f%%\n", progress*100); 90 | } 91 | 92 | MAX CHANNELS 93 | If your image has more than 64 channels, define STBIR_MAX_CHANNELS 94 | to the max you'll have. 95 | 96 | ALPHA CHANNEL 97 | Most of the resizing functions provide the ability to control how 98 | the alpha channel of an image is processed. The important things 99 | to know about this: 100 | 101 | 1. The best mathematically-behaved version of alpha to use is 102 | called "premultiplied alpha", in which the other color channels 103 | have had the alpha value multiplied in. If you use premultiplied 104 | alpha, linear filtering (such as image resampling done by this 105 | library, or performed in texture units on GPUs) does the "right 106 | thing". While premultiplied alpha is standard in the movie CGI 107 | industry, it is still uncommon in the videogame/real-time world. 108 | 109 | If you linearly filter non-premultiplied alpha, strange effects 110 | occur. (For example, the 50/50 average of 99% transparent bright green 111 | and 1% transparent black produces 50% transparent dark green when 112 | non-premultiplied, whereas premultiplied it produces 50% 113 | transparent near-black. The former introduces green energy 114 | that doesn't exist in the source image.) 115 | 116 | 2. Artists should not edit premultiplied-alpha images; artists 117 | want non-premultiplied alpha images. Thus, art tools generally output 118 | non-premultiplied alpha images. 119 | 120 | 3. You will get best results in most cases by converting images 121 | to premultiplied alpha before processing them mathematically. 122 | 123 | 4. If you pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED, the 124 | resizer does not do anything special for the alpha channel; 125 | it is resampled identically to other channels. This produces 126 | the correct results for premultiplied-alpha images, but produces 127 | less-than-ideal results for non-premultiplied-alpha images. 128 | 129 | 5. If you do not pass the flag STBIR_FLAG_ALPHA_PREMULTIPLIED, 130 | then the resizer weights the contribution of input pixels 131 | based on their alpha values, or, equivalently, it multiplies 132 | the alpha value into the color channels, resamples, then divides 133 | by the resultant alpha value. Input pixels which have alpha=0 do 134 | not contribute at all to output pixels unless _all_ of the input 135 | pixels affecting that output pixel have alpha=0, in which case 136 | the result for that pixel is the same as it would be without 137 | STBIR_FLAG_ALPHA_PREMULTIPLIED. However, this is only true for 138 | input images in integer formats. For input images in float format, 139 | input pixels with alpha=0 have no effect, and output pixels 140 | which have alpha=0 will be 0 in all channels. (For float images, 141 | you can manually achieve the same result by adding a tiny epsilon 142 | value to the alpha channel of every image, and then subtracting 143 | or clamping it at the end.) 144 | 145 | 6. You can suppress the behavior described in #5 and make 146 | all-0-alpha pixels have 0 in all channels by #defining 147 | STBIR_NO_ALPHA_EPSILON. 148 | 149 | 7. You can separately control whether the alpha channel is 150 | interpreted as linear or affected by the colorspace. By default 151 | it is linear; you almost never want to apply the colorspace. 152 | (For example, graphics hardware does not apply sRGB conversion 153 | to the alpha channel.) 154 | 155 | CONTRIBUTORS 156 | Jorge L Rodriguez: Implementation 157 | Sean Barrett: API design, optimizations 158 | Aras Pranckevicius: bugfix 159 | Nathan Reed: warning fixes 160 | 161 | REVISIONS 162 | 0.97 (2020-02-02) fixed warning 163 | 0.96 (2019-03-04) fixed warnings 164 | 0.95 (2017-07-23) fixed warnings 165 | 0.94 (2017-03-18) fixed warnings 166 | 0.93 (2017-03-03) fixed bug with certain combinations of heights 167 | 0.92 (2017-01-02) fix integer overflow on large (>2GB) images 168 | 0.91 (2016-04-02) fix warnings; fix handling of subpixel regions 169 | 0.90 (2014-09-17) first released version 170 | 171 | LICENSE 172 | See end of file for license information. 173 | 174 | TODO 175 | Don't decode all of the image data when only processing a partial tile 176 | Don't use full-width decode buffers when only processing a partial tile 177 | When processing wide images, break processing into tiles so data fits in L1 cache 178 | Installable filters? 179 | Resize that respects alpha test coverage 180 | (Reference code: FloatImage::alphaTestCoverage and FloatImage::scaleAlphaToCoverage: 181 | https://code.google.com/p/nvidia-texture-tools/source/browse/trunk/src/nvimage/FloatImage.cpp ) 182 | */ 183 | 184 | #ifndef STBIR_INCLUDE_STB_IMAGE_RESIZE_H 185 | #define STBIR_INCLUDE_STB_IMAGE_RESIZE_H 186 | 187 | #ifdef _MSC_VER 188 | typedef unsigned char stbir_uint8; 189 | typedef unsigned short stbir_uint16; 190 | typedef unsigned int stbir_uint32; 191 | #else 192 | #include 193 | typedef uint8_t stbir_uint8; 194 | typedef uint16_t stbir_uint16; 195 | typedef uint32_t stbir_uint32; 196 | #endif 197 | 198 | #ifndef STBIRDEF 199 | #ifdef STB_IMAGE_RESIZE_STATIC 200 | #define STBIRDEF static 201 | #else 202 | #ifdef __cplusplus 203 | #define STBIRDEF extern "C" 204 | #else 205 | #define STBIRDEF extern 206 | #endif 207 | #endif 208 | #endif 209 | 210 | ////////////////////////////////////////////////////////////////////////////// 211 | // 212 | // Easy-to-use API: 213 | // 214 | // * "input pixels" points to an array of image data with 'num_channels' channels (e.g. RGB=3, RGBA=4) 215 | // * input_w is input image width (x-axis), input_h is input image height (y-axis) 216 | // * stride is the offset between successive rows of image data in memory, in bytes. you can 217 | // specify 0 to mean packed continuously in memory 218 | // * alpha channel is treated identically to other channels. 219 | // * colorspace is linear or sRGB as specified by function name 220 | // * returned result is 1 for success or 0 in case of an error. 221 | // #define STBIR_ASSERT() to trigger an assert on parameter validation errors. 222 | // * Memory required grows approximately linearly with input and output size, but with 223 | // discontinuities at input_w == output_w and input_h == output_h. 224 | // * These functions use a "default" resampling filter defined at compile time. To change the filter, 225 | // you can change the compile-time defaults by #defining STBIR_DEFAULT_FILTER_UPSAMPLE 226 | // and STBIR_DEFAULT_FILTER_DOWNSAMPLE, or you can use the medium-complexity API. 227 | 228 | STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 229 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 230 | int num_channels); 231 | 232 | STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 233 | float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 234 | int num_channels); 235 | 236 | 237 | // The following functions interpret image data as gamma-corrected sRGB. 238 | // Specify STBIR_ALPHA_CHANNEL_NONE if you have no alpha channel, 239 | // or otherwise provide the index of the alpha channel. Flags value 240 | // of 0 will probably do the right thing if you're not sure what 241 | // the flags mean. 242 | 243 | #define STBIR_ALPHA_CHANNEL_NONE -1 244 | 245 | // Set this flag if your texture has premultiplied alpha. Otherwise, stbir will 246 | // use alpha-weighted resampling (effectively premultiplying, resampling, 247 | // then unpremultiplying). 248 | #define STBIR_FLAG_ALPHA_PREMULTIPLIED (1 << 0) 249 | // The specified alpha channel should be handled as gamma-corrected value even 250 | // when doing sRGB operations. 251 | #define STBIR_FLAG_ALPHA_USES_COLORSPACE (1 << 1) 252 | 253 | STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 254 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 255 | int num_channels, int alpha_channel, int flags); 256 | 257 | 258 | typedef enum 259 | { 260 | STBIR_EDGE_CLAMP = 1, 261 | STBIR_EDGE_REFLECT = 2, 262 | STBIR_EDGE_WRAP = 3, 263 | STBIR_EDGE_ZERO = 4, 264 | } stbir_edge; 265 | 266 | // This function adds the ability to specify how requests to sample off the edge of the image are handled. 267 | STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 268 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 269 | int num_channels, int alpha_channel, int flags, 270 | stbir_edge edge_wrap_mode); 271 | 272 | ////////////////////////////////////////////////////////////////////////////// 273 | // 274 | // Medium-complexity API 275 | // 276 | // This extends the easy-to-use API as follows: 277 | // 278 | // * Alpha-channel can be processed separately 279 | // * If alpha_channel is not STBIR_ALPHA_CHANNEL_NONE 280 | // * Alpha channel will not be gamma corrected (unless flags&STBIR_FLAG_GAMMA_CORRECT) 281 | // * Filters will be weighted by alpha channel (unless flags&STBIR_FLAG_ALPHA_PREMULTIPLIED) 282 | // * Filter can be selected explicitly 283 | // * uint16 image type 284 | // * sRGB colorspace available for all types 285 | // * context parameter for passing to STBIR_MALLOC 286 | 287 | typedef enum 288 | { 289 | STBIR_FILTER_DEFAULT = 0, // use same filter type that easy-to-use API chooses 290 | STBIR_FILTER_BOX = 1, // A trapezoid w/1-pixel wide ramps, same result as box for integer scale ratios 291 | STBIR_FILTER_TRIANGLE = 2, // On upsampling, produces same results as bilinear texture filtering 292 | STBIR_FILTER_CUBICBSPLINE = 3, // The cubic b-spline (aka Mitchell-Netrevalli with B=1,C=0), gaussian-esque 293 | STBIR_FILTER_CATMULLROM = 4, // An interpolating cubic spline 294 | STBIR_FILTER_MITCHELL = 5, // Mitchell-Netrevalli filter with B=1/3, C=1/3 295 | } stbir_filter; 296 | 297 | typedef enum 298 | { 299 | STBIR_COLORSPACE_LINEAR, 300 | STBIR_COLORSPACE_SRGB, 301 | 302 | STBIR_MAX_COLORSPACES, 303 | } stbir_colorspace; 304 | 305 | // The following functions are all identical except for the type of the image data 306 | 307 | STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 308 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 309 | int num_channels, int alpha_channel, int flags, 310 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 311 | void *alloc_context); 312 | 313 | STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 314 | stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, 315 | int num_channels, int alpha_channel, int flags, 316 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 317 | void *alloc_context); 318 | 319 | STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 320 | float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, 321 | int num_channels, int alpha_channel, int flags, 322 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 323 | void *alloc_context); 324 | 325 | 326 | 327 | ////////////////////////////////////////////////////////////////////////////// 328 | // 329 | // Full-complexity API 330 | // 331 | // This extends the medium API as follows: 332 | // 333 | // * uint32 image type 334 | // * not typesafe 335 | // * separate filter types for each axis 336 | // * separate edge modes for each axis 337 | // * can specify scale explicitly for subpixel correctness 338 | // * can specify image source tile using texture coordinates 339 | 340 | typedef enum 341 | { 342 | STBIR_TYPE_UINT8 , 343 | STBIR_TYPE_UINT16, 344 | STBIR_TYPE_UINT32, 345 | STBIR_TYPE_FLOAT , 346 | 347 | STBIR_MAX_TYPES 348 | } stbir_datatype; 349 | 350 | STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 351 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 352 | stbir_datatype datatype, 353 | int num_channels, int alpha_channel, int flags, 354 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 355 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 356 | stbir_colorspace space, void *alloc_context); 357 | 358 | STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 359 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 360 | stbir_datatype datatype, 361 | int num_channels, int alpha_channel, int flags, 362 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 363 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 364 | stbir_colorspace space, void *alloc_context, 365 | float x_scale, float y_scale, 366 | float x_offset, float y_offset); 367 | 368 | STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 369 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 370 | stbir_datatype datatype, 371 | int num_channels, int alpha_channel, int flags, 372 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 373 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 374 | stbir_colorspace space, void *alloc_context, 375 | float s0, float t0, float s1, float t1); 376 | // (s0, t0) & (s1, t1) are the top-left and bottom right corner (uv addressing style: [0, 1]x[0, 1]) of a region of the input image to use. 377 | 378 | // 379 | // 380 | //// end header file ///////////////////////////////////////////////////// 381 | #endif // STBIR_INCLUDE_STB_IMAGE_RESIZE_H 382 | 383 | 384 | 385 | 386 | 387 | #ifdef STB_IMAGE_RESIZE_IMPLEMENTATION 388 | 389 | #ifndef STBIR_ASSERT 390 | #include 391 | #define STBIR_ASSERT(x) assert(x) 392 | #endif 393 | 394 | // For memset 395 | #include 396 | 397 | #include 398 | 399 | #ifndef STBIR_MALLOC 400 | #include 401 | // use comma operator to evaluate c, to avoid "unused parameter" warnings 402 | #define STBIR_MALLOC(size,c) ((void)(c), malloc(size)) 403 | #define STBIR_FREE(ptr,c) ((void)(c), free(ptr)) 404 | #endif 405 | 406 | #ifndef _MSC_VER 407 | #ifdef __cplusplus 408 | #define stbir__inline inline 409 | #else 410 | #define stbir__inline 411 | #endif 412 | #else 413 | #define stbir__inline __forceinline 414 | #endif 415 | 416 | 417 | // should produce compiler error if size is wrong 418 | typedef unsigned char stbir__validate_uint32[sizeof(stbir_uint32) == 4 ? 1 : -1]; 419 | 420 | #ifdef _MSC_VER 421 | #define STBIR__NOTUSED(v) (void)(v) 422 | #else 423 | #define STBIR__NOTUSED(v) (void)sizeof(v) 424 | #endif 425 | 426 | #define STBIR__ARRAY_SIZE(a) (sizeof((a))/sizeof((a)[0])) 427 | 428 | #ifndef STBIR_DEFAULT_FILTER_UPSAMPLE 429 | #define STBIR_DEFAULT_FILTER_UPSAMPLE STBIR_FILTER_CATMULLROM 430 | #endif 431 | 432 | #ifndef STBIR_DEFAULT_FILTER_DOWNSAMPLE 433 | #define STBIR_DEFAULT_FILTER_DOWNSAMPLE STBIR_FILTER_MITCHELL 434 | #endif 435 | 436 | #ifndef STBIR_PROGRESS_REPORT 437 | #define STBIR_PROGRESS_REPORT(float_0_to_1) 438 | #endif 439 | 440 | #ifndef STBIR_MAX_CHANNELS 441 | #define STBIR_MAX_CHANNELS 64 442 | #endif 443 | 444 | #if STBIR_MAX_CHANNELS > 65536 445 | #error "Too many channels; STBIR_MAX_CHANNELS must be no more than 65536." 446 | // because we store the indices in 16-bit variables 447 | #endif 448 | 449 | // This value is added to alpha just before premultiplication to avoid 450 | // zeroing out color values. It is equivalent to 2^-80. If you don't want 451 | // that behavior (it may interfere if you have floating point images with 452 | // very small alpha values) then you can define STBIR_NO_ALPHA_EPSILON to 453 | // disable it. 454 | #ifndef STBIR_ALPHA_EPSILON 455 | #define STBIR_ALPHA_EPSILON ((float)1 / (1 << 20) / (1 << 20) / (1 << 20) / (1 << 20)) 456 | #endif 457 | 458 | 459 | 460 | #ifdef _MSC_VER 461 | #define STBIR__UNUSED_PARAM(v) (void)(v) 462 | #else 463 | #define STBIR__UNUSED_PARAM(v) (void)sizeof(v) 464 | #endif 465 | 466 | // must match stbir_datatype 467 | static unsigned char stbir__type_size[] = { 468 | 1, // STBIR_TYPE_UINT8 469 | 2, // STBIR_TYPE_UINT16 470 | 4, // STBIR_TYPE_UINT32 471 | 4, // STBIR_TYPE_FLOAT 472 | }; 473 | 474 | // Kernel function centered at 0 475 | typedef float (stbir__kernel_fn)(float x, float scale); 476 | typedef float (stbir__support_fn)(float scale); 477 | 478 | typedef struct 479 | { 480 | stbir__kernel_fn* kernel; 481 | stbir__support_fn* support; 482 | } stbir__filter_info; 483 | 484 | // When upsampling, the contributors are which source pixels contribute. 485 | // When downsampling, the contributors are which destination pixels are contributed to. 486 | typedef struct 487 | { 488 | int n0; // First contributing pixel 489 | int n1; // Last contributing pixel 490 | } stbir__contributors; 491 | 492 | typedef struct 493 | { 494 | const void* input_data; 495 | int input_w; 496 | int input_h; 497 | int input_stride_bytes; 498 | 499 | void* output_data; 500 | int output_w; 501 | int output_h; 502 | int output_stride_bytes; 503 | 504 | float s0, t0, s1, t1; 505 | 506 | float horizontal_shift; // Units: output pixels 507 | float vertical_shift; // Units: output pixels 508 | float horizontal_scale; 509 | float vertical_scale; 510 | 511 | int channels; 512 | int alpha_channel; 513 | stbir_uint32 flags; 514 | stbir_datatype type; 515 | stbir_filter horizontal_filter; 516 | stbir_filter vertical_filter; 517 | stbir_edge edge_horizontal; 518 | stbir_edge edge_vertical; 519 | stbir_colorspace colorspace; 520 | 521 | stbir__contributors* horizontal_contributors; 522 | float* horizontal_coefficients; 523 | 524 | stbir__contributors* vertical_contributors; 525 | float* vertical_coefficients; 526 | 527 | int decode_buffer_pixels; 528 | float* decode_buffer; 529 | 530 | float* horizontal_buffer; 531 | 532 | // cache these because ceil/floor are inexplicably showing up in profile 533 | int horizontal_coefficient_width; 534 | int vertical_coefficient_width; 535 | int horizontal_filter_pixel_width; 536 | int vertical_filter_pixel_width; 537 | int horizontal_filter_pixel_margin; 538 | int vertical_filter_pixel_margin; 539 | int horizontal_num_contributors; 540 | int vertical_num_contributors; 541 | 542 | int ring_buffer_length_bytes; // The length of an individual entry in the ring buffer. The total number of ring buffers is stbir__get_filter_pixel_width(filter) 543 | int ring_buffer_num_entries; // Total number of entries in the ring buffer. 544 | int ring_buffer_first_scanline; 545 | int ring_buffer_last_scanline; 546 | int ring_buffer_begin_index; // first_scanline is at this index in the ring buffer 547 | float* ring_buffer; 548 | 549 | float* encode_buffer; // A temporary buffer to store floats so we don't lose precision while we do multiply-adds. 550 | 551 | int horizontal_contributors_size; 552 | int horizontal_coefficients_size; 553 | int vertical_contributors_size; 554 | int vertical_coefficients_size; 555 | int decode_buffer_size; 556 | int horizontal_buffer_size; 557 | int ring_buffer_size; 558 | int encode_buffer_size; 559 | } stbir__info; 560 | 561 | 562 | static const float stbir__max_uint8_as_float = 255.0f; 563 | static const float stbir__max_uint16_as_float = 65535.0f; 564 | static const double stbir__max_uint32_as_float = 4294967295.0; 565 | 566 | 567 | static stbir__inline int stbir__min(int a, int b) 568 | { 569 | return a < b ? a : b; 570 | } 571 | 572 | static stbir__inline float stbir__saturate(float x) 573 | { 574 | if (x < 0) 575 | return 0; 576 | 577 | if (x > 1) 578 | return 1; 579 | 580 | return x; 581 | } 582 | 583 | #ifdef STBIR_SATURATE_INT 584 | static stbir__inline stbir_uint8 stbir__saturate8(int x) 585 | { 586 | if ((unsigned int) x <= 255) 587 | return x; 588 | 589 | if (x < 0) 590 | return 0; 591 | 592 | return 255; 593 | } 594 | 595 | static stbir__inline stbir_uint16 stbir__saturate16(int x) 596 | { 597 | if ((unsigned int) x <= 65535) 598 | return x; 599 | 600 | if (x < 0) 601 | return 0; 602 | 603 | return 65535; 604 | } 605 | #endif 606 | 607 | static float stbir__srgb_uchar_to_linear_float[256] = { 608 | 0.000000f, 0.000304f, 0.000607f, 0.000911f, 0.001214f, 0.001518f, 0.001821f, 0.002125f, 0.002428f, 0.002732f, 0.003035f, 609 | 0.003347f, 0.003677f, 0.004025f, 0.004391f, 0.004777f, 0.005182f, 0.005605f, 0.006049f, 0.006512f, 0.006995f, 0.007499f, 610 | 0.008023f, 0.008568f, 0.009134f, 0.009721f, 0.010330f, 0.010960f, 0.011612f, 0.012286f, 0.012983f, 0.013702f, 0.014444f, 611 | 0.015209f, 0.015996f, 0.016807f, 0.017642f, 0.018500f, 0.019382f, 0.020289f, 0.021219f, 0.022174f, 0.023153f, 0.024158f, 612 | 0.025187f, 0.026241f, 0.027321f, 0.028426f, 0.029557f, 0.030713f, 0.031896f, 0.033105f, 0.034340f, 0.035601f, 0.036889f, 613 | 0.038204f, 0.039546f, 0.040915f, 0.042311f, 0.043735f, 0.045186f, 0.046665f, 0.048172f, 0.049707f, 0.051269f, 0.052861f, 614 | 0.054480f, 0.056128f, 0.057805f, 0.059511f, 0.061246f, 0.063010f, 0.064803f, 0.066626f, 0.068478f, 0.070360f, 0.072272f, 615 | 0.074214f, 0.076185f, 0.078187f, 0.080220f, 0.082283f, 0.084376f, 0.086500f, 0.088656f, 0.090842f, 0.093059f, 0.095307f, 616 | 0.097587f, 0.099899f, 0.102242f, 0.104616f, 0.107023f, 0.109462f, 0.111932f, 0.114435f, 0.116971f, 0.119538f, 0.122139f, 617 | 0.124772f, 0.127438f, 0.130136f, 0.132868f, 0.135633f, 0.138432f, 0.141263f, 0.144128f, 0.147027f, 0.149960f, 0.152926f, 618 | 0.155926f, 0.158961f, 0.162029f, 0.165132f, 0.168269f, 0.171441f, 0.174647f, 0.177888f, 0.181164f, 0.184475f, 0.187821f, 619 | 0.191202f, 0.194618f, 0.198069f, 0.201556f, 0.205079f, 0.208637f, 0.212231f, 0.215861f, 0.219526f, 0.223228f, 0.226966f, 620 | 0.230740f, 0.234551f, 0.238398f, 0.242281f, 0.246201f, 0.250158f, 0.254152f, 0.258183f, 0.262251f, 0.266356f, 0.270498f, 621 | 0.274677f, 0.278894f, 0.283149f, 0.287441f, 0.291771f, 0.296138f, 0.300544f, 0.304987f, 0.309469f, 0.313989f, 0.318547f, 622 | 0.323143f, 0.327778f, 0.332452f, 0.337164f, 0.341914f, 0.346704f, 0.351533f, 0.356400f, 0.361307f, 0.366253f, 0.371238f, 623 | 0.376262f, 0.381326f, 0.386430f, 0.391573f, 0.396755f, 0.401978f, 0.407240f, 0.412543f, 0.417885f, 0.423268f, 0.428691f, 624 | 0.434154f, 0.439657f, 0.445201f, 0.450786f, 0.456411f, 0.462077f, 0.467784f, 0.473532f, 0.479320f, 0.485150f, 0.491021f, 625 | 0.496933f, 0.502887f, 0.508881f, 0.514918f, 0.520996f, 0.527115f, 0.533276f, 0.539480f, 0.545725f, 0.552011f, 0.558340f, 626 | 0.564712f, 0.571125f, 0.577581f, 0.584078f, 0.590619f, 0.597202f, 0.603827f, 0.610496f, 0.617207f, 0.623960f, 0.630757f, 627 | 0.637597f, 0.644480f, 0.651406f, 0.658375f, 0.665387f, 0.672443f, 0.679543f, 0.686685f, 0.693872f, 0.701102f, 0.708376f, 628 | 0.715694f, 0.723055f, 0.730461f, 0.737911f, 0.745404f, 0.752942f, 0.760525f, 0.768151f, 0.775822f, 0.783538f, 0.791298f, 629 | 0.799103f, 0.806952f, 0.814847f, 0.822786f, 0.830770f, 0.838799f, 0.846873f, 0.854993f, 0.863157f, 0.871367f, 0.879622f, 630 | 0.887923f, 0.896269f, 0.904661f, 0.913099f, 0.921582f, 0.930111f, 0.938686f, 0.947307f, 0.955974f, 0.964686f, 0.973445f, 631 | 0.982251f, 0.991102f, 1.0f 632 | }; 633 | 634 | static float stbir__srgb_to_linear(float f) 635 | { 636 | if (f <= 0.04045f) 637 | return f / 12.92f; 638 | else 639 | return (float)pow((f + 0.055f) / 1.055f, 2.4f); 640 | } 641 | 642 | static float stbir__linear_to_srgb(float f) 643 | { 644 | if (f <= 0.0031308f) 645 | return f * 12.92f; 646 | else 647 | return 1.055f * (float)pow(f, 1 / 2.4f) - 0.055f; 648 | } 649 | 650 | #ifndef STBIR_NON_IEEE_FLOAT 651 | // From https://gist.github.com/rygorous/2203834 652 | 653 | typedef union 654 | { 655 | stbir_uint32 u; 656 | float f; 657 | } stbir__FP32; 658 | 659 | static const stbir_uint32 fp32_to_srgb8_tab4[104] = { 660 | 0x0073000d, 0x007a000d, 0x0080000d, 0x0087000d, 0x008d000d, 0x0094000d, 0x009a000d, 0x00a1000d, 661 | 0x00a7001a, 0x00b4001a, 0x00c1001a, 0x00ce001a, 0x00da001a, 0x00e7001a, 0x00f4001a, 0x0101001a, 662 | 0x010e0033, 0x01280033, 0x01410033, 0x015b0033, 0x01750033, 0x018f0033, 0x01a80033, 0x01c20033, 663 | 0x01dc0067, 0x020f0067, 0x02430067, 0x02760067, 0x02aa0067, 0x02dd0067, 0x03110067, 0x03440067, 664 | 0x037800ce, 0x03df00ce, 0x044600ce, 0x04ad00ce, 0x051400ce, 0x057b00c5, 0x05dd00bc, 0x063b00b5, 665 | 0x06970158, 0x07420142, 0x07e30130, 0x087b0120, 0x090b0112, 0x09940106, 0x0a1700fc, 0x0a9500f2, 666 | 0x0b0f01cb, 0x0bf401ae, 0x0ccb0195, 0x0d950180, 0x0e56016e, 0x0f0d015e, 0x0fbc0150, 0x10630143, 667 | 0x11070264, 0x1238023e, 0x1357021d, 0x14660201, 0x156601e9, 0x165a01d3, 0x174401c0, 0x182401af, 668 | 0x18fe0331, 0x1a9602fe, 0x1c1502d2, 0x1d7e02ad, 0x1ed4028d, 0x201a0270, 0x21520256, 0x227d0240, 669 | 0x239f0443, 0x25c003fe, 0x27bf03c4, 0x29a10392, 0x2b6a0367, 0x2d1d0341, 0x2ebe031f, 0x304d0300, 670 | 0x31d105b0, 0x34a80555, 0x37520507, 0x39d504c5, 0x3c37048b, 0x3e7c0458, 0x40a8042a, 0x42bd0401, 671 | 0x44c20798, 0x488e071e, 0x4c1c06b6, 0x4f76065d, 0x52a50610, 0x55ac05cc, 0x5892058f, 0x5b590559, 672 | 0x5e0c0a23, 0x631c0980, 0x67db08f6, 0x6c55087f, 0x70940818, 0x74a007bd, 0x787d076c, 0x7c330723, 673 | }; 674 | 675 | static stbir_uint8 stbir__linear_to_srgb_uchar(float in) 676 | { 677 | static const stbir__FP32 almostone = { 0x3f7fffff }; // 1-eps 678 | static const stbir__FP32 minval = { (127-13) << 23 }; 679 | stbir_uint32 tab,bias,scale,t; 680 | stbir__FP32 f; 681 | 682 | // Clamp to [2^(-13), 1-eps]; these two values map to 0 and 1, respectively. 683 | // The tests are carefully written so that NaNs map to 0, same as in the reference 684 | // implementation. 685 | if (!(in > minval.f)) // written this way to catch NaNs 686 | in = minval.f; 687 | if (in > almostone.f) 688 | in = almostone.f; 689 | 690 | // Do the table lookup and unpack bias, scale 691 | f.f = in; 692 | tab = fp32_to_srgb8_tab4[(f.u - minval.u) >> 20]; 693 | bias = (tab >> 16) << 9; 694 | scale = tab & 0xffff; 695 | 696 | // Grab next-highest mantissa bits and perform linear interpolation 697 | t = (f.u >> 12) & 0xff; 698 | return (unsigned char) ((bias + scale*t) >> 16); 699 | } 700 | 701 | #else 702 | // sRGB transition values, scaled by 1<<28 703 | static int stbir__srgb_offset_to_linear_scaled[256] = 704 | { 705 | 0, 40738, 122216, 203693, 285170, 366648, 448125, 529603, 706 | 611080, 692557, 774035, 855852, 942009, 1033024, 1128971, 1229926, 707 | 1335959, 1447142, 1563542, 1685229, 1812268, 1944725, 2082664, 2226148, 708 | 2375238, 2529996, 2690481, 2856753, 3028870, 3206888, 3390865, 3580856, 709 | 3776916, 3979100, 4187460, 4402049, 4622919, 4850123, 5083710, 5323731, 710 | 5570236, 5823273, 6082892, 6349140, 6622065, 6901714, 7188133, 7481369, 711 | 7781466, 8088471, 8402427, 8723380, 9051372, 9386448, 9728650, 10078021, 712 | 10434603, 10798439, 11169569, 11548036, 11933879, 12327139, 12727857, 13136073, 713 | 13551826, 13975156, 14406100, 14844697, 15290987, 15745007, 16206795, 16676389, 714 | 17153826, 17639142, 18132374, 18633560, 19142734, 19659934, 20185196, 20718552, 715 | 21260042, 21809696, 22367554, 22933648, 23508010, 24090680, 24681686, 25281066, 716 | 25888850, 26505076, 27129772, 27762974, 28404716, 29055026, 29713942, 30381490, 717 | 31057708, 31742624, 32436272, 33138682, 33849884, 34569912, 35298800, 36036568, 718 | 36783260, 37538896, 38303512, 39077136, 39859796, 40651528, 41452360, 42262316, 719 | 43081432, 43909732, 44747252, 45594016, 46450052, 47315392, 48190064, 49074096, 720 | 49967516, 50870356, 51782636, 52704392, 53635648, 54576432, 55526772, 56486700, 721 | 57456236, 58435408, 59424248, 60422780, 61431036, 62449032, 63476804, 64514376, 722 | 65561776, 66619028, 67686160, 68763192, 69850160, 70947088, 72053992, 73170912, 723 | 74297864, 75434880, 76581976, 77739184, 78906536, 80084040, 81271736, 82469648, 724 | 83677792, 84896192, 86124888, 87363888, 88613232, 89872928, 91143016, 92423512, 725 | 93714432, 95015816, 96327688, 97650056, 98982952, 100326408, 101680440, 103045072, 726 | 104420320, 105806224, 107202800, 108610064, 110028048, 111456776, 112896264, 114346544, 727 | 115807632, 117279552, 118762328, 120255976, 121760536, 123276016, 124802440, 126339832, 728 | 127888216, 129447616, 131018048, 132599544, 134192112, 135795792, 137410592, 139036528, 729 | 140673648, 142321952, 143981456, 145652208, 147334208, 149027488, 150732064, 152447968, 730 | 154175200, 155913792, 157663776, 159425168, 161197984, 162982240, 164777968, 166585184, 731 | 168403904, 170234160, 172075968, 173929344, 175794320, 177670896, 179559120, 181458992, 732 | 183370528, 185293776, 187228736, 189175424, 191133888, 193104112, 195086128, 197079968, 733 | 199085648, 201103184, 203132592, 205173888, 207227120, 209292272, 211369392, 213458480, 734 | 215559568, 217672656, 219797792, 221934976, 224084240, 226245600, 228419056, 230604656, 735 | 232802400, 235012320, 237234432, 239468736, 241715280, 243974080, 246245120, 248528464, 736 | 250824112, 253132064, 255452368, 257785040, 260130080, 262487520, 264857376, 267239664, 737 | }; 738 | 739 | static stbir_uint8 stbir__linear_to_srgb_uchar(float f) 740 | { 741 | int x = (int) (f * (1 << 28)); // has headroom so you don't need to clamp 742 | int v = 0; 743 | int i; 744 | 745 | // Refine the guess with a short binary search. 746 | i = v + 128; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 747 | i = v + 64; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 748 | i = v + 32; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 749 | i = v + 16; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 750 | i = v + 8; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 751 | i = v + 4; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 752 | i = v + 2; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 753 | i = v + 1; if (x >= stbir__srgb_offset_to_linear_scaled[i]) v = i; 754 | 755 | return (stbir_uint8) v; 756 | } 757 | #endif 758 | 759 | static float stbir__filter_trapezoid(float x, float scale) 760 | { 761 | float halfscale = scale / 2; 762 | float t = 0.5f + halfscale; 763 | STBIR_ASSERT(scale <= 1); 764 | 765 | x = (float)fabs(x); 766 | 767 | if (x >= t) 768 | return 0; 769 | else 770 | { 771 | float r = 0.5f - halfscale; 772 | if (x <= r) 773 | return 1; 774 | else 775 | return (t - x) / scale; 776 | } 777 | } 778 | 779 | static float stbir__support_trapezoid(float scale) 780 | { 781 | STBIR_ASSERT(scale <= 1); 782 | return 0.5f + scale / 2; 783 | } 784 | 785 | static float stbir__filter_triangle(float x, float s) 786 | { 787 | STBIR__UNUSED_PARAM(s); 788 | 789 | x = (float)fabs(x); 790 | 791 | if (x <= 1.0f) 792 | return 1 - x; 793 | else 794 | return 0; 795 | } 796 | 797 | static float stbir__filter_cubic(float x, float s) 798 | { 799 | STBIR__UNUSED_PARAM(s); 800 | 801 | x = (float)fabs(x); 802 | 803 | if (x < 1.0f) 804 | return (4 + x*x*(3*x - 6))/6; 805 | else if (x < 2.0f) 806 | return (8 + x*(-12 + x*(6 - x)))/6; 807 | 808 | return (0.0f); 809 | } 810 | 811 | static float stbir__filter_catmullrom(float x, float s) 812 | { 813 | STBIR__UNUSED_PARAM(s); 814 | 815 | x = (float)fabs(x); 816 | 817 | if (x < 1.0f) 818 | return 1 - x*x*(2.5f - 1.5f*x); 819 | else if (x < 2.0f) 820 | return 2 - x*(4 + x*(0.5f*x - 2.5f)); 821 | 822 | return (0.0f); 823 | } 824 | 825 | static float stbir__filter_mitchell(float x, float s) 826 | { 827 | STBIR__UNUSED_PARAM(s); 828 | 829 | x = (float)fabs(x); 830 | 831 | if (x < 1.0f) 832 | return (16 + x*x*(21 * x - 36))/18; 833 | else if (x < 2.0f) 834 | return (32 + x*(-60 + x*(36 - 7*x)))/18; 835 | 836 | return (0.0f); 837 | } 838 | 839 | static float stbir__support_zero(float s) 840 | { 841 | STBIR__UNUSED_PARAM(s); 842 | return 0; 843 | } 844 | 845 | static float stbir__support_one(float s) 846 | { 847 | STBIR__UNUSED_PARAM(s); 848 | return 1; 849 | } 850 | 851 | static float stbir__support_two(float s) 852 | { 853 | STBIR__UNUSED_PARAM(s); 854 | return 2; 855 | } 856 | 857 | static stbir__filter_info stbir__filter_info_table[] = { 858 | { NULL, stbir__support_zero }, 859 | { stbir__filter_trapezoid, stbir__support_trapezoid }, 860 | { stbir__filter_triangle, stbir__support_one }, 861 | { stbir__filter_cubic, stbir__support_two }, 862 | { stbir__filter_catmullrom, stbir__support_two }, 863 | { stbir__filter_mitchell, stbir__support_two }, 864 | }; 865 | 866 | stbir__inline static int stbir__use_upsampling(float ratio) 867 | { 868 | return ratio > 1; 869 | } 870 | 871 | stbir__inline static int stbir__use_width_upsampling(stbir__info* stbir_info) 872 | { 873 | return stbir__use_upsampling(stbir_info->horizontal_scale); 874 | } 875 | 876 | stbir__inline static int stbir__use_height_upsampling(stbir__info* stbir_info) 877 | { 878 | return stbir__use_upsampling(stbir_info->vertical_scale); 879 | } 880 | 881 | // This is the maximum number of input samples that can affect an output sample 882 | // with the given filter 883 | static int stbir__get_filter_pixel_width(stbir_filter filter, float scale) 884 | { 885 | STBIR_ASSERT(filter != 0); 886 | STBIR_ASSERT(filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); 887 | 888 | if (stbir__use_upsampling(scale)) 889 | return (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2); 890 | else 891 | return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2 / scale); 892 | } 893 | 894 | // This is how much to expand buffers to account for filters seeking outside 895 | // the image boundaries. 896 | static int stbir__get_filter_pixel_margin(stbir_filter filter, float scale) 897 | { 898 | return stbir__get_filter_pixel_width(filter, scale) / 2; 899 | } 900 | 901 | static int stbir__get_coefficient_width(stbir_filter filter, float scale) 902 | { 903 | if (stbir__use_upsampling(scale)) 904 | return (int)ceil(stbir__filter_info_table[filter].support(1 / scale) * 2); 905 | else 906 | return (int)ceil(stbir__filter_info_table[filter].support(scale) * 2); 907 | } 908 | 909 | static int stbir__get_contributors(float scale, stbir_filter filter, int input_size, int output_size) 910 | { 911 | if (stbir__use_upsampling(scale)) 912 | return output_size; 913 | else 914 | return (input_size + stbir__get_filter_pixel_margin(filter, scale) * 2); 915 | } 916 | 917 | static int stbir__get_total_horizontal_coefficients(stbir__info* info) 918 | { 919 | return info->horizontal_num_contributors 920 | * stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale); 921 | } 922 | 923 | static int stbir__get_total_vertical_coefficients(stbir__info* info) 924 | { 925 | return info->vertical_num_contributors 926 | * stbir__get_coefficient_width (info->vertical_filter, info->vertical_scale); 927 | } 928 | 929 | static stbir__contributors* stbir__get_contributor(stbir__contributors* contributors, int n) 930 | { 931 | return &contributors[n]; 932 | } 933 | 934 | // For perf reasons this code is duplicated in stbir__resample_horizontal_upsample/downsample, 935 | // if you change it here change it there too. 936 | static float* stbir__get_coefficient(float* coefficients, stbir_filter filter, float scale, int n, int c) 937 | { 938 | int width = stbir__get_coefficient_width(filter, scale); 939 | return &coefficients[width*n + c]; 940 | } 941 | 942 | static int stbir__edge_wrap_slow(stbir_edge edge, int n, int max) 943 | { 944 | switch (edge) 945 | { 946 | case STBIR_EDGE_ZERO: 947 | return 0; // we'll decode the wrong pixel here, and then overwrite with 0s later 948 | 949 | case STBIR_EDGE_CLAMP: 950 | if (n < 0) 951 | return 0; 952 | 953 | if (n >= max) 954 | return max - 1; 955 | 956 | return n; // NOTREACHED 957 | 958 | case STBIR_EDGE_REFLECT: 959 | { 960 | if (n < 0) 961 | { 962 | if (n < max) 963 | return -n; 964 | else 965 | return max - 1; 966 | } 967 | 968 | if (n >= max) 969 | { 970 | int max2 = max * 2; 971 | if (n >= max2) 972 | return 0; 973 | else 974 | return max2 - n - 1; 975 | } 976 | 977 | return n; // NOTREACHED 978 | } 979 | 980 | case STBIR_EDGE_WRAP: 981 | if (n >= 0) 982 | return (n % max); 983 | else 984 | { 985 | int m = (-n) % max; 986 | 987 | if (m != 0) 988 | m = max - m; 989 | 990 | return (m); 991 | } 992 | // NOTREACHED 993 | 994 | default: 995 | STBIR_ASSERT(!"Unimplemented edge type"); 996 | return 0; 997 | } 998 | } 999 | 1000 | stbir__inline static int stbir__edge_wrap(stbir_edge edge, int n, int max) 1001 | { 1002 | // avoid per-pixel switch 1003 | if (n >= 0 && n < max) 1004 | return n; 1005 | return stbir__edge_wrap_slow(edge, n, max); 1006 | } 1007 | 1008 | // What input pixels contribute to this output pixel? 1009 | static void stbir__calculate_sample_range_upsample(int n, float out_filter_radius, float scale_ratio, float out_shift, int* in_first_pixel, int* in_last_pixel, float* in_center_of_out) 1010 | { 1011 | float out_pixel_center = (float)n + 0.5f; 1012 | float out_pixel_influence_lowerbound = out_pixel_center - out_filter_radius; 1013 | float out_pixel_influence_upperbound = out_pixel_center + out_filter_radius; 1014 | 1015 | float in_pixel_influence_lowerbound = (out_pixel_influence_lowerbound + out_shift) / scale_ratio; 1016 | float in_pixel_influence_upperbound = (out_pixel_influence_upperbound + out_shift) / scale_ratio; 1017 | 1018 | *in_center_of_out = (out_pixel_center + out_shift) / scale_ratio; 1019 | *in_first_pixel = (int)(floor(in_pixel_influence_lowerbound + 0.5)); 1020 | *in_last_pixel = (int)(floor(in_pixel_influence_upperbound - 0.5)); 1021 | } 1022 | 1023 | // What output pixels does this input pixel contribute to? 1024 | static void stbir__calculate_sample_range_downsample(int n, float in_pixels_radius, float scale_ratio, float out_shift, int* out_first_pixel, int* out_last_pixel, float* out_center_of_in) 1025 | { 1026 | float in_pixel_center = (float)n + 0.5f; 1027 | float in_pixel_influence_lowerbound = in_pixel_center - in_pixels_radius; 1028 | float in_pixel_influence_upperbound = in_pixel_center + in_pixels_radius; 1029 | 1030 | float out_pixel_influence_lowerbound = in_pixel_influence_lowerbound * scale_ratio - out_shift; 1031 | float out_pixel_influence_upperbound = in_pixel_influence_upperbound * scale_ratio - out_shift; 1032 | 1033 | *out_center_of_in = in_pixel_center * scale_ratio - out_shift; 1034 | *out_first_pixel = (int)(floor(out_pixel_influence_lowerbound + 0.5)); 1035 | *out_last_pixel = (int)(floor(out_pixel_influence_upperbound - 0.5)); 1036 | } 1037 | 1038 | static void stbir__calculate_coefficients_upsample(stbir_filter filter, float scale, int in_first_pixel, int in_last_pixel, float in_center_of_out, stbir__contributors* contributor, float* coefficient_group) 1039 | { 1040 | int i; 1041 | float total_filter = 0; 1042 | float filter_scale; 1043 | 1044 | STBIR_ASSERT(in_last_pixel - in_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(1/scale) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. 1045 | 1046 | contributor->n0 = in_first_pixel; 1047 | contributor->n1 = in_last_pixel; 1048 | 1049 | STBIR_ASSERT(contributor->n1 >= contributor->n0); 1050 | 1051 | for (i = 0; i <= in_last_pixel - in_first_pixel; i++) 1052 | { 1053 | float in_pixel_center = (float)(i + in_first_pixel) + 0.5f; 1054 | coefficient_group[i] = stbir__filter_info_table[filter].kernel(in_center_of_out - in_pixel_center, 1 / scale); 1055 | 1056 | // If the coefficient is zero, skip it. (Don't do the <0 check here, we want the influence of those outside pixels.) 1057 | if (i == 0 && !coefficient_group[i]) 1058 | { 1059 | contributor->n0 = ++in_first_pixel; 1060 | i--; 1061 | continue; 1062 | } 1063 | 1064 | total_filter += coefficient_group[i]; 1065 | } 1066 | 1067 | STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(in_last_pixel + 1) + 0.5f - in_center_of_out, 1/scale) == 0); 1068 | 1069 | STBIR_ASSERT(total_filter > 0.9); 1070 | STBIR_ASSERT(total_filter < 1.1f); // Make sure it's not way off. 1071 | 1072 | // Make sure the sum of all coefficients is 1. 1073 | filter_scale = 1 / total_filter; 1074 | 1075 | for (i = 0; i <= in_last_pixel - in_first_pixel; i++) 1076 | coefficient_group[i] *= filter_scale; 1077 | 1078 | for (i = in_last_pixel - in_first_pixel; i >= 0; i--) 1079 | { 1080 | if (coefficient_group[i]) 1081 | break; 1082 | 1083 | // This line has no weight. We can skip it. 1084 | contributor->n1 = contributor->n0 + i - 1; 1085 | } 1086 | } 1087 | 1088 | static void stbir__calculate_coefficients_downsample(stbir_filter filter, float scale_ratio, int out_first_pixel, int out_last_pixel, float out_center_of_in, stbir__contributors* contributor, float* coefficient_group) 1089 | { 1090 | int i; 1091 | 1092 | STBIR_ASSERT(out_last_pixel - out_first_pixel <= (int)ceil(stbir__filter_info_table[filter].support(scale_ratio) * 2)); // Taken directly from stbir__get_coefficient_width() which we can't call because we don't know if we're horizontal or vertical. 1093 | 1094 | contributor->n0 = out_first_pixel; 1095 | contributor->n1 = out_last_pixel; 1096 | 1097 | STBIR_ASSERT(contributor->n1 >= contributor->n0); 1098 | 1099 | for (i = 0; i <= out_last_pixel - out_first_pixel; i++) 1100 | { 1101 | float out_pixel_center = (float)(i + out_first_pixel) + 0.5f; 1102 | float x = out_pixel_center - out_center_of_in; 1103 | coefficient_group[i] = stbir__filter_info_table[filter].kernel(x, scale_ratio) * scale_ratio; 1104 | } 1105 | 1106 | STBIR_ASSERT(stbir__filter_info_table[filter].kernel((float)(out_last_pixel + 1) + 0.5f - out_center_of_in, scale_ratio) == 0); 1107 | 1108 | for (i = out_last_pixel - out_first_pixel; i >= 0; i--) 1109 | { 1110 | if (coefficient_group[i]) 1111 | break; 1112 | 1113 | // This line has no weight. We can skip it. 1114 | contributor->n1 = contributor->n0 + i - 1; 1115 | } 1116 | } 1117 | 1118 | static void stbir__normalize_downsample_coefficients(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, int input_size, int output_size) 1119 | { 1120 | int num_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size); 1121 | int num_coefficients = stbir__get_coefficient_width(filter, scale_ratio); 1122 | int i, j; 1123 | int skip; 1124 | 1125 | for (i = 0; i < output_size; i++) 1126 | { 1127 | float scale; 1128 | float total = 0; 1129 | 1130 | for (j = 0; j < num_contributors; j++) 1131 | { 1132 | if (i >= contributors[j].n0 && i <= contributors[j].n1) 1133 | { 1134 | float coefficient = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0); 1135 | total += coefficient; 1136 | } 1137 | else if (i < contributors[j].n0) 1138 | break; 1139 | } 1140 | 1141 | STBIR_ASSERT(total > 0.9f); 1142 | STBIR_ASSERT(total < 1.1f); 1143 | 1144 | scale = 1 / total; 1145 | 1146 | for (j = 0; j < num_contributors; j++) 1147 | { 1148 | if (i >= contributors[j].n0 && i <= contributors[j].n1) 1149 | *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i - contributors[j].n0) *= scale; 1150 | else if (i < contributors[j].n0) 1151 | break; 1152 | } 1153 | } 1154 | 1155 | // Optimize: Skip zero coefficients and contributions outside of image bounds. 1156 | // Do this after normalizing because normalization depends on the n0/n1 values. 1157 | for (j = 0; j < num_contributors; j++) 1158 | { 1159 | int range, max, width; 1160 | 1161 | skip = 0; 1162 | while (*stbir__get_coefficient(coefficients, filter, scale_ratio, j, skip) == 0) 1163 | skip++; 1164 | 1165 | contributors[j].n0 += skip; 1166 | 1167 | while (contributors[j].n0 < 0) 1168 | { 1169 | contributors[j].n0++; 1170 | skip++; 1171 | } 1172 | 1173 | range = contributors[j].n1 - contributors[j].n0 + 1; 1174 | max = stbir__min(num_coefficients, range); 1175 | 1176 | width = stbir__get_coefficient_width(filter, scale_ratio); 1177 | for (i = 0; i < max; i++) 1178 | { 1179 | if (i + skip >= width) 1180 | break; 1181 | 1182 | *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i) = *stbir__get_coefficient(coefficients, filter, scale_ratio, j, i + skip); 1183 | } 1184 | 1185 | continue; 1186 | } 1187 | 1188 | // Using min to avoid writing into invalid pixels. 1189 | for (i = 0; i < num_contributors; i++) 1190 | contributors[i].n1 = stbir__min(contributors[i].n1, output_size - 1); 1191 | } 1192 | 1193 | // Each scan line uses the same kernel values so we should calculate the kernel 1194 | // values once and then we can use them for every scan line. 1195 | static void stbir__calculate_filters(stbir__contributors* contributors, float* coefficients, stbir_filter filter, float scale_ratio, float shift, int input_size, int output_size) 1196 | { 1197 | int n; 1198 | int total_contributors = stbir__get_contributors(scale_ratio, filter, input_size, output_size); 1199 | 1200 | if (stbir__use_upsampling(scale_ratio)) 1201 | { 1202 | float out_pixels_radius = stbir__filter_info_table[filter].support(1 / scale_ratio) * scale_ratio; 1203 | 1204 | // Looping through out pixels 1205 | for (n = 0; n < total_contributors; n++) 1206 | { 1207 | float in_center_of_out; // Center of the current out pixel in the in pixel space 1208 | int in_first_pixel, in_last_pixel; 1209 | 1210 | stbir__calculate_sample_range_upsample(n, out_pixels_radius, scale_ratio, shift, &in_first_pixel, &in_last_pixel, &in_center_of_out); 1211 | 1212 | stbir__calculate_coefficients_upsample(filter, scale_ratio, in_first_pixel, in_last_pixel, in_center_of_out, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0)); 1213 | } 1214 | } 1215 | else 1216 | { 1217 | float in_pixels_radius = stbir__filter_info_table[filter].support(scale_ratio) / scale_ratio; 1218 | 1219 | // Looping through in pixels 1220 | for (n = 0; n < total_contributors; n++) 1221 | { 1222 | float out_center_of_in; // Center of the current out pixel in the in pixel space 1223 | int out_first_pixel, out_last_pixel; 1224 | int n_adjusted = n - stbir__get_filter_pixel_margin(filter, scale_ratio); 1225 | 1226 | stbir__calculate_sample_range_downsample(n_adjusted, in_pixels_radius, scale_ratio, shift, &out_first_pixel, &out_last_pixel, &out_center_of_in); 1227 | 1228 | stbir__calculate_coefficients_downsample(filter, scale_ratio, out_first_pixel, out_last_pixel, out_center_of_in, stbir__get_contributor(contributors, n), stbir__get_coefficient(coefficients, filter, scale_ratio, n, 0)); 1229 | } 1230 | 1231 | stbir__normalize_downsample_coefficients(contributors, coefficients, filter, scale_ratio, input_size, output_size); 1232 | } 1233 | } 1234 | 1235 | static float* stbir__get_decode_buffer(stbir__info* stbir_info) 1236 | { 1237 | // The 0 index of the decode buffer starts after the margin. This makes 1238 | // it okay to use negative indexes on the decode buffer. 1239 | return &stbir_info->decode_buffer[stbir_info->horizontal_filter_pixel_margin * stbir_info->channels]; 1240 | } 1241 | 1242 | #define STBIR__DECODE(type, colorspace) ((int)(type) * (STBIR_MAX_COLORSPACES) + (int)(colorspace)) 1243 | 1244 | static void stbir__decode_scanline(stbir__info* stbir_info, int n) 1245 | { 1246 | int c; 1247 | int channels = stbir_info->channels; 1248 | int alpha_channel = stbir_info->alpha_channel; 1249 | int type = stbir_info->type; 1250 | int colorspace = stbir_info->colorspace; 1251 | int input_w = stbir_info->input_w; 1252 | size_t input_stride_bytes = stbir_info->input_stride_bytes; 1253 | float* decode_buffer = stbir__get_decode_buffer(stbir_info); 1254 | stbir_edge edge_horizontal = stbir_info->edge_horizontal; 1255 | stbir_edge edge_vertical = stbir_info->edge_vertical; 1256 | size_t in_buffer_row_offset = stbir__edge_wrap(edge_vertical, n, stbir_info->input_h) * input_stride_bytes; 1257 | const void* input_data = (char *) stbir_info->input_data + in_buffer_row_offset; 1258 | int max_x = input_w + stbir_info->horizontal_filter_pixel_margin; 1259 | int decode = STBIR__DECODE(type, colorspace); 1260 | 1261 | int x = -stbir_info->horizontal_filter_pixel_margin; 1262 | 1263 | // special handling for STBIR_EDGE_ZERO because it needs to return an item that doesn't appear in the input, 1264 | // and we want to avoid paying overhead on every pixel if not STBIR_EDGE_ZERO 1265 | if (edge_vertical == STBIR_EDGE_ZERO && (n < 0 || n >= stbir_info->input_h)) 1266 | { 1267 | for (; x < max_x; x++) 1268 | for (c = 0; c < channels; c++) 1269 | decode_buffer[x*channels + c] = 0; 1270 | return; 1271 | } 1272 | 1273 | switch (decode) 1274 | { 1275 | case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR): 1276 | for (; x < max_x; x++) 1277 | { 1278 | int decode_pixel_index = x * channels; 1279 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1280 | for (c = 0; c < channels; c++) 1281 | decode_buffer[decode_pixel_index + c] = ((float)((const unsigned char*)input_data)[input_pixel_index + c]) / stbir__max_uint8_as_float; 1282 | } 1283 | break; 1284 | 1285 | case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB): 1286 | for (; x < max_x; x++) 1287 | { 1288 | int decode_pixel_index = x * channels; 1289 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1290 | for (c = 0; c < channels; c++) 1291 | decode_buffer[decode_pixel_index + c] = stbir__srgb_uchar_to_linear_float[((const unsigned char*)input_data)[input_pixel_index + c]]; 1292 | 1293 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1294 | decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned char*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint8_as_float; 1295 | } 1296 | break; 1297 | 1298 | case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR): 1299 | for (; x < max_x; x++) 1300 | { 1301 | int decode_pixel_index = x * channels; 1302 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1303 | for (c = 0; c < channels; c++) 1304 | decode_buffer[decode_pixel_index + c] = ((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float; 1305 | } 1306 | break; 1307 | 1308 | case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB): 1309 | for (; x < max_x; x++) 1310 | { 1311 | int decode_pixel_index = x * channels; 1312 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1313 | for (c = 0; c < channels; c++) 1314 | decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((float)((const unsigned short*)input_data)[input_pixel_index + c]) / stbir__max_uint16_as_float); 1315 | 1316 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1317 | decode_buffer[decode_pixel_index + alpha_channel] = ((float)((const unsigned short*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint16_as_float; 1318 | } 1319 | break; 1320 | 1321 | case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR): 1322 | for (; x < max_x; x++) 1323 | { 1324 | int decode_pixel_index = x * channels; 1325 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1326 | for (c = 0; c < channels; c++) 1327 | decode_buffer[decode_pixel_index + c] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float); 1328 | } 1329 | break; 1330 | 1331 | case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB): 1332 | for (; x < max_x; x++) 1333 | { 1334 | int decode_pixel_index = x * channels; 1335 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1336 | for (c = 0; c < channels; c++) 1337 | decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear((float)(((double)((const unsigned int*)input_data)[input_pixel_index + c]) / stbir__max_uint32_as_float)); 1338 | 1339 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1340 | decode_buffer[decode_pixel_index + alpha_channel] = (float)(((double)((const unsigned int*)input_data)[input_pixel_index + alpha_channel]) / stbir__max_uint32_as_float); 1341 | } 1342 | break; 1343 | 1344 | case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR): 1345 | for (; x < max_x; x++) 1346 | { 1347 | int decode_pixel_index = x * channels; 1348 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1349 | for (c = 0; c < channels; c++) 1350 | decode_buffer[decode_pixel_index + c] = ((const float*)input_data)[input_pixel_index + c]; 1351 | } 1352 | break; 1353 | 1354 | case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB): 1355 | for (; x < max_x; x++) 1356 | { 1357 | int decode_pixel_index = x * channels; 1358 | int input_pixel_index = stbir__edge_wrap(edge_horizontal, x, input_w) * channels; 1359 | for (c = 0; c < channels; c++) 1360 | decode_buffer[decode_pixel_index + c] = stbir__srgb_to_linear(((const float*)input_data)[input_pixel_index + c]); 1361 | 1362 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1363 | decode_buffer[decode_pixel_index + alpha_channel] = ((const float*)input_data)[input_pixel_index + alpha_channel]; 1364 | } 1365 | 1366 | break; 1367 | 1368 | default: 1369 | STBIR_ASSERT(!"Unknown type/colorspace/channels combination."); 1370 | break; 1371 | } 1372 | 1373 | if (!(stbir_info->flags & STBIR_FLAG_ALPHA_PREMULTIPLIED)) 1374 | { 1375 | for (x = -stbir_info->horizontal_filter_pixel_margin; x < max_x; x++) 1376 | { 1377 | int decode_pixel_index = x * channels; 1378 | 1379 | // If the alpha value is 0 it will clobber the color values. Make sure it's not. 1380 | float alpha = decode_buffer[decode_pixel_index + alpha_channel]; 1381 | #ifndef STBIR_NO_ALPHA_EPSILON 1382 | if (stbir_info->type != STBIR_TYPE_FLOAT) { 1383 | alpha += STBIR_ALPHA_EPSILON; 1384 | decode_buffer[decode_pixel_index + alpha_channel] = alpha; 1385 | } 1386 | #endif 1387 | for (c = 0; c < channels; c++) 1388 | { 1389 | if (c == alpha_channel) 1390 | continue; 1391 | 1392 | decode_buffer[decode_pixel_index + c] *= alpha; 1393 | } 1394 | } 1395 | } 1396 | 1397 | if (edge_horizontal == STBIR_EDGE_ZERO) 1398 | { 1399 | for (x = -stbir_info->horizontal_filter_pixel_margin; x < 0; x++) 1400 | { 1401 | for (c = 0; c < channels; c++) 1402 | decode_buffer[x*channels + c] = 0; 1403 | } 1404 | for (x = input_w; x < max_x; x++) 1405 | { 1406 | for (c = 0; c < channels; c++) 1407 | decode_buffer[x*channels + c] = 0; 1408 | } 1409 | } 1410 | } 1411 | 1412 | static float* stbir__get_ring_buffer_entry(float* ring_buffer, int index, int ring_buffer_length) 1413 | { 1414 | return &ring_buffer[index * ring_buffer_length]; 1415 | } 1416 | 1417 | static float* stbir__add_empty_ring_buffer_entry(stbir__info* stbir_info, int n) 1418 | { 1419 | int ring_buffer_index; 1420 | float* ring_buffer; 1421 | 1422 | stbir_info->ring_buffer_last_scanline = n; 1423 | 1424 | if (stbir_info->ring_buffer_begin_index < 0) 1425 | { 1426 | ring_buffer_index = stbir_info->ring_buffer_begin_index = 0; 1427 | stbir_info->ring_buffer_first_scanline = n; 1428 | } 1429 | else 1430 | { 1431 | ring_buffer_index = (stbir_info->ring_buffer_begin_index + (stbir_info->ring_buffer_last_scanline - stbir_info->ring_buffer_first_scanline)) % stbir_info->ring_buffer_num_entries; 1432 | STBIR_ASSERT(ring_buffer_index != stbir_info->ring_buffer_begin_index); 1433 | } 1434 | 1435 | ring_buffer = stbir__get_ring_buffer_entry(stbir_info->ring_buffer, ring_buffer_index, stbir_info->ring_buffer_length_bytes / sizeof(float)); 1436 | memset(ring_buffer, 0, stbir_info->ring_buffer_length_bytes); 1437 | 1438 | return ring_buffer; 1439 | } 1440 | 1441 | 1442 | static void stbir__resample_horizontal_upsample(stbir__info* stbir_info, float* output_buffer) 1443 | { 1444 | int x, k; 1445 | int output_w = stbir_info->output_w; 1446 | int channels = stbir_info->channels; 1447 | float* decode_buffer = stbir__get_decode_buffer(stbir_info); 1448 | stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors; 1449 | float* horizontal_coefficients = stbir_info->horizontal_coefficients; 1450 | int coefficient_width = stbir_info->horizontal_coefficient_width; 1451 | 1452 | for (x = 0; x < output_w; x++) 1453 | { 1454 | int n0 = horizontal_contributors[x].n0; 1455 | int n1 = horizontal_contributors[x].n1; 1456 | 1457 | int out_pixel_index = x * channels; 1458 | int coefficient_group = coefficient_width * x; 1459 | int coefficient_counter = 0; 1460 | 1461 | STBIR_ASSERT(n1 >= n0); 1462 | STBIR_ASSERT(n0 >= -stbir_info->horizontal_filter_pixel_margin); 1463 | STBIR_ASSERT(n1 >= -stbir_info->horizontal_filter_pixel_margin); 1464 | STBIR_ASSERT(n0 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); 1465 | STBIR_ASSERT(n1 < stbir_info->input_w + stbir_info->horizontal_filter_pixel_margin); 1466 | 1467 | switch (channels) { 1468 | case 1: 1469 | for (k = n0; k <= n1; k++) 1470 | { 1471 | int in_pixel_index = k * 1; 1472 | float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; 1473 | STBIR_ASSERT(coefficient != 0); 1474 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1475 | } 1476 | break; 1477 | case 2: 1478 | for (k = n0; k <= n1; k++) 1479 | { 1480 | int in_pixel_index = k * 2; 1481 | float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; 1482 | STBIR_ASSERT(coefficient != 0); 1483 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1484 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1485 | } 1486 | break; 1487 | case 3: 1488 | for (k = n0; k <= n1; k++) 1489 | { 1490 | int in_pixel_index = k * 3; 1491 | float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; 1492 | STBIR_ASSERT(coefficient != 0); 1493 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1494 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1495 | output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; 1496 | } 1497 | break; 1498 | case 4: 1499 | for (k = n0; k <= n1; k++) 1500 | { 1501 | int in_pixel_index = k * 4; 1502 | float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; 1503 | STBIR_ASSERT(coefficient != 0); 1504 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1505 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1506 | output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; 1507 | output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient; 1508 | } 1509 | break; 1510 | default: 1511 | for (k = n0; k <= n1; k++) 1512 | { 1513 | int in_pixel_index = k * channels; 1514 | float coefficient = horizontal_coefficients[coefficient_group + coefficient_counter++]; 1515 | int c; 1516 | STBIR_ASSERT(coefficient != 0); 1517 | for (c = 0; c < channels; c++) 1518 | output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient; 1519 | } 1520 | break; 1521 | } 1522 | } 1523 | } 1524 | 1525 | static void stbir__resample_horizontal_downsample(stbir__info* stbir_info, float* output_buffer) 1526 | { 1527 | int x, k; 1528 | int input_w = stbir_info->input_w; 1529 | int channels = stbir_info->channels; 1530 | float* decode_buffer = stbir__get_decode_buffer(stbir_info); 1531 | stbir__contributors* horizontal_contributors = stbir_info->horizontal_contributors; 1532 | float* horizontal_coefficients = stbir_info->horizontal_coefficients; 1533 | int coefficient_width = stbir_info->horizontal_coefficient_width; 1534 | int filter_pixel_margin = stbir_info->horizontal_filter_pixel_margin; 1535 | int max_x = input_w + filter_pixel_margin * 2; 1536 | 1537 | STBIR_ASSERT(!stbir__use_width_upsampling(stbir_info)); 1538 | 1539 | switch (channels) { 1540 | case 1: 1541 | for (x = 0; x < max_x; x++) 1542 | { 1543 | int n0 = horizontal_contributors[x].n0; 1544 | int n1 = horizontal_contributors[x].n1; 1545 | 1546 | int in_x = x - filter_pixel_margin; 1547 | int in_pixel_index = in_x * 1; 1548 | int max_n = n1; 1549 | int coefficient_group = coefficient_width * x; 1550 | 1551 | for (k = n0; k <= max_n; k++) 1552 | { 1553 | int out_pixel_index = k * 1; 1554 | float coefficient = horizontal_coefficients[coefficient_group + k - n0]; 1555 | STBIR_ASSERT(coefficient != 0); 1556 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1557 | } 1558 | } 1559 | break; 1560 | 1561 | case 2: 1562 | for (x = 0; x < max_x; x++) 1563 | { 1564 | int n0 = horizontal_contributors[x].n0; 1565 | int n1 = horizontal_contributors[x].n1; 1566 | 1567 | int in_x = x - filter_pixel_margin; 1568 | int in_pixel_index = in_x * 2; 1569 | int max_n = n1; 1570 | int coefficient_group = coefficient_width * x; 1571 | 1572 | for (k = n0; k <= max_n; k++) 1573 | { 1574 | int out_pixel_index = k * 2; 1575 | float coefficient = horizontal_coefficients[coefficient_group + k - n0]; 1576 | STBIR_ASSERT(coefficient != 0); 1577 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1578 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1579 | } 1580 | } 1581 | break; 1582 | 1583 | case 3: 1584 | for (x = 0; x < max_x; x++) 1585 | { 1586 | int n0 = horizontal_contributors[x].n0; 1587 | int n1 = horizontal_contributors[x].n1; 1588 | 1589 | int in_x = x - filter_pixel_margin; 1590 | int in_pixel_index = in_x * 3; 1591 | int max_n = n1; 1592 | int coefficient_group = coefficient_width * x; 1593 | 1594 | for (k = n0; k <= max_n; k++) 1595 | { 1596 | int out_pixel_index = k * 3; 1597 | float coefficient = horizontal_coefficients[coefficient_group + k - n0]; 1598 | STBIR_ASSERT(coefficient != 0); 1599 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1600 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1601 | output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; 1602 | } 1603 | } 1604 | break; 1605 | 1606 | case 4: 1607 | for (x = 0; x < max_x; x++) 1608 | { 1609 | int n0 = horizontal_contributors[x].n0; 1610 | int n1 = horizontal_contributors[x].n1; 1611 | 1612 | int in_x = x - filter_pixel_margin; 1613 | int in_pixel_index = in_x * 4; 1614 | int max_n = n1; 1615 | int coefficient_group = coefficient_width * x; 1616 | 1617 | for (k = n0; k <= max_n; k++) 1618 | { 1619 | int out_pixel_index = k * 4; 1620 | float coefficient = horizontal_coefficients[coefficient_group + k - n0]; 1621 | STBIR_ASSERT(coefficient != 0); 1622 | output_buffer[out_pixel_index + 0] += decode_buffer[in_pixel_index + 0] * coefficient; 1623 | output_buffer[out_pixel_index + 1] += decode_buffer[in_pixel_index + 1] * coefficient; 1624 | output_buffer[out_pixel_index + 2] += decode_buffer[in_pixel_index + 2] * coefficient; 1625 | output_buffer[out_pixel_index + 3] += decode_buffer[in_pixel_index + 3] * coefficient; 1626 | } 1627 | } 1628 | break; 1629 | 1630 | default: 1631 | for (x = 0; x < max_x; x++) 1632 | { 1633 | int n0 = horizontal_contributors[x].n0; 1634 | int n1 = horizontal_contributors[x].n1; 1635 | 1636 | int in_x = x - filter_pixel_margin; 1637 | int in_pixel_index = in_x * channels; 1638 | int max_n = n1; 1639 | int coefficient_group = coefficient_width * x; 1640 | 1641 | for (k = n0; k <= max_n; k++) 1642 | { 1643 | int c; 1644 | int out_pixel_index = k * channels; 1645 | float coefficient = horizontal_coefficients[coefficient_group + k - n0]; 1646 | STBIR_ASSERT(coefficient != 0); 1647 | for (c = 0; c < channels; c++) 1648 | output_buffer[out_pixel_index + c] += decode_buffer[in_pixel_index + c] * coefficient; 1649 | } 1650 | } 1651 | break; 1652 | } 1653 | } 1654 | 1655 | static void stbir__decode_and_resample_upsample(stbir__info* stbir_info, int n) 1656 | { 1657 | // Decode the nth scanline from the source image into the decode buffer. 1658 | stbir__decode_scanline(stbir_info, n); 1659 | 1660 | // Now resample it into the ring buffer. 1661 | if (stbir__use_width_upsampling(stbir_info)) 1662 | stbir__resample_horizontal_upsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n)); 1663 | else 1664 | stbir__resample_horizontal_downsample(stbir_info, stbir__add_empty_ring_buffer_entry(stbir_info, n)); 1665 | 1666 | // Now it's sitting in the ring buffer ready to be used as source for the vertical sampling. 1667 | } 1668 | 1669 | static void stbir__decode_and_resample_downsample(stbir__info* stbir_info, int n) 1670 | { 1671 | // Decode the nth scanline from the source image into the decode buffer. 1672 | stbir__decode_scanline(stbir_info, n); 1673 | 1674 | memset(stbir_info->horizontal_buffer, 0, stbir_info->output_w * stbir_info->channels * sizeof(float)); 1675 | 1676 | // Now resample it into the horizontal buffer. 1677 | if (stbir__use_width_upsampling(stbir_info)) 1678 | stbir__resample_horizontal_upsample(stbir_info, stbir_info->horizontal_buffer); 1679 | else 1680 | stbir__resample_horizontal_downsample(stbir_info, stbir_info->horizontal_buffer); 1681 | 1682 | // Now it's sitting in the horizontal buffer ready to be distributed into the ring buffers. 1683 | } 1684 | 1685 | // Get the specified scan line from the ring buffer. 1686 | static float* stbir__get_ring_buffer_scanline(int get_scanline, float* ring_buffer, int begin_index, int first_scanline, int ring_buffer_num_entries, int ring_buffer_length) 1687 | { 1688 | int ring_buffer_index = (begin_index + (get_scanline - first_scanline)) % ring_buffer_num_entries; 1689 | return stbir__get_ring_buffer_entry(ring_buffer, ring_buffer_index, ring_buffer_length); 1690 | } 1691 | 1692 | 1693 | static void stbir__encode_scanline(stbir__info* stbir_info, int num_pixels, void *output_buffer, float *encode_buffer, int channels, int alpha_channel, int decode) 1694 | { 1695 | int x; 1696 | int n; 1697 | int num_nonalpha; 1698 | stbir_uint16 nonalpha[STBIR_MAX_CHANNELS]; 1699 | 1700 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) 1701 | { 1702 | for (x=0; x < num_pixels; ++x) 1703 | { 1704 | int pixel_index = x*channels; 1705 | 1706 | float alpha = encode_buffer[pixel_index + alpha_channel]; 1707 | float reciprocal_alpha = alpha ? 1.0f / alpha : 0; 1708 | 1709 | // unrolling this produced a 1% slowdown upscaling a large RGBA linear-space image on my machine - stb 1710 | for (n = 0; n < channels; n++) 1711 | if (n != alpha_channel) 1712 | encode_buffer[pixel_index + n] *= reciprocal_alpha; 1713 | 1714 | // We added in a small epsilon to prevent the color channel from being deleted with zero alpha. 1715 | // Because we only add it for integer types, it will automatically be discarded on integer 1716 | // conversion, so we don't need to subtract it back out (which would be problematic for 1717 | // numeric precision reasons). 1718 | } 1719 | } 1720 | 1721 | // build a table of all channels that need colorspace correction, so 1722 | // we don't perform colorspace correction on channels that don't need it. 1723 | for (x = 0, num_nonalpha = 0; x < channels; ++x) 1724 | { 1725 | if (x != alpha_channel || (stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1726 | { 1727 | nonalpha[num_nonalpha++] = (stbir_uint16)x; 1728 | } 1729 | } 1730 | 1731 | #define STBIR__ROUND_INT(f) ((int) ((f)+0.5)) 1732 | #define STBIR__ROUND_UINT(f) ((stbir_uint32) ((f)+0.5)) 1733 | 1734 | #ifdef STBIR__SATURATE_INT 1735 | #define STBIR__ENCODE_LINEAR8(f) stbir__saturate8 (STBIR__ROUND_INT((f) * stbir__max_uint8_as_float )) 1736 | #define STBIR__ENCODE_LINEAR16(f) stbir__saturate16(STBIR__ROUND_INT((f) * stbir__max_uint16_as_float)) 1737 | #else 1738 | #define STBIR__ENCODE_LINEAR8(f) (unsigned char ) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint8_as_float ) 1739 | #define STBIR__ENCODE_LINEAR16(f) (unsigned short) STBIR__ROUND_INT(stbir__saturate(f) * stbir__max_uint16_as_float) 1740 | #endif 1741 | 1742 | switch (decode) 1743 | { 1744 | case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_LINEAR): 1745 | for (x=0; x < num_pixels; ++x) 1746 | { 1747 | int pixel_index = x*channels; 1748 | 1749 | for (n = 0; n < channels; n++) 1750 | { 1751 | int index = pixel_index + n; 1752 | ((unsigned char*)output_buffer)[index] = STBIR__ENCODE_LINEAR8(encode_buffer[index]); 1753 | } 1754 | } 1755 | break; 1756 | 1757 | case STBIR__DECODE(STBIR_TYPE_UINT8, STBIR_COLORSPACE_SRGB): 1758 | for (x=0; x < num_pixels; ++x) 1759 | { 1760 | int pixel_index = x*channels; 1761 | 1762 | for (n = 0; n < num_nonalpha; n++) 1763 | { 1764 | int index = pixel_index + nonalpha[n]; 1765 | ((unsigned char*)output_buffer)[index] = stbir__linear_to_srgb_uchar(encode_buffer[index]); 1766 | } 1767 | 1768 | if (!(stbir_info->flags & STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1769 | ((unsigned char *)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR8(encode_buffer[pixel_index+alpha_channel]); 1770 | } 1771 | break; 1772 | 1773 | case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_LINEAR): 1774 | for (x=0; x < num_pixels; ++x) 1775 | { 1776 | int pixel_index = x*channels; 1777 | 1778 | for (n = 0; n < channels; n++) 1779 | { 1780 | int index = pixel_index + n; 1781 | ((unsigned short*)output_buffer)[index] = STBIR__ENCODE_LINEAR16(encode_buffer[index]); 1782 | } 1783 | } 1784 | break; 1785 | 1786 | case STBIR__DECODE(STBIR_TYPE_UINT16, STBIR_COLORSPACE_SRGB): 1787 | for (x=0; x < num_pixels; ++x) 1788 | { 1789 | int pixel_index = x*channels; 1790 | 1791 | for (n = 0; n < num_nonalpha; n++) 1792 | { 1793 | int index = pixel_index + nonalpha[n]; 1794 | ((unsigned short*)output_buffer)[index] = (unsigned short)STBIR__ROUND_INT(stbir__linear_to_srgb(stbir__saturate(encode_buffer[index])) * stbir__max_uint16_as_float); 1795 | } 1796 | 1797 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1798 | ((unsigned short*)output_buffer)[pixel_index + alpha_channel] = STBIR__ENCODE_LINEAR16(encode_buffer[pixel_index + alpha_channel]); 1799 | } 1800 | 1801 | break; 1802 | 1803 | case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_LINEAR): 1804 | for (x=0; x < num_pixels; ++x) 1805 | { 1806 | int pixel_index = x*channels; 1807 | 1808 | for (n = 0; n < channels; n++) 1809 | { 1810 | int index = pixel_index + n; 1811 | ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__saturate(encode_buffer[index])) * stbir__max_uint32_as_float); 1812 | } 1813 | } 1814 | break; 1815 | 1816 | case STBIR__DECODE(STBIR_TYPE_UINT32, STBIR_COLORSPACE_SRGB): 1817 | for (x=0; x < num_pixels; ++x) 1818 | { 1819 | int pixel_index = x*channels; 1820 | 1821 | for (n = 0; n < num_nonalpha; n++) 1822 | { 1823 | int index = pixel_index + nonalpha[n]; 1824 | ((unsigned int*)output_buffer)[index] = (unsigned int)STBIR__ROUND_UINT(((double)stbir__linear_to_srgb(stbir__saturate(encode_buffer[index]))) * stbir__max_uint32_as_float); 1825 | } 1826 | 1827 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1828 | ((unsigned int*)output_buffer)[pixel_index + alpha_channel] = (unsigned int)STBIR__ROUND_INT(((double)stbir__saturate(encode_buffer[pixel_index + alpha_channel])) * stbir__max_uint32_as_float); 1829 | } 1830 | break; 1831 | 1832 | case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_LINEAR): 1833 | for (x=0; x < num_pixels; ++x) 1834 | { 1835 | int pixel_index = x*channels; 1836 | 1837 | for (n = 0; n < channels; n++) 1838 | { 1839 | int index = pixel_index + n; 1840 | ((float*)output_buffer)[index] = encode_buffer[index]; 1841 | } 1842 | } 1843 | break; 1844 | 1845 | case STBIR__DECODE(STBIR_TYPE_FLOAT, STBIR_COLORSPACE_SRGB): 1846 | for (x=0; x < num_pixels; ++x) 1847 | { 1848 | int pixel_index = x*channels; 1849 | 1850 | for (n = 0; n < num_nonalpha; n++) 1851 | { 1852 | int index = pixel_index + nonalpha[n]; 1853 | ((float*)output_buffer)[index] = stbir__linear_to_srgb(encode_buffer[index]); 1854 | } 1855 | 1856 | if (!(stbir_info->flags&STBIR_FLAG_ALPHA_USES_COLORSPACE)) 1857 | ((float*)output_buffer)[pixel_index + alpha_channel] = encode_buffer[pixel_index + alpha_channel]; 1858 | } 1859 | break; 1860 | 1861 | default: 1862 | STBIR_ASSERT(!"Unknown type/colorspace/channels combination."); 1863 | break; 1864 | } 1865 | } 1866 | 1867 | static void stbir__resample_vertical_upsample(stbir__info* stbir_info, int n) 1868 | { 1869 | int x, k; 1870 | int output_w = stbir_info->output_w; 1871 | stbir__contributors* vertical_contributors = stbir_info->vertical_contributors; 1872 | float* vertical_coefficients = stbir_info->vertical_coefficients; 1873 | int channels = stbir_info->channels; 1874 | int alpha_channel = stbir_info->alpha_channel; 1875 | int type = stbir_info->type; 1876 | int colorspace = stbir_info->colorspace; 1877 | int ring_buffer_entries = stbir_info->ring_buffer_num_entries; 1878 | void* output_data = stbir_info->output_data; 1879 | float* encode_buffer = stbir_info->encode_buffer; 1880 | int decode = STBIR__DECODE(type, colorspace); 1881 | int coefficient_width = stbir_info->vertical_coefficient_width; 1882 | int coefficient_counter; 1883 | int contributor = n; 1884 | 1885 | float* ring_buffer = stbir_info->ring_buffer; 1886 | int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index; 1887 | int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline; 1888 | int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float); 1889 | 1890 | int n0,n1, output_row_start; 1891 | int coefficient_group = coefficient_width * contributor; 1892 | 1893 | n0 = vertical_contributors[contributor].n0; 1894 | n1 = vertical_contributors[contributor].n1; 1895 | 1896 | output_row_start = n * stbir_info->output_stride_bytes; 1897 | 1898 | STBIR_ASSERT(stbir__use_height_upsampling(stbir_info)); 1899 | 1900 | memset(encode_buffer, 0, output_w * sizeof(float) * channels); 1901 | 1902 | // I tried reblocking this for better cache usage of encode_buffer 1903 | // (using x_outer, k, x_inner), but it lost speed. -- stb 1904 | 1905 | coefficient_counter = 0; 1906 | switch (channels) { 1907 | case 1: 1908 | for (k = n0; k <= n1; k++) 1909 | { 1910 | int coefficient_index = coefficient_counter++; 1911 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 1912 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 1913 | for (x = 0; x < output_w; ++x) 1914 | { 1915 | int in_pixel_index = x * 1; 1916 | encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient; 1917 | } 1918 | } 1919 | break; 1920 | case 2: 1921 | for (k = n0; k <= n1; k++) 1922 | { 1923 | int coefficient_index = coefficient_counter++; 1924 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 1925 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 1926 | for (x = 0; x < output_w; ++x) 1927 | { 1928 | int in_pixel_index = x * 2; 1929 | encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient; 1930 | encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient; 1931 | } 1932 | } 1933 | break; 1934 | case 3: 1935 | for (k = n0; k <= n1; k++) 1936 | { 1937 | int coefficient_index = coefficient_counter++; 1938 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 1939 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 1940 | for (x = 0; x < output_w; ++x) 1941 | { 1942 | int in_pixel_index = x * 3; 1943 | encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient; 1944 | encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient; 1945 | encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient; 1946 | } 1947 | } 1948 | break; 1949 | case 4: 1950 | for (k = n0; k <= n1; k++) 1951 | { 1952 | int coefficient_index = coefficient_counter++; 1953 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 1954 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 1955 | for (x = 0; x < output_w; ++x) 1956 | { 1957 | int in_pixel_index = x * 4; 1958 | encode_buffer[in_pixel_index + 0] += ring_buffer_entry[in_pixel_index + 0] * coefficient; 1959 | encode_buffer[in_pixel_index + 1] += ring_buffer_entry[in_pixel_index + 1] * coefficient; 1960 | encode_buffer[in_pixel_index + 2] += ring_buffer_entry[in_pixel_index + 2] * coefficient; 1961 | encode_buffer[in_pixel_index + 3] += ring_buffer_entry[in_pixel_index + 3] * coefficient; 1962 | } 1963 | } 1964 | break; 1965 | default: 1966 | for (k = n0; k <= n1; k++) 1967 | { 1968 | int coefficient_index = coefficient_counter++; 1969 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 1970 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 1971 | for (x = 0; x < output_w; ++x) 1972 | { 1973 | int in_pixel_index = x * channels; 1974 | int c; 1975 | for (c = 0; c < channels; c++) 1976 | encode_buffer[in_pixel_index + c] += ring_buffer_entry[in_pixel_index + c] * coefficient; 1977 | } 1978 | } 1979 | break; 1980 | } 1981 | stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, encode_buffer, channels, alpha_channel, decode); 1982 | } 1983 | 1984 | static void stbir__resample_vertical_downsample(stbir__info* stbir_info, int n) 1985 | { 1986 | int x, k; 1987 | int output_w = stbir_info->output_w; 1988 | stbir__contributors* vertical_contributors = stbir_info->vertical_contributors; 1989 | float* vertical_coefficients = stbir_info->vertical_coefficients; 1990 | int channels = stbir_info->channels; 1991 | int ring_buffer_entries = stbir_info->ring_buffer_num_entries; 1992 | float* horizontal_buffer = stbir_info->horizontal_buffer; 1993 | int coefficient_width = stbir_info->vertical_coefficient_width; 1994 | int contributor = n + stbir_info->vertical_filter_pixel_margin; 1995 | 1996 | float* ring_buffer = stbir_info->ring_buffer; 1997 | int ring_buffer_begin_index = stbir_info->ring_buffer_begin_index; 1998 | int ring_buffer_first_scanline = stbir_info->ring_buffer_first_scanline; 1999 | int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float); 2000 | int n0,n1; 2001 | 2002 | n0 = vertical_contributors[contributor].n0; 2003 | n1 = vertical_contributors[contributor].n1; 2004 | 2005 | STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info)); 2006 | 2007 | for (k = n0; k <= n1; k++) 2008 | { 2009 | int coefficient_index = k - n0; 2010 | int coefficient_group = coefficient_width * contributor; 2011 | float coefficient = vertical_coefficients[coefficient_group + coefficient_index]; 2012 | 2013 | float* ring_buffer_entry = stbir__get_ring_buffer_scanline(k, ring_buffer, ring_buffer_begin_index, ring_buffer_first_scanline, ring_buffer_entries, ring_buffer_length); 2014 | 2015 | switch (channels) { 2016 | case 1: 2017 | for (x = 0; x < output_w; x++) 2018 | { 2019 | int in_pixel_index = x * 1; 2020 | ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient; 2021 | } 2022 | break; 2023 | case 2: 2024 | for (x = 0; x < output_w; x++) 2025 | { 2026 | int in_pixel_index = x * 2; 2027 | ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient; 2028 | ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient; 2029 | } 2030 | break; 2031 | case 3: 2032 | for (x = 0; x < output_w; x++) 2033 | { 2034 | int in_pixel_index = x * 3; 2035 | ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient; 2036 | ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient; 2037 | ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient; 2038 | } 2039 | break; 2040 | case 4: 2041 | for (x = 0; x < output_w; x++) 2042 | { 2043 | int in_pixel_index = x * 4; 2044 | ring_buffer_entry[in_pixel_index + 0] += horizontal_buffer[in_pixel_index + 0] * coefficient; 2045 | ring_buffer_entry[in_pixel_index + 1] += horizontal_buffer[in_pixel_index + 1] * coefficient; 2046 | ring_buffer_entry[in_pixel_index + 2] += horizontal_buffer[in_pixel_index + 2] * coefficient; 2047 | ring_buffer_entry[in_pixel_index + 3] += horizontal_buffer[in_pixel_index + 3] * coefficient; 2048 | } 2049 | break; 2050 | default: 2051 | for (x = 0; x < output_w; x++) 2052 | { 2053 | int in_pixel_index = x * channels; 2054 | 2055 | int c; 2056 | for (c = 0; c < channels; c++) 2057 | ring_buffer_entry[in_pixel_index + c] += horizontal_buffer[in_pixel_index + c] * coefficient; 2058 | } 2059 | break; 2060 | } 2061 | } 2062 | } 2063 | 2064 | static void stbir__buffer_loop_upsample(stbir__info* stbir_info) 2065 | { 2066 | int y; 2067 | float scale_ratio = stbir_info->vertical_scale; 2068 | float out_scanlines_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(1/scale_ratio) * scale_ratio; 2069 | 2070 | STBIR_ASSERT(stbir__use_height_upsampling(stbir_info)); 2071 | 2072 | for (y = 0; y < stbir_info->output_h; y++) 2073 | { 2074 | float in_center_of_out = 0; // Center of the current out scanline in the in scanline space 2075 | int in_first_scanline = 0, in_last_scanline = 0; 2076 | 2077 | stbir__calculate_sample_range_upsample(y, out_scanlines_radius, scale_ratio, stbir_info->vertical_shift, &in_first_scanline, &in_last_scanline, &in_center_of_out); 2078 | 2079 | STBIR_ASSERT(in_last_scanline - in_first_scanline + 1 <= stbir_info->ring_buffer_num_entries); 2080 | 2081 | if (stbir_info->ring_buffer_begin_index >= 0) 2082 | { 2083 | // Get rid of whatever we don't need anymore. 2084 | while (in_first_scanline > stbir_info->ring_buffer_first_scanline) 2085 | { 2086 | if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline) 2087 | { 2088 | // We just popped the last scanline off the ring buffer. 2089 | // Reset it to the empty state. 2090 | stbir_info->ring_buffer_begin_index = -1; 2091 | stbir_info->ring_buffer_first_scanline = 0; 2092 | stbir_info->ring_buffer_last_scanline = 0; 2093 | break; 2094 | } 2095 | else 2096 | { 2097 | stbir_info->ring_buffer_first_scanline++; 2098 | stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries; 2099 | } 2100 | } 2101 | } 2102 | 2103 | // Load in new ones. 2104 | if (stbir_info->ring_buffer_begin_index < 0) 2105 | stbir__decode_and_resample_upsample(stbir_info, in_first_scanline); 2106 | 2107 | while (in_last_scanline > stbir_info->ring_buffer_last_scanline) 2108 | stbir__decode_and_resample_upsample(stbir_info, stbir_info->ring_buffer_last_scanline + 1); 2109 | 2110 | // Now all buffers should be ready to write a row of vertical sampling. 2111 | stbir__resample_vertical_upsample(stbir_info, y); 2112 | 2113 | STBIR_PROGRESS_REPORT((float)y / stbir_info->output_h); 2114 | } 2115 | } 2116 | 2117 | static void stbir__empty_ring_buffer(stbir__info* stbir_info, int first_necessary_scanline) 2118 | { 2119 | int output_stride_bytes = stbir_info->output_stride_bytes; 2120 | int channels = stbir_info->channels; 2121 | int alpha_channel = stbir_info->alpha_channel; 2122 | int type = stbir_info->type; 2123 | int colorspace = stbir_info->colorspace; 2124 | int output_w = stbir_info->output_w; 2125 | void* output_data = stbir_info->output_data; 2126 | int decode = STBIR__DECODE(type, colorspace); 2127 | 2128 | float* ring_buffer = stbir_info->ring_buffer; 2129 | int ring_buffer_length = stbir_info->ring_buffer_length_bytes/sizeof(float); 2130 | 2131 | if (stbir_info->ring_buffer_begin_index >= 0) 2132 | { 2133 | // Get rid of whatever we don't need anymore. 2134 | while (first_necessary_scanline > stbir_info->ring_buffer_first_scanline) 2135 | { 2136 | if (stbir_info->ring_buffer_first_scanline >= 0 && stbir_info->ring_buffer_first_scanline < stbir_info->output_h) 2137 | { 2138 | int output_row_start = stbir_info->ring_buffer_first_scanline * output_stride_bytes; 2139 | float* ring_buffer_entry = stbir__get_ring_buffer_entry(ring_buffer, stbir_info->ring_buffer_begin_index, ring_buffer_length); 2140 | stbir__encode_scanline(stbir_info, output_w, (char *) output_data + output_row_start, ring_buffer_entry, channels, alpha_channel, decode); 2141 | STBIR_PROGRESS_REPORT((float)stbir_info->ring_buffer_first_scanline / stbir_info->output_h); 2142 | } 2143 | 2144 | if (stbir_info->ring_buffer_first_scanline == stbir_info->ring_buffer_last_scanline) 2145 | { 2146 | // We just popped the last scanline off the ring buffer. 2147 | // Reset it to the empty state. 2148 | stbir_info->ring_buffer_begin_index = -1; 2149 | stbir_info->ring_buffer_first_scanline = 0; 2150 | stbir_info->ring_buffer_last_scanline = 0; 2151 | break; 2152 | } 2153 | else 2154 | { 2155 | stbir_info->ring_buffer_first_scanline++; 2156 | stbir_info->ring_buffer_begin_index = (stbir_info->ring_buffer_begin_index + 1) % stbir_info->ring_buffer_num_entries; 2157 | } 2158 | } 2159 | } 2160 | } 2161 | 2162 | static void stbir__buffer_loop_downsample(stbir__info* stbir_info) 2163 | { 2164 | int y; 2165 | float scale_ratio = stbir_info->vertical_scale; 2166 | int output_h = stbir_info->output_h; 2167 | float in_pixels_radius = stbir__filter_info_table[stbir_info->vertical_filter].support(scale_ratio) / scale_ratio; 2168 | int pixel_margin = stbir_info->vertical_filter_pixel_margin; 2169 | int max_y = stbir_info->input_h + pixel_margin; 2170 | 2171 | STBIR_ASSERT(!stbir__use_height_upsampling(stbir_info)); 2172 | 2173 | for (y = -pixel_margin; y < max_y; y++) 2174 | { 2175 | float out_center_of_in; // Center of the current out scanline in the in scanline space 2176 | int out_first_scanline, out_last_scanline; 2177 | 2178 | stbir__calculate_sample_range_downsample(y, in_pixels_radius, scale_ratio, stbir_info->vertical_shift, &out_first_scanline, &out_last_scanline, &out_center_of_in); 2179 | 2180 | STBIR_ASSERT(out_last_scanline - out_first_scanline + 1 <= stbir_info->ring_buffer_num_entries); 2181 | 2182 | if (out_last_scanline < 0 || out_first_scanline >= output_h) 2183 | continue; 2184 | 2185 | stbir__empty_ring_buffer(stbir_info, out_first_scanline); 2186 | 2187 | stbir__decode_and_resample_downsample(stbir_info, y); 2188 | 2189 | // Load in new ones. 2190 | if (stbir_info->ring_buffer_begin_index < 0) 2191 | stbir__add_empty_ring_buffer_entry(stbir_info, out_first_scanline); 2192 | 2193 | while (out_last_scanline > stbir_info->ring_buffer_last_scanline) 2194 | stbir__add_empty_ring_buffer_entry(stbir_info, stbir_info->ring_buffer_last_scanline + 1); 2195 | 2196 | // Now the horizontal buffer is ready to write to all ring buffer rows. 2197 | stbir__resample_vertical_downsample(stbir_info, y); 2198 | } 2199 | 2200 | stbir__empty_ring_buffer(stbir_info, stbir_info->output_h); 2201 | } 2202 | 2203 | static void stbir__setup(stbir__info *info, int input_w, int input_h, int output_w, int output_h, int channels) 2204 | { 2205 | info->input_w = input_w; 2206 | info->input_h = input_h; 2207 | info->output_w = output_w; 2208 | info->output_h = output_h; 2209 | info->channels = channels; 2210 | } 2211 | 2212 | static void stbir__calculate_transform(stbir__info *info, float s0, float t0, float s1, float t1, float *transform) 2213 | { 2214 | info->s0 = s0; 2215 | info->t0 = t0; 2216 | info->s1 = s1; 2217 | info->t1 = t1; 2218 | 2219 | if (transform) 2220 | { 2221 | info->horizontal_scale = transform[0]; 2222 | info->vertical_scale = transform[1]; 2223 | info->horizontal_shift = transform[2]; 2224 | info->vertical_shift = transform[3]; 2225 | } 2226 | else 2227 | { 2228 | info->horizontal_scale = ((float)info->output_w / info->input_w) / (s1 - s0); 2229 | info->vertical_scale = ((float)info->output_h / info->input_h) / (t1 - t0); 2230 | 2231 | info->horizontal_shift = s0 * info->output_w / (s1 - s0); 2232 | info->vertical_shift = t0 * info->output_h / (t1 - t0); 2233 | } 2234 | } 2235 | 2236 | static void stbir__choose_filter(stbir__info *info, stbir_filter h_filter, stbir_filter v_filter) 2237 | { 2238 | if (h_filter == 0) 2239 | h_filter = stbir__use_upsampling(info->horizontal_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE; 2240 | if (v_filter == 0) 2241 | v_filter = stbir__use_upsampling(info->vertical_scale) ? STBIR_DEFAULT_FILTER_UPSAMPLE : STBIR_DEFAULT_FILTER_DOWNSAMPLE; 2242 | info->horizontal_filter = h_filter; 2243 | info->vertical_filter = v_filter; 2244 | } 2245 | 2246 | static stbir_uint32 stbir__calculate_memory(stbir__info *info) 2247 | { 2248 | int pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale); 2249 | int filter_height = stbir__get_filter_pixel_width(info->vertical_filter, info->vertical_scale); 2250 | 2251 | info->horizontal_num_contributors = stbir__get_contributors(info->horizontal_scale, info->horizontal_filter, info->input_w, info->output_w); 2252 | info->vertical_num_contributors = stbir__get_contributors(info->vertical_scale , info->vertical_filter , info->input_h, info->output_h); 2253 | 2254 | // One extra entry because floating point precision problems sometimes cause an extra to be necessary. 2255 | info->ring_buffer_num_entries = filter_height + 1; 2256 | 2257 | info->horizontal_contributors_size = info->horizontal_num_contributors * sizeof(stbir__contributors); 2258 | info->horizontal_coefficients_size = stbir__get_total_horizontal_coefficients(info) * sizeof(float); 2259 | info->vertical_contributors_size = info->vertical_num_contributors * sizeof(stbir__contributors); 2260 | info->vertical_coefficients_size = stbir__get_total_vertical_coefficients(info) * sizeof(float); 2261 | info->decode_buffer_size = (info->input_w + pixel_margin * 2) * info->channels * sizeof(float); 2262 | info->horizontal_buffer_size = info->output_w * info->channels * sizeof(float); 2263 | info->ring_buffer_size = info->output_w * info->channels * info->ring_buffer_num_entries * sizeof(float); 2264 | info->encode_buffer_size = info->output_w * info->channels * sizeof(float); 2265 | 2266 | STBIR_ASSERT(info->horizontal_filter != 0); 2267 | STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late 2268 | STBIR_ASSERT(info->vertical_filter != 0); 2269 | STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); // this now happens too late 2270 | 2271 | if (stbir__use_height_upsampling(info)) 2272 | // The horizontal buffer is for when we're downsampling the height and we 2273 | // can't output the result of sampling the decode buffer directly into the 2274 | // ring buffers. 2275 | info->horizontal_buffer_size = 0; 2276 | else 2277 | // The encode buffer is to retain precision in the height upsampling method 2278 | // and isn't used when height downsampling. 2279 | info->encode_buffer_size = 0; 2280 | 2281 | return info->horizontal_contributors_size + info->horizontal_coefficients_size 2282 | + info->vertical_contributors_size + info->vertical_coefficients_size 2283 | + info->decode_buffer_size + info->horizontal_buffer_size 2284 | + info->ring_buffer_size + info->encode_buffer_size; 2285 | } 2286 | 2287 | static int stbir__resize_allocated(stbir__info *info, 2288 | const void* input_data, int input_stride_in_bytes, 2289 | void* output_data, int output_stride_in_bytes, 2290 | int alpha_channel, stbir_uint32 flags, stbir_datatype type, 2291 | stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace, 2292 | void* tempmem, size_t tempmem_size_in_bytes) 2293 | { 2294 | size_t memory_required = stbir__calculate_memory(info); 2295 | 2296 | int width_stride_input = input_stride_in_bytes ? input_stride_in_bytes : info->channels * info->input_w * stbir__type_size[type]; 2297 | int width_stride_output = output_stride_in_bytes ? output_stride_in_bytes : info->channels * info->output_w * stbir__type_size[type]; 2298 | 2299 | #ifdef STBIR_DEBUG_OVERWRITE_TEST 2300 | #define OVERWRITE_ARRAY_SIZE 8 2301 | unsigned char overwrite_output_before_pre[OVERWRITE_ARRAY_SIZE]; 2302 | unsigned char overwrite_tempmem_before_pre[OVERWRITE_ARRAY_SIZE]; 2303 | unsigned char overwrite_output_after_pre[OVERWRITE_ARRAY_SIZE]; 2304 | unsigned char overwrite_tempmem_after_pre[OVERWRITE_ARRAY_SIZE]; 2305 | 2306 | size_t begin_forbidden = width_stride_output * (info->output_h - 1) + info->output_w * info->channels * stbir__type_size[type]; 2307 | memcpy(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE); 2308 | memcpy(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE); 2309 | memcpy(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE); 2310 | memcpy(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE); 2311 | #endif 2312 | 2313 | STBIR_ASSERT(info->channels >= 0); 2314 | STBIR_ASSERT(info->channels <= STBIR_MAX_CHANNELS); 2315 | 2316 | if (info->channels < 0 || info->channels > STBIR_MAX_CHANNELS) 2317 | return 0; 2318 | 2319 | STBIR_ASSERT(info->horizontal_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); 2320 | STBIR_ASSERT(info->vertical_filter < STBIR__ARRAY_SIZE(stbir__filter_info_table)); 2321 | 2322 | if (info->horizontal_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) 2323 | return 0; 2324 | if (info->vertical_filter >= STBIR__ARRAY_SIZE(stbir__filter_info_table)) 2325 | return 0; 2326 | 2327 | if (alpha_channel < 0) 2328 | flags |= STBIR_FLAG_ALPHA_USES_COLORSPACE | STBIR_FLAG_ALPHA_PREMULTIPLIED; 2329 | 2330 | if (!(flags&STBIR_FLAG_ALPHA_USES_COLORSPACE) || !(flags&STBIR_FLAG_ALPHA_PREMULTIPLIED)) { 2331 | STBIR_ASSERT(alpha_channel >= 0 && alpha_channel < info->channels); 2332 | } 2333 | 2334 | if (alpha_channel >= info->channels) 2335 | return 0; 2336 | 2337 | STBIR_ASSERT(tempmem); 2338 | 2339 | if (!tempmem) 2340 | return 0; 2341 | 2342 | STBIR_ASSERT(tempmem_size_in_bytes >= memory_required); 2343 | 2344 | if (tempmem_size_in_bytes < memory_required) 2345 | return 0; 2346 | 2347 | memset(tempmem, 0, tempmem_size_in_bytes); 2348 | 2349 | info->input_data = input_data; 2350 | info->input_stride_bytes = width_stride_input; 2351 | 2352 | info->output_data = output_data; 2353 | info->output_stride_bytes = width_stride_output; 2354 | 2355 | info->alpha_channel = alpha_channel; 2356 | info->flags = flags; 2357 | info->type = type; 2358 | info->edge_horizontal = edge_horizontal; 2359 | info->edge_vertical = edge_vertical; 2360 | info->colorspace = colorspace; 2361 | 2362 | info->horizontal_coefficient_width = stbir__get_coefficient_width (info->horizontal_filter, info->horizontal_scale); 2363 | info->vertical_coefficient_width = stbir__get_coefficient_width (info->vertical_filter , info->vertical_scale ); 2364 | info->horizontal_filter_pixel_width = stbir__get_filter_pixel_width (info->horizontal_filter, info->horizontal_scale); 2365 | info->vertical_filter_pixel_width = stbir__get_filter_pixel_width (info->vertical_filter , info->vertical_scale ); 2366 | info->horizontal_filter_pixel_margin = stbir__get_filter_pixel_margin(info->horizontal_filter, info->horizontal_scale); 2367 | info->vertical_filter_pixel_margin = stbir__get_filter_pixel_margin(info->vertical_filter , info->vertical_scale ); 2368 | 2369 | info->ring_buffer_length_bytes = info->output_w * info->channels * sizeof(float); 2370 | info->decode_buffer_pixels = info->input_w + info->horizontal_filter_pixel_margin * 2; 2371 | 2372 | #define STBIR__NEXT_MEMPTR(current, newtype) (newtype*)(((unsigned char*)current) + current##_size) 2373 | 2374 | info->horizontal_contributors = (stbir__contributors *) tempmem; 2375 | info->horizontal_coefficients = STBIR__NEXT_MEMPTR(info->horizontal_contributors, float); 2376 | info->vertical_contributors = STBIR__NEXT_MEMPTR(info->horizontal_coefficients, stbir__contributors); 2377 | info->vertical_coefficients = STBIR__NEXT_MEMPTR(info->vertical_contributors, float); 2378 | info->decode_buffer = STBIR__NEXT_MEMPTR(info->vertical_coefficients, float); 2379 | 2380 | if (stbir__use_height_upsampling(info)) 2381 | { 2382 | info->horizontal_buffer = NULL; 2383 | info->ring_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float); 2384 | info->encode_buffer = STBIR__NEXT_MEMPTR(info->ring_buffer, float); 2385 | 2386 | STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->encode_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); 2387 | } 2388 | else 2389 | { 2390 | info->horizontal_buffer = STBIR__NEXT_MEMPTR(info->decode_buffer, float); 2391 | info->ring_buffer = STBIR__NEXT_MEMPTR(info->horizontal_buffer, float); 2392 | info->encode_buffer = NULL; 2393 | 2394 | STBIR_ASSERT((size_t)STBIR__NEXT_MEMPTR(info->ring_buffer, unsigned char) == (size_t)tempmem + tempmem_size_in_bytes); 2395 | } 2396 | 2397 | #undef STBIR__NEXT_MEMPTR 2398 | 2399 | // This signals that the ring buffer is empty 2400 | info->ring_buffer_begin_index = -1; 2401 | 2402 | stbir__calculate_filters(info->horizontal_contributors, info->horizontal_coefficients, info->horizontal_filter, info->horizontal_scale, info->horizontal_shift, info->input_w, info->output_w); 2403 | stbir__calculate_filters(info->vertical_contributors, info->vertical_coefficients, info->vertical_filter, info->vertical_scale, info->vertical_shift, info->input_h, info->output_h); 2404 | 2405 | STBIR_PROGRESS_REPORT(0); 2406 | 2407 | if (stbir__use_height_upsampling(info)) 2408 | stbir__buffer_loop_upsample(info); 2409 | else 2410 | stbir__buffer_loop_downsample(info); 2411 | 2412 | STBIR_PROGRESS_REPORT(1); 2413 | 2414 | #ifdef STBIR_DEBUG_OVERWRITE_TEST 2415 | STBIR_ASSERT(memcmp(overwrite_output_before_pre, &((unsigned char*)output_data)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); 2416 | STBIR_ASSERT(memcmp(overwrite_output_after_pre, &((unsigned char*)output_data)[begin_forbidden], OVERWRITE_ARRAY_SIZE) == 0); 2417 | STBIR_ASSERT(memcmp(overwrite_tempmem_before_pre, &((unsigned char*)tempmem)[-OVERWRITE_ARRAY_SIZE], OVERWRITE_ARRAY_SIZE) == 0); 2418 | STBIR_ASSERT(memcmp(overwrite_tempmem_after_pre, &((unsigned char*)tempmem)[tempmem_size_in_bytes], OVERWRITE_ARRAY_SIZE) == 0); 2419 | #endif 2420 | 2421 | return 1; 2422 | } 2423 | 2424 | 2425 | static int stbir__resize_arbitrary( 2426 | void *alloc_context, 2427 | const void* input_data, int input_w, int input_h, int input_stride_in_bytes, 2428 | void* output_data, int output_w, int output_h, int output_stride_in_bytes, 2429 | float s0, float t0, float s1, float t1, float *transform, 2430 | int channels, int alpha_channel, stbir_uint32 flags, stbir_datatype type, 2431 | stbir_filter h_filter, stbir_filter v_filter, 2432 | stbir_edge edge_horizontal, stbir_edge edge_vertical, stbir_colorspace colorspace) 2433 | { 2434 | stbir__info info; 2435 | int result; 2436 | size_t memory_required; 2437 | void* extra_memory; 2438 | 2439 | stbir__setup(&info, input_w, input_h, output_w, output_h, channels); 2440 | stbir__calculate_transform(&info, s0,t0,s1,t1,transform); 2441 | stbir__choose_filter(&info, h_filter, v_filter); 2442 | memory_required = stbir__calculate_memory(&info); 2443 | extra_memory = STBIR_MALLOC(memory_required, alloc_context); 2444 | 2445 | if (!extra_memory) 2446 | return 0; 2447 | 2448 | result = stbir__resize_allocated(&info, input_data, input_stride_in_bytes, 2449 | output_data, output_stride_in_bytes, 2450 | alpha_channel, flags, type, 2451 | edge_horizontal, edge_vertical, 2452 | colorspace, extra_memory, memory_required); 2453 | 2454 | STBIR_FREE(extra_memory, alloc_context); 2455 | 2456 | return result; 2457 | } 2458 | 2459 | STBIRDEF int stbir_resize_uint8( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2460 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2461 | int num_channels) 2462 | { 2463 | return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, 2464 | output_pixels, output_w, output_h, output_stride_in_bytes, 2465 | 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, 2466 | STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); 2467 | } 2468 | 2469 | STBIRDEF int stbir_resize_float( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2470 | float *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2471 | int num_channels) 2472 | { 2473 | return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, 2474 | output_pixels, output_w, output_h, output_stride_in_bytes, 2475 | 0,0,1,1,NULL,num_channels,-1,0, STBIR_TYPE_FLOAT, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, 2476 | STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_LINEAR); 2477 | } 2478 | 2479 | STBIRDEF int stbir_resize_uint8_srgb(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2480 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2481 | int num_channels, int alpha_channel, int flags) 2482 | { 2483 | return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, 2484 | output_pixels, output_w, output_h, output_stride_in_bytes, 2485 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, 2486 | STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP, STBIR_COLORSPACE_SRGB); 2487 | } 2488 | 2489 | STBIRDEF int stbir_resize_uint8_srgb_edgemode(const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2490 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2491 | int num_channels, int alpha_channel, int flags, 2492 | stbir_edge edge_wrap_mode) 2493 | { 2494 | return stbir__resize_arbitrary(NULL, input_pixels, input_w, input_h, input_stride_in_bytes, 2495 | output_pixels, output_w, output_h, output_stride_in_bytes, 2496 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, STBIR_FILTER_DEFAULT, STBIR_FILTER_DEFAULT, 2497 | edge_wrap_mode, edge_wrap_mode, STBIR_COLORSPACE_SRGB); 2498 | } 2499 | 2500 | STBIRDEF int stbir_resize_uint8_generic( const unsigned char *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2501 | unsigned char *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2502 | int num_channels, int alpha_channel, int flags, 2503 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2504 | void *alloc_context) 2505 | { 2506 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2507 | output_pixels, output_w, output_h, output_stride_in_bytes, 2508 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT8, filter, filter, 2509 | edge_wrap_mode, edge_wrap_mode, space); 2510 | } 2511 | 2512 | STBIRDEF int stbir_resize_uint16_generic(const stbir_uint16 *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2513 | stbir_uint16 *output_pixels , int output_w, int output_h, int output_stride_in_bytes, 2514 | int num_channels, int alpha_channel, int flags, 2515 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2516 | void *alloc_context) 2517 | { 2518 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2519 | output_pixels, output_w, output_h, output_stride_in_bytes, 2520 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_UINT16, filter, filter, 2521 | edge_wrap_mode, edge_wrap_mode, space); 2522 | } 2523 | 2524 | 2525 | STBIRDEF int stbir_resize_float_generic( const float *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2526 | float *output_pixels , int output_w, int output_h, int output_stride_in_bytes, 2527 | int num_channels, int alpha_channel, int flags, 2528 | stbir_edge edge_wrap_mode, stbir_filter filter, stbir_colorspace space, 2529 | void *alloc_context) 2530 | { 2531 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2532 | output_pixels, output_w, output_h, output_stride_in_bytes, 2533 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, STBIR_TYPE_FLOAT, filter, filter, 2534 | edge_wrap_mode, edge_wrap_mode, space); 2535 | } 2536 | 2537 | 2538 | STBIRDEF int stbir_resize( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2539 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2540 | stbir_datatype datatype, 2541 | int num_channels, int alpha_channel, int flags, 2542 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2543 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 2544 | stbir_colorspace space, void *alloc_context) 2545 | { 2546 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2547 | output_pixels, output_w, output_h, output_stride_in_bytes, 2548 | 0,0,1,1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, 2549 | edge_mode_horizontal, edge_mode_vertical, space); 2550 | } 2551 | 2552 | 2553 | STBIRDEF int stbir_resize_subpixel(const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2554 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2555 | stbir_datatype datatype, 2556 | int num_channels, int alpha_channel, int flags, 2557 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2558 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 2559 | stbir_colorspace space, void *alloc_context, 2560 | float x_scale, float y_scale, 2561 | float x_offset, float y_offset) 2562 | { 2563 | float transform[4]; 2564 | transform[0] = x_scale; 2565 | transform[1] = y_scale; 2566 | transform[2] = x_offset; 2567 | transform[3] = y_offset; 2568 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2569 | output_pixels, output_w, output_h, output_stride_in_bytes, 2570 | 0,0,1,1,transform,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, 2571 | edge_mode_horizontal, edge_mode_vertical, space); 2572 | } 2573 | 2574 | STBIRDEF int stbir_resize_region( const void *input_pixels , int input_w , int input_h , int input_stride_in_bytes, 2575 | void *output_pixels, int output_w, int output_h, int output_stride_in_bytes, 2576 | stbir_datatype datatype, 2577 | int num_channels, int alpha_channel, int flags, 2578 | stbir_edge edge_mode_horizontal, stbir_edge edge_mode_vertical, 2579 | stbir_filter filter_horizontal, stbir_filter filter_vertical, 2580 | stbir_colorspace space, void *alloc_context, 2581 | float s0, float t0, float s1, float t1) 2582 | { 2583 | return stbir__resize_arbitrary(alloc_context, input_pixels, input_w, input_h, input_stride_in_bytes, 2584 | output_pixels, output_w, output_h, output_stride_in_bytes, 2585 | s0,t0,s1,t1,NULL,num_channels,alpha_channel,flags, datatype, filter_horizontal, filter_vertical, 2586 | edge_mode_horizontal, edge_mode_vertical, space); 2587 | } 2588 | 2589 | #endif // STB_IMAGE_RESIZE_IMPLEMENTATION 2590 | 2591 | /* 2592 | ------------------------------------------------------------------------------ 2593 | This software is available under 2 licenses -- choose whichever you prefer. 2594 | ------------------------------------------------------------------------------ 2595 | ALTERNATIVE A - MIT License 2596 | Copyright (c) 2017 Sean Barrett 2597 | Permission is hereby granted, free of charge, to any person obtaining a copy of 2598 | this software and associated documentation files (the "Software"), to deal in 2599 | the Software without restriction, including without limitation the rights to 2600 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 2601 | of the Software, and to permit persons to whom the Software is furnished to do 2602 | so, subject to the following conditions: 2603 | The above copyright notice and this permission notice shall be included in all 2604 | copies or substantial portions of the Software. 2605 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2606 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2607 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2608 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 2609 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 2610 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 2611 | SOFTWARE. 2612 | ------------------------------------------------------------------------------ 2613 | ALTERNATIVE B - Public Domain (www.unlicense.org) 2614 | This is free and unencumbered software released into the public domain. 2615 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute this 2616 | software, either in source code form or as a compiled binary, for any purpose, 2617 | commercial or non-commercial, and by any means. 2618 | In jurisdictions that recognize copyright laws, the author or authors of this 2619 | software dedicate any and all copyright interest in the software to the public 2620 | domain. We make this dedication for the benefit of the public at large and to 2621 | the detriment of our heirs and successors. We intend this dedication to be an 2622 | overt act of relinquishment in perpetuity of all present and future rights to 2623 | this software under copyright law. 2624 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 2625 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 2626 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 2627 | AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 2628 | ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 2629 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 2630 | ------------------------------------------------------------------------------ 2631 | */ --------------------------------------------------------------------------------