Fix some compiler warnings and add a little optimization in ascii85 encoder

This commit is contained in:
stefan schippers 2023-01-23 11:49:08 +01:00
parent 1069af750a
commit 9b6b7bc19a
4 changed files with 56 additions and 39 deletions

View File

@ -104,6 +104,7 @@ typedef struct
size_t pos;
size_t size;
} png_to_byte_closure_t;
void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, int flip)
{
#if defined(HAS_LIBJPEG) && defined(HAS_CAIRO)
@ -125,6 +126,8 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
int quality=100;
const char *quality_attr;
size_t image_data_len;
size_t oLength;
int idx = 0;
quality_attr = get_tok_value(r->prop_ptr, "jpeg_quality", 0);
if(quality_attr[0]) quality = atoi(quality_attr);
@ -169,7 +172,6 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
unsigned char png_g = png_data[i + 1];
unsigned char png_b = png_data[i + 2];
unsigned char png_a = png_data[i + 3];
double ainv=((double)(0xFF - png_a)) / ((double)(0xFF));
if(invertImage)
@ -187,11 +189,8 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
}
}
cairo_surface_mark_dirty(surface);
cairo_image_surface_write_to_jpeg_mem(surface, &jpgData, &fileSize, 100);
int oLength;
cairo_image_surface_write_to_jpeg_mem(surface, &jpgData, &fileSize, quality);
ascii85EncodedJpeg = ascii85_encode(jpgData, fileSize, &oLength, 0);
fprintf(fd, "gsave\n");
fprintf(fd, "save\n");
fprintf(fd, "/RawData currentfile /ASCII85Decode filter def\n");
@ -208,19 +207,25 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
if(!flip)
{
if(rot==1) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 %g]\n", (double)png_size_y, (double)png_size_x, (double)png_size_y);
else if(rot==2) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g %g]\n", (double)png_size_x, (double)png_size_y, (double)png_size_x, (double)png_size_y);
else if(rot==3) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g 0]\n", (double)png_size_y, (double)png_size_x, (double)png_size_x);
if(rot==1) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 %g]\n",
(double)png_size_y, (double)png_size_x, (double)png_size_y);
else if(rot==2) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g %g]\n",
(double)png_size_x, (double)png_size_y, (double)png_size_x, (double)png_size_y);
else if(rot==3) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g 0]\n",
(double)png_size_y, (double)png_size_x, (double)png_size_x);
else fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 0]\n", (double)png_size_x, (double)png_size_y);
}
else
{
if(rot==1) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g %g]\n", -(double)png_size_y, (double)png_size_x, (double)png_size_x, (double)png_size_y);
else if(rot==2) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 %g]\n", -(double)png_size_x, (double)png_size_y, (double)png_size_y);
else if(rot==3) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 0]\n", -(double)png_size_y, (double)png_size_x);
else fprintf(fd, " /ImageMatrix [%g 0 0 %g %g 0]\n", -(double)png_size_x, (double)png_size_y, (double)png_size_x);
if(rot==1) fprintf(fd, " /ImageMatrix [%g 0 0 %g %g %g]\n",
-(double)png_size_y, (double)png_size_x, (double)png_size_x, (double)png_size_y);
else if(rot==2) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 %g]\n",
-(double)png_size_x, (double)png_size_y, (double)png_size_y);
else if(rot==3) fprintf(fd, " /ImageMatrix [%g 0 0 %g 0 0]\n",
-(double)png_size_y, (double)png_size_x);
else fprintf(fd, " /ImageMatrix [%g 0 0 %g %g 0]\n",
-(double)png_size_x, (double)png_size_y, (double)png_size_x);
}
fprintf(fd, " /DataSource Data\n");
fprintf(fd, " /BitsPerComponent 8\n");
fprintf(fd, " /Decode [0 1 0 1 0 1]\n");
@ -230,8 +235,7 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
fprintf(fd, " restore\n");
fprintf(fd, "} exec\n");
int idx = 0;
for (int i = 0; i < oLength; i++)
for (i = 0; i < oLength; i++)
{
fputc(ascii85EncodedJpeg[i],fd);
idx++;
@ -239,7 +243,8 @@ void ps_drawPNG(xRect* r, double x1, double y1, double x2, double y2, int rot, i
{
idx=0;
fputc('\n',fd);
//if (ascii85Encode[i+1]=='%') idx=63; imageMagic does this for some reason?! Doesn't seem to be necesary.
/* if (ascii85Encode[i+1]=='%') idx=63; imageMagic does this for some reason?!
* Doesn't seem to be necesary. */
}
}
fprintf(fd, "~>\n");
@ -268,6 +273,8 @@ void ps_embedded_graph(xRect* r, double rx1, double ry1, double rx2, double ry2)
unsigned char *ascii85EncodedJpeg;
int quality=100;
const char *quality_attr;
size_t oLength;
int i, idx = 0;
quality_attr = get_tok_value(r->prop_ptr, "jpeg_quality", 0);
if(quality_attr[0]) quality = atoi(quality_attr);
@ -324,7 +331,6 @@ void ps_embedded_graph(xRect* r, double rx1, double ry1, double rx2, double ry2)
#endif
cairo_image_surface_write_to_jpeg_mem(png_sfc, &jpgData, &fileSize, quality);
int oLength;
ascii85EncodedJpeg = ascii85_encode(jpgData, fileSize, &oLength, 0);
free(jpgData);
@ -359,8 +365,7 @@ void ps_embedded_graph(xRect* r, double rx1, double ry1, double rx2, double ry2)
fprintf(fd, " restore\n");
fprintf(fd, "} exec\n");
int idx = 0;
for (int i = 0; i < oLength; i++)
for (i = 0; i < oLength; i++)
{
fputc(ascii85EncodedJpeg[i],fd);
idx++;
@ -368,7 +373,8 @@ void ps_embedded_graph(xRect* r, double rx1, double ry1, double rx2, double ry2)
{
idx=0;
fputc('\n',fd);
//if (ascii85Encode[i+1]=='%') idx=63; imageMagic does this for some reason?! Doesn't seem to be necesary.
/* if (ascii85Encode[i+1]=='%') idx=63; imageMagic does this for some reason?!
* Doesn't seem to be necesary. */
}
}
fprintf(fd, "~>\n");
@ -995,7 +1001,7 @@ void create_ps(char **psfile, int what)
return;
}
}
setbuf(fd, NULL); //To prevent buffer errors, still investigating cause.
setbuf(fd, NULL); /*To prevent buffer errors, still investigating cause. */
ps_colors=my_calloc(311, cadlayers, sizeof(Ps_color));
if(ps_colors==NULL){
fprintf(errfp, "create_ps(): calloc error\n");

View File

@ -202,7 +202,7 @@ char *base64_encode(const unsigned char *data, const size_t input_length, size_t
*output_length = 4 * ((input_length + 2) / 3);
alloc_length = (1 + (*output_length / 4096)) * 4096;
encoded_data = my_malloc(1469, alloc_length);
encoded_data = my_malloc(1665, alloc_length);
if (encoded_data == NULL) return NULL;
cnt = 0;
@ -287,7 +287,7 @@ unsigned char *base64_decode(const char *data, const size_t input_length, size_t
/* Caller should free returned buffer */
/* set brk to 1 if you want newlines added */
char *ascii85_encode(const unsigned char *data, const size_t input_length, size_t *output_length, int brk) {
unsigned char *ascii85_encode(const unsigned char *data, const size_t input_length, size_t *output_length, int brk) {
static const char b85_enc[] = {
'!', '"', '#', '$', '%', '&', '\'', '(',
')', '*', '+', ',', '-', '.', '/', '0',
@ -303,15 +303,18 @@ char *ascii85_encode(const unsigned char *data, const size_t input_length, size_
};
int padding = (4-(input_length % 4))%4;
static u_int32_t pow85[] = {1, 85, 7225, 614125, 52200625};
unsigned char *paddedData = my_calloc(1469, input_length+padding, 1);
unsigned char *encoded_data;
int i, idx = 0;
memcpy( paddedData, data, input_length);
*output_length = 5*(input_length+padding)/4;
char *encoded_data = my_malloc(1469, *output_length +1);
encoded_data = my_malloc(1662, *output_length +1);
encoded_data[*output_length]=0;
int idx = 0;
for (int i = 0; i < input_length+padding; i+=4)
for (i = 0; i < input_length+padding; i+=4)
{
u_int32_t val = ((u_int32_t)(paddedData[i])<<24) + ((u_int32_t)(paddedData[i+1])<<16) + ((u_int32_t)(paddedData[i+2])<<8) + ((u_int32_t)(paddedData[i+3]));
u_int32_t val = ((u_int32_t)(paddedData[i])<<24) + ((u_int32_t)(paddedData[i+1])<<16) +
((u_int32_t)(paddedData[i+2])<<8) + ((u_int32_t)(paddedData[i+3]));
if (val==0)
{
encoded_data[idx]='z';
@ -319,23 +322,23 @@ char *ascii85_encode(const unsigned char *data, const size_t input_length, size_
idx++;
continue;
}
encoded_data[idx] = val / pow(85,4);
val = val - encoded_data[idx] * pow(85,4);
encoded_data[idx] = (unsigned char)(val / pow85[4]);
val = val - encoded_data[idx] * pow85[4];
encoded_data[idx]=b85_enc[encoded_data[idx]];
idx++;
encoded_data[idx] = val / pow(85,3);
val = val - encoded_data[idx] * pow(85,3);
encoded_data[idx] = (unsigned char)(val / pow85[3]);
val = val - encoded_data[idx] * pow85[3];
encoded_data[idx]=b85_enc[encoded_data[idx]];
idx++;
encoded_data[idx] = val / pow(85,2);
val = val - encoded_data[idx] * pow(85,2);
encoded_data[idx] = (unsigned char)(val / pow85[2]);
val = val - encoded_data[idx] * pow85[2];
encoded_data[idx]=b85_enc[encoded_data[idx]];
idx++;
encoded_data[idx] = val / pow(85,1);
val = val - encoded_data[idx] * pow(85,1);
encoded_data[idx] = (unsigned char)(val / pow85[1]);
val = val - encoded_data[idx] * pow85[1];
encoded_data[idx]=b85_enc[encoded_data[idx]];
idx++;
encoded_data[idx] = val;
encoded_data[idx] = (unsigned char)val;
encoded_data[idx]=b85_enc[encoded_data[idx]];
idx++;
}

View File

@ -1085,7 +1085,7 @@ extern int set_rect_flags(xRect *r);
extern int set_rect_extraptr(int what, xRect *drptr);
extern unsigned char *base64_decode(const char *data, const size_t input_length, size_t *output_length);
extern char *base64_encode(const unsigned char *data, const size_t input_length, size_t *output_length, int brk);
extern char *ascii85_encode(const unsigned char *data, const size_t input_length, size_t *output_length, int brk);
extern unsigned char *ascii85_encode(const unsigned char *data, const size_t input_length, size_t *output_length, int brk);
extern int get_raw_index(const char *node);
extern void free_rawfile(int dr);
extern int raw_read(const char *f, const char *type);

View File

@ -22,6 +22,8 @@ divx=8
subdivx=1
dataset=0
unitx=m
color=7
node=tcleval($\{path\}outi)
jpeg_quality=30}
@ -38,8 +40,12 @@ divx=8
subdivx=9
dataset=0
unitx=m
color="8 7"
node=tcleval($\{path\}outi)
node="tcleval(\\"Xm1 power;vpp $\{path\}outi - i(v.$\{path\}vu) *\\"
\\"Average;vpp $\{path\}outi - i(v.$\{path\}vu) * 200u ravg()\\")"
jpeg_quality=30}
B 2 1520 -1130 2440 -830 {flags=graph
y1=-0.19
@ -52,8 +58,10 @@ divx=8
subdivx=9
dataset=0
unitx=m
color="8 7"
node=tcleval($\{path\}outi)
node="tcleval(\\"Xm2 Power;$\{path\}outi vnn - i(v.$\{path\}vd) *\\"
\\"Average;$\{path\}outi vnn - i(v.$\{path\}vd) * 200u ravg()\\")"
jpeg_quality=30}
B 2 960 -1050 1094 -987 {flags=image
alpha=0.7