| 42 | } |
| 43 | |
| 44 | void storeSTB(const Ref<Image>& img, const FileName& fileName) |
| 45 | { |
| 46 | std::string ext = toLowerCase(fileName.ext()); |
| 47 | if (ext != "bmp" && ext != "png" && ext != "jpg") { |
| 48 | THROW_RUNTIME_ERROR("Could not store image") |
| 49 | return; |
| 50 | } |
| 51 | |
| 52 | int width = int(img->width); |
| 53 | int height = int(img->height); |
| 54 | int channels = 3; |
| 55 | std::vector<unsigned char> pixels(width*height*channels); |
| 56 | const float maxColor = 255.f; |
| 57 | for (int y = 0; y < height; y++) |
| 58 | { |
| 59 | for (int x = 0; x < width; x++) |
| 60 | { |
| 61 | Color4 c = img->get(x, y); |
| 62 | pixels[(y*img->width+x)*channels+0] = (unsigned char)(clamp(c.r) * maxColor); |
| 63 | pixels[(y*img->width+x)*channels+1] = (unsigned char)(clamp(c.g) * maxColor); |
| 64 | pixels[(y*img->width+x)*channels+2] = (unsigned char)(clamp(c.b) * maxColor); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | if (ext == "bmp" ) { stbi_write_bmp(fileName.str().c_str(), width, height, channels, pixels.data()); return; } |
| 69 | if (ext == "png" ) { stbi_write_png(fileName.str().c_str(), width, height, channels, pixels.data(), width * channels); return; } |
| 70 | if (ext == "jpg" ) { stbi_write_jpg(fileName.str().c_str(), width, height, channels, pixels.data(), 100); return; } |
| 71 | } |
| 72 | } |
no test coverage detected