Merge with base
This commit is contained in:
103
main.cpp
103
main.cpp
@@ -15,7 +15,9 @@ OK Depth buffer
|
||||
KINDA_OK Gamma correct blending
|
||||
OK Alpha blending??
|
||||
OK Premultiplied alpha???
|
||||
OK Merge with base
|
||||
* Lightning
|
||||
OK GLOBAL Ilumination
|
||||
* LookAt Camera
|
||||
* FPS Camera
|
||||
OK Reading OBJ files
|
||||
@@ -31,38 +33,25 @@ OK Reading OBJ files
|
||||
* Gamma correct and alpha blending
|
||||
*/
|
||||
|
||||
/* What a codebase needs:
|
||||
* Macros for debug, release, slow, fast builds, where debug - tooling, slow - enable asserts
|
||||
* Macros for OS, Compiler, Architecture
|
||||
* Nice way of outputing visible error messages
|
||||
* FatalError and Assert function for release builds with an error message, Debug Assert with error message for slow builds
|
||||
*
|
||||
*/
|
||||
#define OS_WINDOWS 1
|
||||
#define PERSPECTIVE_CORRECT_INTERPOLATION 1
|
||||
#define BILINEAR_BLEND 1
|
||||
#define GAMMA_CORRECT_BLENDING 1
|
||||
#define PREMULTIPLIED_ALPHA_BLENDING 1
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include "main.h"
|
||||
#include "platform.h"
|
||||
#define PLATFORM
|
||||
#include "base.h"
|
||||
#include "math.h"
|
||||
#include "stb_image.h"
|
||||
#include "objparser.h"
|
||||
#include <float.h>
|
||||
|
||||
GLOBAL OS os = {};
|
||||
//GLOBAL OS os = {};
|
||||
GLOBAL bool draw_rects = 0;
|
||||
GLOBAL bool draw_wireframe = 0;
|
||||
|
||||
struct Face {
|
||||
int p[3];
|
||||
Vec2 tex[3];
|
||||
};
|
||||
|
||||
FUNCTION
|
||||
void draw_rect(Image* dst, float X, float Y, float w, float h, U32 color) {
|
||||
void draw_rect(Bitmap* dst, float X, float Y, float w, float h, U32 color) {
|
||||
int max_x = (int)(MIN(X + w, dst->x) + 0.5f);
|
||||
int max_y = (int)(MIN(Y + h, dst->y) + 0.5f);
|
||||
int min_x = (int)(MAX(0, X) + 0.5f);
|
||||
@@ -89,12 +78,12 @@ Vec4 srgb_to_almost_linear(Vec4 a) {
|
||||
|
||||
FUNCTION
|
||||
Vec4 almost_linear_to_srgb(Vec4 a) {
|
||||
Vec4 result = { sqrt(a.r), sqrt(a.g), sqrt(a.b), a.a };
|
||||
Vec4 result = { sqrtf(a.r), sqrtf(a.g), sqrtf(a.b), a.a };
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
void draw_bitmap(Image* dst, Image* src, Vec2 pos) {
|
||||
void draw_bitmap(Bitmap* dst, Bitmap* src, Vec2 pos) {
|
||||
I64 minx = (I64)(pos.x + 0.5);
|
||||
I64 miny = (I64)(pos.y + 0.5);
|
||||
I64 maxx = minx + src->x;
|
||||
@@ -130,14 +119,14 @@ FUNCTION
|
||||
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
||||
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = color_to_u32abgr(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
*dst_pixel = color32;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
||||
void draw_triangle(Bitmap* dst, float *depth_buffer, Bitmap *src, float light,
|
||||
Vec4 p0, Vec4 p1, Vec4 p2,
|
||||
Vec2 tex0, Vec2 tex1, Vec2 tex2) {
|
||||
float min_x1 = (float)(MIN(p0.x, MIN(p1.x, p2.x)));
|
||||
@@ -216,7 +205,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
//ASSERT(result_color.r <= 1 && result_color.g <= 1 && result_color.b <= 1);
|
||||
#endif // GAMMA_CORRECT_BLENDING
|
||||
U32 color32 = color_to_u32abgr(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
#else // BILINEAR_BLEND
|
||||
Vec4 result_color = srgb_to_almost_linear(vec4abgr(*pixel));
|
||||
Vec4 dst_color = srgb_to_almost_linear(vec4abgr(*dst_pixel));
|
||||
@@ -225,7 +214,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
||||
result_color.b = result_color.b + (1-result_color.a) * dst_color.b;
|
||||
result_color.a = result_color.a + dst_color.a - result_color.a*dst_color.a;
|
||||
result_color = almost_linear_to_srgb(result_color);
|
||||
U32 color32 = color_to_u32abgr(result_color);
|
||||
U32 color32 = vec4_to_u32abgr(result_color);
|
||||
#endif // BILINEAR_BLEND
|
||||
|
||||
*dst_pixel = color32;
|
||||
@@ -241,7 +230,7 @@ void draw_triangle(Image* dst, float *depth_buffer, Image *src, float light,
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
void draw_line(Image *dst, float x0, float y0, float x1, float y1) {
|
||||
void draw_line(Bitmap *dst, float x0, float y0, float x1, float y1) {
|
||||
float delta_x = (x1 - x0);
|
||||
float delta_y = (y1 - y0);
|
||||
float longest_side_length = (ABS(delta_x) >= ABS(delta_y)) ? ABS(delta_x) : ABS(delta_y);
|
||||
@@ -264,20 +253,25 @@ struct FaceA {
|
||||
int normal[3];
|
||||
};
|
||||
|
||||
FN void null_terminate(Arena *arena) {
|
||||
PUSH_SIZE(arena, 1);
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Obj load_obj(const char* file) {
|
||||
char* data = os.read_file(file);
|
||||
Obj load_obj(S8 file) {
|
||||
Scratch scratch;
|
||||
S8 data = os_read_file(scratch, file).error_is_fatal();
|
||||
null_terminate(scratch);
|
||||
char* memory = (char*)malloc(100000);
|
||||
Obj result = obj::parse(memory, 100000, data);
|
||||
free(data);
|
||||
Obj result = obj::parse(memory, 100000, (char *)data.str);
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Image load_image(const char* path) {
|
||||
Bitmap load_image(const char* path) {
|
||||
int x, y, n;
|
||||
unsigned char* data = stbi_load(path, &x, &y, &n, 4);
|
||||
Image result = { (U32*)data, x, y };
|
||||
Bitmap result = { (U32*)data, x, y };
|
||||
#if PREMULTIPLIED_ALPHA_BLENDING
|
||||
U32 *p = result.pixels;
|
||||
for (int Y = 0; Y < y; Y++) {
|
||||
@@ -286,7 +280,7 @@ Image load_image(const char* path) {
|
||||
color.r *= color.a;
|
||||
color.g *= color.a;
|
||||
color.b *= color.a;
|
||||
*p++ = color_to_u32abgr(color);
|
||||
*p++ = vec4_to_u32abgr(color);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@@ -295,25 +289,28 @@ Image load_image(const char* path) {
|
||||
|
||||
int main() {
|
||||
obj::test();
|
||||
os.init({ 1280,720 });
|
||||
os.window_size.x = 1280;
|
||||
os.window_size.y = 720;
|
||||
os_init();
|
||||
os_init_software_render();
|
||||
|
||||
float rotation = 0;
|
||||
Vec3 camera_pos = {0,0,-5};
|
||||
|
||||
Obj obj = load_obj("assets/f22.obj");
|
||||
Obj obj = load_obj(LIT("assets/f22.obj"));
|
||||
Vec3* vertices = (Vec3 *)obj.vertices;
|
||||
Vec2* tex_coords = (Vec2*)obj.texture;
|
||||
Vec3 *normals = (Vec3 *)obj.normals;
|
||||
FaceA* faces = (FaceA*)obj.indices;
|
||||
I64 face_count = obj.indices_count;
|
||||
|
||||
|
||||
Image img = load_image("assets/bricksx64.png");
|
||||
Bitmap img = load_image("assets/bricksx64.png");
|
||||
int screen_x = 320;
|
||||
int screen_y = 180;
|
||||
Image screen320 = {(U32 *)malloc(screen_x*screen_y*sizeof(U32)), screen_x, screen_y};
|
||||
Bitmap screen320 = {(U32 *)malloc(screen_x*screen_y*sizeof(U32)), screen_x, screen_y};
|
||||
float* depth320 = (float *)malloc(sizeof(float) * screen_x * screen_y);
|
||||
while (os.game_loop()) {
|
||||
Mat4 perspective = make_matrix_perspective(60.f, (float)os.screen.x, (float)os.screen.y, 0.1f, 100.f);
|
||||
while (os_game_loop()) {
|
||||
Mat4 perspective = make_matrix_perspective(60.f, (float)screen->x, (float)screen->y, 0.1f, 100.f);
|
||||
U32* p = screen320.pixels;
|
||||
for (int y = 0; y < screen320.y; y++) {
|
||||
for (int x = 0; x < screen320.x; x++) {
|
||||
@@ -329,10 +326,11 @@ int main() {
|
||||
|
||||
Mat4 transform = make_matrix_rotation_z(rotation);
|
||||
transform = transform * make_matrix_rotation_x(rotation);
|
||||
if (os.keydown_a) rotation += 0.05f;
|
||||
if (os.keydown_b) rotation -= 0.05f;
|
||||
if (os.keydown_f1) draw_rects = !draw_rects;
|
||||
if (os.keydown_f2) draw_wireframe = !draw_wireframe;
|
||||
if (os.key[Key_Escape].pressed) os.quit = true;
|
||||
if (os.key[Key_O].down) rotation += 0.05f;
|
||||
if (os.key[Key_P].down) rotation -= 0.05f;
|
||||
if (os.key[Key_F1].pressed) draw_rects = !draw_rects;
|
||||
if (os.key[Key_F2].pressed) draw_wireframe = !draw_wireframe;
|
||||
for (int i = 0; i < face_count; i++) {
|
||||
FaceA* face = faces + i;
|
||||
Vec4 pos[3] = {
|
||||
@@ -345,6 +343,11 @@ int main() {
|
||||
tex_coords[face->tex[1] - 1],
|
||||
tex_coords[face->tex[2] - 1],
|
||||
};
|
||||
Vec3 norm[3] = {
|
||||
normals[face->normal[0] - 1],
|
||||
normals[face->normal[1] - 1],
|
||||
normals[face->normal[2] - 1],
|
||||
};
|
||||
|
||||
//@Note: Transform
|
||||
for (int j = 0; j < 3; j++) {
|
||||
@@ -356,7 +359,7 @@ int main() {
|
||||
Vec3 p0_to_p2 = pos[2].xyz - pos[0].xyz;
|
||||
Vec3 normal = normalize(cross(p0_to_p1, p0_to_p2));
|
||||
float light = -dot(normal, vec3(0,1,0));
|
||||
light = CLAMP(0.05, light, 1);
|
||||
light = CLAMP(0.05f, light, 1.f);
|
||||
if (dot(normal, p0_to_camera) > 0) {
|
||||
for (int j = 0; j < 3; j++) {
|
||||
//@Note: Camera
|
||||
@@ -388,13 +391,13 @@ int main() {
|
||||
}
|
||||
|
||||
// @Note: Draw 320screen to OS screen
|
||||
U32* ptr = os.screen.pixels;
|
||||
for (int y = 0; y < os.screen.y; y++) {
|
||||
for (int x = 0; x < os.screen.x; x++) {
|
||||
float u = (float)x / (float)os.screen.x;
|
||||
float v = (float)y / (float)os.screen.y;
|
||||
int tx = (int)(u * screen320.x + 0.5f);
|
||||
int ty = (int)(v * screen320.y + 0.5f);
|
||||
U32* ptr = screen->pixels;
|
||||
for (int y = 0; y < screen->y; y++) {
|
||||
for (int x = 0; x < screen->x; x++) {
|
||||
float u = (float)x / (float)screen->x;
|
||||
float v = (float)y / (float)screen->y;
|
||||
int tx = (int)(u * screen320.x );
|
||||
int ty = (int)(v * screen320.y );
|
||||
*ptr++ = screen320.pixels[tx + ty * (screen320.x)];
|
||||
}
|
||||
}
|
||||
|
||||
13
main.h
13
main.h
@@ -1,13 +0,0 @@
|
||||
#pragma once
|
||||
#define ASSERT(x) if(!(x)) *(volatile int *)0=0;
|
||||
#define ARRAY_CAP(x) sizeof((x))/sizeof(*(x))
|
||||
#define FUNCTION static
|
||||
#define GLOBAL static
|
||||
#define MIN(x,y) ((x)>(y)?(y):(x))
|
||||
#define MAX(x,y) ((x)>(y)?(x):(y))
|
||||
#define CLAMP(min,x,max) MAX(min,MIN(max, x))
|
||||
#define ABS(x) ((x)<0?-(x):(x))
|
||||
#include <stdint.h>
|
||||
typedef int64_t I64;
|
||||
typedef uint64_t U64;
|
||||
typedef uint32_t U32;
|
||||
273
math.h
273
math.h
@@ -1,110 +1,8 @@
|
||||
#include "main.h"
|
||||
#include <intrin.h>
|
||||
|
||||
constexpr float PI32 = 3.14159265359f;
|
||||
|
||||
struct Mat4 {
|
||||
float p[4][4];
|
||||
};
|
||||
|
||||
struct Vec2 {
|
||||
float x, y;
|
||||
};
|
||||
|
||||
union Vec3 {
|
||||
struct { float x, y, z; };
|
||||
struct { float r, g, b; };
|
||||
};
|
||||
|
||||
union Vec4 {
|
||||
struct { float x, y, z, w; };
|
||||
struct { float r, g, b, a; };
|
||||
struct { Vec3 xyz; };
|
||||
};
|
||||
|
||||
FUNCTION
|
||||
Vec4 vec4(Vec3 a, float b) {
|
||||
Vec4 result = { a.x,a.y,a.z,b };
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 vec3(float x, float y, float z) {
|
||||
Vec3 result = { x,y,z };
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Mat4 make_matrix_identity() {
|
||||
Mat4 result = {
|
||||
1,0,0,0,
|
||||
0,1,0,0,
|
||||
0,0,1,0,
|
||||
0,0,0,1,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float sin(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_sin_ps(result128);
|
||||
float result = *(float *)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float cos(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_cos_ps(result128);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float tan(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_tan_ps(result128);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float floor(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_floor_ps(result128);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float ceil(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_ceil_ps(result128);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float round(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_round_ps(result128, _MM_FROUND_TO_NEAREST_INT| _MM_FROUND_NO_EXC);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float sqrt(float value) {
|
||||
__m128 result128 = _mm_set_ps1(value);
|
||||
result128 = _mm_sqrt_ps(result128);
|
||||
float result = *(float*)&result128;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Mat4 make_matrix_rotation_x(float rotation) {
|
||||
float s = sin(rotation);
|
||||
float c = cos(rotation);
|
||||
float s = sinf(rotation);
|
||||
float c = cosf(rotation);
|
||||
Mat4 result = {
|
||||
c, s, 0, 0,
|
||||
-s, c, 0, 0,
|
||||
@@ -116,8 +14,8 @@ Mat4 make_matrix_rotation_x(float rotation) {
|
||||
|
||||
FUNCTION
|
||||
Mat4 make_matrix_rotation_y(float rotation) {
|
||||
float s = sin(rotation);
|
||||
float c = cos(rotation);
|
||||
float s = sinf(rotation);
|
||||
float c = cosf(rotation);
|
||||
Mat4 result = {
|
||||
c, 0, -s, 0,
|
||||
0, 1, 0, 0,
|
||||
@@ -129,8 +27,8 @@ Mat4 make_matrix_rotation_y(float rotation) {
|
||||
|
||||
FUNCTION
|
||||
Mat4 make_matrix_rotation_z(float rotation) {
|
||||
float s = sin(rotation);
|
||||
float c = cos(rotation);
|
||||
float s = sinf(rotation);
|
||||
float c = cosf(rotation);
|
||||
Mat4 result = {
|
||||
1, 0, 0, 0,
|
||||
0, c, s, 0,
|
||||
@@ -143,7 +41,7 @@ Mat4 make_matrix_rotation_z(float rotation) {
|
||||
FUNCTION
|
||||
Mat4 make_matrix_perspective(float fov, float window_x, float window_y, float znear, float zfar) {
|
||||
float aspect_ratio = window_y / window_x;
|
||||
float f = (1.f / tan((fov/2.f)*(180.f/PI32)));
|
||||
float f = (1.f / tanf((fov/2.f)*(180.f/PI32)));
|
||||
Mat4 result = {
|
||||
aspect_ratio*f, 0, 0, 0,
|
||||
0, f, 0, 0,
|
||||
@@ -172,160 +70,3 @@ Mat4 translate(Mat4 a, Vec3 translation) {
|
||||
a.p[0][2] += translation.z;
|
||||
return a;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 operator-(Vec4 a, Vec4 b) {
|
||||
Vec4 result = {
|
||||
a.x - b.x,
|
||||
a.y - b.y,
|
||||
a.z - b.z,
|
||||
a.w - b.w
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 operator+(Vec4 a, Vec4 b) {
|
||||
Vec4 result = {
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
a.z + b.z,
|
||||
a.w + b.w
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 operator-(Vec3 a, Vec3 b) {
|
||||
Vec3 result = {
|
||||
a.x - b.x,
|
||||
a.y - b.y,
|
||||
a.z - b.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 operator+(Vec3 a, Vec3 b) {
|
||||
Vec3 result = {
|
||||
a.x + b.x,
|
||||
a.y + b.y,
|
||||
a.z + b.z,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 operator*(Mat4 a, Vec4 b) {
|
||||
Vec4 result = {
|
||||
a.p[0][0] * b.x + a.p[0][1] * b.y + a.p[0][2] * b.z + a.p[0][3] * b.w,
|
||||
a.p[1][0] * b.x + a.p[1][1] * b.y + a.p[1][2] * b.z + a.p[1][3] * b.w,
|
||||
a.p[2][0] * b.x + a.p[2][1] * b.y + a.p[2][2] * b.z + a.p[2][3] * b.w,
|
||||
a.p[3][0] * b.x + a.p[3][1] * b.y + a.p[3][2] * b.z + a.p[3][3] * b.w,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 operator*(Mat4 a, Vec3 b) {
|
||||
Vec4 result= {
|
||||
a.p[0][0] * b.x + a.p[0][1] * b.y + a.p[0][2] * b.z + a.p[0][3] * 1,
|
||||
a.p[1][0] * b.x + a.p[1][1] * b.y + a.p[1][2] * b.z + a.p[1][3] * 1,
|
||||
a.p[2][0] * b.x + a.p[2][1] * b.y + a.p[2][2] * b.z + a.p[2][3] * 1,
|
||||
a.p[3][0] * b.x + a.p[3][1] * b.y + a.p[3][2] * b.z + a.p[3][3] * 1,
|
||||
};
|
||||
ASSERT(result.w == 1);
|
||||
return result.xyz;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Mat4 operator*(Mat4 a, Mat4 b) {
|
||||
Mat4 result;
|
||||
for (int y = 0; y < 4; y++) {
|
||||
for (int x = 0; x < 4; x++) {
|
||||
result.p[y][x] = a.p[y][0] * b.p[0][x] + a.p[y][1] * b.p[1][x] + a.p[y][2] * b.p[2][x] + a.p[y][3] * b.p[3][x];
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float dot(Vec3 a, Vec3 b) {
|
||||
float result = a.x * b.x + a.y * b.y + a.z * b.z;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 cross(Vec3 a, Vec3 b) {
|
||||
Vec3 result = {
|
||||
a.y * b.z - a.z * b.y,
|
||||
a.z * b.x - a.x * b.z,
|
||||
a.x * b.y - a.y * b.x,
|
||||
};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float length(Vec3 a) {
|
||||
float result = sqrt(a.x*a.x + a.y*a.y + a.z*a.z);
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec3 normalize(Vec3 a) {
|
||||
float len = length(a);
|
||||
Vec3 result = {a.x/len, a.y/len, a.z/len};
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
U32 color_to_u32argb(Vec4 a) {
|
||||
uint8_t r8 = (uint8_t)(a.r * 255.f);
|
||||
uint8_t g8 = (uint8_t)(a.g * 255.f);
|
||||
uint8_t b8 = (uint8_t)(a.b * 255.f);
|
||||
uint8_t a8 = (uint8_t)(a.a * 255.f);
|
||||
U32 result = a8 << 24 | r8 << 16 | g8 << 8 | b8;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
U32 color_to_u32abgr(Vec4 a) {
|
||||
uint8_t r8 = (uint8_t)(a.r * 255.f);
|
||||
uint8_t g8 = (uint8_t)(a.g * 255.f);
|
||||
uint8_t b8 = (uint8_t)(a.b * 255.f);
|
||||
uint8_t a8 = (uint8_t)(a.a * 255.f);
|
||||
U32 result = a8 << 24 | b8 << 16 | g8 << 8 | r8;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 v4argb(U32 c) {
|
||||
float a = ((c & 0xff000000) >> 24) / 255.f;
|
||||
float r = ((c & 0x00ff0000) >> 16) / 255.f;
|
||||
float g = ((c & 0x0000ff00) >> 8) / 255.f;
|
||||
float b = ((c & 0x000000ff) >> 0) / 255.f;
|
||||
Vec4 result = { r,g,b,a };
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 vec4abgr(U32 c) {
|
||||
float a = ((c & 0xff000000) >> 24) / 255.f;
|
||||
float b = ((c & 0x00ff0000) >> 16) / 255.f;
|
||||
float g = ((c & 0x0000ff00) >> 8) / 255.f;
|
||||
float r = ((c & 0x000000ff) >> 0) / 255.f;
|
||||
Vec4 result = { r,g,b,a };
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
float lerp(float a, float b, float t) {
|
||||
float result = a + (b-a)*t; //(1.0f - t) * a + t * b;
|
||||
return result;
|
||||
}
|
||||
|
||||
FUNCTION
|
||||
Vec4 lerp(Vec4 a, Vec4 b, float t) {
|
||||
Vec4 result = {lerp(a.x,b.x,t), lerp(a.y,b.y,t), lerp(a.z,b.z,t), lerp(a.w,b.w,t) };
|
||||
return result;
|
||||
}
|
||||
@@ -134,17 +134,12 @@ namespace obj {
|
||||
Obj parse(char* memory, size_t memory_size, char* data) {
|
||||
Obj_Arena arena = { memory, memory_size };
|
||||
Obj result = {};
|
||||
int parsing_vertices = 0;
|
||||
int parsing_normals = 0;
|
||||
int parsing_textures = 0;
|
||||
|
||||
for (; ; ) {
|
||||
Token token = next_token(&data);
|
||||
if (token.type == TokenType::end) break;
|
||||
else if (token.type == TokenType::word) {
|
||||
if (equals(token, "v")) {
|
||||
assert(parsing_vertices != 2);
|
||||
parsing_vertices = 1;
|
||||
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
||||
ptr[0] = (float)expect_number(&data);
|
||||
ptr[1] = (float)expect_number(&data);
|
||||
@@ -154,10 +149,6 @@ namespace obj {
|
||||
debug_expect_raw(&data, TokenType::whitespace);
|
||||
}
|
||||
else if (equals(token, "vt")) {
|
||||
assert(parsing_textures != 2);
|
||||
parsing_textures = 1;
|
||||
parsing_vertices = 2;
|
||||
|
||||
float* ptr = (float*)push(&arena, sizeof(float) * 2);
|
||||
ptr[0] = (float)expect_number(&data);
|
||||
ptr[1] = (float)expect_number(&data);
|
||||
@@ -165,10 +156,6 @@ namespace obj {
|
||||
debug_expect_raw(&data, TokenType::whitespace);
|
||||
}
|
||||
else if (equals(token, "vn")) {
|
||||
assert((parsing_textures == 1 || parsing_textures == 2) && parsing_vertices == 2);
|
||||
parsing_textures = 2;
|
||||
parsing_normals = 1;
|
||||
|
||||
float* ptr = (float*)push(&arena, sizeof(float) * 3);
|
||||
ptr[0] = (float)expect_number(&data);
|
||||
ptr[1] = (float)expect_number(&data);
|
||||
@@ -177,7 +164,6 @@ namespace obj {
|
||||
debug_expect_raw(&data, TokenType::whitespace);
|
||||
}
|
||||
else if (equals(token, "f")) {
|
||||
assert(parsing_normals == 1 && parsing_textures == 2 && parsing_vertices == 2);
|
||||
int* ptr = (int*)push(&arena, sizeof(int) * 9);
|
||||
ptr[0] = (int)expect_number(&data);
|
||||
expect_token(&data, '/');
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
/*
|
||||
* The OS layer should expect ABGR writes to the screen, the bitmap is bottom up(origin 0,0 is in left bottom corner)
|
||||
*/
|
||||
#if 0
|
||||
|
||||
#define _CRT_SECURE_NO_WARNINGS
|
||||
#include "platform.h"
|
||||
@@ -207,3 +208,5 @@ char* OS::read_file(const char* path) {
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -1,10 +1,10 @@
|
||||
#include "main.h"
|
||||
|
||||
// #include "main.h"
|
||||
struct Image {
|
||||
U32* pixels;
|
||||
I64 x;
|
||||
I64 y;
|
||||
};
|
||||
#if 0
|
||||
|
||||
struct OSInitArgs {
|
||||
int window_x;
|
||||
@@ -30,3 +30,4 @@ struct OS {
|
||||
OS(OSInitArgs args) { init(args); }
|
||||
char os_internal_data[1024];
|
||||
};
|
||||
#endif
|
||||
332
stb_image.h
332
stb_image.h
@@ -644,7 +644,7 @@ typedef signed int stbi__int32;
|
||||
#include <stdint.h>
|
||||
typedef uint16_t stbi__uint16;
|
||||
typedef int16_t stbi__int16;
|
||||
typedef U32 stbi__uint32;
|
||||
typedef uint32_t stbi__uint32;
|
||||
typedef int32_t stbi__int32;
|
||||
#endif
|
||||
|
||||
@@ -1585,8 +1585,7 @@ static void stbi__refill_buffer(stbi__context* s)
|
||||
s->img_buffer = s->buffer_start;
|
||||
s->img_buffer_end = s->buffer_start+1;
|
||||
*s->img_buffer = 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
s->img_buffer = s->buffer_start;
|
||||
s->img_buffer_end = s->buffer_start + n;
|
||||
}
|
||||
@@ -1664,8 +1663,7 @@ static int stbi__getn(stbi__context* s, stbi_uc* buffer, int n)
|
||||
memcpy(buffer, s->img_buffer, n);
|
||||
s->img_buffer += n;
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
@@ -2214,8 +2212,7 @@ static int stbi__jpeg_decode_block(stbi__jpeg* j, short data[64], stbi__huffman*
|
||||
// decode into unzigzag'd location
|
||||
zig = stbi__jpeg_dezigzag[k++];
|
||||
data[zig] = (short) ((r >> 8) * dequant[zig]);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int rs = stbi__jpeg_huff_decode(j, hac);
|
||||
if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
|
||||
s = rs & 15;
|
||||
@@ -2223,8 +2220,7 @@ static int stbi__jpeg_decode_block(stbi__jpeg* j, short data[64], stbi__huffman*
|
||||
if (s == 0) {
|
||||
if (rs != 0xf0) break; // end block
|
||||
k += 16;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
k += r;
|
||||
// decode into unzigzag'd location
|
||||
zig = stbi__jpeg_dezigzag[k++];
|
||||
@@ -2253,8 +2249,7 @@ static int stbi__jpeg_decode_block_prog_dc(stbi__jpeg* j, short data[64], stbi__
|
||||
dc = j->img_comp[b].dc_pred + diff;
|
||||
j->img_comp[b].dc_pred = dc;
|
||||
data[0] = (short) (dc * (1 << j->succ_low));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// refinement scan for DC coefficient
|
||||
if (stbi__jpeg_get_bit(j))
|
||||
data[0] += (short) (1 << j->succ_low);
|
||||
@@ -2291,8 +2286,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg* j, short data[64], stbi__
|
||||
j->code_bits -= s;
|
||||
zig = stbi__jpeg_dezigzag[k++];
|
||||
data[zig] = (short) ((r >> 8) * (1 << shift));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int rs = stbi__jpeg_huff_decode(j, hac);
|
||||
if (rs < 0) return stbi__err("bad huffman code","Corrupt JPEG");
|
||||
s = rs & 15;
|
||||
@@ -2306,16 +2300,14 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg* j, short data[64], stbi__
|
||||
break;
|
||||
}
|
||||
k += 16;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
k += r;
|
||||
zig = stbi__jpeg_dezigzag[k++];
|
||||
data[zig] = (short) (stbi__extend_receive(j,s) * (1 << shift));
|
||||
}
|
||||
}
|
||||
} while (k <= j->spec_end);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// refinement scan for these AC coefficients
|
||||
|
||||
short bit = (short) (1 << j->succ_low);
|
||||
@@ -2333,8 +2325,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg* j, short data[64], stbi__
|
||||
*p -= bit;
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
k = j->spec_start;
|
||||
do {
|
||||
int r,s;
|
||||
@@ -2348,14 +2339,12 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg* j, short data[64], stbi__
|
||||
if (r)
|
||||
j->eob_run += stbi__jpeg_get_bits(j, r);
|
||||
r = 64; // force end of block
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// r=15 s=0 should write 16 0s, so we just do
|
||||
// a run of 15 0s and then write s (which is 0),
|
||||
// so we don't have to do anything special here
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (s != 1) return stbi__err("bad huffman code", "Corrupt JPEG");
|
||||
// sign bit
|
||||
if (stbi__jpeg_get_bit(j))
|
||||
@@ -2375,8 +2364,7 @@ static int stbi__jpeg_decode_block_prog_ac(stbi__jpeg* j, short data[64], stbi__
|
||||
else
|
||||
*p -= bit;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (r == 0) {
|
||||
*p = (short) s;
|
||||
break;
|
||||
@@ -2459,8 +2447,7 @@ static void stbi__idct_block(stbi_uc* out, int out_stride, short data[64])
|
||||
// 1 && 2|3 && 4|5 && 6|7: -0.047 seconds
|
||||
int dcterm = d[0]*4;
|
||||
v[0] = v[8] = v[16] = v[24] = v[32] = v[40] = v[48] = v[56] = dcterm;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI__IDCT_1D(d[ 0],d[ 8],d[16],d[24],d[32],d[40],d[48],d[56])
|
||||
// constants scaled things up by 1<<12; let's bring them back
|
||||
// down, but keep 2 extra bits of precision
|
||||
@@ -2955,8 +2942,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg* z)
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else { // interleaved
|
||||
} else { // interleaved
|
||||
int i,j,k,x,y;
|
||||
STBI_SIMD_ALIGN(short, data[64]);
|
||||
for (j=0; j < z->img_mcu_y; ++j) {
|
||||
@@ -2987,8 +2973,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg* z)
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (z->scan_n == 1) {
|
||||
int i,j;
|
||||
int n = z->order[0];
|
||||
@@ -3004,8 +2989,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg* z)
|
||||
if (z->spec_start == 0) {
|
||||
if (!stbi__jpeg_decode_block_prog_dc(z, data, &z->huff_dc[z->img_comp[n].hd], n))
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int ha = z->img_comp[n].ha;
|
||||
if (!stbi__jpeg_decode_block_prog_ac(z, data, &z->huff_ac[ha], z->fast_ac[ha]))
|
||||
return 0;
|
||||
@@ -3019,8 +3003,7 @@ static int stbi__parse_entropy_coded_data(stbi__jpeg* z)
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else { // interleaved
|
||||
} else { // interleaved
|
||||
int i,j,k,x,y;
|
||||
for (j=0; j < z->img_mcu_y; ++j) {
|
||||
for (i=0; i < z->img_mcu_x; ++i) {
|
||||
@@ -3123,8 +3106,7 @@ static int stbi__process_marker(stbi__jpeg* z, int m)
|
||||
if (tc == 0) {
|
||||
if (!stbi__build_huffman(z->huff_dc+th, sizes)) return 0;
|
||||
v = z->huff_dc[th].values;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!stbi__build_huffman(z->huff_ac+th, sizes)) return 0;
|
||||
v = z->huff_ac[th].values;
|
||||
}
|
||||
@@ -3158,8 +3140,7 @@ static int stbi__process_marker(stbi__jpeg* z, int m)
|
||||
L -= 5;
|
||||
if (ok)
|
||||
z->jfif = 1;
|
||||
}
|
||||
else if (m == 0xEE && L >= 12) { // Adobe APP14 segment
|
||||
} else if (m == 0xEE && L >= 12) { // Adobe APP14 segment
|
||||
static const unsigned char tag[6] = {'A','d','o','b','e','\0'};
|
||||
int ok = 1;
|
||||
int i;
|
||||
@@ -3213,8 +3194,7 @@ static int stbi__process_scan_header(stbi__jpeg* z)
|
||||
if (z->progressive) {
|
||||
if (z->spec_start > 63 || z->spec_end > 63 || z->spec_start > z->spec_end || z->succ_high > 13 || z->succ_low > 13)
|
||||
return stbi__err("bad SOS", "Corrupt JPEG");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (z->spec_start != 0) return stbi__err("bad SOS","Corrupt JPEG");
|
||||
if (z->succ_high != 0 || z->succ_low != 0) return stbi__err("bad SOS","Corrupt JPEG");
|
||||
z->spec_end = 63;
|
||||
@@ -3397,14 +3377,12 @@ static int stbi__decode_jpeg_image(stbi__jpeg* j)
|
||||
}
|
||||
// if we reach eof without hitting a marker, stbi__get_marker() below will fail and we'll eventually return 0
|
||||
}
|
||||
}
|
||||
else if (stbi__DNL(m)) {
|
||||
} else if (stbi__DNL(m)) {
|
||||
int Ld = stbi__get16be(j->s);
|
||||
stbi__uint32 NL = stbi__get16be(j->s);
|
||||
if (Ld != 4) return stbi__err("bad DNL len", "Corrupt JPEG");
|
||||
if (NL != j->s->img_y) return stbi__err("bad DNL height", "Corrupt JPEG");
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!stbi__process_marker(j, m)) return 0;
|
||||
}
|
||||
m = stbi__get_marker(j);
|
||||
@@ -3918,12 +3896,10 @@ static stbi_uc* load_jpeg_image(stbi__jpeg* z, int* out_x, int* out_y, int* comp
|
||||
out[3] = 255;
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
|
||||
}
|
||||
}
|
||||
else if (z->s->img_n == 4) {
|
||||
} else if (z->s->img_n == 4) {
|
||||
if (z->app14_color_transform == 0) { // CMYK
|
||||
for (i=0; i < z->s->img_x; ++i) {
|
||||
stbi_uc m = coutput[3][i];
|
||||
@@ -3933,8 +3909,7 @@ static stbi_uc* load_jpeg_image(stbi__jpeg* z, int* out_x, int* out_y, int* comp
|
||||
out[3] = 255;
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else if (z->app14_color_transform == 2) { // YCCK
|
||||
} else if (z->app14_color_transform == 2) { // YCCK
|
||||
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
|
||||
for (i=0; i < z->s->img_x; ++i) {
|
||||
stbi_uc m = coutput[3][i];
|
||||
@@ -3943,19 +3918,16 @@ static stbi_uc* load_jpeg_image(stbi__jpeg* z, int* out_x, int* out_y, int* comp
|
||||
out[2] = stbi__blinn_8x8(255 - out[2], m);
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else { // YCbCr + alpha? Ignore the fourth channel for now
|
||||
} else { // YCbCr + alpha? Ignore the fourth channel for now
|
||||
z->YCbCr_to_RGB_kernel(out, y, coutput[1], coutput[2], z->s->img_x, n);
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
for (i=0; i < z->s->img_x; ++i) {
|
||||
out[0] = out[1] = out[2] = y[i];
|
||||
out[3] = 255; // not used if n==3
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (is_rgb) {
|
||||
if (n == 1)
|
||||
for (i=0; i < z->s->img_x; ++i)
|
||||
@@ -3966,8 +3938,7 @@ static stbi_uc* load_jpeg_image(stbi__jpeg* z, int* out_x, int* out_y, int* comp
|
||||
out[1] = 255;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
|
||||
} else if (z->s->img_n == 4 && z->app14_color_transform == 0) {
|
||||
for (i=0; i < z->s->img_x; ++i) {
|
||||
stbi_uc m = coutput[3][i];
|
||||
stbi_uc r = stbi__blinn_8x8(coutput[0][i], m);
|
||||
@@ -3977,15 +3948,13 @@ static stbi_uc* load_jpeg_image(stbi__jpeg* z, int* out_x, int* out_y, int* comp
|
||||
out[1] = 255;
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
|
||||
} else if (z->s->img_n == 4 && z->app14_color_transform == 2) {
|
||||
for (i=0; i < z->s->img_x; ++i) {
|
||||
out[0] = stbi__blinn_8x8(255 - coutput[0][i], coutput[3][i]);
|
||||
out[1] = 255;
|
||||
out += n;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi_uc *y = coutput[0];
|
||||
if (n == 1)
|
||||
for (i=0; i < z->s->img_x; ++i) out[i] = y[i];
|
||||
@@ -4280,8 +4249,7 @@ static int stbi__parse_huffman_block(stbi__zbuf* a)
|
||||
zout = a->zout;
|
||||
}
|
||||
*zout++ = (char) z;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi_uc *p;
|
||||
int len,dist;
|
||||
if (z == 256) {
|
||||
@@ -4304,8 +4272,7 @@ static int stbi__parse_huffman_block(stbi__zbuf* a)
|
||||
if (dist == 1) { // run of one byte; common in images.
|
||||
stbi_uc v = *p;
|
||||
if (len) { do *zout++ = v; while (--len); }
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (len) { do *zout++ = *p++; while (--len); }
|
||||
}
|
||||
}
|
||||
@@ -4344,14 +4311,11 @@ static int stbi__compute_huffman_codes(stbi__zbuf* a)
|
||||
c = stbi__zreceive(a,2)+3;
|
||||
if (n == 0) return stbi__err("bad codelengths", "Corrupt PNG");
|
||||
fill = lencodes[n-1];
|
||||
}
|
||||
else if (c == 17) {
|
||||
} else if (c == 17) {
|
||||
c = stbi__zreceive(a,3)+3;
|
||||
}
|
||||
else if (c == 18) {
|
||||
} else if (c == 18) {
|
||||
c = stbi__zreceive(a,7)+11;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return stbi__err("bad codelengths", "Corrupt PNG");
|
||||
}
|
||||
if (ntot - n < c) return stbi__err("bad codelengths", "Corrupt PNG");
|
||||
@@ -4449,17 +4413,14 @@ static int stbi__parse_zlib(stbi__zbuf* a, int parse_header)
|
||||
type = stbi__zreceive(a,2);
|
||||
if (type == 0) {
|
||||
if (!stbi__parse_uncompressed_block(a)) return 0;
|
||||
}
|
||||
else if (type == 3) {
|
||||
} else if (type == 3) {
|
||||
return 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (type == 1) {
|
||||
// use fixed code lengths
|
||||
if (!stbi__zbuild_huffman(&a->z_length , stbi__zdefault_length , STBI__ZNSYMS)) return 0;
|
||||
if (!stbi__zbuild_huffman(&a->z_distance, stbi__zdefault_distance, 32)) return 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!stbi__compute_huffman_codes(a)) return 0;
|
||||
}
|
||||
if (!stbi__parse_huffman_block(a)) return 0;
|
||||
@@ -4488,8 +4449,7 @@ STBIDEF char* stbi_zlib_decode_malloc_guesssize(const char* buffer, int len, int
|
||||
if (stbi__do_zlib(&a, p, initial_size, 1, 1)) {
|
||||
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
||||
return a.zout_start;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_FREE(a.zout_start);
|
||||
return NULL;
|
||||
}
|
||||
@@ -4510,8 +4470,7 @@ STBIDEF char* stbi_zlib_decode_malloc_guesssize_headerflag(const char* buffer, i
|
||||
if (stbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
|
||||
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
||||
return a.zout_start;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_FREE(a.zout_start);
|
||||
return NULL;
|
||||
}
|
||||
@@ -4538,8 +4497,7 @@ STBIDEF char* stbi_zlib_decode_noheader_malloc(char const* buffer, int len, int*
|
||||
if (stbi__do_zlib(&a, p, 16384, 1, 0)) {
|
||||
if (outlen) *outlen = (int) (a.zout - a.zout_start);
|
||||
return a.zout_start;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_FREE(a.zout_start);
|
||||
return NULL;
|
||||
}
|
||||
@@ -4697,8 +4655,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
raw += img_n;
|
||||
cur += out_n;
|
||||
prior += out_n;
|
||||
}
|
||||
else if (depth == 16) {
|
||||
} else if (depth == 16) {
|
||||
if (img_n != out_n) {
|
||||
cur[filter_bytes] = 255; // first pixel top byte
|
||||
cur[filter_bytes+1] = 255; // first pixel bottom byte
|
||||
@@ -4706,8 +4663,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
raw += filter_bytes;
|
||||
cur += output_bytes;
|
||||
prior += output_bytes;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
raw += 1;
|
||||
cur += 1;
|
||||
prior += 1;
|
||||
@@ -4731,8 +4687,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
}
|
||||
#undef STBI__CASE
|
||||
raw += nk;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_ASSERT(img_n+1 == out_n);
|
||||
#define STBI__CASE(f) \
|
||||
case f: \
|
||||
@@ -4783,8 +4738,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
*cur++ = scale * ((*in ) & 0x0f);
|
||||
}
|
||||
if (k > 0) *cur++ = scale * ((*in >> 4) );
|
||||
}
|
||||
else if (depth == 2) {
|
||||
} else if (depth == 2) {
|
||||
for (k=x*img_n; k >= 4; k-=4, ++in) {
|
||||
*cur++ = scale * ((*in >> 6) );
|
||||
*cur++ = scale * ((*in >> 4) & 0x03);
|
||||
@@ -4794,8 +4748,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
if (k > 0) *cur++ = scale * ((*in >> 6) );
|
||||
if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
|
||||
if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
|
||||
}
|
||||
else if (depth == 1) {
|
||||
} else if (depth == 1) {
|
||||
for (k=x*img_n; k >= 8; k-=8, ++in) {
|
||||
*cur++ = scale * ((*in >> 7) );
|
||||
*cur++ = scale * ((*in >> 6) & 0x01);
|
||||
@@ -4823,8 +4776,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
cur[q*2+1] = 255;
|
||||
cur[q*2+0] = cur[q];
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_ASSERT(img_n == 3);
|
||||
for (q=x-1; q >= 0; --q) {
|
||||
cur[q*4+3] = 255;
|
||||
@@ -4835,8 +4787,7 @@ static int stbi__create_png_image_raw(stbi__png* a, stbi_uc* raw, stbi__uint32 r
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (depth == 16) {
|
||||
} else if (depth == 16) {
|
||||
// force the image data from big-endian to platform-native.
|
||||
// this is done in a separate pass due to the decoding relying
|
||||
// on the data being untouched, but could probably be done
|
||||
@@ -4912,8 +4863,7 @@ static int stbi__compute_transparency(stbi__png* z, stbi_uc tc[3], int out_n)
|
||||
p[1] = (p[0] == tc[0] ? 0 : 255);
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (i=0; i < pixel_count; ++i) {
|
||||
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
|
||||
p[3] = 0;
|
||||
@@ -4938,8 +4888,7 @@ static int stbi__compute_transparency16(stbi__png* z, stbi__uint16 tc[3], int ou
|
||||
p[1] = (p[0] == tc[0] ? 0 : 65535);
|
||||
p += 2;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (i = 0; i < pixel_count; ++i) {
|
||||
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
|
||||
p[3] = 0;
|
||||
@@ -4968,8 +4917,7 @@ static int stbi__expand_png_palette(stbi__png* a, stbi_uc* palette, int len, int
|
||||
p[2] = palette[n+2];
|
||||
p += 3;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (i=0; i < pixel_count; ++i) {
|
||||
int n = orig[i]*4;
|
||||
p[0] = palette[n ];
|
||||
@@ -5040,8 +4988,7 @@ static void stbi__de_iphone(stbi__png* z)
|
||||
p[2] = t;
|
||||
p += 3;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
STBI_ASSERT(s->img_out_n == 4);
|
||||
if (stbi__unpremultiply_on_load) {
|
||||
// convert bgr to rgb and unpremultiply
|
||||
@@ -5053,15 +5000,13 @@ static void stbi__de_iphone(stbi__png* z)
|
||||
p[0] = (p[2] * 255 + half) / a;
|
||||
p[1] = (p[1] * 255 + half) / a;
|
||||
p[2] = ( t * 255 + half) / a;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
p[0] = p[2];
|
||||
p[2] = t;
|
||||
}
|
||||
p += 4;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// convert bgr to rgb
|
||||
for (i=0; i < pixel_count; ++i) {
|
||||
stbi_uc t = p[0];
|
||||
@@ -5120,8 +5065,7 @@ static int stbi__parse_png_file(stbi__png* z, int scan, int req_comp)
|
||||
s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
|
||||
if ((1 << 30) / s->img_x / s->img_n < s->img_y) return stbi__err("too large", "Image too large to decode");
|
||||
if (scan == STBI__SCAN_header) return 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// if paletted, then pal_n is our final components, and
|
||||
// img_n is # components to decompress/filter.
|
||||
s->img_n = 1;
|
||||
@@ -5155,15 +5099,13 @@ static int stbi__parse_png_file(stbi__png* z, int scan, int req_comp)
|
||||
pal_img_n = 4;
|
||||
for (i=0; i < c.length; ++i)
|
||||
palette[i*4+3] = stbi__get8(s);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!(s->img_n & 1)) return stbi__err("tRNS with alpha","Corrupt PNG");
|
||||
if (c.length != (stbi__uint32) s->img_n*2) return stbi__err("bad tRNS len","Corrupt PNG");
|
||||
has_trans = 1;
|
||||
if (z->depth == 16) {
|
||||
for (k = 0; k < s->img_n; ++k) tc16[k] = (stbi__uint16)stbi__get16be(s); // copy the values as-is
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (k = 0; k < s->img_n; ++k) tc[k] = (stbi_uc)(stbi__get16be(s) & 255) * stbi__depth_scale_table[z->depth]; // non 8-bit images will be larger
|
||||
}
|
||||
}
|
||||
@@ -5209,8 +5151,7 @@ static int stbi__parse_png_file(stbi__png* z, int scan, int req_comp)
|
||||
if (has_trans) {
|
||||
if (z->depth == 16) {
|
||||
if (!stbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (!stbi__compute_transparency(z, tc, s->img_out_n)) return 0;
|
||||
}
|
||||
}
|
||||
@@ -5223,8 +5164,7 @@ static int stbi__parse_png_file(stbi__png* z, int scan, int req_comp)
|
||||
if (req_comp >= 3) s->img_out_n = req_comp;
|
||||
if (!stbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
|
||||
return 0;
|
||||
}
|
||||
else if (has_trans) {
|
||||
} else if (has_trans) {
|
||||
// non-paletted image with tRNS -> source image has (constant) alpha
|
||||
++s->img_n;
|
||||
}
|
||||
@@ -5426,15 +5366,13 @@ static int stbi__bmp_set_mask_defaults(stbi__bmp_data* info, int compress)
|
||||
info->mr = 31u << 10;
|
||||
info->mg = 31u << 5;
|
||||
info->mb = 31u << 0;
|
||||
}
|
||||
else if (info->bpp == 32) {
|
||||
} else if (info->bpp == 32) {
|
||||
info->mr = 0xffu << 16;
|
||||
info->mg = 0xffu << 8;
|
||||
info->mb = 0xffu << 0;
|
||||
info->ma = 0xffu << 24;
|
||||
info->all_a = 0; // if all_a is 0 at end, then we loaded alpha channel but it was all 0
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// otherwise, use defaults, which is all-0
|
||||
info->mr = info->mg = info->mb = info->ma = 0;
|
||||
}
|
||||
@@ -5461,8 +5399,7 @@ static void* stbi__bmp_parse_header(stbi__context* s, stbi__bmp_data* info)
|
||||
if (hsz == 12) {
|
||||
s->img_x = stbi__get16le(s);
|
||||
s->img_y = stbi__get16le(s);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
s->img_x = stbi__get32le(s);
|
||||
s->img_y = stbi__get32le(s);
|
||||
}
|
||||
@@ -5488,8 +5425,7 @@ static void* stbi__bmp_parse_header(stbi__context* s, stbi__bmp_data* info)
|
||||
if (info->bpp == 16 || info->bpp == 32) {
|
||||
if (compress == 0) {
|
||||
stbi__bmp_set_mask_defaults(info, compress);
|
||||
}
|
||||
else if (compress == 3) {
|
||||
} else if (compress == 3) {
|
||||
info->mr = stbi__get32le(s);
|
||||
info->mg = stbi__get32le(s);
|
||||
info->mb = stbi__get32le(s);
|
||||
@@ -5499,12 +5435,10 @@ static void* stbi__bmp_parse_header(stbi__context* s, stbi__bmp_data* info)
|
||||
// ?!?!?
|
||||
return stbi__errpuc("bad BMP", "bad BMP");
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
return stbi__errpuc("bad BMP", "bad BMP");
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// V4/V5 header
|
||||
int i;
|
||||
if (hsz != 108 && hsz != 124)
|
||||
@@ -5559,8 +5493,7 @@ static void* stbi__bmp_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
if (info.hsz == 12) {
|
||||
if (info.bpp < 24)
|
||||
psize = (info.offset - info.extra_read - 24) / 3;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (info.bpp < 16)
|
||||
psize = (info.offset - info.extra_read - info.hsz) >> 2;
|
||||
}
|
||||
@@ -5618,8 +5551,7 @@ static void* stbi__bmp_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
}
|
||||
stbi__skip(s, pad);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (j=0; j < (int) s->img_y; ++j) {
|
||||
for (i=0; i < (int) s->img_x; i += 2) {
|
||||
int v=stbi__get8(s),v2=0;
|
||||
@@ -5641,8 +5573,7 @@ static void* stbi__bmp_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
stbi__skip(s, pad);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int rshift=0,gshift=0,bshift=0,ashift=0,rcount=0,gcount=0,bcount=0,acount=0;
|
||||
int z = 0;
|
||||
int easy=0;
|
||||
@@ -5653,8 +5584,7 @@ static void* stbi__bmp_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
pad = (-width) & 3;
|
||||
if (info.bpp == 24) {
|
||||
easy = 1;
|
||||
}
|
||||
else if (info.bpp == 32) {
|
||||
} else if (info.bpp == 32) {
|
||||
if (mb == 0xff && mg == 0xff00 && mr == 0x00ff0000 && ma == 0xff000000)
|
||||
easy = 2;
|
||||
}
|
||||
@@ -5679,8 +5609,7 @@ static void* stbi__bmp_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
all_a |= a;
|
||||
if (target == 4) out[z++] = a;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
int bpp = info.bpp;
|
||||
for (i=0; i < (int) s->img_x; ++i) {
|
||||
stbi__uint32 v = (bpp == 16 ? (stbi__uint32) stbi__get16le(s) : stbi__get32le(s));
|
||||
@@ -5769,8 +5698,7 @@ static int stbi__tga_info(stbi__context* s, int* x, int* y, int* comp)
|
||||
}
|
||||
stbi__skip(s,4); // skip image x and y origin
|
||||
tga_colormap_bpp = sz;
|
||||
}
|
||||
else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
|
||||
} else { // "normal" image w/o colormap - only RGB or grey allowed, +/- RLE
|
||||
if ( (tga_image_type != 2) && (tga_image_type != 3) && (tga_image_type != 10) && (tga_image_type != 11) ) {
|
||||
stbi__rewind(s);
|
||||
return 0; // only RGB or grey allowed, +/- RLE
|
||||
@@ -5798,8 +5726,7 @@ static int stbi__tga_info(stbi__context* s, int* x, int* y, int* comp)
|
||||
return 0;
|
||||
}
|
||||
tga_comp = stbi__tga_get_comp(tga_colormap_bpp, 0, NULL);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
tga_comp = stbi__tga_get_comp(tga_bits_per_pixel, (tga_image_type == 3) || (tga_image_type == 11), NULL);
|
||||
}
|
||||
if(!tga_comp) {
|
||||
@@ -5826,8 +5753,7 @@ static int stbi__tga_test(stbi__context* s)
|
||||
sz = stbi__get8(s); // check bits per palette color entry
|
||||
if ( (sz != 8) && (sz != 15) && (sz != 16) && (sz != 24) && (sz != 32) ) goto errorEnd;
|
||||
stbi__skip(s,4); // skip image x and y origin
|
||||
}
|
||||
else { // "normal" image w/o colormap
|
||||
} else { // "normal" image w/o colormap
|
||||
if ( (sz != 2) && (sz != 3) && (sz != 10) && (sz != 11) ) goto errorEnd; // only RGB or grey allowed, +/- RLE
|
||||
stbi__skip(s,9); // skip colormap specification and image x/y origin
|
||||
}
|
||||
@@ -5932,8 +5858,7 @@ static void* stbi__tga_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
stbi_uc *tga_row = tga_data + row*tga_width*tga_comp;
|
||||
stbi__getn(s, tga_row, tga_width * tga_comp);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// do I need to load a palette?
|
||||
if ( tga_indexed)
|
||||
{
|
||||
@@ -5957,8 +5882,7 @@ static void* stbi__tga_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
stbi__tga_read_rgb16(s, pal_entry);
|
||||
pal_entry += tga_comp;
|
||||
}
|
||||
}
|
||||
else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
|
||||
} else if (!stbi__getn(s, tga_palette, tga_palette_len * tga_comp)) {
|
||||
STBI_FREE(tga_data);
|
||||
STBI_FREE(tga_palette);
|
||||
return stbi__errpuc("bad palette", "Corrupt TGA");
|
||||
@@ -5977,13 +5901,11 @@ static void* stbi__tga_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
RLE_count = 1 + (RLE_cmd & 127);
|
||||
RLE_repeating = RLE_cmd >> 7;
|
||||
read_next_pixel = 1;
|
||||
}
|
||||
else if (!RLE_repeating)
|
||||
} else if ( !RLE_repeating )
|
||||
{
|
||||
read_next_pixel = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
} else
|
||||
{
|
||||
read_next_pixel = 1;
|
||||
}
|
||||
@@ -6003,12 +5925,10 @@ static void* stbi__tga_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
for (j = 0; j < tga_comp; ++j) {
|
||||
raw_data[j] = tga_palette[pal_idx+j];
|
||||
}
|
||||
}
|
||||
else if (tga_rgb16) {
|
||||
} else if(tga_rgb16) {
|
||||
STBI_ASSERT(tga_comp == STBI_rgb);
|
||||
stbi__tga_read_rgb16(s, raw_data);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// read in the data raw
|
||||
for (j = 0; j < tga_comp; ++j) {
|
||||
raw_data[j] = stbi__get8(s);
|
||||
@@ -6096,8 +6016,7 @@ static int stbi__psd_decode_rle(stbi__context* s, stbi_uc* p, int pixelCount)
|
||||
len = stbi__get8(s);
|
||||
if (len == 128) {
|
||||
// No-op.
|
||||
}
|
||||
else if (len < 128) {
|
||||
} else if (len < 128) {
|
||||
// Copy next len+1 bytes literally.
|
||||
len++;
|
||||
if (len > nleft) return 0; // corrupt data
|
||||
@@ -6107,8 +6026,7 @@ static int stbi__psd_decode_rle(stbi__context* s, stbi_uc* p, int pixelCount)
|
||||
p += 4;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
else if (len > 128) {
|
||||
} else if (len > 128) {
|
||||
stbi_uc val;
|
||||
// Next -len+1 bytes in the dest are replicated from next source byte.
|
||||
// (Interpret len as a negative 8-bit int.)
|
||||
@@ -6204,8 +6122,7 @@ static void* stbi__psd_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
if (!compression && bitdepth == 16 && bpc == 16) {
|
||||
out = (stbi_uc *) stbi__malloc_mad3(8, w, h, 0);
|
||||
ri->bits_per_channel = 16;
|
||||
}
|
||||
else
|
||||
} else
|
||||
out = (stbi_uc *) stbi__malloc(4 * w*h);
|
||||
|
||||
if (!out) return stbi__errpuc("outofmem", "Out of memory");
|
||||
@@ -6237,8 +6154,7 @@ static void* stbi__psd_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
// Fill this channel with default data.
|
||||
for (i = 0; i < pixelCount; i++, p += 4)
|
||||
*p = (channel == 3 ? 255 : 0);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Read the RLE data.
|
||||
if (!stbi__psd_decode_rle(s, p, pixelCount)) {
|
||||
STBI_FREE(out);
|
||||
@@ -6247,8 +6163,7 @@ static void* stbi__psd_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// We're at the raw image data. It's each channel in order (Red, Green, Blue, Alpha, ...)
|
||||
// where each channel consists of an 8-bit (or 16-bit) value for each pixel in the image.
|
||||
|
||||
@@ -6261,27 +6176,23 @@ static void* stbi__psd_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
stbi__uint16 val = channel == 3 ? 65535 : 0;
|
||||
for (i = 0; i < pixelCount; i++, q += 4)
|
||||
*q = val;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi_uc *p = out+channel;
|
||||
stbi_uc val = channel == 3 ? 255 : 0;
|
||||
for (i = 0; i < pixelCount; i++, p += 4)
|
||||
*p = val;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
if (ri->bits_per_channel == 16) { // output bpc
|
||||
stbi__uint16 *q = ((stbi__uint16 *) out) + channel;
|
||||
for (i = 0; i < pixelCount; i++, q += 4)
|
||||
*q = (stbi__uint16) stbi__get16be(s);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi_uc *p = out+channel;
|
||||
if (bitdepth == 16) { // input bpc
|
||||
for (i = 0; i < pixelCount; i++, p += 4)
|
||||
*p = (stbi_uc) (stbi__get16be(s) >> 8);
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (i = 0; i < pixelCount; i++, p += 4)
|
||||
*p = stbi__get8(s);
|
||||
}
|
||||
@@ -6304,8 +6215,7 @@ static void* stbi__psd_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
pixel[2] = (stbi__uint16) (pixel[2]*ra + inv_a);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
for (i=0; i < w*h; ++i) {
|
||||
unsigned char *pixel = out + 4*i;
|
||||
if (pixel[3] != 0 && pixel[3] != 255) {
|
||||
@@ -6490,8 +6400,7 @@ static stbi_uc* stbi__pic_load_core(stbi__context* s, int width, int height, int
|
||||
|
||||
for(i=0;i<count;++i, dest += 4)
|
||||
stbi__copyval(packet->channel,dest,value);
|
||||
}
|
||||
else { // Raw
|
||||
} else { // Raw
|
||||
++count;
|
||||
if (count>left) return stbi__errpuc("bad file","scanline overrun");
|
||||
|
||||
@@ -6738,8 +6647,7 @@ static stbi_uc* stbi__process_gif_raster(stbi__context* s, stbi__gif* g)
|
||||
--len;
|
||||
bits |= (stbi__int32) stbi__get8(s) << valid_bits;
|
||||
valid_bits += 8;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi__int32 code = bits & codemask;
|
||||
bits >>= codesize;
|
||||
valid_bits -= codesize;
|
||||
@@ -6750,14 +6658,12 @@ static stbi_uc* stbi__process_gif_raster(stbi__context* s, stbi__gif* g)
|
||||
avail = clear + 2;
|
||||
oldcode = -1;
|
||||
first = 0;
|
||||
}
|
||||
else if (code == clear + 1) { // end of stream code
|
||||
} else if (code == clear + 1) { // end of stream code
|
||||
stbi__skip(s, len);
|
||||
while ((len = stbi__get8(s)) > 0)
|
||||
stbi__skip(s,len);
|
||||
return g->out;
|
||||
}
|
||||
else if (code <= avail) {
|
||||
} else if (code <= avail) {
|
||||
if (first) {
|
||||
return stbi__errpuc("no clear code", "Corrupt GIF");
|
||||
}
|
||||
@@ -6771,8 +6677,7 @@ static stbi_uc* stbi__process_gif_raster(stbi__context* s, stbi__gif* g)
|
||||
p->prefix = (stbi__int16) oldcode;
|
||||
p->first = g->codes[oldcode].first;
|
||||
p->suffix = (code == avail) ? p->first : g->codes[code].first;
|
||||
}
|
||||
else if (code == avail)
|
||||
} else if (code == avail)
|
||||
return stbi__errpuc("illegal code in raster", "Corrupt GIF");
|
||||
|
||||
stbi__out_gif_code(g, (stbi__uint16) code);
|
||||
@@ -6783,8 +6688,7 @@ static stbi_uc* stbi__process_gif_raster(stbi__context* s, stbi__gif* g)
|
||||
}
|
||||
|
||||
oldcode = code;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return stbi__errpuc("illegal code in raster", "Corrupt GIF");
|
||||
}
|
||||
}
|
||||
@@ -6821,8 +6725,7 @@ static stbi_uc* stbi__gif_load_next(stbi__context* s, stbi__gif* g, int* comp, i
|
||||
memset(g->background, 0x00, 4 * pcount); // state of the background (starts transparent)
|
||||
memset(g->history, 0x00, pcount); // pixels that were affected previous frame
|
||||
first_frame = 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// second frame - how do we dispose of the previous one?
|
||||
dispose = (g->eflags & 0x1C) >> 2;
|
||||
pcount = g->w * g->h;
|
||||
@@ -6837,16 +6740,14 @@ static stbi_uc* stbi__gif_load_next(stbi__context* s, stbi__gif* g, int* comp, i
|
||||
memcpy( &g->out[pi * 4], &two_back[pi * 4], 4 );
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (dispose == 2) {
|
||||
} else if (dispose == 2) {
|
||||
// restore what was changed last frame to background before that frame;
|
||||
for (pi = 0; pi < pcount; ++pi) {
|
||||
if (g->history[pi]) {
|
||||
memcpy( &g->out[pi * 4], &g->background[pi * 4], 4 );
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// This is a non-disposal case eithe way, so just
|
||||
// leave the pixels as is, and they will become the new background
|
||||
// 1: do not dispose
|
||||
@@ -6895,8 +6796,7 @@ static stbi_uc* stbi__gif_load_next(stbi__context* s, stbi__gif* g, int* comp, i
|
||||
if (g->lflags & 0x40) {
|
||||
g->step = 8 * g->line_size; // first interlaced spacing
|
||||
g->parse = 3;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
g->step = g->line_size;
|
||||
g->parse = 0;
|
||||
}
|
||||
@@ -6904,11 +6804,9 @@ static stbi_uc* stbi__gif_load_next(stbi__context* s, stbi__gif* g, int* comp, i
|
||||
if (g->lflags & 0x80) {
|
||||
stbi__gif_parse_colortable(s,g->lpal, 2 << (g->lflags & 7), g->eflags & 0x01 ? g->transparent : -1);
|
||||
g->color_table = (stbi_uc *) g->lpal;
|
||||
}
|
||||
else if (g->flags & 0x80) {
|
||||
} else if (g->flags & 0x80) {
|
||||
g->color_table = (stbi_uc *) g->pal;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return stbi__errpuc("missing color table", "Corrupt GIF");
|
||||
|
||||
o = stbi__process_gif_raster(s, g);
|
||||
@@ -6948,14 +6846,12 @@ static stbi_uc* stbi__gif_load_next(stbi__context* s, stbi__gif* g, int* comp, i
|
||||
if (g->transparent >= 0) {
|
||||
g->pal[g->transparent][3] = 0;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// don't need transparent
|
||||
stbi__skip(s, 1);
|
||||
g->transparent = -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
stbi__skip(s, len);
|
||||
break;
|
||||
}
|
||||
@@ -7032,8 +6928,7 @@ static void* stbi__load_gif_main(stbi__context* s, int** delays, int* x, int* y,
|
||||
*delays = new_delays;
|
||||
delays_size = layers * sizeof(int);
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
out = (stbi_uc*)stbi__malloc( layers * stride );
|
||||
if (!out)
|
||||
return stbi__load_gif_main_outofmem(&g, out, delays);
|
||||
@@ -7067,8 +6962,7 @@ static void* stbi__load_gif_main(stbi__context* s, int** delays, int* x, int* y,
|
||||
|
||||
*z = layers;
|
||||
return out;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
return stbi__errpuc("not GIF", "Image was not as a gif type.");
|
||||
}
|
||||
}
|
||||
@@ -7090,8 +6984,7 @@ static void* stbi__gif_load(stbi__context* s, int* x, int* y, int* comp, int req
|
||||
// can be done for multiple frames.
|
||||
if (req_comp && req_comp != 4)
|
||||
u = stbi__convert_format(u, 4, req_comp, g.w, g.h);
|
||||
}
|
||||
else if (g.out) {
|
||||
} else if (g.out) {
|
||||
// if there was an error and we allocated an image buffer, free it!
|
||||
STBI_FREE(g.out);
|
||||
}
|
||||
@@ -7172,8 +7065,7 @@ static void stbi__hdr_convert(float* output, stbi_uc* input, int req_comp)
|
||||
}
|
||||
if (req_comp == 2) output[1] = 1;
|
||||
if (req_comp == 4) output[3] = 1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
switch (req_comp) {
|
||||
case 4: output[3] = 1; /* fallthrough */
|
||||
case 3: output[0] = output[1] = output[2] = 0;
|
||||
@@ -7253,8 +7145,7 @@ static float* stbi__hdr_load(stbi__context* s, int* x, int* y, int* comp, int re
|
||||
stbi__hdr_convert(hdr_data + j * width * req_comp + i * req_comp, rgbe, req_comp);
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Read RLE-encoded data
|
||||
scanline = NULL;
|
||||
|
||||
@@ -7299,8 +7190,7 @@ static float* stbi__hdr_load(stbi__context* s, int* x, int* y, int* comp, int re
|
||||
if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
|
||||
for (z = 0; z < count; ++z)
|
||||
scanline[i++ * 4 + k] = value;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
// Dump
|
||||
if (count > nleft) { STBI_FREE(hdr_data); STBI_FREE(scanline); return stbi__errpf("corrupt", "bad RLE data in HDR"); }
|
||||
for (z = 0; z < count; ++z)
|
||||
|
||||
Reference in New Issue
Block a user