src/gui/image/qbmphandler.cpp
changeset 3 41300fa6a67c
parent 0 1918ee327afb
child 4 3b1da2848fc7
child 7 f7bc934e204c
equal deleted inserted replaced
2:56cd8111b7f7 3:41300fa6a67c
    50 QT_BEGIN_NAMESPACE
    50 QT_BEGIN_NAMESPACE
    51 
    51 
    52 static void swapPixel01(QImage *image)        // 1-bpp: swap 0 and 1 pixels
    52 static void swapPixel01(QImage *image)        // 1-bpp: swap 0 and 1 pixels
    53 {
    53 {
    54     int i;
    54     int i;
    55     if (image->depth() == 1 && image->numColors() == 2) {
    55     if (image->depth() == 1 && image->colorCount() == 2) {
    56         register uint *p = (uint *)image->bits();
    56         register uint *p = (uint *)image->bits();
    57         int nbytes = image->numBytes();
    57         int nbytes = image->byteCount();
    58         for (i=0; i<nbytes/4; i++) {
    58         for (i=0; i<nbytes/4; i++) {
    59             *p = ~*p;
    59             *p = ~*p;
    60             p++;
    60             p++;
    61         }
    61         }
    62         uchar *p2 = (uchar *)p;
    62         uchar *p2 = (uchar *)p;
   244             return false;
   244             return false;
   245     }
   245     }
   246 
   246 
   247     if (depth != 32) {
   247     if (depth != 32) {
   248         ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits;
   248         ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits;
   249         image.setNumColors(ncols);
   249         image.setColorCount(ncols);
   250     }
   250     }
   251 
   251 
   252     image.setDotsPerMeterX(bi.biXPelsPerMeter);
   252     image.setDotsPerMeterX(bi.biXPelsPerMeter);
   253     image.setDotsPerMeterY(bi.biYPelsPerMeter);
   253     image.setDotsPerMeterY(bi.biYPelsPerMeter);
   254 
   254 
   524 
   524 
   525     QIODevice* d = s.device();
   525     QIODevice* d = s.device();
   526     if (!d->isWritable())
   526     if (!d->isWritable())
   527         return false;
   527         return false;
   528 
   528 
   529     if (image.depth() == 8 && image.numColors() <= 16) {
   529     if (image.depth() == 8 && image.colorCount() <= 16) {
   530         bpl_bmp = (((bpl+1)/2+3)/4)*4;
   530         bpl_bmp = (((bpl+1)/2+3)/4)*4;
   531         nbits = 4;
   531         nbits = 4;
   532     } else if (image.depth() == 32) {
   532     } else if (image.depth() == 32) {
   533         bpl_bmp = ((image.width()*24+31)/32)*4;
   533         bpl_bmp = ((image.width()*24+31)/32)*4;
   534         nbits = 24;
   534         nbits = 24;
   552     bi.biCompression   = BMP_RGB;
   552     bi.biCompression   = BMP_RGB;
   553     bi.biSizeImage     = bpl_bmp*image.height();
   553     bi.biSizeImage     = bpl_bmp*image.height();
   554     bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX()
   554     bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX()
   555                                                 : 2834; // 72 dpi default
   555                                                 : 2834; // 72 dpi default
   556     bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834;
   556     bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834;
   557     bi.biClrUsed       = image.numColors();
   557     bi.biClrUsed       = image.colorCount();
   558     bi.biClrImportant  = image.numColors();
   558     bi.biClrImportant  = image.colorCount();
   559     s << bi;                                        // write info header
   559     s << bi;                                        // write info header
   560     if (s.status() != QDataStream::Ok)
   560     if (s.status() != QDataStream::Ok)
   561         return false;
   561         return false;
   562 
   562 
   563     if (image.depth() != 32) {                // write color table
   563     if (image.depth() != 32) {                // write color table
   564         uchar *color_table = new uchar[4*image.numColors()];
   564         uchar *color_table = new uchar[4*image.colorCount()];
   565         uchar *rgb = color_table;
   565         uchar *rgb = color_table;
   566         QVector<QRgb> c = image.colorTable();
   566         QVector<QRgb> c = image.colorTable();
   567         for (int i=0; i<image.numColors(); i++) {
   567         for (int i=0; i<image.colorCount(); i++) {
   568             *rgb++ = qBlue (c[i]);
   568             *rgb++ = qBlue (c[i]);
   569             *rgb++ = qGreen(c[i]);
   569             *rgb++ = qGreen(c[i]);
   570             *rgb++ = qRed  (c[i]);
   570             *rgb++ = qRed  (c[i]);
   571             *rgb++ = 0;
   571             *rgb++ = 0;
   572         }
   572         }
   573         if (d->write((char *)color_table, 4*image.numColors()) == -1) {
   573         if (d->write((char *)color_table, 4*image.colorCount()) == -1) {
   574             delete [] color_table;
   574             delete [] color_table;
   575             return false;
   575             return false;
   576         }
   576         }
   577         delete [] color_table;
   577         delete [] color_table;
   578     }
   578     }
   752     BMP_FILEHDR bf;
   752     BMP_FILEHDR bf;
   753     int bpl_bmp;
   753     int bpl_bmp;
   754     int bpl = image.bytesPerLine();
   754     int bpl = image.bytesPerLine();
   755 
   755 
   756     // Code partially repeated in qt_write_dib
   756     // Code partially repeated in qt_write_dib
   757     if (image.depth() == 8 && image.numColors() <= 16) {
   757     if (image.depth() == 8 && image.colorCount() <= 16) {
   758         bpl_bmp = (((bpl+1)/2+3)/4)*4;
   758         bpl_bmp = (((bpl+1)/2+3)/4)*4;
   759     } else if (image.depth() == 32) {
   759     } else if (image.depth() == 32) {
   760         bpl_bmp = ((image.width()*24+31)/32)*4;
   760         bpl_bmp = ((image.width()*24+31)/32)*4;
   761     } else {
   761     } else {
   762         bpl_bmp = bpl;
   762         bpl_bmp = bpl;
   769     memcpy(bf.bfType, "BM", 2);
   769     memcpy(bf.bfType, "BM", 2);
   770 
   770 
   771     // write file header
   771     // write file header
   772     bf.bfReserved1 = 0;
   772     bf.bfReserved1 = 0;
   773     bf.bfReserved2 = 0;
   773     bf.bfReserved2 = 0;
   774     bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.numColors() * 4;
   774     bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.colorCount() * 4;
   775     bf.bfSize = bf.bfOffBits + bpl_bmp*image.height();
   775     bf.bfSize = bf.bfOffBits + bpl_bmp*image.height();
   776     s << bf;
   776     s << bf;
   777 
   777 
   778     // write image
   778     // write image
   779     return qt_write_dib(s, image);
   779     return qt_write_dib(s, image);