BitMiracle.LibJpeg.Classic.Internal.my_2pass_cquantizer.update_box C# (CSharp) Method

update_box() private method

Shrink the min/max bounds of a box to enclose only nonzero elements, and recompute its volume and population
private update_box ( box boxlist, int boxIndex ) : void
boxlist box
boxIndex int
return void
        private void update_box(box[] boxlist, int boxIndex)
        {
            box curBox = boxlist[boxIndex];
            bool have_c0min = false;

            if (curBox.c0max > curBox.c0min)
            {
                for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
                {
                    for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++)
                    {
                        int histogramIndex = c1 * HIST_C2_ELEMS + curBox.c2min;
                        for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++)
                        {
                            if (m_histogram[c0][histogramIndex++] != 0)
                            {
                                curBox.c0min = c0;
                                have_c0min = true;
                                break;
                            }
                        }

                        if (have_c0min)
                            break;
                    }

                    if (have_c0min)
                        break;
                }
            }

            bool have_c0max = false;
            if (curBox.c0max > curBox.c0min)
            {
                for (int c0 = curBox.c0max; c0 >= curBox.c0min; c0--)
                {
                    for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++)
                    {
                        int histogramIndex = c1 * HIST_C2_ELEMS + curBox.c2min;
                        for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++)
                        {
                            if (m_histogram[c0][histogramIndex++] != 0)
                            {
                                curBox.c0max = c0;
                                have_c0max = true;
                                break;
                            }
                        }

                        if (have_c0max)
                            break;
                    }

                    if (have_c0max)
                        break;
                }
            }

            bool have_c1min = false;
            if (curBox.c1max > curBox.c1min)
            {
                for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++)
                {
                    for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
                    {
                        int histogramIndex = c1 * HIST_C2_ELEMS + curBox.c2min;
                        for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++)
                        {
                            if (m_histogram[c0][histogramIndex++] != 0)
                            {
                                curBox.c1min = c1;
                                have_c1min = true;
                                break;
                            }
                        }

                        if (have_c1min)
                            break;
                    }

                    if (have_c1min)
                        break;
                }
            }

            bool have_c1max = false;
            if (curBox.c1max > curBox.c1min)
            {
                for (int c1 = curBox.c1max; c1 >= curBox.c1min; c1--)
                {
                    for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
                    {
                        int histogramIndex = c1 * HIST_C2_ELEMS + curBox.c2min;
                        for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++)
                        {
                            if (m_histogram[c0][histogramIndex++] != 0)
                            {
                                curBox.c1max = c1;
                                have_c1max = true;
                                break;
                            }
                        }

                        if (have_c1max)
                            break;
                    }

                    if (have_c1max)
                        break;
                }
            }

            bool have_c2min = false;
            if (curBox.c2max > curBox.c2min)
            {
                for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++)
                {
                    for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
                    {
                        int histogramIndex = curBox.c1min * HIST_C2_ELEMS + c2;
                        for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++, histogramIndex += HIST_C2_ELEMS)
                        {
                            if (m_histogram[c0][histogramIndex] != 0)
                            {
                                curBox.c2min = c2;
                                have_c2min = true;
                                break;
                            }
                        }

                        if (have_c2min)
                            break;
                    }

                    if (have_c2min)
                        break;
                }
            }

            bool have_c2max = false;
            if (curBox.c2max > curBox.c2min)
            {
                for (int c2 = curBox.c2max; c2 >= curBox.c2min; c2--)
                {
                    for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
                    {
                        int histogramIndex = curBox.c1min * HIST_C2_ELEMS + c2;
                        for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++, histogramIndex += HIST_C2_ELEMS)
                        {
                            if (m_histogram[c0][histogramIndex] != 0)
                            {
                                curBox.c2max = c2;
                                have_c2max = true;
                                break;
                            }
                        }

                        if (have_c2max)
                            break;
                    }

                    if (have_c2max)
                        break;
                }
            }

            /* Update box volume.
             * We use 2-norm rather than real volume here; this biases the method
             * against making long narrow boxes, and it has the side benefit that
             * a box is splittable iff norm > 0.
             * Since the differences are expressed in histogram-cell units,
             * we have to shift back to byte units to get consistent distances;
             * after which, we scale according to the selected distance scale factors.
             */
            int dist0 = ((curBox.c0max - curBox.c0min) << C0_SHIFT) * R_SCALE;
            int dist1 = ((curBox.c1max - curBox.c1min) << C1_SHIFT) * G_SCALE;
            int dist2 = ((curBox.c2max - curBox.c2min) << C2_SHIFT) * B_SCALE;
            curBox.volume = dist0 * dist0 + dist1 * dist1 + dist2 * dist2;

            /* Now scan remaining volume of box and compute population */
            long ccount = 0;
            for (int c0 = curBox.c0min; c0 <= curBox.c0max; c0++)
            {
                for (int c1 = curBox.c1min; c1 <= curBox.c1max; c1++)
                {
                    int histogramIndex = c1 * HIST_C2_ELEMS + curBox.c2min;
                    for (int c2 = curBox.c2min; c2 <= curBox.c2max; c2++, histogramIndex++)
                    {
                        if (m_histogram[c0][histogramIndex] != 0)
                            ccount++;
                    }
                }
            }

            curBox.colorcount = ccount;
            boxlist[boxIndex] = curBox;
        }