diff --git a/ImageStreams.xcodeproj/project.pbxproj b/ImageStreams.xcodeproj/project.pbxproj index 89c7323..62a18b9 100644 --- a/ImageStreams.xcodeproj/project.pbxproj +++ b/ImageStreams.xcodeproj/project.pbxproj @@ -33,6 +33,7 @@ 4263431915FA67D500977AF9 /* png.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = png.hpp; sourceTree = ""; }; 4263431A15FA686300977AF9 /* basics.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = basics.hpp; sourceTree = ""; }; 4263431B15FA6A3200977AF9 /* libpng.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = libpng.framework; path = Library/Frameworks/libpng.framework; sourceTree = SDKROOT; }; + 4263431D1604A79F00977AF9 /* jpg.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = jpg.hpp; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -72,6 +73,7 @@ 4263431A15FA686300977AF9 /* basics.hpp */, 4263431815FA679600977AF9 /* bmp.hpp */, 4263431915FA67D500977AF9 /* png.hpp */, + 4263431D1604A79F00977AF9 /* jpg.hpp */, ); path = ImageStreams; sourceTree = ""; diff --git a/ImageStreams/jpg.hpp b/ImageStreams/jpg.hpp new file mode 100644 index 0000000..c2f9ba0 --- /dev/null +++ b/ImageStreams/jpg.hpp @@ -0,0 +1,89 @@ +// +// jpg.hpp +// ImageStreams +// +// Created by Joshua Moerman on 9/15/12. +// Copyright (c) 2012 Vadovas. All rights reserved. +// + +#ifndef ImageStreams_jpg_hpp +#define ImageStreams_jpg_hpp + +#include +#include +#include +#include +#include "basics.hpp" + +/* + JPEG only supports 8bits or 24bits per pixel (eg grayscale or full color). + Top to bottom + */ + +namespace jpg{ + + template + struct ostream{ + typedef P pixel; + + ostream() = delete; + ostream(ostream const &) = delete; + ostream & operator=(ostream const &) = delete; + + ostream(uint32_t width, uint32_t height, std::string filename) + : fp(0) + , cinfo() + , jerr() + , row(width) + , x(0) + { + fp = fopen(filename.c_str(), "wb"); + if(!fp) throw std::runtime_error("Could not open file"); + + cinfo.err = jpeg_std_error(&jerr); + jpeg_create_compress(&cinfo); + jpeg_stdio_dest(&cinfo, fp); + + cinfo.image_width = width; + cinfo.image_height = height; + cinfo.input_components = pixel_formats::traits::num_colors; + //cinfo.in_color_space = JCS_RGB; // or JCS_GRAYSCALE, will be set in jpeg_set_defaults() + + jpeg_set_defaults(&cinfo); + //jpeg_set_quality(&cinfo, 75, true); // quality in [0, 100], boolean indicates "force_baseline" (only matters when quality < 25) + jpeg_start_compress(&cinfo, true); // true means we will write completely + } + + ~ostream(){ + jpeg_finish_compress(&cinfo); + fclose(fp); + jpeg_destroy_compress(&cinfo); + } + + ostream& operator<<(pixel const & p){ + row[x] = p; + ++x; + if(x >= row.size()){ + // this will return the number of scanlines written + jpeg_write_scanlines(&cinfo, reinterpret_cast(row.data()), 1); + x = 0; + } + + return *this; + } + + private: + FILE* fp; + jpeg_compress_struct cinfo; + jpeg_error_mgr jerr; + + std::vector row; + uint32_t x; + }; + + typedef ostream<> colored_ostream; + typedef ostream gray_ostream; + +} + +#endif diff --git a/ImageStreams/main.cpp b/ImageStreams/main.cpp index e9b5ac9..22a6939 100644 --- a/ImageStreams/main.cpp +++ b/ImageStreams/main.cpp @@ -91,12 +91,29 @@ void logistic(std::string filename) { ImageType image(size, size, filename); for(int i = 0; i < size*size; ++i){ - double c = start + i * (end - start) / (size*size); + double c = start + i * (end - start) / double(size*size - 1); // I know; the order of evaluation is implementation defined image << typename ImageType::pixel(x = logistic_step(x, c), x = logistic_step(x, c), x = logistic_step(x, c)); } } +template +void logistic2(std::string filename) { + size_t width = 1280; + size_t height = 800; + double start = 3.847; + double end = 3.9; + + ImageType image(width, height, filename); + for(int y = 0; y < height; ++y){ + double z = 0.5 * y / double(height - 1); + for(int x = 0; x < width; ++x){ + double c = start + x * (end - start) / double(width - 1); + image << typename ImageType::pixel(z = logistic_step(z, c), z = logistic_step(z, c), z = logistic_step(z, c)); + } + } +} + int main(int argc, const char * argv[]){ xor_color("xor_color.png"); xor_color("xor_color.bmp"); @@ -112,5 +129,8 @@ int main(int argc, const char * argv[]){ logistic("logistic.png"); logistic("logistic.bmp"); + + logistic2("logistic2.png"); + logistic2("logistic2.bmp"); }