"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "layer0/MyPNG.cpp" between
pymol-v1.8.6.0.tar.bz2 and pymol-v2.1.0.tar.bz2

About: PyMOL is a Python-enhanced molecular graphics tool. It excels at 3D visualization of proteins, small molecules, density, surfaces, and trajectories. It also includes molecular editing, ray tracing, and movies. Open Source version.

MyPNG.cpp  (pymol-v1.8.6.0.tar.bz2):MyPNG.cpp  (pymol-v2.1.0.tar.bz2)
skipping to change at line 54 skipping to change at line 54
#endif #endif
#endif #endif
#include"os_std.h" #include"os_std.h"
#include"Base.h" #include"Base.h"
#include "MyPNG.h" #include "MyPNG.h"
#include"MemoryDebug.h" #include"MemoryDebug.h"
#include "Setting.h" #include "Setting.h"
#include "Err.h"
#include "File.h"
/*
* base64 decoding
* http://stackoverflow.com/questions/342409/how-do-i-base64-encode-decode-in-c
*/
static unsigned char base64_decoding_table[] = {
62, 0, 0, 0, 63, 52, 53, 54,
55, 56, 57, 58, 59, 60, 61, 0,
0, 0, 0, 0, 0, 0, 0, 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,
0, 0, 0, 0, 0, 0, 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};
static unsigned char *base64_decode(const char *data,
size_t input_length=0) {
unsigned char *decoded_data = NULL;
unsigned int triple;
unsigned int i = 0, j = 0, k;
char c;
if (input_length < 1)
input_length = strlen(data);
ok_assert(1, decoded_data = (unsigned char*) mmalloc(input_length / 4 * 3));
while (i < input_length) {
triple = 0;
for (k = 4; k && i < input_length;) {
c = data[i++];
if (c < 43 || c > 122)
continue;
triple += base64_decoding_table[c - 43] << (--k) * 6;
}
ok_assert(1, k == 0);
for (k = 3; k;)
decoded_data[j++] = (triple >> (--k) * 8) & 0xFF;
}
return decoded_data;
ok_except1:
mfree(decoded_data);
return NULL;
}
typedef struct {
unsigned char *c; // moving pointer
unsigned char *h; // for freeing
} uchar2p;
/*
* Use with png_set_read_fn, for reading a PNG file from memory.
* This simply copies from the memory pointer to outBytes while
* incrementing the memory pointer.
*/
#ifdef _PYMOL_LIBPNG
void read_data_from_buffer(png_structp png_ptr,
png_bytep outBytes,
png_size_t byteCountToRead) {
uchar2p *io_ptr = (uchar2p*) png_get_io_ptr(png_ptr);
if(!io_ptr)
return;
while(byteCountToRead--) {
*(outBytes++) = *(io_ptr->c++);
}
}
/*
* Use with png_set_write_fn, for writing a PNG file to memory.
*/
static void write_data_to_buffer(png_structp png_ptr,
png_bytep data,
png_size_t length) {
#if 0
auto io_ptr = (std::vector<unsigned char>*) png_get_io_ptr(png_ptr);
io_ptr->insert(io_ptr->end(), data, data + length);
#endif
}
#endif
int MyPNGWrite(PyMOLGlobals * G, const char *file_name, const unsigned char *dat a_ptr, int MyPNGWrite(PyMOLGlobals * G, const char *file_name, const unsigned char *dat a_ptr,
unsigned int width, unsigned int height, float dpi, int format, i unsigned int width, unsigned int height, float dpi, int format, i
nt quiet) nt quiet,
void * io_ptr)
{ {
switch (format) { switch (format) {
case cMyPNG_FormatPNG: case cMyPNG_FormatPNG:
{ {
#ifdef _PYMOL_LIBPNG #ifdef _PYMOL_LIBPNG
int ok = true; int ok = true;
FILE *fp = NULL; FILE *fp = NULL;
png_structp png_ptr; png_structp png_ptr;
png_infop info_ptr; png_infop info_ptr;
int bit_depth = 8; int bit_depth = 8;
int bytes_per_pixel = 4; int bytes_per_pixel = 4;
png_uint_32 k; png_uint_32 k;
png_byte *image = (png_byte *) data_ptr; png_byte *image = (png_byte *) data_ptr;
png_bytep *row_pointers; png_bytep *row_pointers;
int fd = 0; int fd = 0;
row_pointers = Alloc(png_bytep, height); row_pointers = Alloc(png_bytep, height);
/* open the file, allowing use of an encoded file descriptor, with /* open the file, allowing use of an encoded file descriptor, with
approach adapted from TJO: chr(1) followed by ascii-format integer */ approach adapted from TJO: chr(1) followed by ascii-format integer */
if(file_name[0] == 1) { if (!io_ptr) {
if(sscanf(file_name + 1, "%d", &fd) == 1) { if(file_name[0] == 1) {
fp = fdopen(fd, "wb"); if(sscanf(file_name + 1, "%d", &fd) == 1) {
fp = fdopen(fd, "wb");
}
} else {
fp = pymol_fopen(file_name, "wb");
}
if(fp == NULL) {
ok = false;
goto cleanup;
} else if(feof(fp)) {
ok = false;
goto cleanup;
} }
} else {
fp = fopen(file_name, "wb");
}
if(fp == NULL) {
ok = false;
goto cleanup;
} else if(feof(fp)) {
ok = false;
goto cleanup;
} }
/* Create and initialize the png_struct with the desired error handler /* Create and initialize the png_struct with the desired error handler
* functions. If you want to use the default stderr and longjump method, * functions. If you want to use the default stderr and longjump method,
* you can supply NULL for the last three parameters. We also check that * you can supply NULL for the last three parameters. We also check that
* the library version is compatible with the one used at compile time, * the library version is compatible with the one used at compile time,
* in case we are using dynamically linked libraries. REQUIRED. * in case we are using dynamically linked libraries. REQUIRED.
*/ */
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL) ; png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL) ;
if(png_ptr == NULL) { if(png_ptr == NULL) {
skipping to change at line 122 skipping to change at line 217
/* Set error handling. REQUIRED if you aren't supplying your own /* Set error handling. REQUIRED if you aren't supplying your own
* error handling functions in the png_create_write_struct() call. * error handling functions in the png_create_write_struct() call.
*/ */
if(setjmp(png_jmpbuf(png_ptr))) { if(setjmp(png_jmpbuf(png_ptr))) {
/* If we get here, we had a problem reading the file */ /* If we get here, we had a problem reading the file */
png_destroy_write_struct(&png_ptr, (png_infopp) NULL); png_destroy_write_struct(&png_ptr, (png_infopp) NULL);
ok = false; ok = false;
goto cleanup; goto cleanup;
} }
/* set up the output control if you are using standard C streams */ if (io_ptr) {
png_init_io(png_ptr, fp); png_set_write_fn(png_ptr, (void*) io_ptr, write_data_to_buffer, NULL);
} else {
/* set up the output control if you are using standard C streams */
png_init_io(png_ptr, fp);
}
/* Set the image information here. Width and height are up to 2^31, /* Set the image information here. Width and height are up to 2^31,
* bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
* the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
* PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
* or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
* PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
* currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIR ED * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIR ED
*/ */
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_R GB_ALPHA, png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_R GB_ALPHA,
skipping to change at line 202 skipping to change at line 301
return ok; return ok;
#else #else
return 0; return 0;
#endif #endif
} }
break; break;
case cMyPNG_FormatPPM: case cMyPNG_FormatPPM:
{ {
FILE *fil = fopen(file_name, "wb"); FILE *fil = pymol_fopen(file_name, "wb");
unsigned char *buffer = Alloc(unsigned char, 3 * width * height); unsigned char *buffer = Alloc(unsigned char, 3 * width * height);
if(fil && buffer) { if(fil && buffer) {
fprintf(fil, "P6\n"); fprintf(fil, "P6\n");
fprintf(fil, "%d %d\n", width, height); fprintf(fil, "%d %d\n", width, height);
fprintf(fil, "255\n"); fprintf(fil, "255\n");
{ {
unsigned int a, b; unsigned int a, b;
unsigned char *q = buffer; unsigned char *q = buffer;
const unsigned char *p; const unsigned char *p;
skipping to change at line 262 skipping to change at line 361
png_uint_32 width; png_uint_32 width;
png_uint_32 height; png_uint_32 height;
int bit_depth; int bit_depth;
int color_type; int color_type;
int row, col; int row, col;
int ret; int ret;
int i; int i;
int ok = true; int ok = true;
unsigned char *p = NULL; unsigned char *p = NULL;
double file_gamma; double file_gamma;
uchar2p data = {NULL, NULL};
if(!file_name) if(!file_name)
return 0; return 0;
png_file = fopen(file_name, "rb"); if(!strncmp(file_name, "data:image/png;base64,", 22)) {
if(png_file == NULL) const char *base64str = file_name + 22;
return 0; data.h = data.c = base64_decode(base64str);
memcpy(buf, data.c, 8);
data.c += 8;
} else {
png_file = pymol_fopen(file_name, "rb");
if(png_file == NULL)
return 0;
/* read and check signature in PNG file */ /* read and check signature in PNG file */
ret = fread(buf, 1, 8, png_file); ret = fread(buf, 1, 8, png_file);
if(ret != 8) if(ret != 8)
ok = false; ok = false;
}
if(ok) { if(ok) {
ret = png_check_sig(buf, 8); ret = png_check_sig(buf, 8);
if(!ret) if(!ret)
ok = false; ok = false;
} }
/* create png and info structures */ /* create png and info structures */
if(ok) { if(ok) {
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr) if(!png_ptr)
ok = false; ok = false;
} }
if(ok) { if(ok) {
info_ptr = png_create_info_struct(png_ptr); info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr) if(!info_ptr)
ok = false; ok = false;
} }
if(setjmp(png_jmpbuf(png_ptr))) if(ok && setjmp(png_jmpbuf(png_ptr)))
ok = false; ok = false;
if(ok) { if(ok) {
/* set up the input control for C streams */ /* set up the input control for C streams */
png_init_io(png_ptr, png_file);
if(data.h) {
png_set_read_fn(png_ptr, (void*) &data, read_data_from_buffer);
} else {
png_init_io(png_ptr, png_file);
}
png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */ png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */
/* read the file information */ /* read the file information */
png_read_info(png_ptr, info_ptr); png_read_info(png_ptr, info_ptr);
/* get size and bit-depth of the PNG-image */ /* get size and bit-depth of the PNG-image */
png_get_IHDR(png_ptr, info_ptr, png_get_IHDR(png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type, NULL, NULL, NULL); &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
/* set-up the transformations */ /* set-up the transformations */
skipping to change at line 371 skipping to change at line 485
/* now reformat image into PyMOL format */ /* now reformat image into PyMOL format */
p = (unsigned char *) mmalloc(4 * width * height); p = (unsigned char *) mmalloc(4 * width * height);
if(!p) if(!p)
ok = false; ok = false;
} }
if(ok) { if(ok) {
*(p_ptr) = p; *(p_ptr) = p;
*(width_ptr) = width; *(width_ptr) = width;
*(height_ptr) = height; *(height_ptr) = height;
for(row = 0; row < (signed) height; row++) { for(row = 0; row < (signed) height; row++) {
pix_ptr = row_pointers[(height - 1) - row]; pix_ptr = row_pointers[(height - 1) - row];
for(col = 0; col < (signed) width; col++) { for(col = 0; col < (signed) width; col++) {
*p++ = *pix_ptr++; *p++ = *pix_ptr++;
*p++ = *pix_ptr++; *p++ = *pix_ptr++;
*p++ = *pix_ptr++; *p++ = *pix_ptr++;
*p++ = *pix_ptr++; *p++ = *pix_ptr++;
} }
} }
skipping to change at line 394 skipping to change at line 507
if(row_pointers != (unsigned char **) NULL) if(row_pointers != (unsigned char **) NULL)
free(row_pointers); free(row_pointers);
if(png_pixels != (unsigned char *) NULL) if(png_pixels != (unsigned char *) NULL)
free(png_pixels); free(png_pixels);
if(png_ptr) { if(png_ptr) {
png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL); png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp) NULL);
} }
if(png_file) if(png_file)
fclose(png_file); fclose(png_file);
if(data.h)
mfree(data.h);
return (ok); return (ok);
#else #else
return (false); return (false);
#endif #endif
} /* end of source */ } /* end of source */
 End of changes. 13 change blocks. 
27 lines changed or deleted 142 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)