From 446dc14dafc553b1d6e4bef959affa20ef3bc15c Mon Sep 17 00:00:00 2001 From: Joshua Moerman Date: Sat, 15 Sep 2012 17:03:08 +0200 Subject: [PATCH] Fixed jpg. Added a basic image for testing colors --- ImageStreams.xcodeproj/project.pbxproj | 4 +++ ImageStreams/jpg.hpp | 16 ++++++++-- ImageStreams/main.cpp | 42 +++++++++++++++----------- 3 files changed, 41 insertions(+), 21 deletions(-) diff --git a/ImageStreams.xcodeproj/project.pbxproj b/ImageStreams.xcodeproj/project.pbxproj index 62a18b9..23e6eec 100644 --- a/ImageStreams.xcodeproj/project.pbxproj +++ b/ImageStreams.xcodeproj/project.pbxproj @@ -10,6 +10,7 @@ 4263431015FA676F00977AF9 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4263430F15FA676F00977AF9 /* main.cpp */; }; 4263431215FA676F00977AF9 /* ImageStreams.1 in CopyFiles */ = {isa = PBXBuildFile; fileRef = 4263431115FA676F00977AF9 /* ImageStreams.1 */; }; 4263431C15FA6A3200977AF9 /* libpng.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 4263431B15FA6A3200977AF9 /* libpng.framework */; }; + 4263431F1604CA3900977AF9 /* libjpeg.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 4263431E1604CA3900977AF9 /* libjpeg.a */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ @@ -34,6 +35,7 @@ 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 = ""; }; + 4263431E1604CA3900977AF9 /* libjpeg.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libjpeg.a; path = ../../../../../usr/local/lib/libjpeg.a; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -42,6 +44,7 @@ buildActionMask = 2147483647; files = ( 4263431C15FA6A3200977AF9 /* libpng.framework in Frameworks */, + 4263431F1604CA3900977AF9 /* libjpeg.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -51,6 +54,7 @@ 4263430015FA676F00977AF9 = { isa = PBXGroup; children = ( + 4263431E1604CA3900977AF9 /* libjpeg.a */, 4263431B15FA6A3200977AF9 /* libpng.framework */, 4263430E15FA676F00977AF9 /* ImageStreams */, 4263430C15FA676F00977AF9 /* Products */, diff --git a/ImageStreams/jpg.hpp b/ImageStreams/jpg.hpp index c2f9ba0..07d3fea 100644 --- a/ImageStreams/jpg.hpp +++ b/ImageStreams/jpg.hpp @@ -21,6 +21,15 @@ */ namespace jpg{ + template + J_COLOR_SPACE color_space(){ + int num_colors = pixel_formats::traits

::num_colors; + switch (num_colors) { + case 1: return JCS_GRAYSCALE; + case 3: return JCS_RGB; + default: return JCS_UNKNOWN; + } + } template struct ostream{ @@ -47,10 +56,10 @@ namespace jpg{ 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() + cinfo.in_color_space = color_space(); jpeg_set_defaults(&cinfo); - //jpeg_set_quality(&cinfo, 75, true); // quality in [0, 100], boolean indicates "force_baseline" (only matters when quality < 25) + jpeg_set_quality(&cinfo, 95, 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 } @@ -65,7 +74,8 @@ namespace jpg{ ++x; if(x >= row.size()){ // this will return the number of scanlines written - jpeg_write_scanlines(&cinfo, reinterpret_cast(row.data()), 1); + unsigned char * ptr = reinterpret_cast(row.data()); + jpeg_write_scanlines(&cinfo, &ptr, 1); x = 0; } diff --git a/ImageStreams/main.cpp b/ImageStreams/main.cpp index 041d27f..1acd3da 100644 --- a/ImageStreams/main.cpp +++ b/ImageStreams/main.cpp @@ -10,12 +10,21 @@ #include "png.hpp" #include "bmp.hpp" +#include "jpg.hpp" #include "basics.hpp" #include #include +#include #include +template +void basic_colors(std::string filename) { + ImageType image(8, 1, filename); + std::array v = {0.0, 1.0}; + for(auto b : v) for(auto g : v) for(auto r : v) image << typename ImageType::pixel(r,g,b); +} + template void xor_color(std::string filename){ size_t size = 512; @@ -142,25 +151,22 @@ void mandelbrot(std::string filename) { } int main(int argc, const char * argv[]){ - xor_color("xor_color.png"); - xor_color("xor_color.bmp"); - - xor_grad("xor_grad.png"); - xor_grad("xor_grad.bmp"); - - noise("noise.png"); - noise("noise.bmp"); - - automata("automata.png"); - automata("automata.bmp"); - - logistic("logistic.png"); - logistic("logistic.bmp"); + #define test(fun, kind) \ + std::cout << "Testing " #fun << std::endl; \ + fun(#fun ".png"); \ + fun(#fun ".bmp"); \ + fun(#fun ".jpg") + + test(basic_colors, colored); + test(xor_color, colored); + test(logistic, colored); + test(logistic2, colored); - logistic2("logistic2.png"); - logistic2("logistic2.bmp"); + test(noise, gray); + test(xor_grad, gray); + test(automata, gray); + test(mandelbrot, gray); - mandelbrot("mandelbrot.png"); - mandelbrot("mandelbrot.bmp"); + #undef test }