Browse Source

added another example and (untested) jpg support

master
Joshua Moerman 12 years ago
parent
commit
f57ae8380e
  1. 2
      ImageStreams.xcodeproj/project.pbxproj
  2. 89
      ImageStreams/jpg.hpp
  3. 22
      ImageStreams/main.cpp

2
ImageStreams.xcodeproj/project.pbxproj

@ -33,6 +33,7 @@
4263431915FA67D500977AF9 /* png.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = png.hpp; sourceTree = "<group>"; };
4263431A15FA686300977AF9 /* basics.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = basics.hpp; sourceTree = "<group>"; };
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 = "<group>"; };
/* 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 = "<group>";

89
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 <stdio.h>
#include <stdexcept>
#include <vector>
#include <jpeglib.h>
#include "basics.hpp"
/*
JPEG only supports 8bits or 24bits per pixel (eg grayscale or full color).
Top to bottom
*/
namespace jpg{
template <typename P = pixel_formats::rgb>
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<pixel>::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<unsigned char const*>(row.data()), 1);
x = 0;
}
return *this;
}
private:
FILE* fp;
jpeg_compress_struct cinfo;
jpeg_error_mgr jerr;
std::vector<pixel> row;
uint32_t x;
};
typedef ostream<> colored_ostream;
typedef ostream<pixel_formats::gray> gray_ostream;
}
#endif

22
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 <typename ImageType>
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<png::colored_ostream>("xor_color.png");
xor_color<bmp::colored_ostream>("xor_color.bmp");
@ -112,5 +129,8 @@ int main(int argc, const char * argv[]){
logistic<png::colored_ostream>("logistic.png");
logistic<bmp::colored_ostream>("logistic.bmp");
logistic2<png::colored_ostream>("logistic2.png");
logistic2<bmp::colored_ostream>("logistic2.bmp");
}