1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
|
/**
* libdmtx - Data Matrix Encoding/Decoding Library
* Copyright 2007, 2008, 2009 Mike Laughton. All rights reserved.
*
* See LICENSE file in the main project directory for full
* terms of use and distribution.
*
* Contact: Mike Laughton <mike@dragonflylogic.com>
*
* \file image.c
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
#include <png.h>
#include "dmtx.h"
#include "rotate_test.h"
#include "image.h"
/**
*
*
*/
unsigned char *
loadTextureImage(int *width, int *height)
{
unsigned char *pxl;
int error;
char filepath[128];
strcpy(filepath, "images/");
strcat(filepath, gFilename[gFileIdx]);
fprintf(stdout, "Opening %s\n", filepath);
pxl = loadPng(filepath, width, height);
assert(pxl != NULL);
gFileIdx++;
if(gFileIdx == gFileCount)
gFileIdx = 0;
/* Set up texture */
glGenTextures(1, &barcodeTexture);
glBindTexture(GL_TEXTURE_2D, barcodeTexture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
/* Read barcode image */
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, *width, *height, GL_RGB, GL_UNSIGNED_BYTE, pxl);
/* Create the barcode list */
barcodeList = glGenLists(1);
glNewList(barcodeList, GL_COMPILE);
DrawBarCode();
glEndList();
return pxl;
}
/**
*
*
*/
unsigned char *
loadPng(char *filename, int *width, int *height)
{
png_byte pngHeader[8];
FILE *fp;
int headerTestSize = sizeof(pngHeader);
int isPng;
int bitDepth, color_type, interlace_type, compression_type, filter_method;
int row;
png_uint_32 png_width, png_height;
png_structp png_ptr;
png_infop info_ptr;
png_infop end_info;
png_bytepp row_pointers;
unsigned char *pxl = NULL;
fp = fopen(filename, "rb");
if(!fp)
return NULL;
fread(pngHeader, 1, headerTestSize, fp);
isPng = !png_sig_cmp(pngHeader, 0, headerTestSize);
if(!isPng)
return NULL;
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
return NULL;
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) {
png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
return NULL;
}
end_info = png_create_info_struct(png_ptr);
if(!end_info) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
return NULL;
}
if(setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return NULL;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, headerTestSize);
png_read_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &png_width, &png_height, &bitDepth,
&color_type, &interlace_type, &compression_type, &filter_method);
png_set_strip_16(png_ptr);
png_set_strip_alpha(png_ptr);
png_set_packswap(png_ptr);
if(color_type == PNG_COLOR_TYPE_PALETTE)
png_set_palette_to_rgb(png_ptr);
if (color_type == PNG_COLOR_TYPE_GRAY || PNG_COLOR_TYPE_GRAY_ALPHA)
png_set_gray_to_rgb(png_ptr);
png_read_update_info(png_ptr, info_ptr);
png_get_IHDR(png_ptr, info_ptr, &png_width, &png_height, &bitDepth,
&color_type, &interlace_type, &compression_type, &filter_method);
*width = (int)png_width;
*height = (int)png_height;
row_pointers = (png_bytepp)png_malloc(png_ptr, sizeof(png_bytep) * png_height);
if(row_pointers == NULL) {
fprintf(stdout, "Fatal error!\n"); fflush(stdout); /* XXX finish later */
; /* FatalError(1, "Error while during malloc for row_pointers"); */
}
for(row = 0; row < *height; row++) {
row_pointers[row] = (png_bytep)png_malloc(png_ptr,
png_get_rowbytes(png_ptr, info_ptr));
}
png_read_image(png_ptr, row_pointers);
png_read_end(png_ptr, info_ptr);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
pxl = (unsigned char *)malloc((*width) * (*height) * 3);
assert(pxl != NULL);
for(row = 0; row < *height; row++) {
memcpy(pxl + (row * (*width) * 3), row_pointers[(*height) - row - 1], (*width) * 3);
}
for(row = 0; row < (*height); row++) {
png_free(png_ptr, row_pointers[row]);
}
png_free(png_ptr, row_pointers);
fclose(fp);
return pxl;
}
/**
*
*
*/
void plotPoint(DmtxImage *img, float rowFloat, float colFloat, int targetColor)
{
int i, row, col;
float xFloat, yFloat;
int offset[4];
int color[4];
row = (int)rowFloat;
col = (int)colFloat;
xFloat = colFloat - col;
yFloat = rowFloat - row;
offset[0] = row * img->width + col;
offset[1] = row * img->width + (col + 1);
offset[2] = (row + 1) * img->width + col;
offset[3] = (row + 1) * img->width + (col + 1);
color[0] = clampRGB(255.0 * ((1.0 - xFloat) * (1.0 - yFloat)));
color[1] = clampRGB(255.0 * (xFloat * (1.0 - yFloat)));
color[2] = clampRGB(255.0 * ((1.0 - xFloat) * yFloat));
color[3] = clampRGB(255.0 * (xFloat * yFloat));
for(i = 0; i < 4; i++) {
if((i == 1 || i== 3) && col + 1 > 319)
continue;
else if((i == 2 || i== 3) && row + 1 > 319)
continue;
if(targetColor & (ColorWhite | ColorRed | ColorYellow))
img->pxl[offset[i]*3+0] = max(img->pxl[offset[i]*3+0], color[i]);
if(targetColor & (ColorWhite | ColorGreen | ColorYellow))
img->pxl[offset[i]*3+1] = max(img->pxl[offset[i]*3+1], color[i]);
if(targetColor & (ColorWhite | ColorBlue))
img->pxl[offset[i]*3+2] = max(img->pxl[offset[i]*3+2], color[i]);
}
}
/**
*
*
*/
int clampRGB(float color)
{
if(color < 0.0)
return 0;
else if(color > 255.0)
return 255;
else
return (int)(color + 0.5);
}
|