bool CJ2kFile::Save(CBgraFrame* pFrame, const std::wstring& wsDstPath, const std::wstring& wsXmlOptions) { // TODO: Запись не реализована, надо доделать. return false; if (!pFrame) return false; LONG lWidth = pFrame->get_Width(); LONG lHeight = pFrame->get_Height(); BYTE* pSourceBuffer = pFrame->get_Data(); LONG lBufferSize = 4 * lWidth * lHeight; // Далее обрабатываем Xml с параметрами компрессии EncoderParams oParameters; int nFormat = ApplyEncoderOptions(&oParameters, wsXmlOptions); // TODO: Добавить возможность записи альфа-канала ImageComponentParams aComponentParams[3]; // Пока пусть будет максимально три компоненты (RGB) Image *pImage = NULL; int nComponentsCount = oParameters.nComponentsCount; memset(&aComponentParams[0], 0, sizeof(ImageComponentParams)); for (int nIndex = 0; nIndex < nComponentsCount; nIndex++) { aComponentParams[nIndex].nPrecision = 8; aComponentParams[nIndex].nBPP = 8; aComponentParams[nIndex].nSigned = 0; aComponentParams[nIndex].nXRsiz = oParameters.nSubSamplingDx; aComponentParams[nIndex].nYRsiz = oParameters.nSubSamplingDy; aComponentParams[nIndex].nWidth = (int)lWidth; aComponentParams[nIndex].nHeight = (int)lHeight; } // Создаем структуру Image pImage = Image_Create(nComponentsCount, &aComponentParams[0], csRGB); if (!pImage) return false; pImage->nXOsiz = oParameters.nImageOffsetX0; pImage->nYOsiz = oParameters.nImageOffsetY0; pImage->nXsiz = (!pImage->nXOsiz) ? (lWidth - 1) * oParameters.nSubSamplingDx + 1 : pImage->nXOsiz + (lWidth - 1) * oParameters.nSubSamplingDy + 1; pImage->nYsiz = (!pImage->nYOsiz) ? (lHeight - 1) * oParameters.nSubSamplingDy + 1 : pImage->nYOsiz + (lHeight - 1) * oParameters.nSubSamplingDy + 1; if (3 == nComponentsCount) { int nIndex = 0; for (int nY = 0; nY < (int)lHeight; nY++) { for (int nX = 0; nX < (int)lWidth; nX++, pSourceBuffer += 4) { pImage->pComponents[0].pData[nIndex] = pSourceBuffer[2]; pImage->pComponents[1].pData[nIndex] = pSourceBuffer[1]; pImage->pComponents[2].pData[nIndex] = pSourceBuffer[0]; nIndex++; } } } else if (1 == nComponentsCount) { int nIndex = 0; for (int nY = 0; nY < (int)lHeight; nY++) { for (int nX = 0; nX < (int)lWidth; nX++, pSourceBuffer += 4) { pImage->pComponents[0].pData[nIndex] = pSourceBuffer[0]; nIndex++; } } } else { Image_Destroy(pImage); return false; } bool bRes = false; switch (nFormat) { case 0: bRes = ImageToJ2k(pImage, wsDstPath, &oParameters); break; case 1: bRes = ImageToJ2p(pImage, wsDstPath, &oParameters); break; case -1: default: bRes = false; } Image_Destroy(pImage); return bRes; }
IMAGE * PPM_Load ( char * filename ) { FILE * in_fp; char * tempstr; int bits; int w, h; int c; int binary_file = 0; IMAGE * newimg; if (filename == NULL) { printf("Nothing to save.\n"); return NULL; } in_fp = fopen(filename, "r"); if (in_fp == NULL) { printf ("%s: Unable to open file\n", filename); return NULL; } /* insert loader code here. */ tempstr = read_string(in_fp); if (strcmp(tempstr, "P3")) { if (strcmp(tempstr, "P6")) { printf ("%s: Not a PPM file! Expecting \"P3\" or \"P6\" but read \"%s\".\n", filename, tempstr); fclose(in_fp); free(tempstr); return NULL; } else { binary_file = 1; } } free(tempstr); w = read_int(in_fp); h = read_int(in_fp); bits = read_int(in_fp); if (w == 0 || h == 0) { printf ("%s: Unsupported PPM file: %d x %d %d bits per color\n", filename, w,h,bits); } newimg = Image_Create(w,h,bits); if (newimg == NULL) { printf ("%s: Unable to load file. Memory error\n", filename); return NULL; } newimg->width = w; newimg->height = h; newimg->bits = bits; if (!binary_file) { for (c=0 ; c<(w*h) ; c++) { /* the format is: r g b r g b ... */ newimg->data[c].r = read_int(in_fp); newimg->data[c].g = read_int(in_fp); newimg->data[c].b = read_int(in_fp); } } else { for (c=0 ; c<(w*h) ; c++) { /* the format is: r g b r g b ... */ newimg->data[c].r = fgetc(in_fp); newimg->data[c].g = fgetc(in_fp); newimg->data[c].b = fgetc(in_fp); } } fclose (in_fp); return newimg; }