示例#1
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description: Recovers pictures from card.raw, solution to pset 5 from Harvard's CS50
 * =====================================================================================
 */
    int
main ( int argc, char *argv[] )
{
    
    // open file card.raw to read data
    FILE *fp = fopen("card.raw", "r");
    if(fp == NULL)
    {
        printf("Could not open file.\n");
        return 1;
    }

    //int to hold value for sequential filenames 
    int num = 0;

    //allocate space for buffer to hold 512 byte blocks and next block
    BYTE buffer[512];
    
    //sequence through filenames and open files for writing
    while(fread(buffer, sizeof(BYTE), 512, fp) != 0)
    {
        while(isJpeg(buffer))
        {
            // create outfile 
            FILE *outfile = make_file(num);
            
            // write out jpeg to outfile
            fwrite(buffer, sizeof(BYTE), 512, outfile);
            
            // read next block checking for eof 
            if(fread(buffer, sizeof(BYTE), 512, fp) == 0)
                break;
            
            // while next block isnt the start of new jpeg keep writing
            while(!isJpeg(buffer))
            {
                fwrite(buffer, sizeof(BYTE), 512, outfile);
                if(fread(buffer, sizeof(BYTE), 512, fp) == 0)
                    break;
            }

            // close outfile 
            fclose(outfile);
            num++;
        }
        
    }
    // close file pointer 
    fclose(fp);
    return EXIT_SUCCESS;
}			
示例#2
0
int main(void)
{
	FILE* inptr = fopen("card.raw", "rb");
    if (inptr == NULL)
    {
        
        return 1;
    }
    char output_name[8];
    char buffer[512];
    int count = 0;
    while(!feof(inptr))
    {
        while(!isJpeg(buffer))
         {
            fread(&buffer, 512, 1, inptr);
         }   

        
       
            
            sprintf(output_name, "%03d.jpg", count);
            count++;
            FILE* outptr = fopen(output_name, "w");
            do
            {                
                fwrite(&buffer, 512, 1, outptr);
                fread(&buffer, 512, 1, inptr);
            } while (!isJpeg(buffer) && !feof(inptr));

            fclose(outptr);
            

              
    }

    fclose(inptr);

}
const QImage ImageLoaderFreeImage::imageAsRGB(const QSize &size) const
{
    const QSize resultSize = size.isValid() ? size : sizePixels();
    const bool isRGB24 = colorDataType() == Types::ColorTypeRGB && bitsPerPixel() == 24;
    const bool isARGB32 = colorDataType() == Types::ColorTypeRGBA && bitsPerPixel() == 32;
    QImage result(resultSize, isARGB32 ? QImage::Format_ARGB32 : QImage::Format_RGB32);

    const int width = resultSize.width();
    const int height = resultSize.height();
    const QSize sizePixels = this->sizePixels();

    FIBITMAP* originalImage = m_bitmap;
    FIBITMAP* temp24BPPImage = NULL;
    FIBITMAP* scaledImage = NULL;

    if (!(isRGB24 || isARGB32)) {
        if (colorDataType() == Types::ColorTypeCMYK) {
            const bool isCmykJpeg = isJpeg(); // Value range inverted
            temp24BPPImage = FreeImage_Allocate(sizePixels.width(), sizePixels.height(), 24);
            const unsigned int columnsCount = sizePixels.width();
            const unsigned int scanlinesCount = sizePixels.height();
            for (unsigned int scanline = 0; scanline < scanlinesCount; scanline++) {
                const BYTE* const cmykBits = FreeImage_GetScanLine(m_bitmap, scanline);
                tagRGBTRIPLE* const rgbBits = (tagRGBTRIPLE *)FreeImage_GetScanLine(temp24BPPImage, scanline);

                for (unsigned int column = 0; column < columnsCount; column++) {
                    const unsigned int cmykColumn = column * 4;

                    const QColor rgbColor = isCmykJpeg ?
                        QColor::fromCmyk(255 - cmykBits[cmykColumn], 255 - cmykBits[cmykColumn + 1], 255 - cmykBits[cmykColumn + 2], 255 - cmykBits[cmykColumn + 3])
                        : QColor::fromCmyk(cmykBits[cmykColumn], cmykBits[cmykColumn + 1], cmykBits[cmykColumn + 2], cmykBits[cmykColumn + 3]);

                    rgbBits[column].rgbtRed = (BYTE)rgbColor.red();
                    rgbBits[column].rgbtGreen = (BYTE)rgbColor.green();
                    rgbBits[column].rgbtBlue = (BYTE)rgbColor.blue();
                }
            }
        } else {
            temp24BPPImage = FreeImage_ConvertTo24Bits(originalImage);
        }
        originalImage = temp24BPPImage;
    }

    if (resultSize != sizePixels) {
        scaledImage = FreeImage_Rescale(originalImage, width, height, FILTER_BOX);
        originalImage = scaledImage;
    }

    for (int scanline = 0; scanline < height; scanline++) {
        QRgb *targetData = (QRgb*)result.scanLine(scanline);
        if (isARGB32) {
            const tagRGBQUAD *sourceRgba = (tagRGBQUAD*)FreeImage_GetScanLine(originalImage, height - scanline - 1);
            for (int column = 0; column < width; column++) {
                *targetData++ = qRgba(sourceRgba->rgbRed, sourceRgba->rgbGreen, sourceRgba->rgbBlue, sourceRgba->rgbReserved);
                sourceRgba++;
            }
        } else {
            const tagRGBTRIPLE *sourceRgb = (tagRGBTRIPLE*)FreeImage_GetScanLine(originalImage, height - scanline - 1);
            for (int column = 0; column < width; column++) {
                *targetData++ = qRgb(sourceRgb->rgbtRed, sourceRgb->rgbtGreen, sourceRgb->rgbtBlue);
                sourceRgb++;
            }
        }
    }

    if (temp24BPPImage)
        FreeImage_Unload(temp24BPPImage);

    if (scaledImage)
        FreeImage_Unload(scaledImage);

    return result;
}