summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorleo1999-12-09 22:00:04 +0000
committerleo1999-12-09 22:00:04 +0000
commit13a29d52c47942a1d4c65a9aabd5f916ed0dd4d0 (patch)
treec2b3f6e7ee4ca4f902a4225ed72ddc9d3785f840
parent7df7d4b627e5c09b665d8916d2a2e05e8d27bba9 (diff)
Added an option to sort the columns of the piece list from the properties dialog
Cleaned the image reading routines a little more git-svn-id: http://svn.leocad.org/trunk@29 c7d43263-9d01-0410-8a33-9dba5d9f93d6
-rwxr-xr-xcommon/im_bmp.cpp525
-rw-r--r--common/image.cpp523
-rw-r--r--common/module.mk2
-rw-r--r--win/LeoCAD.dsp17
-rw-r--r--win/Leocad.clw5
-rw-r--r--win/Piecebar.cpp2
-rw-r--r--win/Propspgs.cpp90
-rw-r--r--win/Propspgs.h5
8 files changed, 642 insertions, 527 deletions
diff --git a/common/im_bmp.cpp b/common/im_bmp.cpp
new file mode 100755
index 0000000..0f6a175
--- /dev/null
+++ b/common/im_bmp.cpp
@@ -0,0 +1,525 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "typedefs.h"
+#include "quant.h"
+
+// ========================================================
+
+LC_IMAGE* OpenBMP (char* filename)
+{
+ int bmWidth;
+ int bmHeight;
+ unsigned char bmPlanes;
+ unsigned char bmBitsPixel;
+ typedef struct {
+ unsigned char rgbBlue;
+ unsigned char rgbGreen;
+ unsigned char rgbRed;
+ unsigned char rgbReserved;
+ } RGBQUAD;
+ unsigned char m1,m2;
+ unsigned long sizeimage;
+ short res1,res2;
+ long filesize, pixoff;
+ long bmisize, compression;
+ long xscale, yscale;
+ long colors, impcol;
+ LC_IMAGE* image;
+ unsigned long m_bytesRead = 0;
+ FILE *fp;
+
+ fp = fopen(filename,"rb");
+ if (fp == NULL)
+ return NULL;
+
+ long rc;
+ rc = fread(&m1, 1, 1, fp);
+ m_bytesRead++;
+ if (rc == -1)
+ {
+ fclose(fp);
+ return NULL;
+ }
+
+ rc = fread(&m2, 1, 1, fp);
+ m_bytesRead++;
+ if ((m1!='B') || (m2!='M'))
+ {
+ fclose(fp);
+ return NULL;
+ }
+
+ rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long *)&(bmWidth),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) {fclose(fp); return NULL; }
+
+ rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4;
+ if (rc != 1) { fclose(fp); return NULL; }
+
+ if (colors == 0)
+ colors = 1 << bmBitsPixel;
+
+ RGBQUAD *colormap = NULL;
+
+ if (bmBitsPixel != 24)
+ {
+ colormap = new RGBQUAD[colors];
+ if (colormap == NULL)
+ {
+ fclose(fp);
+ return NULL;
+ }
+
+ int i;
+ for (i = 0; i < colors; i++)
+ {
+ unsigned char r ,g, b, dummy;
+
+ rc = fread(&b, 1, 1, fp);
+ m_bytesRead++;
+ if (rc!=1)
+ {
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+
+ rc = fread(&g, 1, 1, fp);
+ m_bytesRead++;
+ if (rc!=1)
+ {
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+
+ rc = fread(&r, 1, 1, fp);
+ m_bytesRead++;
+ if (rc != 1)
+ {
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+
+
+ rc = fread(&dummy, 1, 1, fp);
+ m_bytesRead++;
+ if (rc != 1)
+ {
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+
+ colormap[i].rgbRed=r;
+ colormap[i].rgbGreen=g;
+ colormap[i].rgbBlue=b;
+ }
+ }
+
+ if ((long)m_bytesRead > pixoff)
+ {
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+
+ while ((long)m_bytesRead < pixoff)
+ {
+ char dummy;
+ fread(&dummy,1,1,fp);
+ m_bytesRead++;
+ }
+
+ int w = bmWidth;
+ int h = bmHeight;
+
+ // set the output params
+ image = (LC_IMAGE*)malloc(w*h*3 + sizeof(LC_IMAGE));
+ long row_size = w * 3;
+ long bufsize = (long)w * 3 * (long)h;
+
+ if (image != NULL)
+ {
+ image->width = w;
+ image->height = h;
+ image->bits = (char*)image + sizeof(LC_IMAGE);
+ unsigned char* outbuf = (unsigned char*)image->bits;
+ long row = 0;
+ long rowOffset = 0;
+
+ if (compression == 0) // BI_RGB
+ {
+ // read rows in reverse order
+ for (row=bmHeight-1;row>=0;row--)
+ {
+ // which row are we working on?
+ rowOffset = (long unsigned)row*row_size;
+
+ if (bmBitsPixel == 24)
+ {
+ for (int col=0;col<w;col++)
+ {
+ long offset = col * 3;
+ char pixel[3];
+
+ if (fread((void*)(pixel),1,3,fp)==3)
+ {
+ // we swap red and blue here
+ *(outbuf + rowOffset + offset + 0)=pixel[2]; // r
+ *(outbuf + rowOffset + offset + 1)=pixel[1]; // g
+ *(outbuf + rowOffset + offset + 2)=pixel[0]; // b
+ }
+ }
+ m_bytesRead += row_size;
+
+ // read DWORD padding
+ while ((m_bytesRead-pixoff)&3)
+ {
+ char dummy;
+ if (fread(&dummy,1,1,fp) != 1)
+ {
+ free(image);
+ fclose(fp);
+ return NULL;
+ }
+ m_bytesRead++;
+ }
+ }
+ else
+ {
+ // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
+ int bit_count = 0;
+ unsigned long mask = (1 << bmBitsPixel) - 1;
+ unsigned char inbyte = 0;
+
+ for (int col=0;col<w;col++)
+ {
+ int pix = 0;
+
+ // if we need another byte
+ if (bit_count <= 0)
+ {
+ bit_count = 8;
+ if (fread(&inbyte,1,1,fp) != 1)
+ {
+ free(image);
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+ m_bytesRead++;
+ }
+
+ // keep track of where we are in the bytes
+ bit_count -= bmBitsPixel;
+ pix = ( inbyte >> bit_count) & mask;
+
+ // lookup the color from the colormap - stuff it in our buffer
+ // swap red and blue
+ *(outbuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
+ *(outbuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
+ *(outbuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
+ }
+
+ // read DWORD padding
+ while ((m_bytesRead-pixoff)&3)
+ {
+ char dummy;
+ if (fread(&dummy,1,1,fp)!=1)
+ {
+ free(image);
+ if (colormap)
+ delete [] colormap;
+ fclose(fp);
+ return NULL;
+ }
+ m_bytesRead++;
+ }
+ }
+ }
+ }
+ else
+ {
+ int i, x = 0;
+ unsigned char c, c1, *pp;
+ row = 0;
+ pp = outbuf + (bmHeight-1)*bmWidth*3;
+
+ if (bmBitsPixel == 8)
+ {
+ while (row < bmHeight)
+ {
+ c = getc(fp);
+
+ if (c)
+ {
+ // encoded mode
+ c1 = getc(fp);
+ for (i = 0; i < c; x++, i++)
+ {
+ *pp = colormap[c1].rgbRed; pp++;
+ *pp = colormap[c1].rgbGreen; pp++;
+ *pp = colormap[c1].rgbBlue; pp++;
+ }
+ }
+ else
+ {
+ // c==0x00, escape codes
+ c = getc(fp);
+
+ if (c == 0x00) // end of line
+ {
+ row++;
+ x = 0;
+ pp = outbuf + (bmHeight-row-1)*bmWidth*3;
+ }
+ else if (c == 0x01)
+ break; // end of pic
+ else if (c == 0x02) // delta
+ {
+ c = getc(fp);
+ x += c;
+ c = getc(fp);
+ row += c;
+ pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
+ }
+ else // absolute mode
+ {
+ for (i = 0; i < c; x++, i++)
+ {
+ c1 = getc(fp);
+ *pp = colormap[c1].rgbRed; pp++;
+ *pp = colormap[c1].rgbGreen; pp++;
+ *pp = colormap[c1].rgbBlue; pp++;
+ }
+
+ if (c & 1)
+ getc(fp); // odd length run: read an extra pad byte
+ }
+ }
+ }
+ }
+ else if (bmBitsPixel == 4)
+ {
+ while (row < bmHeight)
+ {
+ c = getc(fp);
+
+ if (c)
+ {
+ // encoded mode
+ c1 = getc(fp);
+ for (i = 0; i < c; x++, i++)
+ {
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
+ }
+ }
+ else
+ {
+ // c==0x00, escape codes
+ c = getc(fp);
+
+ if (c == 0x00) // end of line
+ {
+ row++;
+ x = 0;
+ pp = outbuf + (bmHeight-row-1)*bmWidth*3;
+ }
+ else if (c == 0x01)
+ break; // end of pic
+ else if (c == 0x02) // delta
+ {
+ c = getc(fp);
+ x += c;
+ c = getc(fp);
+ row += c;
+ pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
+ }
+ else // absolute mode
+ {
+ for (i = 0; i < c; x++, i++)
+ {
+ if ((i&1) == 0)
+ c1 = getc(fp);
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
+ *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
+ }
+
+ if (((c&3) == 1) || ((c&3) == 2))
+ getc(fp); // odd length run: read an extra pad byte
+ }
+ }
+ }
+ }
+ }
+
+ if (colormap)
+ delete [] colormap;
+
+ fclose(fp);
+ }
+
+ return image;
+}
+
+// ========================================================
+
+bool SaveBMP(char* filename, LC_IMAGE* image, bool quantize)
+{
+ FILE *fp;
+ fp = fopen(filename, "wb");
+ if (fp == NULL)
+ return false;
+
+ unsigned short bits;
+ unsigned long cmap, bfSize;
+ unsigned char pal[3][256], *colormappedbuffer;
+
+ if (quantize)
+ {
+ colormappedbuffer = (unsigned char*)malloc(image->width*image->height);
+ dl1quant((unsigned char*)image->bits, colormappedbuffer, image->width, image->height, 256, true, pal);
+ bits = 8;
+ cmap = 256;
+ bfSize = 1078 + image->width*image->height;
+ }
+ else
+ {
+ bits = 24;
+ cmap = 0;
+ bfSize = 54 + image->width*image->height*3;
+ }
+
+ long byteswritten = 0;
+ long pixoff = 54 + cmap*4;
+ short res = 0;
+ char m1 ='B', m2 ='M';
+ fwrite(&m1, 1, 1, fp); byteswritten++; // B
+ fwrite(&m2, 1, 1, fp); byteswritten++; // M
+ fwrite(&bfSize, 4, 1, fp); byteswritten+=4;// bfSize
+ fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved1
+ fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved2
+ fwrite(&pixoff, 4, 1, fp); byteswritten+=4;// bfOffBits
+
+ unsigned long biSize = 40, compress = 0, size = 0;
+ long width = image->width, height = image->height, pixels = 0;
+ unsigned short planes = 1;
+ fwrite(&biSize, 4, 1, fp); byteswritten+=4;// biSize
+ fwrite(&width, 4, 1, fp); byteswritten+=4;// biWidth
+ fwrite(&height, 4, 1, fp); byteswritten+=4;// biHeight
+ fwrite(&planes, 2, 1, fp); byteswritten+=2;// biPlanes
+ fwrite(&bits, 2, 1, fp); byteswritten+=2;// biBitCount
+ fwrite(&compress, 4, 1, fp);byteswritten+=4;// biCompression
+ fwrite(&size, 4, 1, fp); byteswritten+=4;// biSizeImage
+ fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biXPelsPerMeter
+ fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biYPelsPerMeter
+ fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrUsed
+ fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrImportant
+
+ if (quantize)
+ {
+ for (int i = 0; i < 256; i++)
+ {
+ putc(pal[2][i], fp);
+ putc(pal[1][i], fp);
+ putc(pal[0][i], fp);
+ putc(0, fp); // dummy
+ }
+
+ for (int row = 0; row < image->height; row++)
+ {
+ int pixbuf;
+
+ for (int col = 0; col < image->width; col++)
+ {
+ int offset = (image->height-row-1) * width + col; // offset into our color-mapped RGB buffer
+ unsigned char pval = *(colormappedbuffer + offset);
+
+ pixbuf = (pixbuf << 8) | pval;
+
+ putc(pixbuf, fp);
+ pixbuf = 0;
+ byteswritten++;
+ }
+
+ // DWORD align
+ while ((byteswritten - pixoff) & 3)
+ {
+ putc(0, fp);
+ byteswritten++;
+ }
+ }
+
+ free(colormappedbuffer);
+ }
+ else
+ {
+ unsigned long widthDW = (((image->width*24) + 31) / 32 * 4);
+ long row, row_size = image->width*3;
+ for (row = 0; row < image->height; row++)
+ {
+ unsigned char* buf = (unsigned char*)image->bits+(image->height-row-1)*row_size;
+
+ // write a row
+ for (int col = 0; col < row_size; col += 3)
+ {
+ putc(buf[col+2], fp);
+ putc(buf[col+1], fp);
+ putc(buf[col], fp);
+ }
+ byteswritten += row_size;
+
+ for (unsigned long count = row_size; count < widthDW; count++)
+ {
+ putc(0, fp); // dummy
+ byteswritten++;
+ }
+ }
+ }
+
+ fclose(fp);
+ return true;
+}
diff --git a/common/image.cpp b/common/image.cpp
index ae919cb..7c624a9 100644
--- a/common/image.cpp
+++ b/common/image.cpp
@@ -17,13 +17,14 @@ extern "C" {
// static declarations
static LC_IMAGE* OpenJPG(char* filename);
-static LC_IMAGE* OpenBMP(char* filename);
+LC_IMAGE* OpenBMP(char* filename);
LC_IMAGE* OpenPNG(char* filename);
static LC_IMAGE* OpenGIF(File* file);
+
static bool SaveJPG(char* filename, LC_IMAGE* image, int quality, bool progressive);
-static bool SaveBMP(char* filename, LC_IMAGE* image, bool quantize);
-static bool SaveGIF(File* file, LC_IMAGE* image, bool transparent, bool interlaced, unsigned char* background);
+bool SaveBMP(char* filename, LC_IMAGE* image, bool quantize);
bool SavePNG(char* filename, LC_IMAGE* image, bool transparent, bool interlaced, unsigned char* background);
+static bool SaveGIF(File* file, LC_IMAGE* image, bool transparent, bool interlaced, unsigned char* background);
typedef struct bt_jpeg_error_mgr
{
@@ -290,406 +291,6 @@ static LC_IMAGE* OpenJPG(char* filename)
return image;
}
-static LC_IMAGE* OpenBMP (char* filename)
-{
- int bmWidth;
- int bmHeight;
- unsigned char bmPlanes;
- unsigned char bmBitsPixel;
- typedef struct {
- unsigned char rgbBlue;
- unsigned char rgbGreen;
- unsigned char rgbRed;
- unsigned char rgbReserved;
- } RGBQUAD;
- unsigned char m1,m2;
- unsigned long sizeimage;
- short res1,res2;
- long filesize, pixoff;
- long bmisize, compression;
- long xscale, yscale;
- long colors, impcol;
- LC_IMAGE* image;
- unsigned long m_bytesRead = 0;
- FILE *fp;
-
- fp = fopen(filename,"rb");
- if (fp == NULL)
- return NULL;
-
- long rc;
- rc = fread(&m1, 1, 1, fp);
- m_bytesRead++;
- if (rc == -1)
- {
- fclose(fp);
- return NULL;
- }
-
- rc = fread(&m2, 1, 1, fp);
- m_bytesRead++;
- if ((m1!='B') || (m2!='M'))
- {
- fclose(fp);
- return NULL;
- }
-
- rc = fread((long*)&(filesize),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((int*)&(res1),2,1,fp); m_bytesRead+=2;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((int*)&(res2),2,1,fp); m_bytesRead+=2;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(pixoff),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(bmisize),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long *)&(bmWidth),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(bmHeight),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((int*)&(bmPlanes),2,1,fp); m_bytesRead+=2;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((int*)&(bmBitsPixel),2,1,fp); m_bytesRead+=2;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(compression),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(sizeimage),4,1,fp); m_bytesRead+=4;
- if (rc != 1) {fclose(fp); return NULL; }
-
- rc = fread((long*)&(xscale),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(yscale),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(colors),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- rc = fread((long*)&(impcol),4,1,fp); m_bytesRead+=4;
- if (rc != 1) { fclose(fp); return NULL; }
-
- if (colors == 0)
- colors = 1 << bmBitsPixel;
-
- RGBQUAD *colormap = NULL;
-
- if (bmBitsPixel != 24)
- {
- colormap = new RGBQUAD[colors];
- if (colormap == NULL)
- {
- fclose(fp);
- return NULL;
- }
-
- int i;
- for (i = 0; i < colors; i++)
- {
- unsigned char r ,g, b, dummy;
-
- rc = fread(&b, 1, 1, fp);
- m_bytesRead++;
- if (rc!=1)
- {
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
-
- rc = fread(&g, 1, 1, fp);
- m_bytesRead++;
- if (rc!=1)
- {
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
-
- rc = fread(&r, 1, 1, fp);
- m_bytesRead++;
- if (rc != 1)
- {
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
-
-
- rc = fread(&dummy, 1, 1, fp);
- m_bytesRead++;
- if (rc != 1)
- {
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
-
- colormap[i].rgbRed=r;
- colormap[i].rgbGreen=g;
- colormap[i].rgbBlue=b;
- }
- }
-
- if ((long)m_bytesRead > pixoff)
- {
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
-
- while ((long)m_bytesRead < pixoff)
- {
- char dummy;
- fread(&dummy,1,1,fp);
- m_bytesRead++;
- }
-
- int w = bmWidth;
- int h = bmHeight;
-
- // set the output params
- image = (LC_IMAGE*)malloc(w*h*3 + sizeof(LC_IMAGE));
- long row_size = w * 3;
- long bufsize = (long)w * 3 * (long)h;
-
- if (image != NULL)
- {
- image->width = w;
- image->height = h;
- image->bits = (char*)image + sizeof(LC_IMAGE);
- unsigned char* outbuf = (unsigned char*)image->bits;
- long row = 0;
- long rowOffset = 0;
-
- if (compression == 0) // BI_RGB
- {
- // read rows in reverse order
- for (row=bmHeight-1;row>=0;row--)
- {
- // which row are we working on?
- rowOffset = (long unsigned)row*row_size;
-
- if (bmBitsPixel == 24)
- {
- for (int col=0;col<w;col++)
- {
- long offset = col * 3;
- char pixel[3];
-
- if (fread((void*)(pixel),1,3,fp)==3)
- {
- // we swap red and blue here
- *(outbuf + rowOffset + offset + 0)=pixel[2]; // r
- *(outbuf + rowOffset + offset + 1)=pixel[1]; // g
- *(outbuf + rowOffset + offset + 2)=pixel[0]; // b
- }
- }
- m_bytesRead += row_size;
-
- // read DWORD padding
- while ((m_bytesRead-pixoff)&3)
- {
- char dummy;
- if (fread(&dummy,1,1,fp) != 1)
- {
- free(image);
- fclose(fp);
- return NULL;
- }
- m_bytesRead++;
- }
- }
- else
- {
- // pixels are packed as 1 , 4 or 8 bit vals. need to unpack them
- int bit_count = 0;
- unsigned long mask = (1 << bmBitsPixel) - 1;
- unsigned char inbyte = 0;
-
- for (int col=0;col<w;col++)
- {
- int pix = 0;
-
- // if we need another byte
- if (bit_count <= 0)
- {
- bit_count = 8;
- if (fread(&inbyte,1,1,fp) != 1)
- {
- free(image);
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
- m_bytesRead++;
- }
-
- // keep track of where we are in the bytes
- bit_count -= bmBitsPixel;
- pix = ( inbyte >> bit_count) & mask;
-
- // lookup the color from the colormap - stuff it in our buffer
- // swap red and blue
- *(outbuf + rowOffset + col * 3 + 2) = colormap[pix].rgbBlue;
- *(outbuf + rowOffset + col * 3 + 1) = colormap[pix].rgbGreen;
- *(outbuf + rowOffset + col * 3 + 0) = colormap[pix].rgbRed;
- }
-
- // read DWORD padding
- while ((m_bytesRead-pixoff)&3)
- {
- char dummy;
- if (fread(&dummy,1,1,fp)!=1)
- {
- free(image);
- if (colormap)
- delete [] colormap;
- fclose(fp);
- return NULL;
- }
- m_bytesRead++;
- }
- }
- }
- }
- else
- {
- int i, x = 0;
- unsigned char c, c1, *pp;
- row = 0;
- pp = outbuf + (bmHeight-1)*bmWidth*3;
-
- if (bmBitsPixel == 8)
- {
- while (row < bmHeight)
- {
- c = getc(fp);
-
- if (c)
- {
- // encoded mode
- c1 = getc(fp);
- for (i = 0; i < c; x++, i++)
- {
- *pp = colormap[c1].rgbRed; pp++;
- *pp = colormap[c1].rgbGreen; pp++;
- *pp = colormap[c1].rgbBlue; pp++;
- }
- }
- else
- {
- // c==0x00, escape codes
- c = getc(fp);
-
- if (c == 0x00) // end of line
- {
- row++;
- x = 0;
- pp = outbuf + (bmHeight-row-1)*bmWidth*3;
- }
- else if (c == 0x01)
- break; // end of pic
- else if (c == 0x02) // delta
- {
- c = getc(fp);
- x += c;
- c = getc(fp);
- row += c;
- pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
- }
- else // absolute mode
- {
- for (i = 0; i < c; x++, i++)
- {
- c1 = getc(fp);
- *pp = colormap[c1].rgbRed; pp++;
- *pp = colormap[c1].rgbGreen; pp++;
- *pp = colormap[c1].rgbBlue; pp++;
- }
-
- if (c & 1)
- getc(fp); // odd length run: read an extra pad byte
- }
- }
- }
- }
- else if (bmBitsPixel == 4)
- {
- while (row < bmHeight)
- {
- c = getc(fp);
-
- if (c)
- {
- // encoded mode
- c1 = getc(fp);
- for (i = 0; i < c; x++, i++)
- {
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
- }
- }
- else
- {
- // c==0x00, escape codes
- c = getc(fp);
-
- if (c == 0x00) // end of line
- {
- row++;
- x = 0;
- pp = outbuf + (bmHeight-row-1)*bmWidth*3;
- }
- else if (c == 0x01)
- break; // end of pic
- else if (c == 0x02) // delta
- {
- c = getc(fp);
- x += c;
- c = getc(fp);
- row += c;
- pp = outbuf + x*3 + (bmHeight-row-1)*bmWidth*3;
- }
- else // absolute mode
- {
- for (i = 0; i < c; x++, i++)
- {
- if ((i&1) == 0)
- c1 = getc(fp);
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbRed; pp++;
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbGreen; pp++;
- *pp = colormap[(i&1) ? (c1 & 0x0f) : ((c1>>4)&0x0f)].rgbBlue; pp++;
- }
-
- if (((c&3) == 1) || ((c&3) == 2))
- getc(fp); // odd length run: read an extra pad byte
- }
- }
- }
- }
- }
-
- if (colormap)
- delete [] colormap;
-
- fclose(fp);
- }
-
- return image;
-}
-
typedef struct
{
unsigned char colormap[3][256];
@@ -1121,122 +722,6 @@ static bool SaveJPG (char* filename, LC_IMAGE* image, int quality, bool progress
return true;
}
-static bool SaveBMP(char* filename, LC_IMAGE* image, bool quantize)
-{
- FILE *fp;
- fp = fopen(filename, "wb");
- if (fp == NULL)
- return false;
-
- unsigned short bits;
- unsigned long cmap, bfSize;
- unsigned char pal[3][256], *colormappedbuffer;
-
- if (quantize)
- {
- colormappedbuffer = (unsigned char*)malloc(image->width*image->height);
- dl1quant((unsigned char*)image->bits, colormappedbuffer, image->width, image->height, 256, TRUE, pal);
- bits = 8;
- cmap = 256;
- bfSize = 1078 + image->width*image->height;
- }
- else
- {
- bits = 24;
- cmap = 0;
- bfSize = 54 + image->width*image->height*3;
- }
-
- long byteswritten = 0;
- long pixoff = 54 + cmap*4;
- short res = 0;
- char m1 ='B', m2 ='M';
- fwrite(&m1, 1, 1, fp); byteswritten++; // B
- fwrite(&m2, 1, 1, fp); byteswritten++; // M
- fwrite(&bfSize, 4, 1, fp); byteswritten+=4;// bfSize
- fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved1
- fwrite(&res, 2, 1, fp); byteswritten+=2;// bfReserved2
- fwrite(&pixoff, 4, 1, fp); byteswritten+=4;// bfOffBits
-
- unsigned long biSize = 40, compress = 0, size = 0;
- long width = image->width, height = image->height, pixels = 0;
- unsigned short planes = 1;
- fwrite(&biSize, 4, 1, fp); byteswritten+=4;// biSize
- fwrite(&width, 4, 1, fp); byteswritten+=4;// biWidth
- fwrite(&height, 4, 1, fp); byteswritten+=4;// biHeight
- fwrite(&planes, 2, 1, fp); byteswritten+=2;// biPlanes
- fwrite(&bits, 2, 1, fp); byteswritten+=2;// biBitCount
- fwrite(&compress, 4, 1, fp);byteswritten+=4;// biCompression
- fwrite(&size, 4, 1, fp); byteswritten+=4;// biSizeImage
- fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biXPelsPerMeter
- fwrite(&pixels, 4, 1, fp); byteswritten+=4;// biYPelsPerMeter
- fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrUsed
- fwrite(&cmap, 4, 1, fp); byteswritten+=4;// biClrImportant
-
- if (quantize)
- {
- for (int i = 0; i < 256; i++)
- {
- putc(pal[2][i], fp);
- putc(pal[1][i], fp);
- putc(pal[0][i], fp);
- putc(0, fp); // dummy
- }
-
- for (int row = 0; row < image->height; row++)
- {
- int pixbuf;
-
- for (int col = 0; col < image->width; col++)
- {
- int offset = (image->height-row-1) * width + col; // offset into our color-mapped RGB buffer
- unsigned char pval = *(colormappedbuffer + offset);
-
- pixbuf = (pixbuf << 8) | pval;
-
- putc(pixbuf, fp);
- pixbuf = 0;
- byteswritten++;
- }
-
- // DWORD align
- while ((byteswritten - pixoff) & 3)
- {
- putc(0, fp);
- byteswritten++;
- }
- }
-
- free(colormappedbuffer);
- }
- else
- {
- unsigned long widthDW = (((image->width*24) + 31) / 32 * 4);
- long row, row_size = image->width*3;
- for (row = 0; row < image->height; row++)
- {
- unsigned char* buf = (unsigned char*)image->bits+(image->height-row-1)*row_size;
-
- // write a row
- for (int col = 0; col < row_size; col += 3)
- {
- putc(buf[col+2], fp);
- putc(buf[col+1], fp);
- putc(buf[col], fp);
- }
- byteswritten += row_size;
-
- for (unsigned long count = row_size; count < widthDW; count++)
- {
- putc(0, fp); // dummy
- byteswritten++;
- }
- }
- }
-
- fclose(fp);
- return true;
-}
#undef LZW_TABLE_SIZE
#define MAX_LZW_BITS 12
diff --git a/common/module.mk b/common/module.mk
index 3783f84..fa5697c 100644
--- a/common/module.mk
+++ b/common/module.mk
@@ -1,4 +1,4 @@
-SRC += common/boundbox.cpp common/camera.cpp common/file.cpp common/globals.cpp common/group.cpp common/image.cpp common/im_png.cpp common/light.cpp common/matrix.cpp common/piece.cpp common/pieceinf.cpp common/project.cpp common/quant.cpp common/terrain.cpp common/texture.cpp common/tr.cpp common/vector.cpp
+SRC += common/boundbox.cpp common/camera.cpp common/file.cpp common/globals.cpp common/group.cpp common/image.cpp common/im_bmp.cpp common/im_png.cpp common/light.cpp common/matrix.cpp common/piece.cpp common/pieceinf.cpp common/project.cpp common/quant.cpp common/terrain.cpp common/texture.cpp common/tr.cpp common/vector.cpp
#LIBS += -lGLU -lGL -ljpeg -lpng -lm -lX11
# Work around for debian bug in mesag-dev
diff --git a/win/LeoCAD.dsp b/win/LeoCAD.dsp
index 64193c9..7d15c4c 100644
--- a/win/LeoCAD.dsp
+++ b/win/LeoCAD.dsp
@@ -42,7 +42,7 @@ RSC=rc.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
-# ADD CPP /nologo /MT /W3 /GX /O2 /I "../common" /I "../win" /I "./jpeglib" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /MT /W3 /GX /O2 /I "../common" /I "../win" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /o NUL /win32
# ADD BASE RSC /l 0x409 /d "NDEBUG"
@@ -68,7 +68,7 @@ LINK32=link.exe
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MTd /W3 /Gm /GX /Zi /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
-# ADD CPP /nologo /G6 /MTd /W3 /Gm /GX /Zi /Od /I "../common" /I "../win" /I "./jpeglib" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
+# ADD CPP /nologo /G6 /MTd /W3 /Gm /GX /Zi /Od /I "../common" /I "../win" /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /Yu"stdafx.h" /FD /c
# ADD BASE MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32
# ADD MTL /nologo /D "_DEBUG" /mktyplib203 /o NUL /win32
# ADD BASE RSC /l 0x409 /d "_DEBUG"
@@ -207,6 +207,13 @@ InputPath=.\hlp\LeoCAD.hpj
# Begin Source File
SOURCE=.\Leocad.rc
+
+!IF "$(CFG)" == "LeoCAD - Win32 Release"
+
+!ELSEIF "$(CFG)" == "LeoCAD - Win32 Debug"
+
+!ENDIF
+
# End Source File
# Begin Source File
@@ -988,6 +995,11 @@ SOURCE=..\Common\group.h
# End Source File
# Begin Source File
+SOURCE=..\common\im_bmp.cpp
+# SUBTRACT CPP /YX /Yc /Yu
+# End Source File
+# Begin Source File
+
SOURCE=..\common\im_png.cpp
# ADD CPP /I "./libpng" /I "./zlib"
# SUBTRACT CPP /YX /Yc /Yu
@@ -995,6 +1007,7 @@ SOURCE=..\common\im_png.cpp
# Begin Source File
SOURCE=..\Common\image.cpp
+# ADD CPP /I "./jpeglib"
# SUBTRACT CPP /YX /Yc /Yu
# End Source File
# Begin Source File
diff --git a/win/Leocad.clw b/win/Leocad.clw
index 1a171a1..9122449 100644
--- a/win/Leocad.clw
+++ b/win/Leocad.clw
@@ -2,7 +2,7 @@
[General Info]
Version=1
-LastClass=CPiecesList
+LastClass=CPropertiesPieces
LastTemplate=CHeaderCtrl
NewFileInclude1=#include "stdafx.h"
NewFileInclude2=#include "leocad.h"
@@ -341,6 +341,9 @@ Type=0
BaseClass=CPropertyPage
HeaderFile=Propspgs.h
ImplementationFile=Propspgs.cpp
+Filter=D
+VirtualFilter=idWC
+LastObject=CPropertiesPieces
[CLS:CPropertiesSheet]
Type=0
diff --git a/win/Piecebar.cpp b/win/Piecebar.cpp
index 901213e..90410b0 100644
--- a/win/Piecebar.cpp
+++ b/win/Piecebar.cpp
@@ -693,7 +693,7 @@ int CPiecesBar::OnCreate(LPCREATESTRUCT lpCreateStruct)
for (int i = 0; i < LC_MAXCOLORS; i++)
m_wndColorsList.AddString("");
- m_wndPiecesCombo.Create (CBS_DROPDOWN|CBS_HASSTRINGS|WS_VISIBLE|WS_CHILD|
+ m_wndPiecesCombo.Create (CBS_DROPDOWN|CBS_SORT|CBS_HASSTRINGS|WS_VISIBLE|WS_CHILD|
WS_VSCROLL|WS_TABSTOP, CRect (0,0,0,0), this, IDW_PIECESCOMBO);
// Create a font for the combobox
diff --git a/win/Propspgs.cpp b/win/Propspgs.cpp
index dd5488e..5ac47ce 100644
--- a/win/Propspgs.cpp
+++ b/win/Propspgs.cpp
@@ -146,6 +146,7 @@ void CPropertiesPieces::DoDataExchange(CDataExchange* pDX)
BEGIN_MESSAGE_MAP(CPropertiesPieces, CPropertyPage)
//{{AFX_MSG_MAP(CPropertiesPieces)
+ ON_NOTIFY(LVN_COLUMNCLICK, IDC_PROP_PIECES_LIST, OnColumnclickPropPiecesList)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
@@ -155,7 +156,7 @@ BOOL CPropertiesPieces::OnInitDialog()
CPropertyPage::OnInitDialog();
char tmp[4];
- int i, j, col[LC_MAXCOLORS], totalcount[LC_MAXCOLORS];
+ int i, j;
memset (&totalcount, 0, sizeof (totalcount));
for (i = 0; i < lines; i++)
for (j = 0; j < LC_MAXCOLORS; j++)
@@ -173,6 +174,8 @@ BOOL CPropertiesPieces::OnInitDialog()
str.LoadString(IDS_COLOR01 + i);
m_List.InsertColumn(ID, (LPCSTR)str, LVCFMT_LEFT, 80, 0);
}
+ else
+ col[i] = -1;
ID++;
m_List.InsertColumn(ID, "Total", LVCFMT_LEFT, 60, 0);
@@ -188,14 +191,16 @@ BOOL CPropertiesPieces::OnInitDialog()
char name[65];
LV_ITEM lvi;
- lvi.mask = LVIF_TEXT;
+ lvi.mask = LVIF_TEXT|LVIF_PARAM;
lvi.iItem = 0;
lvi.iSubItem = 0;
lvi.pszText = name;
+ lvi.lParam = i;
strcpy (name, names[i]);
int idx = m_List.InsertItem(&lvi);
for (j = 0; j < LC_MAXCOLORS; j++)
+// if (totalcount[j])
if (count[i*LC_MAXCOLORS+j])
{
sprintf (tmp, "%d", count[i*LC_MAXCOLORS+j]);
@@ -215,10 +220,11 @@ BOOL CPropertiesPieces::OnInitDialog()
char name[65];
strcpy (name, "Total");
LV_ITEM lvi;
- lvi.mask = LVIF_TEXT;
+ lvi.mask = LVIF_TEXT|LVIF_PARAM;
lvi.iItem = m_List.GetItemCount();
lvi.iSubItem = 0;
lvi.pszText = name;
+ lvi.lParam = -1;
int idx = m_List.InsertItem(&lvi), total = 0;
for (i = 0; i < LC_MAXCOLORS; i++)
@@ -238,3 +244,81 @@ BOOL CPropertiesPieces::OnInitDialog()
return TRUE;
}
+
+typedef struct
+{
+ CPropertiesPieces* page;
+ int color;
+} COMPARE_DATA;
+
+static int CALLBACK ListViewCompareProc(LPARAM lP1, LPARAM lP2, LPARAM lParamData)
+{
+ int i, a, b;
+ COMPARE_DATA* data = (COMPARE_DATA*)lParamData;
+
+ if (data->color == -1)
+ {
+ // check if we're comparing the "total" row
+ if (lP1 == -1)
+ return 1;
+ else if (lP2 == -1)
+ return -1;
+
+ return strcmpi(data->page->names[lP1], data->page->names[lP2]);
+ }
+
+ // last column
+ if (data->color == LC_MAXCOLORS)
+ {
+ a = b = 0;
+ for (i = 0; i < LC_MAXCOLORS; i++)
+ {
+ a += data->page->count[lP1*LC_MAXCOLORS+i];
+ b += data->page->count[lP2*LC_MAXCOLORS+i];
+ }
+ }
+ else
+ {
+ if (lP1 == -1)
+ a = data->page->totalcount[data->color];
+ else
+ a = data->page->count[lP1*LC_MAXCOLORS+data->color];
+
+ if (lP2 == -1)
+ b = data->page->totalcount[data->color];
+ else
+ b = data->page->count[lP2*LC_MAXCOLORS+data->color];
+ }
+
+ if (a == b)
+ return 0;
+
+ if (a < b)
+ return -1;
+ else
+ return 1;
+}
+
+void CPropertiesPieces::OnColumnclickPropPiecesList(NMHDR* pNMHDR, LRESULT* pResult)
+{
+ int i;
+ NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
+ COMPARE_DATA data;
+
+ data.page = this;
+
+ if (pNMListView->iSubItem == 0)
+ data.color = -1;
+ else
+ {
+ for (i = 0; i < LC_MAXCOLORS; i++)
+ if (col[i] == pNMListView->iSubItem-1)
+ break;
+
+ data.color = i;
+ }
+
+ m_List.SortItems((PFNLVCOMPARE)ListViewCompareProc, (LPARAM)&data);
+
+ *pResult = 0;
+}
diff --git a/win/Propspgs.h b/win/Propspgs.h
index 0b78683..a6b475e 100644
--- a/win/Propspgs.h
+++ b/win/Propspgs.h
@@ -4,6 +4,8 @@
#ifndef __PROPSPGS_H__
#define __PROPSPGS_H__
+#include "defines.h"
+
/////////////////////////////////////////////////////////////////////////////
// CPropertiesGeneral dialog
@@ -96,6 +98,8 @@ public:
char** names;
unsigned short* count;
int lines;
+ int col[LC_MAXCOLORS];
+ int totalcount[LC_MAXCOLORS];
// Dialog Data
//{{AFX_DATA(CPropertiesPieces)
@@ -116,6 +120,7 @@ protected:
// Generated message map functions
//{{AFX_MSG(CPropertiesPieces)
virtual BOOL OnInitDialog();
+ afx_msg void OnColumnclickPropPiecesList(NMHDR* pNMHDR, LRESULT* pResult);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()