// CHK: unpadded works // DESC: redimensions/(un)tiles a dib into a column of tiles with // dimensions dstW x tileH. Can also work in reverse (if srcW<dstW) // NOTE: padding pixels may cause problems CLDIB *dib_redim_copy(CLDIB *src, int dstW, int tileH, int tileN) { if(src == NULL) return NULL; int srcW, srcH, srcB, srcP; dib_get_attr(src, &srcW, &srcH, &srcB, &srcP); // Force byte alignment if( (dstW*srcB&7) ) return NULL; // setup redim int srcR= srcW*srcB>>3, dstR= dstW*srcB>>3; // bytes/row RECORD srcRec= { srcR, srcH, dib_get_img(src) }; RECORD dstRec= { dstR, 0, 0 }; int ii; BYTE *srcD= NULL; if(srcR&3) // depad for src { srcD= (BYTE*)malloc(srcR*srcH); if(srcD == NULL) return NULL; for(ii=0; ii<srcH; ii++) memcpy(&srcD[ii*srcR], &srcRec.data[ii*srcP], srcR); srcRec.data= srcD; } bool bOK= data_redim(&srcRec, &dstRec, tileH, tileN); SAFE_FREE(srcD); if(!bOK) return NULL; CLDIB *dst= dib_alloc(dstW, dstRec.height, srcB, NULL, dib_is_topdown(src)); if(dst == NULL) return NULL; int dstH= dib_get_height(dst); // bytes/row int dstP= dib_get_pitch(dst); BYTE *dstD= dib_get_img(dst); if(dstR&3) // repad for dst { for(ii=0; ii<dstH; ii++) memcpy(&dstD[ii*dstP], &dstRec.data[ii*dstR], dstP); } else memcpy(dstD, dstRec.data, dstP*dstH); SAFE_FREE(dstRec.data); memcpy(dib_get_pal(dst), dib_get_pal(src), dib_get_nclrs(src)*RGB_SIZE); return dst; }
bool CTgaFile::Save(const char *fpath) { int ii, iy; FILE *fp= NULL; bool bOK= true; try { if(!mDib) throw CImgFile::sMsgs[ERR_GENERAL]; fp = fopen(fpath, "wb"); if(!fp) throw CImgFile::sMsgs[ERR_NO_FILE]; int imgW= dib_get_width(mDib); int imgH= dib_get_height(mDib), imgHs= dib_get_height2(mDib); int imgP= dib_get_pitch(mDib); int imgB= dib_get_bpp(mDib); int imgS= imgH*imgP, nclrs= dib_get_nclrs(mDib); TGAHDR hdr; memset(&hdr, 0, sizeof(TGAHDR)); if(imgB==1) hdr.type= TGA_BW; else if(imgB <= 8) hdr.type= TGA_PAL; else hdr.type= TGA_true; if(imgB<=8) // paletted { hdr.has_table= 1; hdr.pal_len= dib_get_nclrs(mDib); hdr.pal_bpp= 24; } else // true color hdr.has_table=0; hdr.width= imgW; hdr.height= imgH; hdr.img_bpp= imgB; hdr.img_desc= 0; fwrite(&hdr, 1, sizeof(TGAHDR), fp); // write palette if(imgB <= 8) { RGBQUAD *pal= dib_get_pal(mDib); for(ii=0; ii<hdr.pal_len; ii++) fwrite(&pal[ii], 1, 3, fp); } // TGA should be bottom up: BYTE *imgL= dib_get_img(mDib); if(dib_is_topdown(mDib)) { imgL += imgP*(imgH-1); imgP= -imgP; } // write image (not RLEd, because that's awful to do) int tgaP= (imgW*imgB+7)/8; for(iy=0; iy<imgH; iy++) fwrite(&imgL[iy*imgP], 1, tgaP, fp); } catch(const char *msg) { SetMsg(msg); bOK= false; } if(fp) fclose(fp); return bOK; }