Example #1
0
	void Texture::resetData()
	{
		if (m_textureType == TextureType::kColor)
		{
			m_data.reset(new UCharTextureData);
		}
		else if (m_textureType == TextureType::kFloat)
		{
			m_data.reset(new FloatTextureData);
		}
		else
		{
			throw std::runtime_error("Unrecognised texture type");
		}
		m_data->resize(width() * height() * nChannels());
	}
Example #2
0
	int MRC::save(const char* filename) {
		char *filenamesave_ext = new char[ strlen(filename) + 8 ];
		strcpy(filenamesave_ext, filename);
		strcat(filenamesave_ext,".mrc");

		std::ofstream fout(filenamesave_ext, std::ios::binary);

		fout.write(reinterpret_cast<const char*> (&nx), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&ny), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&nz), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&mode), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&nxStart), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&nyStart), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&nzStart), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&mx), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&my), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&mz), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&xlen), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&ylen), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&zlen), sizeof(float));

		fout.write(reinterpret_cast<const char*> (&alpha), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&beta), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&gamma), sizeof(float));

		fout.write(reinterpret_cast<const char*> (&mapc), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&mapr), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&maps), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&amin), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&amax), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&amean), sizeof(float));

		fout.write(reinterpret_cast<const char*> (&ispq), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&next), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&creatid), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&extra_data), 30 * sizeof(char)); // !!! char 30

		fout.write(reinterpret_cast<const char*> (&nint), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&nreal), sizeof(short));

		fout.write(reinterpret_cast<const char*> (&extra_data_2), 20 * sizeof(char)); // !!! char 20

		fout.write(reinterpret_cast<const char*> (&imodStamp), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&imodFlag), sizeof(int));

		fout.write(reinterpret_cast<const char*> (&idtype), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&lens), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&nd1), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&nd2), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&vd1), sizeof(short));
		fout.write(reinterpret_cast<const char*> (&vd2), sizeof(short));

		fout.write(reinterpret_cast<const char*> (&tiltangles), 6 * sizeof(float));

		fout.write(reinterpret_cast<const char*> (&xorg), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&yorg), sizeof(float));
		fout.write(reinterpret_cast<const char*> (&zorg), sizeof(float));

		fout.write(reinterpret_cast<const char*> (&cmap), 4 * sizeof(char));
		fout.write(reinterpret_cast<const char*> (&stamp), 4 * sizeof(char));

		fout.write(reinterpret_cast<const char*> (&rms), sizeof(float));

		fout.write(reinterpret_cast<const char*> (&nlabl), sizeof(int));
		fout.write(reinterpret_cast<const char*> (&vlabl), 10 * 80 * sizeof(char));

		int nChannel = nChannels(mode);

		void *oData = nullptr;
				
		switch(mode) {
		case 0:
			oData = (unsigned char*) malloc1D(nx * ny * nz * nChannel, sizeof(char), "Output data unsigned char");
			break;
		case 1:
			oData = (short*) malloc1D(nx * ny * nz * nChannel, sizeof(short), "Output data signed short");
			break;
		case 2:
			oData = (float*) malloc1D(nx * ny * nz * nChannel, sizeof(float), "Output data float");
			break;
		case 3:
			oData = (short*) malloc1D(nx * ny * nz * nChannel, sizeof(short), "Output data short * 2");
			break;
		case 4:
			oData = (float*) malloc1D(nx * ny * nz * nChannel, sizeof(float), "Output data float * 2");
			break;
		case 6:
			oData = (unsigned short*) malloc1D(nx * ny * nz * nChannel, sizeof(short), "Output data unsigned short");
			break;
		case 16:
			oData = (unsigned char*) malloc1D(nx * ny * nz * nChannel, sizeof(char), "Output data unsigned char * 3");
		default:
			oData = nullptr;
			break;
		}

		for(size_t k = 0; k < nz; k++) {
			for(size_t i = 0; i < ny; i++) {
				for(size_t j = 0; j < nx; j++) {
					for(size_t l = 0; l < nChannel; l++) {
						switch(mode) {
						case 0:		// unsigned char
						case 16:
							((unsigned char*) oData)[ nChannel * (nx * ny * k + nx * i + j) + l ] = ( (double*) pData)[ nChannel * (nx * ny * k + nx * i + j) + l ];
							break;
						case 1:		// short
						case 3:
							((short*) oData)[ nChannel * (nx * ny * k + nx * i + j) + l ] = ( (double*) pData)[ nChannel * (nx * ny * k + nx * i + j) + l ];
							break;
						case 2:		// float
						case 4:
							((float*) oData)[ nChannel * (nx * ny * k + nx * i + j) + l ] = ( (double*) pData)[ nChannel * (nx * ny * k + nx * i + j) + l ];
							break;
						case 6:		// unsigned short
							((unsigned short*) oData)[ nChannel * (nx * ny * k + nx * i + j) + l ] = ( (double*) pData)[ nChannel * (nx * ny * k + nx * i + j) + l ];
							break;
						default:
							break;
						}
						
					}
				}
			}
		}

		fout.write(reinterpret_cast<const char*> (oData), this->nx * this->ny * this->nz * sizeof(float) * nChannel);
		delete[] oData;
		
		fout.close();
		
		delete[] filenamesave_ext;

		return 0;
	}
