Joshua Moerman
12 years ago
6 changed files with 160 additions and 4 deletions
@ -0,0 +1,3 @@ |
|||
build |
|||
.DS_Store |
|||
xcuserdata |
@ -0,0 +1,86 @@ |
|||
//
|
|||
// texture.h
|
|||
// J
|
|||
//
|
|||
// Created by Joshua Moerman on 6/23/12.
|
|||
// Copyright (c) 2012 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef J_texture_h |
|||
#define J_texture_h |
|||
|
|||
#include "basic.h" |
|||
#include <vector> |
|||
#include <algorithm> |
|||
|
|||
namespace J { |
|||
|
|||
struct no_texture {}; |
|||
|
|||
template <typename T> |
|||
struct gl_type {}; |
|||
|
|||
template <> |
|||
struct gl_type<uint8_t> { |
|||
static constexpr GLenum value = GL_UNSIGNED_BYTE; |
|||
}; |
|||
|
|||
template <typename T = uint8_t> |
|||
struct rgba { |
|||
typedef typename std::vector<T>::iterator iterator; |
|||
typedef typename std::vector<T>::iterator const_iterator; |
|||
typedef typename std::vector<T>::size_type size_type; |
|||
static constexpr GLenum gl_type = J::gl_type<T>::value; |
|||
static constexpr GLenum gl_format = GL_RGBA; |
|||
|
|||
rgba(size_type w, size_type h, T const * data = nullptr) |
|||
: width_(w) |
|||
, height_(h) |
|||
, pixels(w*h*4){ |
|||
if(data) |
|||
std::copy_n(data, w*h*4, pixels.begin()); |
|||
} |
|||
|
|||
size_type size() const { return pixels.size(); } |
|||
size_type width() const { return width; } |
|||
size_type height() const { return height; } |
|||
|
|||
struct row_proxy { |
|||
row_proxy(T* data) : data(data) {} |
|||
T& operator[](size_type x){ return data[x]; } |
|||
T operator[](size_type x) const { return data[x]; } |
|||
T* data; |
|||
}; |
|||
|
|||
row_proxy operator[](size_type y){ |
|||
return &pixels[y*width()]; |
|||
} |
|||
|
|||
struct const_row_proxy { |
|||
const_row_proxy(T const * data) : data(data) {} |
|||
T operator[](size_type x) const { return data[x]; } |
|||
T const * data; |
|||
}; |
|||
|
|||
const_row_proxy operator[](size_type y) const { |
|||
return &pixels[y*width()]; |
|||
} |
|||
|
|||
T * data() { return pixels.data(); } |
|||
T const * data() const { return pixels.data(); } |
|||
|
|||
iterator begin() { return pixels.begin(); } |
|||
iterator end() { return pixels.begin(); } |
|||
|
|||
const_iterator begin() const { return pixels.begin(); } |
|||
const_iterator end() const { return pixels.begin(); } |
|||
|
|||
private: |
|||
size_type width_; |
|||
size_type height_; |
|||
std::vector<T> pixels; |
|||
}; |
|||
|
|||
} |
|||
|
|||
#endif |
@ -0,0 +1,22 @@ |
|||
//
|
|||
// texture_objc.h
|
|||
// J
|
|||
//
|
|||
// Created by Joshua Moerman on 6/23/12.
|
|||
// Copyright (c) 2012 Vadovas. All rights reserved.
|
|||
//
|
|||
|
|||
#ifndef J_texture_objc_h |
|||
#define J_texture_objc_h |
|||
|
|||
#include "texture.h" |
|||
|
|||
@class UIImage; |
|||
|
|||
namespace J { |
|||
|
|||
rgba<> texture_from_UIImage(UIImage * image); |
|||
|
|||
} |
|||
|
|||
#endif |
@ -0,0 +1,23 @@ |
|||
// |
|||
// texture_objc.mm |
|||
// J |
|||
// |
|||
// Created by Joshua Moerman on 6/23/12. |
|||
// Copyright (c) 2012 Vadovas. All rights reserved. |
|||
// |
|||
|
|||
#include "texture_objc.h" |
|||
|
|||
#import <UIKit/UIKit.h> |
|||
//#import <Quartz/Quartz.h> |
|||
|
|||
namespace J { |
|||
rgba<> texture_from_UIImage(UIImage * image){ |
|||
CGImageRef cgImage = image.CGImage; |
|||
CGDataProviderRef provider = CGImageGetDataProvider(cgImage); |
|||
CFDataRef bitmapData = CGDataProviderCopyData(provider); |
|||
CFDataGetBytePtr(bitmapData); |
|||
|
|||
return rgba<>(image.size.width, image.size.height, CFDataGetBytePtr(bitmapData)); |
|||
} |
|||
} |
Reference in new issue