Skip to content

Commit

Permalink
Fix the corruption of 4-bits colors icon
Browse files Browse the repository at this point in the history
20x20 and 40x40 icons with 4-bits colors are corrupted when converted
from 16x16 and 32x32 icons, respectively.

    modified:   src/Icon.cpp
    modified:   src/Icon.hpp
    modified:   src/IconMgr.cpp
    modified:   src/check.cmd
    new file:   src/testcases/mksh.ico
    modified:   src/testcases/readme
  • Loading branch information
komh committed Mar 26, 2017
1 parent 1b5230c commit ca38def
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 38 deletions.
21 changes: 0 additions & 21 deletions src/Icon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,6 @@

#include "Icon.hpp"

static inline int calcStride(int w, int bpp)
{
int stride;

// Convert pixel to bits
stride = w * bpp;

// Conver to bytes
stride = (stride + 7) / 8;

// Align with 4 bytes boundary
stride = (stride + 3 ) & ~3;

return stride ;
}

static inline int calcImageSize(int w, int h, int bpp)
{
return calcStride(w, bpp) * h;
}

int Os2Icon::ipow(int b, int e){
int p=b;
while(--e)
Expand Down
21 changes: 21 additions & 0 deletions src/Icon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@
#include "IconsVar.hpp"
#include "IconImage.hpp"

static inline int calcStride(int w, int bpp)
{
int stride;

// Convert pixel to bits
stride = w * bpp;

// Conver to bytes
stride = (stride + 7) / 8;

// Align with 4 bytes boundary
stride = (stride + 3) & ~3;

return stride ;
}

static inline int calcImageSize(int w, int h, int bpp)
{
return calcStride(w, bpp) * h;
}

struct IconError{
const char* msg;
const char* function;
Expand Down
27 changes: 11 additions & 16 deletions src/IconMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -433,39 +433,34 @@ PBYTE Os2IconMgr::centerMaskData(PBYTE data, const int tamanio){
}

PBYTE Os2IconMgr::centerImageData(PBYTE data, const int tamanio, int bits){
int delta,i,largoScan,largoScanOr,ppc,numScans;
int delta,i,largoScan,largoScanOr,numScans;
PBYTE scanor;
PBYTE scands;
PBYTE retorno;
if(bits==24)
ppc=bits/8;
else
ppc=1;
if(tamanio == ICONO_20X20){
delta = 2;
numScans=16;
largoScan = 20*ppc;
largoScanOr = 16*ppc;
retorno = (PBYTE)malloc(400*ppc);
largoScan = calcStride(20, bits);
largoScanOr = calcStride(16, bits);
retorno = (PBYTE)malloc(largoScan * 20);
// El color de fondo es el mismo del primer dato (si no fuera fondo, la imagen saldra corrupta)
memset(retorno,*data,400*ppc);
memset(retorno,*data,largoScan * 20);
}
else if(tamanio == ICONO_40X40){
delta = 4;
numScans=32;
largoScan = 40*ppc;
largoScanOr = 32*ppc;
retorno = (PBYTE)malloc(1600*ppc);
largoScan = calcStride(40, bits);
largoScanOr = calcStride(32, bits);
retorno = (PBYTE)malloc(largoScan * 40);
// El color de fondo es el mismo del primer dato (si no fuera fondo, la imagen saldra corrupta)
memset(retorno,*data,1600*ppc);
memset(retorno,*data,largoScan * 40);
}
scands = retorno + largoScan * delta;
scanor = data;
i=0;
while(i < numScans){
scands = scands + delta*ppc;
memcpy(scands,scanor,largoScanOr);
scands = scands + delta*ppc + largoScanOr;
memcpy(scands + delta * bits / 8,scanor,largoScanOr);
scands = scands + largoScan;
scanor = scanor + largoScanOr;
i++;
}
Expand Down
3 changes: 2 additions & 1 deletion src/check.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ if rc \= 0 then

sWinIconList = 'testcases\qmetrics.ico',
'testcases\vlc.ico',
'testcases\kalarm.ico'
'testcases\kalarm.ico',
'testcases\mksh.ico'

call convert sWinIconList

Expand Down
Binary file added src/testcases/mksh.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions src/testcases/readme
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ qmetrics.ico:

vlc.ico:
WinIco crashes due to zero-image-size in bitmap header.

mksh.ico:
4bits icons( 16x16, 32x32 ) are not converted correctly.

0 comments on commit ca38def

Please sign in to comment.