Example #3
0
bool MipData::loadMy4DImage(const My4DImage* img, const My4DImage* maskImg)
{
    // Validate data in this thread
    if (!img) return false;

    benchmark.start();

    dataMin = 1e9;
    dataMax = -1e9;

    data.assign(img->getXDim(), MipData::Column(img->getYDim(), MipData::Pixel(img->getCDim()))); // 50 ms

    // qDebug() << "size = " << data.size();
    // qDebug() << "nColumns = " << nColumns();
    // MipData& t = *this;
    volume4DImage = img;
    My4DImage * mutable_img = const_cast<My4DImage*>(img);
    Image4DProxy<My4DImage> imgProxy(mutable_img);

    numNeurons = 0;

    NeuronChannelIntegratorList neuronColors;
    // First loop "quickly" updates intensity, without updating neuron masks
    for (int x = 0; x < nColumns(); ++x) {
        for (int y = 0; y < nRows(); ++y) {
            for (int z = 0; z < img->getZDim(); ++z)
            {
                int neuronIndex = -1;
                if (maskImg) {
                    neuronIndex = maskImg->at(x, y, z);
                    if (neuronIndex >= numNeurons) {
                        numNeurons = neuronIndex + 1;
                        neuronColors.resize(numNeurons, NeuronChannelIntegrator(nChannels(), 0.0));
                    }
                }
                float intensity = 0.0;
                for (int c = 0; c < nChannels(); ++c) {
                    float val = (float)imgProxy.value_at(x,y,z,c);
                    // Update minimum and maximum values
                    if (val > dataMax) dataMax = val;
                    if (val < dataMin) dataMin = val;
                    // Update current voxel intensity
                    intensity += val;
                    if (neuronIndex >= 0)
                        neuronColors[neuronIndex][c] += val;
                }
                assert(intensity >= 0.0);
                // Maximum intensity projection - regardless of neuron masks
                if (intensity > data[x][y].intensity) {
                    for (int c = 0; c < nChannels(); ++c)
                        data[x][y][c] = (float)imgProxy.value_at(x,y,z,c);
                    data[x][y].z = z; // remember z-value of max intensity pixel
                    data[x][y].intensity = intensity;
                    if (maskImg) {
                        data[x][y].neuronIndex = (int) maskImg->at(x, y, z);
                    }
                }
            }
        }
        if (! (x % 10))
            emit processedXColumn(x + 1);
        // qDebug() << "processed column " << x + 1;
    }
    qDebug() << "Computing MIP took " << benchmark.restart() << " milliseconds";
    for (int n = 0; n < neuronColors.size(); ++n)
    {
        NeuronChannelIntegrator& neuronColor = neuronColors[n];
        // find maximum
        double maxCount = -1e9;
        for (int c = 0; c < neuronColor.size(); ++c) {
            double channelCount = neuronColor[c];
            if (channelCount > maxCount)
                maxCount = channelCount;
        }
        // scale by maximum
        for (int c = 0; c < neuronColor.size(); ++c) {
            neuronColor[c] /= maxCount;
        }
    }
    qDebug() << "Computing neuron colors took " << benchmark.restart() << " milliseconds";
    // TODO - actually use the color information

    emit intensitiesUpdated();

    // Populate individual neuron mip layers
    if (maskImg && (numNeurons > 0))
    {
        if (neuronLayers) delete [] neuronLayers;
        neuronLayers = new MipLayer*[numNeurons];
        for (int i = 0; i < numNeurons; ++i)
            neuronLayers[i] = new MipLayer(QSize(nColumns(), nRows()), this);

        qDebug() << "processing MIP masks";
        for (int x = 0; x < nColumns(); ++x) {
            for (int y = 0; y < nRows(); ++y) {
                for (int z = 0; z < img->getZDim(); ++z)
                {
                    int neuronMaskId = maskImg->at(x,y,z);
                    if (neuronMaskId < 0) continue;
                    float intensity = 0.0;
                    for (int c = 0; c < nChannels(); ++c)
                        intensity += (float)imgProxy.value_at(x,y,z,c);
                    assert(intensity >= 0.0);
                    MipLayer::Pixel& currentPixel = neuronLayers[neuronMaskId]->getPixel(x, y);
                    if (   (currentPixel.neuronIndex != neuronMaskId) // no data for this neuron so far
                        || (intensity > currentPixel.intensity) ) // brightest intensity seen so far
                    {
                        currentPixel.neuronIndex = neuronMaskId;
                        currentPixel.zCoordinate = z;
                        currentPixel.intensity = intensity;
                    }
                }
            }
        }
        qDebug() << "finished creating MIP masks; took " << benchmark.restart() << " milliseconds";
        // TODO create binary tree of mip layers leading to combined image
        std::vector<MipLayer*> layers;
        for (int n = 0; n < numNeurons; ++n) {
            layers.push_back(neuronLayers[n]);
        }
        while (layers.size() > 1) {
            std::vector<MipLayer*> nextLevel;
            while (layers.size() > 0) {
                MipLayer* node1 = layers.back(); layers.pop_back();
                MipLayer* node2 = NULL;
                if (layers.size() > 0) {
                    node2 = layers.back();
                    layers.pop_back();
                }
                nextLevel.push_back(new MipLayer(node1, node2, this));
            }
            layers = nextLevel;
            qDebug() << "layers size = " << layers.size();
        }
        assert(layers.size() == 1);
        combinedMipLayer = layers.back();
        connect(combinedMipLayer, SIGNAL(layerChanged()),
                this, SLOT(onCombinedMipLayerUpdated()));
        qDebug() << "Creating MIP layer binary tree took " << benchmark.restart() << " milliseconds";
    }

    return true;
}